forked from kubernetes/minikube
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtunnel_test.go
162 lines (130 loc) · 4.46 KB
/
tunnel_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
Copyright 2018 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package integration
import (
"fmt"
"io/ioutil"
"net/http"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/minikube/pkg/minikube/tunnel"
commonutil "k8s.io/minikube/pkg/util"
"k8s.io/minikube/test/integration/util"
)
func testTunnel(t *testing.T) {
if runtime.GOOS != "windows" {
// Otherwise minikube fails waiting for a password.
if err := exec.Command("sudo", "-n", "route").Run(); err != nil {
t.Skipf("password required to execute 'route', skipping testTunnel: %v", err)
}
}
t.Log("starting tunnel test...")
runner := NewMinikubeRunner(t)
go func() {
output := runner.RunCommand("tunnel --alsologtostderr -v 8 --logtostderr", true)
if t.Failed() {
fmt.Println(output)
}
}()
err := tunnel.NewManager().CleanupNotRunningTunnels()
if err != nil {
t.Fatal(errors.Wrap(err, "cleaning up tunnels"))
}
kubectlRunner := util.NewKubectlRunner(t)
t.Log("deploying nginx...")
curdir, err := filepath.Abs("")
if err != nil {
t.Errorf("Error getting the file path for current directory: %s", curdir)
}
podPath := path.Join(curdir, "testdata", "testsvc.yaml")
if _, err := kubectlRunner.RunCommand([]string{"apply", "-f", podPath}); err != nil {
t.Fatalf("creating nginx ingress resource: %s", err)
}
client, err := commonutil.GetClient()
if err != nil {
t.Fatal(errors.Wrap(err, "getting kubernetes client"))
}
selector := labels.SelectorFromSet(labels.Set(map[string]string{"run": "nginx-svc"}))
if err := commonutil.WaitForPodsWithLabelRunning(client, "default", selector); err != nil {
t.Fatal(errors.Wrap(err, "waiting for nginx pods"))
}
if err := commonutil.WaitForService(client, "default", "nginx-svc", true, 1*time.Second, 2*time.Minute); err != nil {
t.Fatal(errors.Wrap(err, "Error waiting for nginx service to be up"))
}
t.Log("getting nginx ingress...")
nginxIP := ""
err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
cmd := []string{"get", "svc", "nginx-svc", "-o", "jsonpath={.status.loadBalancer.ingress[0].ip}"}
stdout, err := kubectlRunner.RunCommand(cmd)
switch {
case err == nil:
nginxIP = string(stdout)
return len(stdout) != 0, nil
case !commonutil.IsRetryableAPIError(err):
t.Errorf("`%s` failed with non retriable error: %v", cmd, err)
return false, err
default:
t.Errorf("`%s` failed: %v", cmd, err)
return false, nil
}
})
if err != nil {
t.Errorf("error getting ingress IP for nginx: %s", err)
}
if len(nginxIP) == 0 {
stdout, err := kubectlRunner.RunCommand([]string{"get", "svc", "nginx-svc", "-o", "jsonpath={.status}"})
if err != nil {
t.Errorf("error debugging nginx service: %s", err)
}
t.Fatalf("svc should have ingress after tunnel is created, but it was empty! Result of `kubectl describe svc nginx-svc`:\n %s", string(stdout))
}
responseBody, err := getResponseBody(nginxIP)
if err != nil {
t.Fatalf("error reading from nginx at address(%s): %s", nginxIP, err)
}
if !strings.Contains(responseBody, "Welcome to nginx!") {
t.Fatalf("response body doesn't seem like an nginx response:\n%s", responseBody)
}
}
// getResponseBody returns the contents of a URL
func getResponseBody(address string) (string, error) {
httpClient := http.DefaultClient
httpClient.Timeout = 5 * time.Second
var resp *http.Response
var err error
request := func() error {
resp, err = httpClient.Get(fmt.Sprintf("http://%s", address))
if err != nil {
retriable := &commonutil.RetriableError{Err: err}
return retriable
}
return nil
}
if err = commonutil.RetryAfter(5, request, 1*time.Second); err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil || len(body) == 0 {
return "", errors.Wrapf(err, "error reading body, len bytes read: %d", len(body))
}
return string(body), nil
}