github.com/lablabs/operator-sdk@v0.8.2/pkg/ansible/controller/status/utils_test.go (about) 1 // Copyright 2018 The Operator-SDK Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package status 16 17 import ( 18 "reflect" 19 "testing" 20 21 "k8s.io/api/core/v1" 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 ) 24 25 func TestNewCondition(t *testing.T) { 26 testCases := []struct { 27 name string 28 condType ConditionType 29 status v1.ConditionStatus 30 ansibleResult *AnsibleResult 31 reason string 32 message string 33 expectedCondtion Condition 34 }{ 35 { 36 name: "running condition creating", 37 condType: RunningConditionType, 38 status: v1.ConditionTrue, 39 ansibleResult: nil, 40 reason: RunningReason, 41 message: RunningMessage, 42 expectedCondtion: Condition{ 43 Type: RunningConditionType, 44 Status: v1.ConditionTrue, 45 Reason: RunningReason, 46 Message: RunningMessage, 47 }, 48 }, 49 { 50 name: "failure condition creating", 51 condType: FailureConditionType, 52 status: v1.ConditionFalse, 53 ansibleResult: &AnsibleResult{ 54 Changed: 0, 55 Failures: 1, 56 Ok: 10, 57 Skipped: 1, 58 }, 59 reason: FailedReason, 60 message: "invalid parameter", 61 expectedCondtion: Condition{ 62 Type: FailureConditionType, 63 Status: v1.ConditionFalse, 64 Reason: FailedReason, 65 Message: "invalid parameter", 66 AnsibleResult: &AnsibleResult{ 67 Changed: 0, 68 Failures: 1, 69 Ok: 10, 70 Skipped: 1, 71 }, 72 }, 73 }, 74 } 75 76 for _, tc := range testCases { 77 t.Run(tc.name, func(t *testing.T) { 78 ac := NewCondition(tc.condType, tc.status, tc.ansibleResult, tc.reason, tc.message) 79 tc.expectedCondtion.LastTransitionTime = ac.LastTransitionTime 80 if !reflect.DeepEqual(*ac, tc.expectedCondtion) { 81 t.Fatalf("Condition did no match expected:\nActual: %#v\nExpected: %#v", *ac, tc.expectedCondtion) 82 } 83 }) 84 } 85 } 86 87 func TestGetCondition(t *testing.T) { 88 testCases := []struct { 89 name string 90 condType ConditionType 91 status Status 92 expectedCondition *Condition 93 }{ 94 { 95 name: "find RunningCondition", 96 condType: RunningConditionType, 97 status: Status{ 98 Conditions: []Condition{ 99 Condition{ 100 Type: RunningConditionType, 101 }, 102 }, 103 }, 104 expectedCondition: &Condition{ 105 Type: RunningConditionType, 106 }, 107 }, 108 { 109 name: "did not find RunningCondition", 110 condType: RunningConditionType, 111 status: Status{ 112 Conditions: []Condition{ 113 Condition{ 114 Type: FailureConditionType, 115 }, 116 }, 117 }, 118 expectedCondition: nil, 119 }, 120 { 121 name: "find FailureCondition", 122 condType: FailureConditionType, 123 status: Status{ 124 Conditions: []Condition{ 125 Condition{ 126 Type: FailureConditionType, 127 }, 128 }, 129 }, 130 expectedCondition: &Condition{ 131 Type: FailureConditionType, 132 }, 133 }, 134 { 135 name: "did not find FailureCondition", 136 condType: FailureConditionType, 137 status: Status{ 138 Conditions: []Condition{ 139 Condition{ 140 Type: RunningConditionType, 141 }, 142 }, 143 }, 144 expectedCondition: nil, 145 }, 146 } 147 148 for _, tc := range testCases { 149 t.Run(tc.name, func(t *testing.T) { 150 ac := GetCondition(tc.status, tc.condType) 151 if !reflect.DeepEqual(ac, tc.expectedCondition) { 152 t.Fatalf("Condition did no match expected:\nActual: %#v\nExpected: %#v", ac, tc.expectedCondition) 153 } 154 }) 155 } 156 } 157 158 func TestRemoveCondition(t *testing.T) { 159 testCases := []struct { 160 name string 161 condType ConditionType 162 status Status 163 expectedSize int 164 }{ 165 { 166 name: "remove RunningCondition", 167 condType: RunningConditionType, 168 status: Status{ 169 Conditions: []Condition{ 170 Condition{ 171 Type: RunningConditionType, 172 }, 173 }, 174 }, 175 expectedSize: 0, 176 }, 177 { 178 name: "did not find RunningCondition", 179 condType: RunningConditionType, 180 status: Status{ 181 Conditions: []Condition{ 182 Condition{ 183 Type: FailureConditionType, 184 }, 185 }, 186 }, 187 expectedSize: 1, 188 }, 189 { 190 name: "remove FailureCondition", 191 condType: FailureConditionType, 192 status: Status{ 193 Conditions: []Condition{ 194 Condition{ 195 Type: FailureConditionType, 196 }, 197 }, 198 }, 199 expectedSize: 0, 200 }, 201 { 202 name: "did not find FailureCondition", 203 condType: FailureConditionType, 204 status: Status{ 205 Conditions: []Condition{ 206 Condition{ 207 Type: RunningConditionType, 208 }, 209 }, 210 }, 211 expectedSize: 1, 212 }, 213 } 214 215 for _, tc := range testCases { 216 t.Run(tc.name, func(t *testing.T) { 217 RemoveCondition(&tc.status, tc.condType) 218 if tc.expectedSize != len(tc.status.Conditions) { 219 t.Fatalf("Conditions did no match expected size:\nActual: %#v\nExpected: %#v", len(tc.status.Conditions), tc.expectedSize) 220 } 221 }) 222 } 223 } 224 225 func TestSetCondition(t *testing.T) { 226 lastTransitionTime := metav1.Now() 227 keeptMessage := SuccessfulMessage 228 testCases := []struct { 229 name string 230 status *Status 231 condition *Condition 232 expectedNewSize int 233 keepLastTransitionTime bool 234 keepMessage bool 235 }{ 236 { 237 name: "add new condition", 238 status: &Status{ 239 Conditions: []Condition{}, 240 }, 241 condition: NewCondition(RunningConditionType, v1.ConditionTrue, nil, RunningReason, RunningMessage), 242 expectedNewSize: 1, 243 keepLastTransitionTime: false, 244 }, 245 { 246 name: "update running condition", 247 status: &Status{ 248 Conditions: []Condition{ 249 Condition{ 250 Type: RunningConditionType, 251 Status: v1.ConditionTrue, 252 Reason: SuccessfulReason, 253 Message: SuccessfulMessage, 254 LastTransitionTime: lastTransitionTime, 255 }, 256 }, 257 }, 258 condition: NewCondition(RunningConditionType, v1.ConditionTrue, nil, RunningReason, RunningMessage), 259 expectedNewSize: 1, 260 keepLastTransitionTime: true, 261 }, 262 { 263 name: "do not update running condition", 264 status: &Status{ 265 Conditions: []Condition{ 266 Condition{ 267 Type: RunningConditionType, 268 Status: v1.ConditionTrue, 269 Reason: RunningReason, 270 Message: SuccessfulMessage, 271 LastTransitionTime: lastTransitionTime, 272 }, 273 }, 274 }, 275 condition: NewCondition(RunningConditionType, v1.ConditionTrue, nil, RunningReason, RunningMessage), 276 expectedNewSize: 1, 277 keepLastTransitionTime: true, 278 keepMessage: true, 279 }, 280 } 281 282 for _, tc := range testCases { 283 t.Run(tc.name, func(t *testing.T) { 284 SetCondition(tc.status, *tc.condition) 285 if tc.expectedNewSize != len(tc.status.Conditions) { 286 t.Fatalf("New size of conditions did not match expected\nActual: %v\nExpected: %v", len(tc.status.Conditions), tc.expectedNewSize) 287 } 288 if tc.keepLastTransitionTime { 289 tc.condition.LastTransitionTime = lastTransitionTime 290 } 291 if tc.keepMessage { 292 tc.condition.Message = keeptMessage 293 } 294 ac := GetCondition(*tc.status, tc.condition.Type) 295 if !reflect.DeepEqual(ac, tc.condition) { 296 t.Fatalf("Condition did not match expected:\nActual: %#v\nExpected: %#v", ac, tc.condition) 297 } 298 }) 299 } 300 }