status_test.go 3.5 KB
Newer Older
T
Thomas Stromberg 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
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 cmd

import (
	"bytes"
	"encoding/json"
	"testing"
)

func TestExitCode(t *testing.T) {
	var tests = []struct {
		name  string
		want  int
		state *Status
	}{
		{"ok", 0, &Status{Host: "Running", Kubelet: "Running", APIServer: "Running", Kubeconfig: Configured}},
		{"paused", 2, &Status{Host: "Running", Kubelet: "Stopped", APIServer: "Paused", Kubeconfig: Configured}},
		{"down", 7, &Status{Host: "Stopped", Kubelet: "Stopped", APIServer: "Stopped", Kubeconfig: Misconfigured}},
34
		{"missing", 7, &Status{Host: "Nonexistent", Kubelet: "Nonexistent", APIServer: "Nonexistent", Kubeconfig: "Nonexistent"}},
T
Thomas Stromberg 已提交
35 36 37
	}
	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
38
			got := exitCode([]*Status{tc.state})
T
Thomas Stromberg 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
			if got != tc.want {
				t.Errorf("exitcode(%+v) = %d, want: %d", tc.state, got, tc.want)
			}
		})
	}
}

func TestStatusText(t *testing.T) {
	var tests = []struct {
		name  string
		state *Status
		want  string
	}{
		{
			name:  "ok",
T
Tharun 已提交
54 55
			state: &Status{Name: "minikube", Host: "Running", Kubelet: "Running", APIServer: "Running", Kubeconfig: Configured, TimeToStop: "10m"},
			want:  "minikube\ntype: Control Plane\nhost: Running\nkubelet: Running\napiserver: Running\nkubeconfig: Configured\ntimeToStop: 10m\n\n",
T
Thomas Stromberg 已提交
56 57 58
		},
		{
			name:  "paused",
59 60
			state: &Status{Name: "minikube", Host: "Running", Kubelet: "Stopped", APIServer: "Paused", Kubeconfig: Configured},
			want:  "minikube\ntype: Control Plane\nhost: Running\nkubelet: Stopped\napiserver: Paused\nkubeconfig: Configured\n\n",
T
Thomas Stromberg 已提交
61 62 63
		},
		{
			name:  "down",
64 65
			state: &Status{Name: "minikube", Host: "Stopped", Kubelet: "Stopped", APIServer: "Stopped", Kubeconfig: Misconfigured},
			want:  "minikube\ntype: Control Plane\nhost: Stopped\nkubelet: Stopped\napiserver: Stopped\nkubeconfig: Misconfigured\n\n\nWARNING: Your kubectl is pointing to stale minikube-vm.\nTo fix the kubectl context, run `minikube update-context`\n",
T
Thomas Stromberg 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
		},
	}
	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			var b bytes.Buffer
			err := statusText(tc.state, &b)
			if err != nil {
				t.Errorf("text(%+v) error: %v", tc.state, err)
			}

			got := b.String()
			if got != tc.want {
				t.Errorf("text(%+v) = %q, want: %q", tc.state, got, tc.want)
			}
		})
	}
}

func TestStatusJSON(t *testing.T) {
	var tests = []struct {
		name  string
		state *Status
	}{
T
Tharun 已提交
89
		{"ok", &Status{Host: "Running", Kubelet: "Running", APIServer: "Running", Kubeconfig: Configured, TimeToStop: "10m"}},
90 91
		{"paused", &Status{Host: "Running", Kubelet: "Stopped", APIServer: "Paused", Kubeconfig: Configured}},
		{"down", &Status{Host: "Stopped", Kubelet: "Stopped", APIServer: "Stopped", Kubeconfig: Misconfigured}},
T
Thomas Stromberg 已提交
92 93 94 95
	}
	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			var b bytes.Buffer
96
			err := statusJSON([]*Status{tc.state}, &b)
T
Thomas Stromberg 已提交
97 98 99 100 101 102 103 104 105 106 107
			if err != nil {
				t.Errorf("json(%+v) error: %v", tc.state, err)
			}

			st := &Status{}
			if err := json.Unmarshal(b.Bytes(), st); err != nil {
				t.Errorf("json(%+v) unmarshal error: %v", tc.state, err)
			}
		})
	}
}