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()
124
	mk := NewMinikubeRunner(t, "--wait=false")
125
	kr := util.NewKubectlRunner(t)
K
kairen 已提交
126

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

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

M
Medya Gh 已提交
136 137 138 139 140
	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")
141
	if _, err := kr.RunCommand([]string{"create", "-f", ingressPath}); err != nil {
142
		t.Fatalf("creating nginx ingress resource: %v", err)
K
kairen 已提交
143 144
	}

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

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

154 155 156
	checkIngress := func() error {
		expectedStr := "Welcome to nginx!"
		runCmd := fmt.Sprintf("curl http://127.0.0.1:80 -H 'Host: nginx.example.com'")
157
		sshCmdOutput, _ := mk.SSH(runCmd)
158 159 160 161 162 163 164 165
		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 已提交
166 167
	}

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

178 179
func testServicesList(t *testing.T) {
	t.Parallel()
180
	mk := NewMinikubeRunner(t)
181

182
	checkServices := func() error {
183
		output := mk.RunCommand("service list", false)
184
		if !strings.Contains(output, "kubernetes") {
185
			return fmt.Errorf("Error, kubernetes service missing from output %s", output)
186
		}
187 188
		return nil
	}
189
	if err := util.Retry(t, checkServices, 2*time.Second, 5); err != nil {
190
		t.Fatalf(err.Error())
191 192
	}
}
193 194
func testRegistry(t *testing.T) {
	t.Parallel()
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	mk := NewMinikubeRunner(t)
	t.Log("enabling registry")
	mk.RunCommand("addons enable registry", true)
	t.Log("enabled")
	client, err := pkgutil.GetClient()
	if err != nil {
		t.Fatalf("getting kubernetes client: %v", err)
	}
	t.Log("wait for registry replicacontroller")
	if err := pkgutil.WaitForRCToStabilize(client, "kube-system", "registry", time.Minute*5); err != nil {
		t.Fatalf("waiting for registry replicacontroller to stabilize: %v", err)
	}
	t.Log("wait for registry pod")
	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)
	}
	t.Log("wait for registry-proxy pod")
	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)
	}
220

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

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

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

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

247
	t.Log("checking registry access from inside cluster")
248 249
	kr := util.NewKubectlRunner(t)
	out, _ := kr.RunCommand([]string{
250 251 252 253 254 255 256 257 258 259
		"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)
260
	expectedStr := "200"
261 262 263
	if !strings.Contains(internalCheckOutput, expectedStr) {
		t.Fatalf("ExpectedStr internalCheckOutput to be: %s. Output was: %s", expectedStr, internalCheckOutput)
	}
264

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

	t.Log("waiting for gvisor controller to come up")
	if err := util.WaitForGvisorControllerRunning(t); err != nil {
		t.Fatalf("waiting for gvisor controller to be up: %v", err)
	}

281
	createUntrustedWorkload(t)
282 283 284 285 286 287 288

	t.Log("making sure untrusted workload is Running")
	if err := util.WaitForUntrustedNginxRunning(); err != nil {
		t.Fatalf("waiting for nginx to be up: %v", err)
	}

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

295
	createUntrustedWorkload(t)
296 297 298 299 300

	t.Log("waiting for FailedCreatePodSandBox event")
	if err := util.WaitForFailedCreatePodSandBoxEvent(); err != nil {
		t.Fatalf("waiting for FailedCreatePodSandBox event: %v", err)
	}
301 302 303 304
	deleteUntrustedWorkload(t)
}

func testGvisorRestart(t *testing.T) {
305
	mk := NewMinikubeRunner(t, "--wait=false")
306 307
	mk.EnsureRunning()
	mk.RunCommand("addons enable gvisor", true)
308 309 310 311 312 313 314

	t.Log("waiting for gvisor controller to come up")
	if err := util.WaitForGvisorControllerRunning(t); err != nil {
		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 321 322 323 324 325 326 327 328 329 330 331

	t.Log("waiting for gvisor controller to come up")
	if err := util.WaitForGvisorControllerRunning(t); err != nil {
		t.Fatalf("waiting for gvisor controller to be up: %v", err)
	}

	createUntrustedWorkload(t)
	t.Log("making sure untrusted workload is Running")
	if err := util.WaitForUntrustedNginxRunning(); err != nil {
		t.Fatalf("waiting for nginx to be up: %v", err)
	}
	deleteUntrustedWorkload(t)
}
332

333
func createUntrustedWorkload(t *testing.T) {
334
	kr := util.NewKubectlRunner(t)
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 346
		t.Fatalf("creating untrusted nginx resource: %v", err)
	}
}

func deleteUntrustedWorkload(t *testing.T) {
347
	kr := util.NewKubectlRunner(t)
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)
	}
}