@@ -20,6 +20,7 @@ import (
2020 "testing"
2121
2222 "github.com/google/go-cmp/cmp"
23+ appsv1 "k8s.io/api/apps/v1"
2324)
2425
2526func TestGetParentNameAndOrdinal (t * testing.T ) {
@@ -76,3 +77,77 @@ func TestGetParentNameAndOrdinal(t *testing.T) {
7677 })
7778 }
7879}
80+
81+ func TestStatefulsetReady (t * testing.T ) {
82+ replicas := int32 (3 )
83+ tests := []struct {
84+ name string
85+ sts appsv1.StatefulSet
86+ wantBool bool
87+ }{
88+ {
89+ name : "statefulset is ready when AvailableReplicas equals Spec.Replicas and revisions match" ,
90+ sts : appsv1.StatefulSet {
91+ Spec : appsv1.StatefulSetSpec {
92+ Replicas : & replicas ,
93+ },
94+ Status : appsv1.StatefulSetStatus {
95+ AvailableReplicas : 3 ,
96+ CurrentRevision : "rev-1" ,
97+ UpdateRevision : "rev-1" ,
98+ },
99+ },
100+ wantBool : true ,
101+ },
102+ {
103+ name : "statefulset is not ready when AvailableReplicas less than Spec.Replicas" ,
104+ sts : appsv1.StatefulSet {
105+ Spec : appsv1.StatefulSetSpec {
106+ Replicas : & replicas ,
107+ },
108+ Status : appsv1.StatefulSetStatus {
109+ AvailableReplicas : 2 ,
110+ CurrentRevision : "rev-1" ,
111+ UpdateRevision : "rev-1" ,
112+ },
113+ },
114+ wantBool : false ,
115+ },
116+ {
117+ name : "statefulset is not ready when CurrentRevision != UpdateRevision" ,
118+ sts : appsv1.StatefulSet {
119+ Spec : appsv1.StatefulSetSpec {
120+ Replicas : & replicas ,
121+ },
122+ Status : appsv1.StatefulSetStatus {
123+ AvailableReplicas : 3 ,
124+ CurrentRevision : "rev-1" ,
125+ UpdateRevision : "rev-2" ,
126+ },
127+ },
128+ wantBool : false ,
129+ },
130+ {
131+ name : "statefulset is not ready when both conditions fail" ,
132+ sts : appsv1.StatefulSet {
133+ Spec : appsv1.StatefulSetSpec {
134+ Replicas : & replicas ,
135+ },
136+ Status : appsv1.StatefulSetStatus {
137+ AvailableReplicas : 2 ,
138+ CurrentRevision : "rev-1" ,
139+ UpdateRevision : "rev-2" ,
140+ },
141+ },
142+ wantBool : false ,
143+ },
144+ }
145+ for _ , tc := range tests {
146+ t .Run (tc .name , func (t * testing.T ) {
147+ got := StatefulsetReady (tc .sts )
148+ if diff := cmp .Diff (tc .wantBool , got ); diff != "" {
149+ t .Errorf ("unexpected StatefulsetReady result: %s" , diff )
150+ }
151+ })
152+ }
153+ }
0 commit comments