addons_test.go 5.1 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
	"k8s.io/apimachinery/pkg/labels"
M
Matt Rickard 已提交
34
	pkgutil "k8s.io/minikube/pkg/util"
35 36 37
	"k8s.io/minikube/test/integration/util"
)

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

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

87 88
	t.Logf("Waiting for URL to be output by minikube dashboard...")
	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 109 110 111 112 113 114 115 116 117 118
		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)
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 136 137
	}

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

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

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

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

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

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

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