addons_test.go 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// +build integration

/*
Copyright 2016 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 (
22
	"bufio"
23
	"fmt"
24
	"io/ioutil"
25
	"net"
26
	"net/http"
27
	"net/url"
M
Medya Gh 已提交
28
	"path"
K
kairen 已提交
29
	"path/filepath"
30
	"strings"
31 32 33
	"testing"
	"time"

34
	"github.com/docker/machine/libmachine/state"
35
	retryablehttp "github.com/hashicorp/go-retryablehttp"
36
	"k8s.io/apimachinery/pkg/labels"
M
Matt Rickard 已提交
37
	pkgutil "k8s.io/minikube/pkg/util"
38 39 40
	"k8s.io/minikube/test/integration/util"
)

41 42
func testAddons(t *testing.T) {
	t.Parallel()
M
Matt Rickard 已提交
43
	client, err := pkgutil.GetClient()
44
	if err != nil {
45
		t.Fatalf("Could not get kubernetes client: %v", err)
46
	}
47
	selector := labels.SelectorFromSet(labels.Set(map[string]string{"component": "kube-addon-manager"}))
M
Matt Rickard 已提交
48
	if err := pkgutil.WaitForPodsWithLabelRunning(client, "kube-system", selector); err != nil {
49
		t.Errorf("Error waiting for addon manager to be up")
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
func readLineWithTimeout(b *bufio.Reader, timeout time.Duration) (string, error) {
	s := make(chan string)
	e := make(chan error)
	go func() {
		read, err := b.ReadString('\n')
		if err != nil {
			e <- err
		} else {
			s <- read
		}
		close(s)
		close(e)
	}()

	select {
	case line := <-s:
		return line, nil
	case err := <-e:
		return "", err
	case <-time.After(timeout):
		return "", fmt.Errorf("timeout after %s", timeout)
	}
}

77 78
func testDashboard(t *testing.T) {
	t.Parallel()
79 80
	mk := NewMinikubeRunner(t, "--wait=false")
	cmd, out := mk.RunDaemon("dashboard --url")
81 82
	defer func() {
		err := cmd.Process.Kill()
83
		if err != nil {
84
			t.Logf("Failed to kill dashboard command: %v", err)
85
		}
86 87
	}()

88
	s, err := readLineWithTimeout(out, 180*time.Second)
89 90
	if err != nil {
		t.Fatalf("failed to read url: %v", err)
91
	}
92

93 94 95
	u, err := url.Parse(strings.TrimSpace(s))
	if err != nil {
		t.Fatalf("failed to parse %q: %v", s, err)
96
	}
97

98
	if u.Scheme != "http" {
99
		t.Errorf("got Scheme %s, expected http", u.Scheme)
100
	}
101
	host, _, err := net.SplitHostPort(u.Host)
102
	if err != nil {
103
		t.Fatalf("failed SplitHostPort: %v", err)
104 105
	}
	if host != "127.0.0.1" {
106 107 108
		t.Errorf("got host %s, expected 127.0.0.1", host)
	}

109
	resp, err := retryablehttp.Get(u.String())
110 111 112 113 114 115 116 117 118
	if err != nil {
		t.Fatalf("failed get: %v", err)
	}
	if resp.StatusCode != http.StatusOK {
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			t.Fatalf("Unable to read http response body: %v", err)
		}
		t.Errorf("%s returned status code %d, expected %d.\nbody:\n%s", u, resp.StatusCode, http.StatusOK, body)
119
	}
120
}
121

K
kairen 已提交
122 123
func testIngressController(t *testing.T) {
	t.Parallel()
M
Medya Gh 已提交
124 125 126
	p := "minikube"
	mk := NewMinikubeRunner(t, p, "--wait=false")
	kr := util.NewKubectlRunner(t, p)
K
kairen 已提交
127

128
	mk.RunCommand("addons enable ingress", true)
M
Medya Gh 已提交
129
	if err := util.WaitForIngressControllerRunning(t, p); err != nil {
130
		t.Fatalf("waiting for ingress-controller to be up: %v", err)
K
kairen 已提交
131 132
	}

M
Medya Gh 已提交
133
	if err := util.WaitForIngressDefaultBackendRunning(t, p); err != nil {
134
		t.Fatalf("waiting for default-http-backend to be up: %v", err)
K
kairen 已提交
135 136
	}

M
Medya Gh 已提交
137 138 139 140 141
	curdir, err := filepath.Abs("")
	if err != nil {
		t.Errorf("Error getting the file path for current directory: %s", curdir)
	}
	ingressPath := path.Join(curdir, "testdata", "nginx-ing.yaml")
142
	if _, err := kr.RunCommand([]string{"create", "-f", ingressPath}); err != nil {
143
		t.Fatalf("creating nginx ingress resource: %v", err)
K
kairen 已提交
144 145
	}

M
Medya Gh 已提交
146
	podPath := path.Join(curdir, "testdata", "nginx-pod-svc.yaml")
147
	if _, err := kr.RunCommand([]string{"create", "-f", podPath}); err != nil {
148
		t.Fatalf("creating nginx ingress resource: %v", err)
K
kairen 已提交
149 150
	}

M
Medya Gh 已提交
151
	if err := util.WaitForNginxRunning(t, p); err != nil {
152
		t.Fatalf("waiting for nginx to be up: %v", err)
K
kairen 已提交
153 154
	}

155 156 157
	checkIngress := func() error {
		expectedStr := "Welcome to nginx!"
		runCmd := fmt.Sprintf("curl http://127.0.0.1:80 -H 'Host: nginx.example.com'")
158
		sshCmdOutput, _ := mk.SSH(runCmd)
159 160 161 162 163 164 165 166
		if !strings.Contains(sshCmdOutput, expectedStr) {
			return fmt.Errorf("ExpectedStr sshCmdOutput to be: %s. Output was: %s", expectedStr, sshCmdOutput)
		}
		return nil
	}

	if err := util.Retry(t, checkIngress, 3*time.Second, 5); err != nil {
		t.Fatalf(err.Error())
K
kairen 已提交
167 168
	}

169 170
	defer func() {
		for _, p := range []string{podPath, ingressPath} {
171
			if out, err := kr.RunCommand([]string{"delete", "-f", p}); err != nil {
172 173 174 175
				t.Logf("delete -f %s failed: %v\noutput: %s\n", p, err, out)
			}
		}
	}()
176
	mk.RunCommand("addons disable ingress", true)
K
kairen 已提交
177 178
}

179 180
func testServicesList(t *testing.T) {
	t.Parallel()
M
Medya Gh 已提交
181 182
	p := "minikube"
	mk := NewMinikubeRunner(t, p)
183

184
	checkServices := func() error {
185
		output := mk.RunCommand("service list", false)
186
		if !strings.Contains(output, "kubernetes") {
187
			return fmt.Errorf("Error, kubernetes service missing from output %s", output)
188
		}
189 190
		return nil
	}
191
	if err := util.Retry(t, checkServices, 2*time.Second, 5); err != nil {
192
		t.Fatalf(err.Error())
193 194
	}
}
195 196
func testRegistry(t *testing.T) {
	t.Parallel()
M
Medya Gh 已提交
197 198
	p := "minikube"
	mk := NewMinikubeRunner(t, p)
199
	mk.RunCommand("addons enable registry", true)
M
Medya Gh 已提交
200
	client, err := pkgutil.GetClient(p)
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	if err != nil {
		t.Fatalf("getting kubernetes client: %v", err)
	}
	if err := pkgutil.WaitForRCToStabilize(client, "kube-system", "registry", time.Minute*5); err != nil {
		t.Fatalf("waiting for registry replicacontroller to stabilize: %v", err)
	}
	rs := labels.SelectorFromSet(labels.Set(map[string]string{"actual-registry": "true"}))
	if err := pkgutil.WaitForPodsWithLabelRunning(client, "kube-system", rs); err != nil {
		t.Fatalf("waiting for registry pods: %v", err)
	}
	ps, err := labels.Parse("kubernetes.io/minikube-addons=registry,actual-registry!=true")
	if err != nil {
		t.Fatalf("Unable to parse selector: %v", err)
	}
	if err := pkgutil.WaitForPodsWithLabelRunning(client, "kube-system", ps); err != nil {
		t.Fatalf("waiting for registry-proxy pods: %v", err)
	}
218

219 220 221 222 223
	ip := strings.TrimSpace(mk.RunCommand("ip", true))
	endpoint := fmt.Sprintf("http://%s:%d", ip, 5000)
	u, err := url.Parse(endpoint)
	if err != nil {
		t.Fatalf("failed to parse %q: %v", endpoint, err)
224
	}
225
	t.Log("checking registry access from outside cluster")
B
Ben Ebsworth 已提交
226

227 228
	// Check access from outside the cluster on port 5000, validing connectivity via registry-proxy
	checkExternalAccess := func() error {
B
Ben Ebsworth 已提交
229 230
		resp, err := retryablehttp.Get(u.String())
		if err != nil {
231
			t.Errorf("failed get: %v", err)
B
Ben Ebsworth 已提交
232 233 234
		}

		if resp.StatusCode != http.StatusOK {
235
			t.Errorf("%s returned status code %d, expected %d.\n", u, resp.StatusCode, http.StatusOK)
B
Ben Ebsworth 已提交
236
		}
237 238
		return nil
	}
239

240 241 242
	if err := util.Retry(t, checkExternalAccess, 2*time.Second, 5); err != nil {
		t.Fatalf(err.Error())
	}
243

244
	t.Log("checking registry access from inside cluster")
M
Medya Gh 已提交
245 246
	kr := util.NewKubectlRunner(t, p)
	// TODO: Fix this
247
	out, _ := kr.RunCommand([]string{
248 249 250 251 252 253 254 255 256 257
		"run",
		"registry-test",
		"--restart=Never",
		"--image=busybox",
		"-it",
		"--",
		"sh",
		"-c",
		"wget --spider -S 'http://registry.kube-system.svc.cluster.local' 2>&1 | grep 'HTTP/' | awk '{print $2}'"})
	internalCheckOutput := string(out)
258
	expectedStr := "200"
259 260 261
	if !strings.Contains(internalCheckOutput, expectedStr) {
		t.Fatalf("ExpectedStr internalCheckOutput to be: %s. Output was: %s", expectedStr, internalCheckOutput)
	}
262

263
	defer func() {
264
		if _, err := kr.RunCommand([]string{"delete", "pod", "registry-test"}); err != nil {
265
			t.Fatalf("failed to delete pod registry-test")
B
Ben Ebsworth 已提交
266
		}
267
	}()
268
	mk.RunCommand("addons disable registry", true)
269
}
270
func testGvisor(t *testing.T) {
M
Medya Gh 已提交
271 272
	p := "minikube"
	mk := NewMinikubeRunner(t, p, "--wait=false")
273
	mk.RunCommand("addons enable gvisor", true)
274 275

	t.Log("waiting for gvisor controller to come up")
M
Medya Gh 已提交
276
	if err := util.WaitForGvisorControllerRunning(t, p); err != nil {
277 278 279
		t.Fatalf("waiting for gvisor controller to be up: %v", err)
	}

M
Medya Gh 已提交
280
	createUntrustedWorkload(t, p)
281 282

	t.Log("making sure untrusted workload is Running")
M
Medya Gh 已提交
283
	if err := util.WaitForUntrustedNginxRunning(p); err != nil {
284 285 286 287
		t.Fatalf("waiting for nginx to be up: %v", err)
	}

	t.Log("disabling gvisor addon")
288
	mk.RunCommand("addons disable gvisor", true)
289
	t.Log("waiting for gvisor controller pod to be deleted")
M
Medya Gh 已提交
290
	if err := util.WaitForGvisorControllerDeleted(p); err != nil {
291 292 293
		t.Fatalf("waiting for gvisor controller to be deleted: %v", err)
	}

M
Medya Gh 已提交
294
	createUntrustedWorkload(t, p)
295 296

	t.Log("waiting for FailedCreatePodSandBox event")
M
Medya Gh 已提交
297
	if err := util.WaitForFailedCreatePodSandBoxEvent(p); err != nil {
298 299
		t.Fatalf("waiting for FailedCreatePodSandBox event: %v", err)
	}
M
Medya Gh 已提交
300
	deleteUntrustedWorkload(t, p)
301 302 303
}

func testGvisorRestart(t *testing.T) {
M
Medya Gh 已提交
304 305
	p := "minikube"
	mk := NewMinikubeRunner(t, p, "--wait=false")
306 307
	mk.EnsureRunning()
	mk.RunCommand("addons enable gvisor", true)
308 309

	t.Log("waiting for gvisor controller to come up")
M
Medya Gh 已提交
310
	if err := util.WaitForGvisorControllerRunning(t, p); err != nil {
311 312 313 314
		t.Fatalf("waiting for gvisor controller to be up: %v", err)
	}

	// TODO: @priyawadhwa to add test for stop as well
315 316 317 318
	mk.RunCommand("delete", false)
	mk.CheckStatus(state.None.String())
	mk.Start()
	mk.CheckStatus(state.Running.String())
319 320

	t.Log("waiting for gvisor controller to come up")
M
Medya Gh 已提交
321
	if err := util.WaitForGvisorControllerRunning(t, p); err != nil {
322 323 324
		t.Fatalf("waiting for gvisor controller to be up: %v", err)
	}

M
Medya Gh 已提交
325
	createUntrustedWorkload(t, p)
326
	t.Log("making sure untrusted workload is Running")
M
Medya Gh 已提交
327
	if err := util.WaitForUntrustedNginxRunning(p); err != nil {
328 329
		t.Fatalf("waiting for nginx to be up: %v", err)
	}
M
Medya Gh 已提交
330
	deleteUntrustedWorkload(t, p)
331
}
332

M
Medya Gh 已提交
333 334
func createUntrustedWorkload(t *testing.T, profile string) {
	kr := util.NewKubectlRunner(t, profile)
M
Medya Gh 已提交
335 336 337 338 339
	curdir, err := filepath.Abs("")
	if err != nil {
		t.Errorf("Error getting the file path for current directory: %s", curdir)
	}
	untrustedPath := path.Join(curdir, "testdata", "nginx-untrusted.yaml")
340
	t.Log("creating pod with untrusted workload annotation")
341
	if _, err := kr.RunCommand([]string{"replace", "-f", untrustedPath, "--force"}); err != nil {
342 343 344 345
		t.Fatalf("creating untrusted nginx resource: %v", err)
	}
}

M
Medya Gh 已提交
346 347
func deleteUntrustedWorkload(t *testing.T, profile string) {
	kr := util.NewKubectlRunner(t, profile)
M
Medya Gh 已提交
348 349 350 351 352
	curdir, err := filepath.Abs("")
	if err != nil {
		t.Errorf("Error getting the file path for current directory: %s", curdir)
	}
	untrustedPath := path.Join(curdir, "testdata", "nginx-untrusted.yaml")
353
	if _, err := kr.RunCommand([]string{"delete", "-f", untrustedPath}); err != nil {
354 355 356
		t.Logf("error deleting untrusted nginx resource: %v", err)
	}
}