|
| 1 | +/* |
| 2 | +Copyright 2016 The Kubernetes Authors All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "encoding/json" |
| 22 | + "testing" |
| 23 | +) |
| 24 | + |
| 25 | +func TestExitCode(t *testing.T) { |
| 26 | + var tests = []struct { |
| 27 | + name string |
| 28 | + want int |
| 29 | + state *Status |
| 30 | + }{ |
| 31 | + {"ok", 0, &Status{Host: "Running", Kubelet: "Running", APIServer: "Running", Kubeconfig: Configured}}, |
| 32 | + {"paused", 2, &Status{Host: "Running", Kubelet: "Stopped", APIServer: "Paused", Kubeconfig: Configured}}, |
| 33 | + {"down", 7, &Status{Host: "Stopped", Kubelet: "Stopped", APIServer: "Stopped", Kubeconfig: Misconfigured}}, |
| 34 | + } |
| 35 | + for _, tc := range tests { |
| 36 | + t.Run(tc.name, func(t *testing.T) { |
| 37 | + got := exitCode(tc.state) |
| 38 | + if got != tc.want { |
| 39 | + t.Errorf("exitcode(%+v) = %d, want: %d", tc.state, got, tc.want) |
| 40 | + } |
| 41 | + }) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestStatusText(t *testing.T) { |
| 46 | + var tests = []struct { |
| 47 | + name string |
| 48 | + state *Status |
| 49 | + want string |
| 50 | + }{ |
| 51 | + { |
| 52 | + name: "ok", |
| 53 | + state: &Status{Host: "Running", Kubelet: "Running", APIServer: "Running", Kubeconfig: Configured}, |
| 54 | + want: "host: Running\nkubelet: Running\napiserver: Running\nkubeconfig: Configured\n", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "paused", |
| 58 | + state: &Status{Host: "Running", Kubelet: "Stopped", APIServer: "Paused", Kubeconfig: Configured}, |
| 59 | + want: "host: Running\nkubelet: Stopped\napiserver: Paused\nkubeconfig: Configured\n", |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "down", |
| 63 | + state: &Status{Host: "Stopped", Kubelet: "Stopped", APIServer: "Stopped", Kubeconfig: Misconfigured}, |
| 64 | + want: "host: Stopped\nkubelet: Stopped\napiserver: Stopped\nkubeconfig: Misconfigured\n\nWARNING: Your kubectl is pointing to stale minikube-vm.\nTo fix the kubectl context, run `minikube update-context`\n", |
| 65 | + }, |
| 66 | + } |
| 67 | + for _, tc := range tests { |
| 68 | + t.Run(tc.name, func(t *testing.T) { |
| 69 | + var b bytes.Buffer |
| 70 | + err := statusText(tc.state, &b) |
| 71 | + if err != nil { |
| 72 | + t.Errorf("text(%+v) error: %v", tc.state, err) |
| 73 | + } |
| 74 | + |
| 75 | + got := b.String() |
| 76 | + if got != tc.want { |
| 77 | + t.Errorf("text(%+v) = %q, want: %q", tc.state, got, tc.want) |
| 78 | + } |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestStatusJSON(t *testing.T) { |
| 84 | + var tests = []struct { |
| 85 | + name string |
| 86 | + state *Status |
| 87 | + }{ |
| 88 | + {"ok", &Status{Host: "Running", Kubelet: "Running", APIServer: "Running", Kubeconfig: Configured}}, |
| 89 | + {"paused", &Status{Host: "Running", Kubelet: "Stopped", APIServer: "Paused", Kubeconfig: Configured}}, |
| 90 | + {"down", &Status{Host: "Stopped", Kubelet: "Stopped", APIServer: "Stopped", Kubeconfig: Misconfigured}}, |
| 91 | + } |
| 92 | + for _, tc := range tests { |
| 93 | + t.Run(tc.name, func(t *testing.T) { |
| 94 | + var b bytes.Buffer |
| 95 | + err := statusJSON(tc.state, &b) |
| 96 | + if err != nil { |
| 97 | + t.Errorf("json(%+v) error: %v", tc.state, err) |
| 98 | + } |
| 99 | + |
| 100 | + st := &Status{} |
| 101 | + if err := json.Unmarshal(b.Bytes(), st); err != nil { |
| 102 | + t.Errorf("json(%+v) unmarshal error: %v", tc.state, err) |
| 103 | + } |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
0 commit comments