@@ -354,7 +354,7 @@ func TestNonLinearIntervals(t *testing.T) {
354354 }
355355}
356356
357- func TestNonLinearIntervalHandling (t * testing.T ) {
357+ func TestPrintReleaseTimeBoard_NonLinearIntervalHandling (t * testing.T ) {
358358 ts := func (t time.Time ) string {
359359 return t .Format ("15:04 MST" )
360360 }
@@ -436,3 +436,109 @@ func TestNonLinearIntervalHandling(t *testing.T) {
436436 })
437437 }
438438}
439+
440+ func TestDeployToAllReleaseBranches_NonLinearIntervalHandling (t * testing.T ) {
441+ ts := func (t time.Time ) string {
442+ return t .Format ("15:04:05" )
443+ }
444+ releaseTime := time .Date (2022 , 8 , 4 , 13 , 37 , 0 , 0 , time .UTC )
445+ tests := []struct {
446+ name string
447+ branches []string
448+ intervals string
449+ expectedPrints []string
450+ expectedFinalTime time.Time
451+ }{
452+ {
453+ name : "single branch, single interval" ,
454+ branches : []string {"branch1" },
455+ intervals : "10m" ,
456+ expectedPrints : []string {
457+ fmt .Sprintf ("Deploying %v branch1" , ts (releaseTime )),
458+ fmt .Sprintf ("%v Triggered Successfully" , ts (releaseTime )),
459+ },
460+ expectedFinalTime : releaseTime ,
461+ },
462+ {
463+ name : "multiple branches, single interval" ,
464+ branches : []string {"branch1" , "branch2" },
465+ intervals : "10m" ,
466+ expectedPrints : []string {
467+ fmt .Sprintf ("Deploying %v branch1" , ts (releaseTime )),
468+ fmt .Sprintf ("%v Triggered Successfully" , ts (releaseTime )),
469+ fmt .Sprintf ("Deploying %v branch2" , ts (releaseTime .Add (10 * time .Minute ))),
470+ fmt .Sprintf ("%v Triggered Successfully" , ts (releaseTime .Add (10 * time .Minute ))),
471+ },
472+ expectedFinalTime : releaseTime .Add (10 * time .Minute ),
473+ },
474+ {
475+ name : "multiple branches, decreasing interval" ,
476+ branches : []string {"branch1" , "branch2" , "branch3" , "branch4" , "branch5" },
477+ intervals : "10m,5m,1m,1m" ,
478+ expectedPrints : []string {
479+ fmt .Sprintf ("Deploying %v branch1" , ts (releaseTime )),
480+ fmt .Sprintf ("%v Triggered Successfully" , ts (releaseTime )),
481+ fmt .Sprintf ("Deploying %v branch2" , ts (releaseTime .Add (10 * time .Minute ))), // 10
482+ fmt .Sprintf ("%v Triggered Successfully" , ts (releaseTime .Add (10 * time .Minute ))),
483+ fmt .Sprintf ("Deploying %v branch3" , ts (releaseTime .Add (15 * time .Minute ))), // 10 + 5
484+ fmt .Sprintf ("%v Triggered Successfully" , ts (releaseTime .Add (15 * time .Minute ))),
485+ fmt .Sprintf ("Deploying %v branch4" , ts (releaseTime .Add (16 * time .Minute ))), // 10 + 5 + 1
486+ fmt .Sprintf ("%v Triggered Successfully" , ts (releaseTime .Add (16 * time .Minute ))),
487+ fmt .Sprintf ("Deploying %v branch5" , ts (releaseTime .Add (17 * time .Minute ))), // 10 + 5 + 1 + 1
488+ fmt .Sprintf ("%v Triggered Successfully" , ts (releaseTime .Add (17 * time .Minute ))),
489+ },
490+ expectedFinalTime : releaseTime .Add (17 * time .Minute ),
491+ },
492+ {
493+ name : "multiple branches, fewer intervals" ,
494+ branches : []string {"branch1" , "branch2" , "branch3" , "branch4" , "branch5" },
495+ intervals : "10m,5m" ,
496+ expectedPrints : []string {
497+ fmt .Sprintf ("Deploying %v branch1" , ts (releaseTime )),
498+ fmt .Sprintf ("%v Triggered Successfully" , ts (releaseTime )),
499+ fmt .Sprintf ("Deploying %v branch2" , ts (releaseTime .Add (10 * time .Minute ))), // 10
500+ fmt .Sprintf ("%v Triggered Successfully" , ts (releaseTime .Add (10 * time .Minute ))),
501+ fmt .Sprintf ("Deploying %v branch3" , ts (releaseTime .Add (15 * time .Minute ))), // 10 + 5
502+ fmt .Sprintf ("%v Triggered Successfully" , ts (releaseTime .Add (15 * time .Minute ))),
503+ fmt .Sprintf ("Deploying %v branch4" , ts (releaseTime .Add (25 * time .Minute ))), // 10 + 5 + 10
504+ fmt .Sprintf ("%v Triggered Successfully" , ts (releaseTime .Add (25 * time .Minute ))),
505+ fmt .Sprintf ("Deploying %v branch5" , ts (releaseTime .Add (30 * time .Minute ))), // 10 + 5 + 10 + 5
506+ fmt .Sprintf ("%v Triggered Successfully" , ts (releaseTime .Add (30 * time .Minute ))),
507+ },
508+ expectedFinalTime : releaseTime .Add (30 * time .Minute ),
509+ },
510+ }
511+ for _ , test := range tests {
512+ t .Run (test .name , func (t * testing.T ) {
513+ cliMock := & mock.CLI {}
514+ timeMock := mock .NewMockedTime (releaseTime )
515+ deploy := & Deploy {
516+ c : cliMock ,
517+ releaseBranches : test .branches ,
518+ host : & mock.RepositoryClient {},
519+ time : timeMock ,
520+ }
521+ intervalDurations , _ , err := deploy .calculateReleaseTime (test .intervals , "1ms" )
522+ if err != nil {
523+ t .Errorf ("NewDeploy().Do() returned error: %v" , err )
524+ }
525+ err = deploy .deployToAllReleaseBranches (context .Background (), intervalDurations , & ergo.Release {}, false )
526+ if err != nil {
527+ t .Errorf ("Deploy.deployToAllReleaseBranches() returned error %v" , err )
528+ }
529+ if len (test .expectedPrints ) != len (cliMock .PrintLines ) {
530+ t .Errorf ("Expected %d number of prints to the mock, got %d" , len (test .expectedPrints ), len (cliMock .PrintLines ))
531+ } else {
532+ for i , expected := range test .expectedPrints {
533+ got := cliMock .PrintLines [i ]
534+ if ! reflect .DeepEqual (expected , got ) {
535+ t .Errorf ("Expected print %d: '%v' to equal '%v'" , i , expected , got )
536+ }
537+ }
538+ }
539+ if test .expectedFinalTime != timeMock .CurrentTime {
540+ t .Errorf ("Expected final time to equal %v, got %v" , test .expectedFinalTime , timeMock .CurrentTime )
541+ }
542+ })
543+ }
544+ }
0 commit comments