halkyon.io/api@v1.0.0-rc.6/v1beta1/commons_test.go (about) 1 package v1beta1 2 3 import ( 4 "k8s.io/apimachinery/pkg/runtime/schema" 5 "testing" 6 ) 7 8 const initialStatusMessage = "Initial" 9 10 func status(conditions ...*DependentCondition) Status { 11 return statusWithInitialReason("", conditions...) 12 } 13 14 func statusWithInitialReason(reason string, conditions ...*DependentCondition) Status { 15 status := Status{} 16 dc := make([]DependentCondition, 0, len(conditions)) 17 for _, condition := range conditions { 18 dc = append(dc, *condition) 19 } 20 status.Conditions = dc 21 status.Message = initialStatusMessage 22 if len(reason) > 0 { 23 status.Reason = reason 24 } 25 return status 26 } 27 28 func condition(conditionType DependentConditionType, name ...string) *DependentCondition { 29 d := &DependentCondition{ 30 Type: conditionType, 31 DependentType: schema.GroupVersionKind{Group: "foo", Version: "bar"}, 32 DependentName: "default-condition", 33 } 34 if len(name) == 1 { 35 d.DependentName = name[0] 36 } 37 return d 38 } 39 40 func TestSetCondition(t *testing.T) { 41 tests := []struct { 42 testName string 43 status Status 44 expectedReason string 45 condition *DependentCondition 46 conditionNb int 47 changed bool 48 }{ 49 { 50 testName: "setting nil shouldn't do anything", 51 status: status(), 52 }, 53 { 54 testName: "properly add condition", 55 status: status(), 56 expectedReason: ReasonReady, 57 condition: condition(DependentReady), 58 conditionNb: 1, 59 changed: true, 60 }, 61 { 62 testName: "updating a condition to ready when it was pending before should result in ready status", 63 status: status(condition(DependentPending)), 64 expectedReason: ReasonReady, 65 condition: condition(DependentReady), 66 conditionNb: 1, 67 changed: true, 68 }, 69 { 70 testName: "failed condition should result in failed status", 71 status: status(condition(DependentReady, "ready"), condition(DependentPending, "pending"), condition(DependentReady, "failed")), 72 expectedReason: ReasonFailed, 73 condition: condition(DependentFailed, "failed"), 74 conditionNb: 3, 75 changed: true, 76 }, 77 { 78 testName: "not ready condition should result in not ready status", 79 status: status(condition(DependentReady, "ready"), condition(DependentPending, "pending"), condition(DependentFailed, "failed")), 80 expectedReason: ReasonPending, 81 condition: condition(DependentReady, "failed"), 82 conditionNb: 3, 83 changed: true, 84 }, 85 { 86 testName: "status should be ready if all dependents become ready", 87 status: status(condition(DependentPending, "pending"), condition(DependentReady, "ready"), condition(DependentReady, "ready2")), 88 expectedReason: ReasonReady, 89 condition: condition(DependentReady, "pending"), 90 conditionNb: 3, 91 changed: true, 92 }, 93 { 94 testName: "status should be ready only if all dependents become ready", 95 status: status(condition(DependentPending, "pending"), condition(DependentReady, "ready"), condition(DependentPending, "pending2")), 96 expectedReason: ReasonPending, 97 condition: condition(DependentReady, "pending"), 98 conditionNb: 3, 99 changed: true, 100 }, 101 { 102 testName: "status should not change when setting unchanged condition", 103 status: statusWithInitialReason(ReasonPending, condition(DependentPending, "pending"), condition(DependentReady, "ready")), 104 expectedReason: ReasonPending, 105 condition: condition(DependentPending, "pending"), 106 conditionNb: 2, 107 changed: false, 108 }, 109 } 110 111 for _, tt := range tests { 112 t.Run(tt.testName, func(t *testing.T) { 113 changed := tt.status.SetCondition(tt.condition) 114 if changed && tt.status.Message == initialStatusMessage { 115 t.Errorf("status has changed so its message should have been updated but hasn't") 116 } 117 if changed != tt.changed { 118 t.Errorf("expected changed status to be %t, got %t", tt.changed, changed) 119 } 120 i := len(tt.status.Conditions) 121 if i != tt.conditionNb { 122 t.Errorf("expected to have %d conditions, got %d", tt.conditionNb, i) 123 } 124 if tt.expectedReason != tt.status.Reason { 125 t.Errorf("expected to status to be have %s status, got %s", tt.expectedReason, tt.status.Reason) 126 } 127 }) 128 } 129 }