-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support waiting for components (#171)
* support waiting for components * add simple wait tf test * implement wait validation and parse special options * add wait acceptance test
- Loading branch information
1 parent
3bb2d55
commit c55e199
Showing
6 changed files
with
316 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package lib | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
var standardOptions = []string{ | ||
"apiserver", | ||
"system_pods", | ||
"default_sa", | ||
"apps_running", | ||
"node_ready", | ||
"kubelet", | ||
} | ||
|
||
var specialOptions = []string{ | ||
"all", | ||
"none", | ||
"true", | ||
"false", | ||
} | ||
|
||
func ValidateWait(v map[string]bool) error { | ||
var invalidOptions []string | ||
|
||
for key := range v { | ||
if !contains(standardOptions, key) || contains(specialOptions, key) { | ||
invalidOptions = append(invalidOptions, key) | ||
} | ||
} | ||
|
||
if len(invalidOptions) > 0 { | ||
return fmt.Errorf("invalid wait option(s): %s", strings.Join(invalidOptions, ", ")) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func contains(slice []string, item string) bool { | ||
for _, s := range slice { | ||
if s == item { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func ResolveSpecialWaitOptions(input map[string]bool) map[string]bool { | ||
if input["all"] || input["true"] { | ||
result := make(map[string]bool) | ||
for _, opt := range standardOptions { | ||
result[opt] = true | ||
} | ||
return result | ||
} | ||
|
||
if input["none"] || input["false"] { | ||
return make(map[string]bool) | ||
} | ||
|
||
return input | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package lib | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestValidateWait(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input map[string]bool | ||
expectedError string | ||
}{ | ||
{ | ||
name: "Valid options", | ||
input: map[string]bool{"apiserver": true, "system_pods": true}, | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "Invalid option", | ||
input: map[string]bool{"invalid_option": true}, | ||
expectedError: "invalid wait option(s): invalid_option", | ||
}, | ||
{ | ||
name: "Multiple invalid options", | ||
input: map[string]bool{"invalid1": true, "invalid2": true, "apiserver": true}, | ||
expectedError: "invalid wait option(s): invalid1, invalid2", | ||
}, | ||
{ | ||
name: "Special option", | ||
input: map[string]bool{"all": true}, | ||
expectedError: "invalid wait option(s): all", | ||
}, | ||
{ | ||
name: "Empty input", | ||
input: map[string]bool{}, | ||
expectedError: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := ValidateWait(tt.input) | ||
if (err == nil && tt.expectedError != "") || (err != nil && err.Error() != tt.expectedError) { | ||
t.Errorf("ValidateWait() error = %v, expectedError %v", err, tt.expectedError) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestResolveSpecialWaitOptions(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input map[string]bool | ||
expected map[string]bool | ||
}{ | ||
{ | ||
name: "All true", | ||
input: map[string]bool{"all": true}, | ||
expected: map[string]bool{"apiserver": true, "system_pods": true, "default_sa": true, "apps_running": true, "node_ready": true, "kubelet": true}, | ||
}, | ||
{ | ||
name: "True", | ||
input: map[string]bool{"true": true}, | ||
expected: map[string]bool{"apiserver": true, "system_pods": true, "default_sa": true, "apps_running": true, "node_ready": true, "kubelet": true}, | ||
}, | ||
{ | ||
name: "None", | ||
input: map[string]bool{"none": true}, | ||
expected: map[string]bool{}, | ||
}, | ||
{ | ||
name: "False", | ||
input: map[string]bool{"false": true}, | ||
expected: map[string]bool{}, | ||
}, | ||
{ | ||
name: "Standard options", | ||
input: map[string]bool{"apiserver": true, "system_pods": true}, | ||
expected: map[string]bool{"apiserver": true, "system_pods": true}, | ||
}, | ||
{ | ||
name: "Empty input", | ||
input: map[string]bool{}, | ||
expected: map[string]bool{}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := ResolveSpecialWaitOptions(tt.input) | ||
if !reflect.DeepEqual(result, tt.expected) { | ||
t.Errorf("ResolveSpecialWaitOptions() = %v, want %v", result, tt.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestContains(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
slice []string | ||
item string | ||
expected bool | ||
}{ | ||
{ | ||
name: "Item present", | ||
slice: []string{"a", "b", "c"}, | ||
item: "b", | ||
expected: true, | ||
}, | ||
{ | ||
name: "Item not present", | ||
slice: []string{"a", "b", "c"}, | ||
item: "d", | ||
expected: false, | ||
}, | ||
{ | ||
name: "Empty slice", | ||
slice: []string{}, | ||
item: "a", | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := contains(tt.slice, tt.item) | ||
if result != tt.expected { | ||
t.Errorf("contains() = %v, want %v", result, tt.expected) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.