@@ -7,31 +7,23 @@ package driver
77import (
88 "testing"
99
10+ esv1 "github.com/elastic/cloud-on-k8s/pkg/apis/elasticsearch/v1"
1011 "github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/sset"
1112 "github.com/elastic/cloud-on-k8s/pkg/utils/k8s"
1213 "github.com/stretchr/testify/assert"
1314 appsv1 "k8s.io/api/apps/v1"
14- corev1 "k8s.io/api/core/v1"
15- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16- "k8s.io/apimachinery/pkg/runtime"
1715)
1816
19- const testNamespace = "ns"
20-
21- func podWithRevision (name , revision string ) * corev1.Pod {
22- return & corev1.Pod {
23- ObjectMeta : metav1.ObjectMeta {
24- Name : name ,
25- Namespace : testNamespace ,
26- Labels : map [string ]string {appsv1 .StatefulSetRevisionLabel : revision },
17+ func Test_podsToUpgrade (t * testing.T ) {
18+ defaultEs := esv1.Elasticsearch {
19+ Spec : esv1.ElasticsearchSpec {
20+ Version : "7.1.0" ,
2721 },
2822 }
29- }
30-
31- func Test_podsToUpgrade (t * testing.T ) {
3223 type args struct {
33- pods []runtime. Object
24+ pods upgradeTestPods
3425 statefulSets sset.StatefulSetList
26+ es esv1.Elasticsearch
3527 }
3628 tests := []struct {
3729 name string
@@ -52,13 +44,14 @@ func Test_podsToUpgrade(t *testing.T) {
5244 Status : appsv1.StatefulSetStatus {CurrentRevision : "rev-a" , UpdateRevision : "rev-b" , UpdatedReplicas : 0 , Replicas : 3 },
5345 }.Build (),
5446 },
55- pods : []runtime.Object {
56- podWithRevision ("masters-0" , "rev-a" ),
57- podWithRevision ("masters-1" , "rev-a" ),
58- podWithRevision ("nodes-0" , "rev-a" ),
59- podWithRevision ("nodes-1" , "rev-a" ),
60- podWithRevision ("nodes-2" , "rev-a" ),
61- },
47+ pods : newUpgradeTestPods (
48+ newTestPod ("masters-0" ).withRevision ("rev-a" ).withVersion ("7.1.0" ),
49+ newTestPod ("masters-1" ).withRevision ("rev-a" ).withVersion ("7.1.0" ),
50+ newTestPod ("nodes-0" ).withRevision ("rev-a" ).withVersion ("7.1.0" ),
51+ newTestPod ("nodes-1" ).withRevision ("rev-a" ).withVersion ("7.1.0" ),
52+ newTestPod ("nodes-2" ).withRevision ("rev-a" ).withVersion ("7.1.0" ),
53+ ),
54+ es : defaultEs ,
6255 },
6356 want : []string {"masters-0" , "masters-1" , "nodes-0" , "nodes-1" , "nodes-2" },
6457 },
@@ -75,10 +68,11 @@ func Test_podsToUpgrade(t *testing.T) {
7568 Status : appsv1.StatefulSetStatus {CurrentRevision : "rev-b" , UpdateRevision : "rev-b" , UpdatedReplicas : 3 , Replicas : 3 },
7669 }.Build (),
7770 },
78- pods : []runtime.Object {
79- podWithRevision ("masters-0" , "rev-a" ),
80- podWithRevision ("masters-1" , "rev-a" ),
81- },
71+ pods : newUpgradeTestPods (
72+ newTestPod ("masters-0" ).withRevision ("rev-a" ).withVersion ("7.1.0" ),
73+ newTestPod ("masters-1" ).withRevision ("rev-a" ).withVersion ("7.1.0" ),
74+ ),
75+ es : defaultEs ,
8276 },
8377 want : []string {"masters-0" , "masters-1" },
8478 },
@@ -95,10 +89,11 @@ func Test_podsToUpgrade(t *testing.T) {
9589 Status : appsv1.StatefulSetStatus {CurrentRevision : "rev-b" , UpdateRevision : "" , UpdatedReplicas : 3 , Replicas : 3 },
9690 }.Build (),
9791 },
98- pods : []runtime.Object {
99- podWithRevision ("masters-0" , "rev-a" ),
100- podWithRevision ("masters-1" , "rev-a" ),
101- },
92+ pods : newUpgradeTestPods (
93+ newTestPod ("masters-0" ).withRevision ("rev-a" ).withVersion ("7.1.0" ),
94+ newTestPod ("masters-1" ).withRevision ("rev-a" ).withVersion ("7.1.0" ),
95+ ),
96+ es : defaultEs ,
10297 },
10398 want : []string {},
10499 },
@@ -115,18 +110,43 @@ func Test_podsToUpgrade(t *testing.T) {
115110 Status : appsv1.StatefulSetStatus {CurrentRevision : "rev-b" , UpdateRevision : "rev-b" , UpdatedReplicas : 3 , Replicas : 3 },
116111 }.Build (),
117112 },
118- pods : []runtime.Object {
119- podWithRevision ("masters-0" , "rev-b" ),
120- podWithRevision ("masters-1" , "rev-a" ),
121- },
113+ pods : newUpgradeTestPods (
114+ newTestPod ("masters-0" ).withRevision ("rev-b" ).withVersion ("7.1.0" ),
115+ newTestPod ("masters-1" ).withRevision ("rev-a" ).withVersion ("7.1.0" ),
116+ ),
117+ es : defaultEs ,
122118 },
123119 want : []string {"masters-1" },
124120 },
121+ {
122+ name : "StatefulSet has been updated with a new ES version but StatefulSet update revision is not yet up to date" ,
123+ args : args {
124+ statefulSets : sset.StatefulSetList {
125+ sset.TestSset {
126+ Name : "masters" , Replicas : 2 , Master : true , Data : false ,
127+ Status : appsv1.StatefulSetStatus {CurrentRevision : "rev-master-a" , UpdateRevision : "rev-master-b" , UpdatedReplicas : 0 , Replicas : 2 },
128+ }.Build (),
129+ sset.TestSset {
130+ Name : "nodes" , Replicas : 3 , Master : false , Data : true ,
131+ Status : appsv1.StatefulSetStatus {CurrentRevision : "rev-nodes-a" , UpdateRevision : "rev-nodes-a" , UpdatedReplicas : 0 , Replicas : 3 },
132+ }.Build (),
133+ },
134+ pods : newUpgradeTestPods (
135+ newTestPod ("masters-0" ).withRevision ("rev-master-a" ).withVersion ("6.8.2" ),
136+ newTestPod ("masters-1" ).withRevision ("rev-master-a" ).withVersion ("6.8.2" ),
137+ newTestPod ("nodes-0" ).withRevision ("rev-nodes-a" ).withVersion ("6.8.2" ),
138+ newTestPod ("nodes-1" ).withRevision ("rev-nodes-a" ).withVersion ("6.8.2" ),
139+ newTestPod ("nodes-2" ).withRevision ("rev-nodes-a" ).withVersion ("6.8.2" ),
140+ ),
141+ es : defaultEs ,
142+ },
143+ want : []string {"masters-0" , "masters-1" , "nodes-0" , "nodes-1" , "nodes-2" },
144+ },
125145 }
126146 for _ , tt := range tests {
127147 t .Run (tt .name , func (t * testing.T ) {
128- client := k8s .WrappedFakeClient (tt .args .pods ... )
129- got , err := podsToUpgrade (client , tt .args .statefulSets )
148+ client := k8s .WrappedFakeClient (tt .args .pods .toRuntimeObjects ( tt . args . es . Spec . Version , 1 , nothing ) . .. )
149+ got , err := podsToUpgrade (tt . args . es , client , tt .args .statefulSets )
130150 if (err != nil ) != tt .wantErr {
131151 t .Errorf ("podsToUpgrade() error = %v, wantErr %v" , err , tt .wantErr )
132152 return
0 commit comments