func TestAddons(t *testing.T) { minikubeRunner := util.MinikubeRunner{ BinaryPath: *binaryPath, Args: *args, T: t} minikubeRunner.EnsureRunning() kubectlRunner := util.NewKubectlRunner(t) checkAddon := func() error { pods := api.PodList{} if err := kubectlRunner.RunCommandParseOutput(addonManagerCmd, &pods); err != nil { return err } for _, p := range pods.Items { if strings.HasPrefix(p.ObjectMeta.Name, "kube-addon-manager-") { if p.Status.Phase == "Running" { return nil } return fmt.Errorf("Pod is not Running. Status: %s", p.Status.Phase) } } return fmt.Errorf("Addon manager not found. Found pods: %s", pods) } if err := commonutil.RetryAfter(20, checkAddon, 5*time.Second); err != nil { t.Fatalf("Addon Manager pod is unhealthy: %s", err) } }
func TestDashboard(t *testing.T) { minikubeRunner := util.MinikubeRunner{ BinaryPath: *binaryPath, Args: *args, T: t} minikubeRunner.Start() minikubeRunner.CheckStatus("Running") kubectlRunner := util.NewKubectlRunner(t) checkDashboard := func() error { rc := api.ReplicationController{} svc := api.Service{} if err := kubectlRunner.RunCommandParseOutput(dashboardRcCmd, &rc); err != nil { return err } if err := kubectlRunner.RunCommandParseOutput(dashboardSvcCmd, &svc); err != nil { return err } if rc.Status.Replicas != rc.Status.FullyLabeledReplicas { return fmt.Errorf("Not enough pods running. Expected %s, got %s.", rc.Status.Replicas, rc.Status.FullyLabeledReplicas) } if svc.Spec.Ports[0].NodePort != 30000 { return fmt.Errorf("Dashboard is not exposed on port {}", svc.Spec.Ports[0].NodePort) } return nil } if err := commonutil.RetryAfter(10, checkDashboard, 5*time.Second); err != nil { t.Fatalf("Dashboard is unhealthy: %s", err) } }
func TestAddons(t *testing.T) { minikubeRunner := util.MinikubeRunner{ BinaryPath: *binaryPath, Args: *args, T: t} minikubeRunner.EnsureRunning() kubectlRunner := util.NewKubectlRunner(t) checkAddon := func() error { p := api.Pod{} if err := kubectlRunner.RunCommandParseOutput(addonManagerCmd, &p); err != nil { return err } if p.Status.Phase != "Running" { return fmt.Errorf("Pod is not Running. Status: %s", p.Status.Phase) } return nil } if err := commonutil.RetryAfter(10, checkAddon, 5*time.Second); err != nil { t.Fatalf("Addon Manager pod is unhealthy: %s", err) } }
func TestClusterStatus(t *testing.T) { minikubeRunner := util.MinikubeRunner{ Args: *args, BinaryPath: *binaryPath, T: t} minikubeRunner.EnsureRunning() kubectlRunner := util.NewKubectlRunner(t) cs := api.ComponentStatusList{} healthy := func() error { if err := kubectlRunner.RunCommandParseOutput([]string{"get", "cs"}, &cs); err != nil { return err } for _, i := range cs.Items { status := api.ConditionFalse for _, c := range i.Conditions { if c.Type != api.ComponentHealthy { continue } fmt.Printf("Component: %s, Healthy: %s.\n", i.GetName(), c.Status) status = c.Status } if status != api.ConditionTrue { return fmt.Errorf("Component %s is not Healthy! Status: %s", i.GetName(), status) } } return nil } if err := commonutil.RetryAfter(4, healthy, 1*time.Second); err != nil { t.Fatalf("Cluster is not healthy: %s", err) } }
func (k *KubectlRunner) RunCommand(args []string) (stdout []byte, err error) { inner := func() error { cmd := exec.Command(k.BinaryPath, args...) stdout, err = cmd.CombinedOutput() if err != nil { log.Errorf("Error %s running command %s. Return code: %s", stdout, args, err) return fmt.Errorf("Error running command. Error %s. Output: %s", err, stdout) } return nil } err = commonutil.RetryAfter(3, inner, 2*time.Second) return stdout, err }
func TestClusterDNS(t *testing.T) { minikubeRunner := util.MinikubeRunner{ BinaryPath: *binaryPath, Args: *args, T: t} minikubeRunner.EnsureRunning() kubectlRunner := util.NewKubectlRunner(t) podName := "busybox" podPath, _ := filepath.Abs("testdata/busybox.yaml") dnsTest := func() error { podNamespace := kubectlRunner.CreateRandomNamespace() defer kubectlRunner.DeleteNamespace(podNamespace) if _, err := kubectlRunner.RunCommand([]string{"create", "-f", podPath, "--namespace=" + podNamespace}); err != nil { return err } defer kubectlRunner.RunCommand([]string{"delete", "-f", podPath, "--namespace=" + podNamespace}) p := api.Pod{} for p.Status.Phase != "Running" { if err := kubectlRunner.RunCommandParseOutput([]string{"get", "pod", podName, "--namespace=" + podNamespace}, &p); err != nil { return err } } dnsByteArr, err := kubectlRunner.RunCommand([]string{"exec", podName, "--namespace=" + podNamespace, "nslookup", "kubernetes.default"}) dnsOutput := string(dnsByteArr) if err != nil { return err } if !strings.Contains(dnsOutput, "10.0.0.1") || !strings.Contains(dnsOutput, "10.0.0.10") { return fmt.Errorf("DNS lookup failed, could not find both 10.0.0.1 and 10.0.0.10. Output: %s", dnsOutput) } return nil } if err := commonutil.RetryAfter(4, dnsTest, 1*time.Second); err != nil { t.Fatalf("DNS lookup failed with error:", err) } }
func TestClusterEnv(t *testing.T) { minikubeRunner := util.MinikubeRunner{ Args: *args, BinaryPath: *binaryPath, T: t} minikubeRunner.EnsureRunning() dockerEnvVars := minikubeRunner.RunCommand("docker-env", true) if err := minikubeRunner.SetEnvFromEnvCmdOutput(dockerEnvVars); err != nil { t.Fatalf("Error parsing output: %s", err) } path, err := exec.LookPath("docker") var output []byte dockerPs := func() error { cmd := exec.Command(path, "ps") output, err = cmd.CombinedOutput() return err } if err := commonutil.RetryAfter(5, dockerPs, 3*time.Second); err != nil { t.Fatalf("Error running command: %s. Error: %s Output: %s", "docker ps", err, output) } }
) // dashboardCmd represents the dashboard command var dashboardCmd = &cobra.Command{ Use: "dashboard", Short: "Opens/displays the kubernetes dashboard URL for your local cluster", Long: `Opens/displays the kubernetes dashboard URL for your local cluster`, Run: func(cmd *cobra.Command, args []string) { api := libmachine.NewClient(constants.Minipath, constants.MakeMiniPath("certs")) defer api.Close() cluster.EnsureMinikubeRunningOrExit(api) namespace := "kube-system" service := "kubernetes-dashboard" if err := commonutil.RetryAfter(20, func() error { return CheckService(namespace, service) }, 6*time.Second); err != nil { fmt.Fprintf(os.Stderr, "Could not find finalized endpoint being pointed to by %s: %s\n", service, err) os.Exit(1) } url, err := cluster.GetServiceURL(api, namespace, service) if err != nil { fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, "Check that minikube is running.") os.Exit(1) } if dashboardURLMode { fmt.Fprintln(os.Stdout, url) } else { fmt.Fprintln(os.Stdout, "Opening kubernetes dashboard in default browser...") browser.OpenURL(url)
func TestPersistence(t *testing.T) { minikubeRunner := util.MinikubeRunner{BinaryPath: *binaryPath, T: t} minikubeRunner.EnsureRunning() kubectlRunner := util.NewKubectlRunner(t) podName := "busybox" podPath, _ := filepath.Abs("testdata/busybox.yaml") podNamespace := kubectlRunner.CreateRandomNamespace() defer kubectlRunner.DeleteNamespace(podNamespace) // Create a pod and wait for it to be running. if _, err := kubectlRunner.RunCommand([]string{"create", "-f", podPath, "--namespace=" + podNamespace}); err != nil { t.Fatalf("Error creating test pod: %s", err) } checkPod := func() error { p := kubectlRunner.GetPod(podName, podNamespace) if util.IsPodReady(p) { return nil } return fmt.Errorf("Pod %s is not ready yet.", podName) } if err := commonutil.RetryAfter(20, checkPod, 6*time.Second); err != nil { t.Fatalf("Error checking the status of pod %s. Err: %s", podName, err) } checkDashboard := func() error { pods := api.PodList{} cmd := []string{"get", "pods", "--namespace=kube-system", "--selector=app=kubernetes-dashboard"} if err := kubectlRunner.RunCommandParseOutput(cmd, &pods); err != nil { return err } if len(pods.Items) < 1 { return fmt.Errorf("No pods found matching query: %v", cmd) } db := pods.Items[0] if util.IsPodReady(&db) { return nil } return fmt.Errorf("Dashboard pod is not ready yet.") } // Make sure the dashboard is running before we stop the VM. // On slow networks it can take several minutes to pull the addon-manager then the dashboard image. if err := commonutil.RetryAfter(20, checkDashboard, 6*time.Second); err != nil { t.Fatalf("Dashboard pod is not healthy: %s", err) } // Now restart minikube and make sure the pod is still there. minikubeRunner.RunCommand("stop", true) minikubeRunner.CheckStatus("Stopped") minikubeRunner.RunCommand("start", true) minikubeRunner.CheckStatus("Running") if err := commonutil.RetryAfter(5, checkPod, 3*time.Second); err != nil { t.Fatalf("Error checking the status of pod %s. Err: %s", podName, err) } // Now make sure it's still running after. if err := commonutil.RetryAfter(5, checkDashboard, 3*time.Second); err != nil { t.Fatalf("Dashboard pod is not healthy: %s", err) } }