addons_test.go 7.6 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"
K
kairen 已提交
28
	"path/filepath"
29
	"strings"
30 31 32
	"testing"
	"time"

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

39 40
func testAddons(t *testing.T) {
	t.Parallel()
M
Matt Rickard 已提交
41
	client, err := pkgutil.GetClient()
42
	if err != nil {
43
		t.Fatalf("Could not get kubernetes client: %v", err)
44
	}
45
	selector := labels.SelectorFromSet(labels.Set(map[string]string{"component": "kube-addon-manager"}))
M
Matt Rickard 已提交
46
	if err := pkgutil.WaitForPodsWithLabelRunning(client, "kube-system", selector); err != nil {
47
		t.Errorf("Error waiting for addon manager to be up")
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
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)
	}
}

75
func testDashboard(t *testing.T) {
76
	t.Parallel()
77
	minikubeRunner := NewMinikubeRunner(t)
T
Thomas Stromberg 已提交
78
	cmd, out := minikubeRunner.RunDaemon("dashboard --url")
79 80
	defer func() {
		err := cmd.Process.Kill()
81
		if err != nil {
82
			t.Logf("Failed to kill dashboard command: %v", err)
83
		}
84 85
	}()

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

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

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

	resp, err := http.Get(u.String())
	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)
117
	}
118
}
119

K
kairen 已提交
120 121 122 123 124 125 126
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 {
127
		t.Fatalf("waiting for ingress-controller to be up: %v", err)
K
kairen 已提交
128 129 130
	}

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

	ingressPath, _ := filepath.Abs("testdata/nginx-ing.yaml")
	if _, err := kubectlRunner.RunCommand([]string{"create", "-f", ingressPath}); err != nil {
136
		t.Fatalf("creating nginx ingress resource: %v", err)
K
kairen 已提交
137 138
	}

139 140
	podPath, _ := filepath.Abs("testdata/nginx-pod-svc.yaml")
	if _, err := kubectlRunner.RunCommand([]string{"create", "-f", podPath}); err != nil {
141
		t.Fatalf("creating nginx ingress resource: %v", err)
K
kairen 已提交
142 143 144
	}

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

148 149 150 151 152 153 154 155 156 157 158 159
	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 已提交
160 161
	}

162 163 164 165 166 167 168
	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 已提交
169 170 171
	minikubeRunner.RunCommand("addons disable ingress", true)
}

172 173
func testServicesList(t *testing.T) {
	t.Parallel()
174
	minikubeRunner := NewMinikubeRunner(t)
175

176 177 178
	checkServices := func() error {
		output := minikubeRunner.RunCommand("service list", false)
		if !strings.Contains(output, "kubernetes") {
179
			return fmt.Errorf("Error, kubernetes service missing from output %s", output)
180
		}
181 182
		return nil
	}
183
	if err := util.Retry(t, checkServices, 2*time.Second, 5); err != nil {
184
		t.Fatalf(err.Error())
185 186
	}
}
187 188 189 190 191 192 193 194 195 196

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

197
	createUntrustedWorkload(t)
198 199 200 201 202 203 204 205 206 207 208 209 210

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

211
	createUntrustedWorkload(t)
212 213 214 215 216

	t.Log("waiting for FailedCreatePodSandBox event")
	if err := util.WaitForFailedCreatePodSandBoxEvent(); err != nil {
		t.Fatalf("waiting for FailedCreatePodSandBox event: %v", err)
	}
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	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)
}
248

249 250 251 252 253 254 255 256 257 258 259 260
func createUntrustedWorkload(t *testing.T) {
	kubectlRunner := util.NewKubectlRunner(t)
	untrustedPath, _ := filepath.Abs("testdata/nginx-untrusted.yaml")
	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)
	untrustedPath, _ := filepath.Abs("testdata/nginx-untrusted.yaml")
261 262 263 264
	if _, err := kubectlRunner.RunCommand([]string{"delete", "-f", untrustedPath}); err != nil {
		t.Logf("error deleting untrusted nginx resource: %v", err)
	}
}