|
| 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 | +} |
0 commit comments