Skip to content

Commit 65ed4ad

Browse files
Merge pull request #12839 from spowelljr/addonListNotRunning
`minikube addons list` tests
2 parents 78dec3b + f0f1819 commit 65ed4ad

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

cmd/minikube/cmd/config/addons_list.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ var printAddonsJSON = func(cc *config.ClusterConfig) {
146146
addonsMap := map[string]map[string]interface{}{}
147147

148148
for _, addonName := range addonNames {
149+
if cc == nil {
150+
addonsMap[addonName] = map[string]interface{}{}
151+
continue
152+
}
153+
149154
addonBundle := assets.Addons[addonName]
150155
enabled := addonBundle.IsEnabled(cc)
151156

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Copyright 2021 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 config
18+
19+
import (
20+
"encoding/json"
21+
"os"
22+
"testing"
23+
24+
"k8s.io/minikube/pkg/minikube/out"
25+
)
26+
27+
func TestAddonsList(t *testing.T) {
28+
t.Run("NonExistingClusterTable", func(t *testing.T) {
29+
b := make([]byte, 167)
30+
r, w, err := os.Pipe()
31+
if err != nil {
32+
t.Fatalf("failed to create pipe: %v", err)
33+
}
34+
old := os.Stdout
35+
defer func() { os.Stdout = old }()
36+
os.Stdout = w
37+
printAddonsList(nil)
38+
if err := w.Close(); err != nil {
39+
t.Fatalf("failed to close pipe: %v", err)
40+
}
41+
if _, err := r.Read(b); err != nil {
42+
t.Fatalf("failed to read bytes: %v", err)
43+
}
44+
got := string(b)
45+
expected := `|-----------------------------|-----------------------|
46+
| ADDON NAME | MAINTAINER |
47+
|-----------------------------|-----------------------|`
48+
if got != expected {
49+
t.Errorf("Expected header to be: %q; got = %q", expected, got)
50+
}
51+
})
52+
53+
t.Run("NonExistingClusterJSON", func(t *testing.T) {
54+
type addons struct {
55+
Ambassador *interface{} `json:"ambassador"`
56+
}
57+
58+
b := make([]byte, 534)
59+
r, w, err := os.Pipe()
60+
if err != nil {
61+
t.Fatalf("failed to create pipe: %v", err)
62+
}
63+
old := os.Stdout
64+
defer func() {
65+
os.Stdout = old
66+
out.SetOutFile(os.Stdout)
67+
}()
68+
os.Stdout = w
69+
out.SetOutFile(os.Stdout)
70+
printAddonsJSON(nil)
71+
if err := w.Close(); err != nil {
72+
t.Fatalf("failed to close pipe: %v", err)
73+
}
74+
if _, err := r.Read(b); err != nil {
75+
t.Fatalf("failed to read bytes: %v", err)
76+
}
77+
got := addons{}
78+
if err := json.Unmarshal(b, &got); err != nil {
79+
t.Fatalf("failed to unmarshal output; output: %q; err: %v", string(b), err)
80+
}
81+
if got.Ambassador == nil {
82+
t.Errorf("expected `ambassador` field to not be nil, but was")
83+
}
84+
})
85+
}

0 commit comments

Comments
 (0)