@@ -7,31 +7,23 @@ package driver
7
7
import (
8
8
"testing"
9
9
10
+ esv1 "github.com/elastic/cloud-on-k8s/pkg/apis/elasticsearch/v1"
10
11
"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/sset"
11
12
"github.com/elastic/cloud-on-k8s/pkg/utils/k8s"
12
13
"github.com/stretchr/testify/assert"
13
14
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"
17
15
)
18
16
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" ,
27
21
},
28
22
}
29
- }
30
-
31
- func Test_podsToUpgrade (t * testing.T ) {
32
23
type args struct {
33
- pods []runtime. Object
24
+ pods upgradeTestPods
34
25
statefulSets sset.StatefulSetList
26
+ es esv1.Elasticsearch
35
27
}
36
28
tests := []struct {
37
29
name string
@@ -52,13 +44,14 @@ func Test_podsToUpgrade(t *testing.T) {
52
44
Status : appsv1.StatefulSetStatus {CurrentRevision : "rev-a" , UpdateRevision : "rev-b" , UpdatedReplicas : 0 , Replicas : 3 },
53
45
}.Build (),
54
46
},
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 ,
62
55
},
63
56
want : []string {"masters-0" , "masters-1" , "nodes-0" , "nodes-1" , "nodes-2" },
64
57
},
@@ -75,10 +68,11 @@ func Test_podsToUpgrade(t *testing.T) {
75
68
Status : appsv1.StatefulSetStatus {CurrentRevision : "rev-b" , UpdateRevision : "rev-b" , UpdatedReplicas : 3 , Replicas : 3 },
76
69
}.Build (),
77
70
},
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 ,
82
76
},
83
77
want : []string {"masters-0" , "masters-1" },
84
78
},
@@ -95,10 +89,11 @@ func Test_podsToUpgrade(t *testing.T) {
95
89
Status : appsv1.StatefulSetStatus {CurrentRevision : "rev-b" , UpdateRevision : "" , UpdatedReplicas : 3 , Replicas : 3 },
96
90
}.Build (),
97
91
},
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 ,
102
97
},
103
98
want : []string {},
104
99
},
@@ -115,18 +110,43 @@ func Test_podsToUpgrade(t *testing.T) {
115
110
Status : appsv1.StatefulSetStatus {CurrentRevision : "rev-b" , UpdateRevision : "rev-b" , UpdatedReplicas : 3 , Replicas : 3 },
116
111
}.Build (),
117
112
},
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 ,
122
118
},
123
119
want : []string {"masters-1" },
124
120
},
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
+ },
125
145
}
126
146
for _ , tt := range tests {
127
147
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 )
130
150
if (err != nil ) != tt .wantErr {
131
151
t .Errorf ("podsToUpgrade() error = %v, wantErr %v" , err , tt .wantErr )
132
152
return
0 commit comments