@@ -19,6 +19,7 @@ package statefulset
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "strconv"
22
23
"testing"
23
24
"time"
24
25
@@ -655,42 +656,42 @@ func TestDeletingPodForRollingUpdatePartition(t *testing.T) {
655
656
656
657
func TestStatefulSetStartOrdinal (t * testing.T ) {
657
658
tests := []struct {
658
- ordinals * appsv1.StatefulSetOrdinals
659
- name string
660
- namespace string
661
- replicas int
662
- expectedPodNames []string
659
+ ordinals * appsv1.StatefulSetOrdinals
660
+ name string
661
+ namespace string
662
+ replicas int
663
+ expectedPodIndexes []int
663
664
}{
664
665
{
665
- name : "default start ordinal, no ordinals set" ,
666
- namespace : "no-ordinals" ,
667
- replicas : 3 ,
668
- expectedPodNames : []string { "sts-0" , "sts-1" , "sts-2" },
666
+ name : "default start ordinal, no ordinals set" ,
667
+ namespace : "no-ordinals" ,
668
+ replicas : 3 ,
669
+ expectedPodIndexes : []int { 0 , 1 , 2 },
669
670
},
670
671
{
671
- name : "default start ordinal" ,
672
- namespace : "no-start-ordinals" ,
673
- ordinals : & appsv1.StatefulSetOrdinals {},
674
- replicas : 3 ,
675
- expectedPodNames : []string { "sts-0" , "sts-1" , "sts-2" },
672
+ name : "default start ordinal" ,
673
+ namespace : "no-start-ordinals" ,
674
+ ordinals : & appsv1.StatefulSetOrdinals {},
675
+ replicas : 3 ,
676
+ expectedPodIndexes : []int { 0 , 1 , 2 },
676
677
},
677
678
{
678
679
name : "start ordinal 4" ,
679
680
namespace : "start-ordinal-4" ,
680
681
ordinals : & appsv1.StatefulSetOrdinals {
681
682
Start : 4 ,
682
683
},
683
- replicas : 4 ,
684
- expectedPodNames : []string { "sts-4" , "sts-5" , "sts-6" , "sts-7" },
684
+ replicas : 4 ,
685
+ expectedPodIndexes : []int { 4 , 5 , 6 , 7 },
685
686
},
686
687
{
687
688
name : "start ordinal 5" ,
688
689
namespace : "start-ordinal-5" ,
689
690
ordinals : & appsv1.StatefulSetOrdinals {
690
691
Start : 2 ,
691
692
},
692
- replicas : 7 ,
693
- expectedPodNames : []string { "sts-2" , "sts-3" , "sts-4" , "sts-5" , "sts-6" , "sts-7" , "sts-8" },
693
+ replicas : 7 ,
694
+ expectedPodIndexes : []int { 2 , 3 , 4 , 5 , 6 , 7 , 8 },
694
695
},
695
696
}
696
697
@@ -719,17 +720,39 @@ func TestStatefulSetStartOrdinal(t *testing.T) {
719
720
}
720
721
721
722
var podNames []string
723
+ var podLabelIndexes []int
722
724
for _ , pod := range pods .Items {
723
725
podNames = append (podNames , pod .Name )
726
+ if idx , ok := pod .Labels [appsv1 .PodIndexLabel ]; ! ok {
727
+ t .Errorf ("Expected pod index label with key: %s" , appsv1 .PodIndexLabel )
728
+ } else {
729
+ idxInt , err := strconv .Atoi (idx )
730
+ if err != nil {
731
+ t .Errorf ("Unable to convert pod index to int, unexpected pod index: %s" , idx )
732
+ }
733
+ podLabelIndexes = append (podLabelIndexes , idxInt )
734
+ }
724
735
}
725
736
ignoreOrder := cmpopts .SortSlices (func (a , b string ) bool {
726
737
return a < b
727
738
})
739
+ ignoreOrderForOrdinals := cmpopts .SortSlices (func (a , b int ) bool {
740
+ return a < b
741
+ })
742
+
743
+ expectedNames := []string {}
744
+ for _ , ord := range test .expectedPodIndexes {
745
+ expectedNames = append (expectedNames , fmt .Sprintf ("sts-%d" , ord ))
746
+ }
728
747
729
748
// Validate all the expected pods were created.
730
- if diff := cmp .Diff (test . expectedPodNames , podNames , ignoreOrder ); diff != "" {
749
+ if diff := cmp .Diff (expectedNames , podNames , ignoreOrder ); diff != "" {
731
750
t .Errorf ("Unexpected pod names: (-want +got): %v" , diff )
732
751
}
752
+ // Validate all the expected index labels were added.
753
+ if diff := cmp .Diff (test .expectedPodIndexes , podLabelIndexes , ignoreOrderForOrdinals ); diff != "" {
754
+ t .Errorf ("Unexpected pod indices: (-want +got): %v" , diff )
755
+ }
733
756
734
757
// Scale down to 1 pod and verify it matches the first pod.
735
758
scaleSTS (t , c , sts , 1 )
@@ -739,8 +762,8 @@ func TestStatefulSetStartOrdinal(t *testing.T) {
739
762
if len (pods .Items ) != 1 {
740
763
t .Errorf ("len(pods) = %v, want %v" , len (pods .Items ), 1 )
741
764
}
742
- if pods .Items [0 ].Name != test . expectedPodNames [0 ] {
743
- t .Errorf ("Unexpected singleton pod name: got = %v, want %v" , pods .Items [0 ].Name , test . expectedPodNames [0 ])
765
+ if pods .Items [0 ].Name != expectedNames [0 ] {
766
+ t .Errorf ("Unexpected singleton pod name: got = %v, want %v" , pods .Items [0 ].Name , expectedNames [0 ])
744
767
}
745
768
})
746
769
}
0 commit comments