1
1
//
2
2
// DISCLAIMER
3
3
//
4
- // Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
4
+ // Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
5
5
//
6
6
// Licensed under the Apache License, Version 2.0 (the "License");
7
7
// you may not use this file except in compliance with the License.
21
21
package policy
22
22
23
23
import (
24
+ "context"
24
25
"testing"
25
26
"time"
26
27
@@ -30,6 +31,7 @@ import (
30
31
31
32
backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1"
32
33
"github.com/arangodb/kube-arangodb/pkg/operatorV2/operation"
34
+ "github.com/arangodb/kube-arangodb/pkg/util"
33
35
)
34
36
35
37
func Test_Scheduler_Schedule (t * testing.T ) {
@@ -39,7 +41,7 @@ func Test_Scheduler_Schedule(t *testing.T) {
39
41
name := string (uuid .NewUUID ())
40
42
namespace := string (uuid .NewUUID ())
41
43
42
- policy := newArangoBackupPolicy ("* * * */2 *" , namespace , name , map [ string ] string {}, backupApi. ArangoBackupTemplate {} )
44
+ policy := newArangoBackupPolicy (namespace , name , newSimpleArangoBackupPolicySpec ( "* * * */2 *" ) )
43
45
44
46
database := newArangoDeployment (namespace , map [string ]string {
45
47
"test" : "me" ,
@@ -67,7 +69,7 @@ func Test_Scheduler_InvalidSchedule(t *testing.T) {
67
69
name := string (uuid .NewUUID ())
68
70
namespace := string (uuid .NewUUID ())
69
71
70
- policy := newArangoBackupPolicy ("" , namespace , name , map [ string ] string {}, backupApi. ArangoBackupTemplate {} )
72
+ policy := newArangoBackupPolicy (namespace , name , newSimpleArangoBackupPolicySpec ( "" ) )
71
73
72
74
database := newArangoDeployment (namespace , map [string ]string {})
73
75
@@ -93,7 +95,7 @@ func Test_Scheduler_Valid_OneObject_SelectAll(t *testing.T) {
93
95
name := string (uuid .NewUUID ())
94
96
namespace := string (uuid .NewUUID ())
95
97
96
- policy := newArangoBackupPolicy ("* * * */2 *" , namespace , name , map [ string ] string {}, backupApi. ArangoBackupTemplate {} )
98
+ policy := newArangoBackupPolicy (namespace , name , newSimpleArangoBackupPolicySpec ( "* * * */2 *" ) )
97
99
policy .Status .Scheduled = meta.Time {
98
100
Time : time .Now ().Add (- 1 * time .Hour ),
99
101
}
@@ -131,8 +133,9 @@ func Test_Scheduler_Valid_OneObject_Selector(t *testing.T) {
131
133
selectors := map [string ]string {
132
134
"SELECTOR" : string (uuid .NewUUID ()),
133
135
}
134
-
135
- policy := newArangoBackupPolicy ("* * * */2 *" , namespace , name , selectors , backupApi.ArangoBackupTemplate {})
136
+ spec := newSimpleArangoBackupPolicySpec ("* * * */2 *" )
137
+ spec .DeploymentSelector = & meta.LabelSelector {MatchLabels : selectors }
138
+ policy := newArangoBackupPolicy (namespace , name , spec )
136
139
policy .Status .Scheduled = meta.Time {
137
140
Time : time .Now ().Add (- 1 * time .Hour ),
138
141
}
@@ -170,7 +173,9 @@ func Test_Scheduler_Valid_MultipleObject_Selector(t *testing.T) {
170
173
"SELECTOR" : string (uuid .NewUUID ()),
171
174
}
172
175
173
- policy := newArangoBackupPolicy ("* * * */2 *" , namespace , name , selectors , backupApi.ArangoBackupTemplate {})
176
+ spec := newSimpleArangoBackupPolicySpec ("* * * */2 *" )
177
+ spec .DeploymentSelector = & meta.LabelSelector {MatchLabels : selectors }
178
+ policy := newArangoBackupPolicy (namespace , name , spec )
174
179
policy .Status .Scheduled = meta.Time {
175
180
Time : time .Now ().Add (- 1 * time .Hour ),
176
181
}
@@ -211,7 +216,9 @@ func Test_Reschedule(t *testing.T) {
211
216
"SELECTOR" : string (uuid .NewUUID ()),
212
217
}
213
218
214
- policy := newArangoBackupPolicy ("* 13 * * *" , namespace , name , selectors , backupApi.ArangoBackupTemplate {})
219
+ spec := newSimpleArangoBackupPolicySpec ("* 13 * * *" )
220
+ spec .DeploymentSelector = & meta.LabelSelector {MatchLabels : selectors }
221
+ policy := newArangoBackupPolicy (namespace , name , spec )
215
222
216
223
// Act
217
224
createArangoBackupPolicy (t , handler , policy )
@@ -269,8 +276,9 @@ func Test_Validate(t *testing.T) {
269
276
selectors := map [string ]string {
270
277
"SELECTOR" : string (uuid .NewUUID ()),
271
278
}
272
-
273
- policy := newArangoBackupPolicy (c , namespace , name , selectors , backupApi.ArangoBackupTemplate {})
279
+ spec := newSimpleArangoBackupPolicySpec (c )
280
+ spec .DeploymentSelector = & meta.LabelSelector {MatchLabels : selectors }
281
+ policy := newArangoBackupPolicy (namespace , name , spec )
274
282
275
283
require .NoError (t , policy .Validate ())
276
284
@@ -286,3 +294,72 @@ func Test_Validate(t *testing.T) {
286
294
})
287
295
}
288
296
}
297
+
298
+ func Test_Concurrent (t * testing.T ) {
299
+ testCase := func (t * testing.T , allowConcurrent * bool ) {
300
+ handler := newFakeHandler ()
301
+
302
+ name := string (uuid .NewUUID ())
303
+ namespace := string (uuid .NewUUID ())
304
+
305
+ spec := newSimpleArangoBackupPolicySpec ("* * * */2 *" )
306
+ spec .AllowConcurrent = allowConcurrent
307
+ policy := newArangoBackupPolicy (namespace , name , spec )
308
+ policy .Status .Scheduled = meta.Time {
309
+ Time : time .Now ().Add (- 1 * time .Hour ),
310
+ }
311
+
312
+ database := newArangoDeployment (namespace , map [string ]string {
313
+ "test" : "me" ,
314
+ })
315
+
316
+ createArangoBackupPolicy (t , handler , policy )
317
+ createArangoDeployment (t , handler , database )
318
+
319
+ // "create" first backup
320
+ require .NoError (t , handler .Handle (newItemFromBackupPolicy (operation .Update , policy )))
321
+ backups := listArangoBackups (t , handler , namespace )
322
+ require .Len (t , backups , 1 )
323
+
324
+ // "try create" second backup but first one still is not in TerminalState
325
+ policy = refreshArangoBackupPolicy (t , handler , policy )
326
+ policy .Status .Scheduled = meta.Time {
327
+ Time : time .Now ().Add (- 1 * time .Hour ),
328
+ }
329
+ updateArangoBackupPolicy (t , handler , policy )
330
+ require .NoError (t , handler .Handle (newItemFromBackupPolicy (operation .Update , policy )))
331
+ backups = listArangoBackups (t , handler , namespace )
332
+ require .Len (t , backups , util .BoolSwitch (policy .Spec .GetAllowConcurrent (), 2 , 1 ))
333
+
334
+ // mark previous backup as Ready
335
+ backup := backups [0 ].DeepCopy ()
336
+ backup .Status .State = backupApi .ArangoBackupStateReady
337
+ backup .Status .Backup = & backupApi.ArangoBackupDetails {
338
+ ID : "SOME_ID" ,
339
+ }
340
+ _ , err := handler .client .BackupV1 ().ArangoBackups (namespace ).UpdateStatus (context .Background (), backup , meta.UpdateOptions {})
341
+ require .NoError (t , err )
342
+
343
+ // "try create" second backup again, should succeed
344
+ policy = refreshArangoBackupPolicy (t , handler , policy )
345
+ policy .Status .Scheduled = meta.Time {
346
+ Time : time .Now ().Add (- 1 * time .Hour ),
347
+ }
348
+ updateArangoBackupPolicy (t , handler , policy )
349
+ require .NoError (t , handler .Handle (newItemFromBackupPolicy (operation .Update , policy )))
350
+ backups = listArangoBackups (t , handler , namespace )
351
+ require .Len (t , backups , util .BoolSwitch (policy .Spec .GetAllowConcurrent (), 3 , 2 ))
352
+ }
353
+
354
+ t .Run ("Explicit Allow" , func (t * testing.T ) {
355
+ testCase (t , util .NewType (true ))
356
+ })
357
+
358
+ t .Run ("Default Allow" , func (t * testing.T ) {
359
+ testCase (t , nil )
360
+ })
361
+
362
+ t .Run ("Explicit Disallow" , func (t * testing.T ) {
363
+ testCase (t , util .NewType (false ))
364
+ })
365
+ }
0 commit comments