@@ -15,6 +15,7 @@ import (
15
15
"k8s.io/apimachinery/pkg/util/yaml"
16
16
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
17
17
controllerruntime "sigs.k8s.io/controller-runtime"
18
+ "sigs.k8s.io/controller-runtime/pkg/client"
18
19
"sigs.k8s.io/controller-runtime/pkg/client/fake"
19
20
20
21
apiv1alpha1 "github.com/percona/percona-server-mysql-operator/api/v1alpha1"
@@ -37,8 +38,7 @@ func TestRestoreStatusErrStateDesc(t *testing.T) {
37
38
name string
38
39
cr * apiv1alpha1.PerconaServerMySQLRestore
39
40
cluster * apiv1alpha1.PerconaServerMySQL
40
- backup * apiv1alpha1.PerconaServerMySQLBackup
41
- secret * corev1.Secret
41
+ objects []client.Object
42
42
stateDesc string
43
43
shouldSucceed bool
44
44
}{
@@ -112,14 +112,16 @@ func TestRestoreStatusErrStateDesc(t *testing.T) {
112
112
{
113
113
name : "without backup storage in cluster" ,
114
114
cr : cr ,
115
- backup : & apiv1alpha1.PerconaServerMySQLBackup {
116
- ObjectMeta : metav1.ObjectMeta {
117
- Name : backupName ,
118
- Namespace : namespace ,
119
- },
120
- Spec : apiv1alpha1.PerconaServerMySQLBackupSpec {
121
- ClusterName : clusterName ,
122
- StorageName : storageName ,
115
+ objects : []client.Object {
116
+ & apiv1alpha1.PerconaServerMySQLBackup {
117
+ ObjectMeta : metav1.ObjectMeta {
118
+ Name : backupName ,
119
+ Namespace : namespace ,
120
+ },
121
+ Spec : apiv1alpha1.PerconaServerMySQLBackupSpec {
122
+ ClusterName : clusterName ,
123
+ StorageName : storageName ,
124
+ },
123
125
},
124
126
},
125
127
cluster : & apiv1alpha1.PerconaServerMySQL {
@@ -138,14 +140,16 @@ func TestRestoreStatusErrStateDesc(t *testing.T) {
138
140
{
139
141
name : "without secret" ,
140
142
cr : cr ,
141
- backup : & apiv1alpha1.PerconaServerMySQLBackup {
142
- ObjectMeta : metav1.ObjectMeta {
143
- Name : backupName ,
144
- Namespace : namespace ,
145
- },
146
- Spec : apiv1alpha1.PerconaServerMySQLBackupSpec {
147
- ClusterName : clusterName ,
148
- StorageName : storageName ,
143
+ objects : []client.Object {
144
+ & apiv1alpha1.PerconaServerMySQLBackup {
145
+ ObjectMeta : metav1.ObjectMeta {
146
+ Name : backupName ,
147
+ Namespace : namespace ,
148
+ },
149
+ Spec : apiv1alpha1.PerconaServerMySQLBackupSpec {
150
+ ClusterName : clusterName ,
151
+ StorageName : storageName ,
152
+ },
149
153
},
150
154
},
151
155
cluster : & apiv1alpha1.PerconaServerMySQL {
@@ -172,14 +176,77 @@ func TestRestoreStatusErrStateDesc(t *testing.T) {
172
176
{
173
177
name : "should succeed" ,
174
178
cr : cr ,
175
- backup : & apiv1alpha1.PerconaServerMySQLBackup {
179
+ objects : []client.Object {
180
+ & apiv1alpha1.PerconaServerMySQLBackup {
181
+ ObjectMeta : metav1.ObjectMeta {
182
+ Name : backupName ,
183
+ Namespace : namespace ,
184
+ },
185
+ Spec : apiv1alpha1.PerconaServerMySQLBackupSpec {
186
+ ClusterName : clusterName ,
187
+ StorageName : storageName ,
188
+ },
189
+ },
190
+ & corev1.Secret {
191
+ ObjectMeta : metav1.ObjectMeta {
192
+ Name : "aws-secret" ,
193
+ Namespace : namespace ,
194
+ },
195
+ },
196
+ },
197
+ cluster : & apiv1alpha1.PerconaServerMySQL {
176
198
ObjectMeta : metav1.ObjectMeta {
177
- Name : backupName ,
199
+ Name : clusterName ,
178
200
Namespace : namespace ,
179
201
},
180
- Spec : apiv1alpha1.PerconaServerMySQLBackupSpec {
181
- ClusterName : clusterName ,
182
- StorageName : storageName ,
202
+ Spec : apiv1alpha1.PerconaServerMySQLSpec {
203
+ Backup : & apiv1alpha1.BackupSpec {
204
+ Storages : map [string ]* apiv1alpha1.BackupStorageSpec {
205
+ storageName : {
206
+ S3 : & apiv1alpha1.BackupStorageS3Spec {
207
+ CredentialsSecret : "aws-secret" ,
208
+ },
209
+ Type : apiv1alpha1 .BackupStorageS3 ,
210
+ },
211
+ },
212
+ InitImage : "operator-image" ,
213
+ },
214
+ },
215
+ },
216
+ stateDesc : "" ,
217
+ shouldSucceed : true ,
218
+ },
219
+ {
220
+ name : "with running restore" ,
221
+ cr : cr ,
222
+ objects : []client.Object {
223
+ & apiv1alpha1.PerconaServerMySQLBackup {
224
+ ObjectMeta : metav1.ObjectMeta {
225
+ Name : backupName ,
226
+ Namespace : namespace ,
227
+ },
228
+ Spec : apiv1alpha1.PerconaServerMySQLBackupSpec {
229
+ ClusterName : clusterName ,
230
+ StorageName : storageName ,
231
+ },
232
+ },
233
+ & corev1.Secret {
234
+ ObjectMeta : metav1.ObjectMeta {
235
+ Name : "aws-secret" ,
236
+ Namespace : namespace ,
237
+ },
238
+ },
239
+ & apiv1alpha1.PerconaServerMySQLRestore {
240
+ ObjectMeta : metav1.ObjectMeta {
241
+ Name : "running-restore" ,
242
+ Namespace : namespace ,
243
+ },
244
+ Spec : apiv1alpha1.PerconaServerMySQLRestoreSpec {
245
+ ClusterName : clusterName ,
246
+ },
247
+ Status : apiv1alpha1.PerconaServerMySQLRestoreStatus {
248
+ State : apiv1alpha1 .RestoreRunning ,
249
+ },
183
250
},
184
251
},
185
252
cluster : & apiv1alpha1.PerconaServerMySQL {
@@ -201,11 +268,96 @@ func TestRestoreStatusErrStateDesc(t *testing.T) {
201
268
},
202
269
},
203
270
},
204
- secret : & corev1.Secret {
271
+ stateDesc : "PerconaServerMySQLRestore running-restore is already running" ,
272
+ shouldSucceed : false ,
273
+ },
274
+ {
275
+ name : "with new, failed, errored and succeeded restore" ,
276
+ cr : cr ,
277
+ objects : []client.Object {
278
+ & apiv1alpha1.PerconaServerMySQLBackup {
279
+ ObjectMeta : metav1.ObjectMeta {
280
+ Name : backupName ,
281
+ Namespace : namespace ,
282
+ },
283
+ Spec : apiv1alpha1.PerconaServerMySQLBackupSpec {
284
+ ClusterName : clusterName ,
285
+ StorageName : storageName ,
286
+ },
287
+ },
288
+ & corev1.Secret {
289
+ ObjectMeta : metav1.ObjectMeta {
290
+ Name : "aws-secret" ,
291
+ Namespace : namespace ,
292
+ },
293
+ },
294
+ & apiv1alpha1.PerconaServerMySQLRestore {
295
+ ObjectMeta : metav1.ObjectMeta {
296
+ Name : "new-restore" ,
297
+ Namespace : namespace ,
298
+ },
299
+ Spec : apiv1alpha1.PerconaServerMySQLRestoreSpec {
300
+ ClusterName : clusterName ,
301
+ },
302
+ Status : apiv1alpha1.PerconaServerMySQLRestoreStatus {
303
+ State : apiv1alpha1 .RestoreNew ,
304
+ },
305
+ },
306
+ & apiv1alpha1.PerconaServerMySQLRestore {
307
+ ObjectMeta : metav1.ObjectMeta {
308
+ Name : "failed-restore" ,
309
+ Namespace : namespace ,
310
+ },
311
+ Spec : apiv1alpha1.PerconaServerMySQLRestoreSpec {
312
+ ClusterName : clusterName ,
313
+ },
314
+ Status : apiv1alpha1.PerconaServerMySQLRestoreStatus {
315
+ State : apiv1alpha1 .RestoreFailed ,
316
+ },
317
+ },
318
+ & apiv1alpha1.PerconaServerMySQLRestore {
319
+ ObjectMeta : metav1.ObjectMeta {
320
+ Name : "succeeded-restore" ,
321
+ Namespace : namespace ,
322
+ },
323
+ Spec : apiv1alpha1.PerconaServerMySQLRestoreSpec {
324
+ ClusterName : clusterName ,
325
+ },
326
+ Status : apiv1alpha1.PerconaServerMySQLRestoreStatus {
327
+ State : apiv1alpha1 .RestoreSucceeded ,
328
+ },
329
+ },
330
+ & apiv1alpha1.PerconaServerMySQLRestore {
331
+ ObjectMeta : metav1.ObjectMeta {
332
+ Name : "error-restore" ,
333
+ Namespace : namespace ,
334
+ },
335
+ Spec : apiv1alpha1.PerconaServerMySQLRestoreSpec {
336
+ ClusterName : clusterName ,
337
+ },
338
+ Status : apiv1alpha1.PerconaServerMySQLRestoreStatus {
339
+ State : apiv1alpha1 .RestoreError ,
340
+ },
341
+ },
342
+ },
343
+ cluster : & apiv1alpha1.PerconaServerMySQL {
205
344
ObjectMeta : metav1.ObjectMeta {
206
- Name : "aws-secret" ,
345
+ Name : clusterName ,
207
346
Namespace : namespace ,
208
347
},
348
+ Spec : apiv1alpha1.PerconaServerMySQLSpec {
349
+ Backup : & apiv1alpha1.BackupSpec {
350
+ Storages : map [string ]* apiv1alpha1.BackupStorageSpec {
351
+ storageName : {
352
+ S3 : & apiv1alpha1.BackupStorageS3Spec {
353
+ CredentialsSecret : "aws-secret" ,
354
+ },
355
+ Type : apiv1alpha1 .BackupStorageS3 ,
356
+ },
357
+ },
358
+ InitImage : "operator-image" ,
359
+ },
360
+ },
209
361
},
210
362
stateDesc : "" ,
211
363
shouldSucceed : true ,
@@ -224,7 +376,7 @@ func TestRestoreStatusErrStateDesc(t *testing.T) {
224
376
225
377
for _ , tt := range tests {
226
378
t .Run (tt .name , func (t * testing.T ) {
227
- cb := fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (tt .cr ).WithStatusSubresource (tt .cr )
379
+ cb := fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (tt .cr ).WithStatusSubresource (tt .cr ). WithObjects ( tt . objects ... )
228
380
if tt .cluster != nil {
229
381
cb .WithObjects (tt .cluster , & appsv1.StatefulSet {
230
382
ObjectMeta : metav1.ObjectMeta {
@@ -233,12 +385,6 @@ func TestRestoreStatusErrStateDesc(t *testing.T) {
233
385
},
234
386
})
235
387
}
236
- if tt .backup != nil {
237
- cb .WithObjects (tt .backup )
238
- }
239
- if tt .secret != nil {
240
- cb .WithObjects (tt .secret )
241
- }
242
388
243
389
r := PerconaServerMySQLRestoreReconciler {
244
390
Client : cb .Build (),
0 commit comments