|
| 1 | +/* |
| 2 | +Copyright 2024 The Karmada Authors. |
| 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 framework |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "sort" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +func TestPluginToResult_Merge(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + results PluginToResult |
| 31 | + want *Result |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "empty results", |
| 35 | + results: PluginToResult{}, |
| 36 | + want: nil, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "all success results", |
| 40 | + results: PluginToResult{ |
| 41 | + "plugin1": NewResult(Success), |
| 42 | + "plugin2": NewResult(Success), |
| 43 | + }, |
| 44 | + want: NewResult(Success), |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "mixed results with unschedulable", |
| 48 | + results: PluginToResult{ |
| 49 | + "plugin1": NewResult(Success), |
| 50 | + "plugin2": NewResult(Unschedulable, "reason1"), |
| 51 | + "plugin3": NewResult(Success), |
| 52 | + }, |
| 53 | + want: NewResult(Unschedulable, "reason1"), |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "mixed results with error", |
| 57 | + results: PluginToResult{ |
| 58 | + "plugin1": NewResult(Success), |
| 59 | + "plugin2": NewResult(Unschedulable, "reason1"), |
| 60 | + "plugin3": NewResult(Error, "error occurred"), |
| 61 | + }, |
| 62 | + want: NewResult(Error, "reason1", "error occurred"), |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tt := range tests { |
| 67 | + t.Run(tt.name, func(t *testing.T) { |
| 68 | + got := tt.results.Merge() |
| 69 | + if tt.want == nil { |
| 70 | + assert.Nil(t, got) |
| 71 | + } else { |
| 72 | + assert.NotNil(t, got) |
| 73 | + assert.Equal(t, tt.want.code, got.code) |
| 74 | + |
| 75 | + // Sort the reasons before comparing |
| 76 | + sortedWantReasons := make([]string, len(tt.want.reasons)) |
| 77 | + copy(sortedWantReasons, tt.want.reasons) |
| 78 | + sort.Strings(sortedWantReasons) |
| 79 | + |
| 80 | + sortedGotReasons := make([]string, len(got.reasons)) |
| 81 | + copy(sortedGotReasons, got.reasons) |
| 82 | + sort.Strings(sortedGotReasons) |
| 83 | + |
| 84 | + assert.Equal(t, sortedWantReasons, sortedGotReasons) |
| 85 | + |
| 86 | + if tt.want.err != nil { |
| 87 | + assert.Error(t, got.err) |
| 88 | + } else { |
| 89 | + assert.NoError(t, got.err) |
| 90 | + } |
| 91 | + } |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestResult_IsSuccess(t *testing.T) { |
| 97 | + tests := []struct { |
| 98 | + name string |
| 99 | + result *Result |
| 100 | + want bool |
| 101 | + }{ |
| 102 | + { |
| 103 | + name: "nil result", |
| 104 | + result: nil, |
| 105 | + want: true, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "success result", |
| 109 | + result: NewResult(Success), |
| 110 | + want: true, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "unschedulable result", |
| 114 | + result: NewResult(Unschedulable), |
| 115 | + want: false, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "error result", |
| 119 | + result: NewResult(Error), |
| 120 | + want: false, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + for _, tt := range tests { |
| 125 | + t.Run(tt.name, func(t *testing.T) { |
| 126 | + assert.Equal(t, tt.want, tt.result.IsSuccess()) |
| 127 | + }) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestResult_AsError(t *testing.T) { |
| 132 | + tests := []struct { |
| 133 | + name string |
| 134 | + result *Result |
| 135 | + wantErr bool |
| 136 | + errorMsg string |
| 137 | + }{ |
| 138 | + { |
| 139 | + name: "success result", |
| 140 | + result: NewResult(Success), |
| 141 | + wantErr: false, |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "unschedulable result", |
| 145 | + result: NewResult(Unschedulable, "reason1", "reason2"), |
| 146 | + wantErr: true, |
| 147 | + errorMsg: "reason1, reason2", |
| 148 | + }, |
| 149 | + { |
| 150 | + name: "error result", |
| 151 | + result: NewResult(Error, "error occurred"), |
| 152 | + wantErr: true, |
| 153 | + errorMsg: "error occurred", |
| 154 | + }, |
| 155 | + } |
| 156 | + |
| 157 | + for _, tt := range tests { |
| 158 | + t.Run(tt.name, func(t *testing.T) { |
| 159 | + err := tt.result.AsError() |
| 160 | + if tt.wantErr { |
| 161 | + assert.Error(t, err) |
| 162 | + assert.Equal(t, tt.errorMsg, err.Error()) |
| 163 | + } else { |
| 164 | + assert.NoError(t, err) |
| 165 | + } |
| 166 | + }) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func TestResult_AsResult(t *testing.T) { |
| 171 | + tests := []struct { |
| 172 | + name string |
| 173 | + err error |
| 174 | + wantCode Code |
| 175 | + wantReasons []string |
| 176 | + }{ |
| 177 | + { |
| 178 | + name: "non-nil error", |
| 179 | + err: errors.New("test error"), |
| 180 | + wantCode: Error, |
| 181 | + wantReasons: []string{"test error"}, |
| 182 | + }, |
| 183 | + } |
| 184 | + |
| 185 | + for _, tt := range tests { |
| 186 | + t.Run(tt.name, func(t *testing.T) { |
| 187 | + got := AsResult(tt.err) |
| 188 | + assert.Equal(t, tt.wantCode, got.code) |
| 189 | + assert.Equal(t, tt.wantReasons, got.reasons) |
| 190 | + assert.Equal(t, tt.err, got.err) |
| 191 | + }) |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +func TestResult_Code(t *testing.T) { |
| 196 | + tests := []struct { |
| 197 | + name string |
| 198 | + result *Result |
| 199 | + want Code |
| 200 | + }{ |
| 201 | + { |
| 202 | + name: "nil result", |
| 203 | + result: nil, |
| 204 | + want: Success, |
| 205 | + }, |
| 206 | + { |
| 207 | + name: "success result", |
| 208 | + result: NewResult(Success), |
| 209 | + want: Success, |
| 210 | + }, |
| 211 | + { |
| 212 | + name: "unschedulable result", |
| 213 | + result: NewResult(Unschedulable), |
| 214 | + want: Unschedulable, |
| 215 | + }, |
| 216 | + { |
| 217 | + name: "error result", |
| 218 | + result: NewResult(Error), |
| 219 | + want: Error, |
| 220 | + }, |
| 221 | + } |
| 222 | + |
| 223 | + for _, tt := range tests { |
| 224 | + t.Run(tt.name, func(t *testing.T) { |
| 225 | + got := tt.result.Code() |
| 226 | + assert.Equal(t, tt.want, got) |
| 227 | + }) |
| 228 | + } |
| 229 | +} |
| 230 | + |
| 231 | +func TestCode_String(t *testing.T) { |
| 232 | + tests := []struct { |
| 233 | + name string |
| 234 | + code Code |
| 235 | + want string |
| 236 | + }{ |
| 237 | + { |
| 238 | + name: "Success code", |
| 239 | + code: Success, |
| 240 | + want: "Success", |
| 241 | + }, |
| 242 | + { |
| 243 | + name: "Unschedulable code", |
| 244 | + code: Unschedulable, |
| 245 | + want: "Unschedulable", |
| 246 | + }, |
| 247 | + { |
| 248 | + name: "Error code", |
| 249 | + code: Error, |
| 250 | + want: "Error", |
| 251 | + }, |
| 252 | + } |
| 253 | + |
| 254 | + for _, tt := range tests { |
| 255 | + t.Run(tt.name, func(t *testing.T) { |
| 256 | + got := tt.code.String() |
| 257 | + assert.Equal(t, tt.want, got) |
| 258 | + }) |
| 259 | + } |
| 260 | +} |
0 commit comments