@@ -20,7 +20,7 @@ import (
2020 "context"
2121 "testing"
2222
23- . "github.com/onsi/gomega "
23+ "github.com/stretchr/testify/assert "
2424 appsv1 "k8s.io/api/apps/v1"
2525 corev1 "k8s.io/api/core/v1"
2626 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -31,80 +31,103 @@ import (
3131)
3232
3333func TestReadinessChecker_CheckConditions (t * testing.T ) {
34- scheme := runtime .NewScheme ()
35- _ = clientgoscheme .AddToScheme (scheme )
36-
37- t .Run ("always-true" , withReadinessChecker (scheme ,
38- func (g * WithT , rc * ReadinessChecker , u * unstructured.Unstructured ) {
39- msg , ok , err := rc .CheckConditions (context .TODO (), u , []string {ConditionTypeAlwaysTrue })
40- g .Expect (err ).ShouldNot (HaveOccurred ())
41- g .Expect (ok ).To (BeTrue ())
42- g .Expect (msg ).To (BeEmpty ())
34+ cases := []struct {
35+ desc string
36+ objs []runtime.Object
37+ conditionTypes []string
38+ msg string
39+ ready bool
40+ err error
41+ }{
42+ {
43+ desc : "always-true" ,
44+ conditionTypes : []string {ConditionTypeAlwaysTrue },
45+ ready : true ,
4346 },
44- ))
47+ {
48+ desc : "pod-ready" ,
49+ conditionTypes : []string {ConditionTypePodReady },
50+ ready : true ,
4551
46- t .Run ("pod-ready" , withReadinessChecker (scheme ,
47- func (g * WithT , rc * ReadinessChecker , u * unstructured.Unstructured ) {
48- msg , ok , err := rc .CheckConditions (context .TODO (), u , []string {ConditionTypePodReady })
49- g .Expect (err ).ShouldNot (HaveOccurred ())
50- g .Expect (ok ).To (BeTrue ())
51- g .Expect (msg ).To (BeEmpty ())
52- },
53- & appsv1.StatefulSet {
54- Spec : appsv1.StatefulSetSpec {
55- Selector : & metav1.LabelSelector {
56- MatchLabels : map [string ]string {"test" : "test" },
52+ objs : []runtime.Object {
53+ & appsv1.StatefulSet {
54+ Spec : appsv1.StatefulSetSpec {
55+ Selector : & metav1.LabelSelector {
56+ MatchLabels : map [string ]string {"test" : "test" },
57+ },
58+ },
59+ },
60+ & corev1.Pod {
61+ ObjectMeta : metav1.ObjectMeta {Labels : map [string ]string {"test" : "test" }},
62+ Status : corev1.PodStatus {
63+ Conditions : []corev1.PodCondition {{
64+ Type : corev1 .PodReady ,
65+ Status : corev1 .ConditionTrue ,
66+ }},
67+ },
5768 },
5869 },
5970 },
60- & corev1.Pod {
61- ObjectMeta : metav1.ObjectMeta {Labels : map [string ]string {"test" : "test" }},
62- Status : corev1.PodStatus {
63- Conditions : []corev1.PodCondition {{
64- Type : corev1 .PodReady ,
65- Status : corev1 .ConditionTrue ,
66- }},
71+ {
72+ desc : "unschedulable" ,
73+ conditionTypes : []string {ConditionTypePodReady },
74+ err : & ReadinessError {
75+ Reason : corev1 .PodReasonUnschedulable ,
76+ error : "pod unschedulable" ,
6777 },
68- },
69- ))
7078
71- t .Run ("unschedulable" , withReadinessChecker (scheme ,
72- func (g * WithT , rc * ReadinessChecker , u * unstructured.Unstructured ) {
73- _ , _ , err := rc .CheckConditions (context .TODO (), u , []string {ConditionTypePodReady })
74- g .Expect (err ).Should (MatchError (corev1 .PodReasonUnschedulable ))
75- },
76- & appsv1.Deployment {
77- Spec : appsv1.DeploymentSpec {
78- Selector : & metav1.LabelSelector {
79- MatchLabels : map [string ]string {"test" : "test" },
79+ objs : []runtime.Object {
80+ & appsv1.Deployment {
81+ Spec : appsv1.DeploymentSpec {
82+ Selector : & metav1.LabelSelector {
83+ MatchLabels : map [string ]string {"test" : "test" },
84+ },
85+ },
86+ },
87+ & corev1.Pod {
88+ ObjectMeta : metav1.ObjectMeta {Labels : map [string ]string {"test" : "test" }},
89+ Status : corev1.PodStatus {
90+ Conditions : []corev1.PodCondition {{
91+ Type : corev1 .PodScheduled ,
92+ Status : corev1 .ConditionFalse ,
93+ Reason : corev1 .PodReasonUnschedulable ,
94+ }},
95+ },
8096 },
8197 },
8298 },
83- & corev1.Pod {
84- ObjectMeta : metav1.ObjectMeta {Labels : map [string ]string {"test" : "test" }},
85- Status : corev1.PodStatus {
86- Conditions : []corev1.PodCondition {{
87- Type : corev1 .PodScheduled ,
88- Status : corev1 .ConditionFalse ,
89- Reason : corev1 .PodReasonUnschedulable ,
90- }},
91- },
92- },
93- ))
94- }
99+ }
100+
101+ ctx := context .TODO ()
102+ scheme := runtime .NewScheme ()
103+ _ = clientgoscheme .AddToScheme (scheme )
104+ for _ , c := range cases {
105+ t .Run (c .desc , func (t * testing.T ) {
106+ // Setup a readiness checker for the test case using the supplied objects
107+ u := & unstructured.Unstructured {}
108+ if len (c .objs ) > 0 {
109+ if err := scheme .Convert (c .objs [0 ], u , nil ); err != nil {
110+ t .Fatalf ("Could not convert to unstructured: %v" , err )
111+ }
112+ c .objs = c .objs [1 :]
113+ }
114+ rc := & ReadinessChecker {Reader : fake .NewFakeClientWithScheme (scheme , c .objs ... )}
95115
96- // withReadinessChecker wraps a ReadinessChecker test function; the first object will be converted to an unstructured
97- // object for use as the target object, the remaining objects will be available through the checker's reader
98- func withReadinessChecker (scheme * runtime.Scheme , f func (* WithT , * ReadinessChecker , * unstructured.Unstructured ), objs ... runtime.Object ) func (* testing.T ) {
99- return func (t * testing.T ) {
100- u := & unstructured.Unstructured {}
101- if len (objs ) > 0 {
102- if err := scheme .Convert (objs [0 ], u , nil ); err != nil {
103- t .Fatalf ("Could not convert to unstructured: %v" , err )
116+ // Verify the results
117+ msg , ready , err := rc .CheckConditions (ctx , u , c .conditionTypes )
118+ assert .Equal (t , c .ready , ready )
119+ assert .Equal (t , c .msg , msg )
120+ if c .err != nil {
121+ assert .EqualError (t , err , c .err .Error ())
122+ if rerr , ok := c .err .(* ReadinessError ); ok {
123+ if assert .IsType (t , err , & ReadinessError {}) {
124+ assert .Equal (t , rerr .Reason , err .(* ReadinessError ).Reason )
125+ assert .Equal (t , rerr .Message , err .(* ReadinessError ).Message )
126+ }
127+ }
128+ } else {
129+ assert .NoError (t , err )
104130 }
105- objs = objs [1 :]
106- }
107- reader := fake .NewFakeClientWithScheme (scheme , objs ... )
108- f (NewWithT (t ), & ReadinessChecker {Reader : reader }, u )
131+ })
109132 }
110133}
0 commit comments