Skip to content

Commit 6ce0b0e

Browse files
implement wait validation and parse special options
1 parent fd8bcae commit 6ce0b0e

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed

minikube/lib/wait_validator.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package lib
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
var standardOptions = []string{
9+
"apiserver",
10+
"system_pods",
11+
"default_sa",
12+
"apps_running",
13+
"node_ready",
14+
"kubelet",
15+
}
16+
17+
var specialOptions = []string{
18+
"all",
19+
"none",
20+
"true",
21+
"false",
22+
}
23+
24+
func ValidateWait(v map[string]bool) error {
25+
var invalidOptions []string
26+
27+
for key := range v {
28+
if !contains(standardOptions, key) || contains(specialOptions, key) {
29+
invalidOptions = append(invalidOptions, key)
30+
}
31+
}
32+
33+
if len(invalidOptions) > 0 {
34+
return fmt.Errorf("invalid wait option(s): %s", strings.Join(invalidOptions, ", "))
35+
}
36+
37+
return nil
38+
}
39+
40+
func contains(slice []string, item string) bool {
41+
for _, s := range slice {
42+
if s == item {
43+
return true
44+
}
45+
}
46+
return false
47+
}
48+
49+
func ResolveSpecialWaitOptions(input map[string]bool) map[string]bool {
50+
if input["all"] || input["true"] {
51+
result := make(map[string]bool)
52+
for _, opt := range standardOptions {
53+
result[opt] = true
54+
}
55+
return result
56+
}
57+
58+
if input["none"] || input["false"] {
59+
return make(map[string]bool)
60+
}
61+
62+
return input
63+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package lib
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestValidateWait(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
input map[string]bool
12+
expectedError string
13+
}{
14+
{
15+
name: "Valid options",
16+
input: map[string]bool{"apiserver": true, "system_pods": true},
17+
expectedError: "",
18+
},
19+
{
20+
name: "Invalid option",
21+
input: map[string]bool{"invalid_option": true},
22+
expectedError: "invalid wait option(s): invalid_option",
23+
},
24+
{
25+
name: "Multiple invalid options",
26+
input: map[string]bool{"invalid1": true, "invalid2": true, "apiserver": true},
27+
expectedError: "invalid wait option(s): invalid1, invalid2",
28+
},
29+
{
30+
name: "Special option",
31+
input: map[string]bool{"all": true},
32+
expectedError: "invalid wait option(s): all",
33+
},
34+
{
35+
name: "Empty input",
36+
input: map[string]bool{},
37+
expectedError: "",
38+
},
39+
}
40+
41+
for _, tt := range tests {
42+
t.Run(tt.name, func(t *testing.T) {
43+
err := ValidateWait(tt.input)
44+
if (err == nil && tt.expectedError != "") || (err != nil && err.Error() != tt.expectedError) {
45+
t.Errorf("ValidateWait() error = %v, expectedError %v", err, tt.expectedError)
46+
}
47+
})
48+
}
49+
}
50+
51+
func TestResolveSpecialWaitOptions(t *testing.T) {
52+
tests := []struct {
53+
name string
54+
input map[string]bool
55+
expected map[string]bool
56+
}{
57+
{
58+
name: "All true",
59+
input: map[string]bool{"all": true},
60+
expected: map[string]bool{"apiserver": true, "system_pods": true, "default_sa": true, "apps_running": true, "node_ready": true, "kubelet": true},
61+
},
62+
{
63+
name: "True",
64+
input: map[string]bool{"true": true},
65+
expected: map[string]bool{"apiserver": true, "system_pods": true, "default_sa": true, "apps_running": true, "node_ready": true, "kubelet": true},
66+
},
67+
{
68+
name: "None",
69+
input: map[string]bool{"none": true},
70+
expected: map[string]bool{},
71+
},
72+
{
73+
name: "False",
74+
input: map[string]bool{"false": true},
75+
expected: map[string]bool{},
76+
},
77+
{
78+
name: "Standard options",
79+
input: map[string]bool{"apiserver": true, "system_pods": true},
80+
expected: map[string]bool{"apiserver": true, "system_pods": true},
81+
},
82+
{
83+
name: "Empty input",
84+
input: map[string]bool{},
85+
expected: map[string]bool{},
86+
},
87+
}
88+
89+
for _, tt := range tests {
90+
t.Run(tt.name, func(t *testing.T) {
91+
result := ResolveSpecialWaitOptions(tt.input)
92+
if !reflect.DeepEqual(result, tt.expected) {
93+
t.Errorf("ResolveSpecialWaitOptions() = %v, want %v", result, tt.expected)
94+
}
95+
})
96+
}
97+
}
98+
99+
func TestContains(t *testing.T) {
100+
tests := []struct {
101+
name string
102+
slice []string
103+
item string
104+
expected bool
105+
}{
106+
{
107+
name: "Item present",
108+
slice: []string{"a", "b", "c"},
109+
item: "b",
110+
expected: true,
111+
},
112+
{
113+
name: "Item not present",
114+
slice: []string{"a", "b", "c"},
115+
item: "d",
116+
expected: false,
117+
},
118+
{
119+
name: "Empty slice",
120+
slice: []string{},
121+
item: "a",
122+
expected: false,
123+
},
124+
}
125+
126+
for _, tt := range tests {
127+
t.Run(tt.name, func(t *testing.T) {
128+
result := contains(tt.slice, tt.item)
129+
if result != tt.expected {
130+
t.Errorf("contains() = %v, want %v", result, tt.expected)
131+
}
132+
})
133+
}
134+
}

minikube/resource_cluster.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,13 @@ func initialiseMinikubeClient(d *schema.ResourceData, m interface{}) (lib.Cluste
363363
vc[c] = true
364364
}
365365

366+
err = lib.ValidateWait(vc)
367+
if err != nil {
368+
return nil, err
369+
}
370+
371+
vc = lib.ResolveSpecialWaitOptions(vc)
372+
366373
cc := config.ClusterConfig{
367374
Addons: addonConfig,
368375
APIServerPort: d.Get("apiserver_port").(int),

0 commit comments

Comments
 (0)