addons_test.go 10.2 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
	minikubeRunner := NewMinikubeRunner(t)
T
Thomas Stromberg 已提交
80
	cmd, out := minikubeRunner.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 124 125 126 127 128
func testIngressController(t *testing.T) {
	t.Parallel()
	minikubeRunner := NewMinikubeRunner(t)
	kubectlRunner := util.NewKubectlRunner(t)

	minikubeRunner.RunCommand("addons enable ingress", true)
	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")
K
kairen 已提交
141
	if _, err := kubectlRunner.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 := kubectlRunner.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 157 158 159 160 161 162 163 164 165
	checkIngress := func() error {
		expectedStr := "Welcome to nginx!"
		runCmd := fmt.Sprintf("curl http://127.0.0.1:80 -H 'Host: nginx.example.com'")
		sshCmdOutput, _ := minikubeRunner.SSH(runCmd)
		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 170 171 172 173 174
	defer func() {
		for _, p := range []string{podPath, ingressPath} {
			if out, err := kubectlRunner.RunCommand([]string{"delete", "-f", p}); err != nil {
				t.Logf("delete -f %s failed: %v\noutput: %s\n", p, err, out)
			}
		}
	}()
K
kairen 已提交
175 176 177
	minikubeRunner.RunCommand("addons disable ingress", true)
}

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

182 183 184
	checkServices := func() error {
		output := minikubeRunner.RunCommand("service list", false)
		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 195 196 197 198 199 200
func testRegistry(t *testing.T) {
	t.Parallel()
	minikubeRunner := NewMinikubeRunner(t)
	kubectlRunner := util.NewKubectlRunner(t)
	minikubeRunner.RunCommand("addons enable registry", true)
	t.Log("wait for registry to come up")

	if err := util.WaitForDockerRegistryRunning(t); err != nil {
B
Ben Ebsworth 已提交
201
		t.Fatalf("waiting for registry to be up: %v", err)
202
	}
B
Ben Ebsworth 已提交
203

204 205 206
	// Check access from outside the cluster on port 5000, validing connectivity via registry-proxy
	checkExternalAccess := func() error {
		t.Log("checking registry access from outside cluster")
B
Ben Ebsworth 已提交
207 208 209 210 211 212 213 214 215 216 217 218
		_, out := minikubeRunner.RunDaemon("ip")
		s, err := readLineWithTimeout(out, 180*time.Second)

		if err != nil {
			t.Fatalf("failed to read minikubeIP: %v", err)
		}

		registryEndpoint := "http://" + strings.TrimSpace(s) + "5000"
		u, err := url.Parse(registryEndpoint)

		if err != nil {
			t.Fatalf("failed to parse %q: %v", s, err)
219
		}
B
Ben Ebsworth 已提交
220 221 222 223 224 225 226 227 228 229

		resp, err := retryablehttp.Get(u.String())
		if err != nil {
			t.Fatalf("failed get: %v", err)
		}

		if resp.StatusCode != http.StatusOK {
			t.Errorf("%s returned status code %d, expected %d.\n", registryEndpoint, resp.StatusCode, http.StatusOK)
		}

230 231
		return nil
	}
232

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
	if err := util.Retry(t, checkExternalAccess, 2*time.Second, 5); err != nil {
		t.Fatalf(err.Error())
	}
	// check access from inside the cluster via a busybox container running inside cluster
	t.Log("checking registry access from inside cluster")
	expectedStr := "200"
	out, _ := kubectlRunner.RunCommand([]string{
		"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)
	if !strings.Contains(internalCheckOutput, expectedStr) {
		t.Fatalf("ExpectedStr internalCheckOutput to be: %s. Output was: %s", expectedStr, internalCheckOutput)
	}
B
Ben Ebsworth 已提交
253

254 255 256
	defer func() {
		if _, err := kubectlRunner.RunCommand([]string{"delete", "pod", "registry-test"}); err != nil {
			t.Fatalf("failed to delete pod registry-test")
B
Ben Ebsworth 已提交
257
		}
258 259 260
	}()
	minikubeRunner.RunCommand("addons disable registry", true)
}
261 262 263 264 265 266 267 268 269
func testGvisor(t *testing.T) {
	minikubeRunner := NewMinikubeRunner(t)
	minikubeRunner.RunCommand("addons enable gvisor", true)

	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)
	}

270
	createUntrustedWorkload(t)
271 272 273 274 275 276 277 278 279 280 281 282 283

	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")
	minikubeRunner.RunCommand("addons disable gvisor", true)
	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)
	}

284
	createUntrustedWorkload(t)
285 286 287 288 289

	t.Log("waiting for FailedCreatePodSandBox event")
	if err := util.WaitForFailedCreatePodSandBoxEvent(); err != nil {
		t.Fatalf("waiting for FailedCreatePodSandBox event: %v", err)
	}
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	deleteUntrustedWorkload(t)
}

func testGvisorRestart(t *testing.T) {
	minikubeRunner := NewMinikubeRunner(t)
	minikubeRunner.EnsureRunning()
	minikubeRunner.RunCommand("addons enable gvisor", true)

	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
	minikubeRunner.RunCommand("delete", false)
	minikubeRunner.CheckStatus(state.None.String())
	minikubeRunner.Start()
	minikubeRunner.CheckStatus(state.Running.String())

	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)
}
321

322 323
func createUntrustedWorkload(t *testing.T) {
	kubectlRunner := util.NewKubectlRunner(t)
M
Medya Gh 已提交
324 325 326 327 328
	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")
329 330 331 332 333 334 335 336
	t.Log("creating pod with untrusted workload annotation")
	if _, err := kubectlRunner.RunCommand([]string{"replace", "-f", untrustedPath, "--force"}); err != nil {
		t.Fatalf("creating untrusted nginx resource: %v", err)
	}
}

func deleteUntrustedWorkload(t *testing.T) {
	kubectlRunner := util.NewKubectlRunner(t)
M
Medya Gh 已提交
337 338 339 340 341
	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")
342 343 344 345
	if _, err := kubectlRunner.RunCommand([]string{"delete", "-f", untrustedPath}); err != nil {
		t.Logf("error deleting untrusted nginx resource: %v", err)
	}
}