addons_test.go 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// +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 (
	"fmt"
23
	"io/ioutil"
24
	"net"
25
	"net/http"
26
	"net/url"
K
kairen 已提交
27
	"path/filepath"
28
	"strings"
29 30 31
	"testing"
	"time"

32
	"k8s.io/apimachinery/pkg/labels"
M
Matt Rickard 已提交
33
	pkgutil "k8s.io/minikube/pkg/util"
34 35 36
	"k8s.io/minikube/test/integration/util"
)

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

49 50
func testDashboard(t *testing.T) {
	t.Parallel()
51
	minikubeRunner := NewMinikubeRunner(t)
52

T
Thomas Stromberg 已提交
53
	cmd, out := minikubeRunner.RunDaemon("dashboard --url")
54 55
	defer func() {
		err := cmd.Process.Kill()
56
		if err != nil {
57
			t.Logf("Failed to kill mount command: %v", err)
58
		}
59 60 61 62 63
	}()

	s, err := out.ReadString('\n')
	if err != nil {
		t.Fatalf("failed to read url: %v", err)
64
	}
65

66 67 68
	u, err := url.Parse(strings.TrimSpace(s))
	if err != nil {
		t.Fatalf("failed to parse %q: %v", s, err)
69
	}
70

71
	if u.Scheme != "http" {
72
		t.Errorf("got Scheme %s, expected http", u.Scheme)
73
	}
74
	host, _, err := net.SplitHostPort(u.Host)
75
	if err != nil {
76
		t.Fatalf("failed SplitHostPort: %v", err)
77 78
	}
	if host != "127.0.0.1" {
79
		t.Errorf("got host %s, expected 127.0.0.1", host)
80
	}
81 82 83 84 85 86 87 88 89 90 91

	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)
92
	}
93
}
94

K
kairen 已提交
95 96 97 98 99 100 101
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 {
102
		t.Fatalf("waiting for ingress-controller to be up: %v", err)
K
kairen 已提交
103 104 105
	}

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

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

114 115
	podPath, _ := filepath.Abs("testdata/nginx-pod-svc.yaml")
	if _, err := kubectlRunner.RunCommand([]string{"create", "-f", podPath}); err != nil {
116
		t.Fatalf("creating nginx ingress resource: %v", err)
K
kairen 已提交
117 118 119
	}

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

123 124 125 126 127 128 129 130 131 132 133 134
	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 已提交
135 136
	}

137 138
	defer kubectlRunner.RunCommand([]string{"delete", "-f", podPath})
	defer kubectlRunner.RunCommand([]string{"delete", "-f", ingressPath})
K
kairen 已提交
139 140 141
	minikubeRunner.RunCommand("addons disable ingress", true)
}

142 143
func testServicesList(t *testing.T) {
	t.Parallel()
144
	minikubeRunner := NewMinikubeRunner(t)
145

146 147 148
	checkServices := func() error {
		output := minikubeRunner.RunCommand("service list", false)
		if !strings.Contains(output, "kubernetes") {
149
			return fmt.Errorf("Error, kubernetes service missing from output %s", output)
150
		}
151 152
		return nil
	}
153
	if err := util.Retry(t, checkServices, 2*time.Second, 5); err != nil {
154
		t.Fatalf(err.Error())
155 156
	}
}