11//
22// DISCLAIMER
33//
4- // Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
4+ // Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
55//
66// Licensed under the Apache License, Version 2.0 (the "License");
77// you may not use this file except in compliance with the License.
2121package policy
2222
2323import (
24+ "context"
2425 "testing"
2526 "time"
2627
@@ -30,6 +31,7 @@ import (
3031
3132 backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1"
3233 "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation"
34+ "github.com/arangodb/kube-arangodb/pkg/util"
3335)
3436
3537func Test_Scheduler_Schedule (t * testing.T ) {
@@ -39,7 +41,7 @@ func Test_Scheduler_Schedule(t *testing.T) {
3941 name := string (uuid .NewUUID ())
4042 namespace := string (uuid .NewUUID ())
4143
42- policy := newArangoBackupPolicy ("* * * */2 *" , namespace , name , map [ string ] string {}, backupApi. ArangoBackupTemplate {} )
44+ policy := newArangoBackupPolicy (namespace , name , newSimpleArangoBackupPolicySpec ( "* * * */2 *" ) )
4345
4446 database := newArangoDeployment (namespace , map [string ]string {
4547 "test" : "me" ,
@@ -67,7 +69,7 @@ func Test_Scheduler_InvalidSchedule(t *testing.T) {
6769 name := string (uuid .NewUUID ())
6870 namespace := string (uuid .NewUUID ())
6971
70- policy := newArangoBackupPolicy ("" , namespace , name , map [ string ] string {}, backupApi. ArangoBackupTemplate {} )
72+ policy := newArangoBackupPolicy (namespace , name , newSimpleArangoBackupPolicySpec ( "" ) )
7173
7274 database := newArangoDeployment (namespace , map [string ]string {})
7375
@@ -93,7 +95,7 @@ func Test_Scheduler_Valid_OneObject_SelectAll(t *testing.T) {
9395 name := string (uuid .NewUUID ())
9496 namespace := string (uuid .NewUUID ())
9597
96- policy := newArangoBackupPolicy ("* * * */2 *" , namespace , name , map [ string ] string {}, backupApi. ArangoBackupTemplate {} )
98+ policy := newArangoBackupPolicy (namespace , name , newSimpleArangoBackupPolicySpec ( "* * * */2 *" ) )
9799 policy .Status .Scheduled = meta.Time {
98100 Time : time .Now ().Add (- 1 * time .Hour ),
99101 }
@@ -131,8 +133,9 @@ func Test_Scheduler_Valid_OneObject_Selector(t *testing.T) {
131133 selectors := map [string ]string {
132134 "SELECTOR" : string (uuid .NewUUID ()),
133135 }
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 )
136139 policy .Status .Scheduled = meta.Time {
137140 Time : time .Now ().Add (- 1 * time .Hour ),
138141 }
@@ -170,7 +173,9 @@ func Test_Scheduler_Valid_MultipleObject_Selector(t *testing.T) {
170173 "SELECTOR" : string (uuid .NewUUID ()),
171174 }
172175
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 )
174179 policy .Status .Scheduled = meta.Time {
175180 Time : time .Now ().Add (- 1 * time .Hour ),
176181 }
@@ -211,7 +216,9 @@ func Test_Reschedule(t *testing.T) {
211216 "SELECTOR" : string (uuid .NewUUID ()),
212217 }
213218
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 )
215222
216223 // Act
217224 createArangoBackupPolicy (t , handler , policy )
@@ -269,8 +276,9 @@ func Test_Validate(t *testing.T) {
269276 selectors := map [string ]string {
270277 "SELECTOR" : string (uuid .NewUUID ()),
271278 }
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 )
274282
275283 require .NoError (t , policy .Validate ())
276284
@@ -286,3 +294,72 @@ func Test_Validate(t *testing.T) {
286294 })
287295 }
288296}
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