github.com/giantswarm/apiextensions/v2@v2.6.2/pkg/apis/core/v1alpha1/drainer_funcs_test.go (about) 1 package v1alpha1 2 3 import ( 4 "testing" 5 "time" 6 7 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 8 ) 9 10 func Test_HasDrainedCondition(t *testing.T) { 11 testCases := []struct { 12 name string 13 status DrainerConfigStatus 14 expectedResult bool 15 }{ 16 { 17 name: "case 0: DrainerConfigStatus with empty Conditions doesn't have Drained condition", 18 status: DrainerConfigStatus{}, 19 expectedResult: false, 20 }, 21 { 22 name: "case 1: DrainerConfigStatus with Drained status condition in conditions", 23 status: DrainerConfigStatus{ 24 Conditions: []DrainerConfigStatusCondition{ 25 { 26 LastTransitionTime: metav1.Time{Time: time.Now()}, 27 Status: DrainerConfigStatusStatusTrue, 28 Type: DrainerConfigStatusTypeDrained, 29 }, 30 }, 31 }, 32 expectedResult: true, 33 }, 34 { 35 name: "case 2: DrainerConfigStatus with Timeout status condition in conditions doesn't have Drained condition", 36 status: DrainerConfigStatus{ 37 Conditions: []DrainerConfigStatusCondition{ 38 { 39 LastTransitionTime: metav1.Time{Time: time.Now()}, 40 Status: DrainerConfigStatusStatusTrue, 41 Type: DrainerConfigStatusTypeTimeout, 42 }, 43 }, 44 }, 45 expectedResult: false, 46 }, 47 } 48 49 for _, tc := range testCases { 50 t.Run(tc.name, func(t *testing.T) { 51 h := tc.status.HasDrainedCondition() 52 53 if h != tc.expectedResult { 54 t.Fatalf("HasDrainedCondition() == %v, expected %v", h, tc.expectedResult) 55 } 56 }) 57 } 58 } 59 60 func Test_HasTimeoutCondition(t *testing.T) { 61 testCases := []struct { 62 name string 63 status DrainerConfigStatus 64 expectedResult bool 65 }{ 66 { 67 name: "case 0: DrainerConfigStatus with empty Conditions doesn't have Timeout condition", 68 status: DrainerConfigStatus{}, 69 expectedResult: false, 70 }, 71 { 72 name: "case 1: DrainerConfigStatus with Timeout status condition in conditions", 73 status: DrainerConfigStatus{ 74 Conditions: []DrainerConfigStatusCondition{ 75 { 76 LastTransitionTime: metav1.Time{Time: time.Now()}, 77 Status: DrainerConfigStatusStatusTrue, 78 Type: DrainerConfigStatusTypeTimeout, 79 }, 80 }, 81 }, 82 expectedResult: true, 83 }, 84 { 85 name: "case 2: DrainerConfigStatus with Drained status condition in conditions doesn't have Timeout condition", 86 status: DrainerConfigStatus{ 87 Conditions: []DrainerConfigStatusCondition{ 88 { 89 LastTransitionTime: metav1.Time{Time: time.Now()}, 90 Status: DrainerConfigStatusStatusTrue, 91 Type: DrainerConfigStatusTypeDrained, 92 }, 93 }, 94 }, 95 expectedResult: false, 96 }, 97 } 98 99 for _, tc := range testCases { 100 t.Run(tc.name, func(t *testing.T) { 101 h := tc.status.HasTimeoutCondition() 102 103 if h != tc.expectedResult { 104 t.Fatalf("HasTimeoutCondition() == %v, expected %v", h, tc.expectedResult) 105 } 106 }) 107 } 108 } 109 110 func Test_NewDrainedCondition(t *testing.T) { 111 status := DrainerConfigStatus{} 112 status.Conditions = append(status.Conditions, status.NewDrainedCondition()) 113 114 if !status.HasDrainedCondition() { 115 t.Fatalf("DrainerConfigStatus doesn't have Drained condition after NewDrainedCondition() call") 116 } 117 } 118 119 func Test_NewTimeoutCondition(t *testing.T) { 120 status := DrainerConfigStatus{} 121 status.Conditions = append(status.Conditions, status.NewTimeoutCondition()) 122 123 if !status.HasTimeoutCondition() { 124 t.Fatalf("DrainerConfigStatus doesn't have Timeout condition after NewTimeoutCondition() call") 125 } 126 }