generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from m00g3n/add-requeue-tests
Add requeue tests
- Loading branch information
Showing
7 changed files
with
110 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package controller | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
func findLastSyncTime(annotations map[string]string) (time.Time, bool) { | ||
_, found := annotations[lastKubeconfigSyncAnnotation] | ||
if !found { | ||
return time.Time{}, false | ||
} | ||
|
||
lastSyncTimeString := annotations[lastKubeconfigSyncAnnotation] | ||
lastSyncTime, err := time.Parse(time.RFC3339, lastSyncTimeString) | ||
if err != nil { | ||
return time.Time{}, false | ||
} | ||
return lastSyncTime, true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package controller | ||
|
||
import ( | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("findLastSyncTime", func() { | ||
|
||
DescribeTable("should return expected values when", | ||
func(annotations map[string]string, expectedFound bool, expectedTime time.Time) { | ||
lastSyncTime, found := findLastSyncTime(annotations) | ||
Expect(found).To(Equal(expectedFound)) | ||
Expect(lastSyncTime).To(Equal(expectedTime)) | ||
}, | ||
Entry("receives empty annotation map", make(map[string]string), false, time.Time{}), | ||
Entry("receives annotation map containing valid date value", | ||
map[string]string{lastKubeconfigSyncAnnotation: "2023-01-01T12:00:00Z"}, true, | ||
func() time.Time { | ||
t, _ := time.Parse(time.RFC3339, "2023-01-01T12:00:00Z") | ||
return t | ||
}()), | ||
Entry("receives annotation map containing invalid date value", map[string]string{lastKubeconfigSyncAnnotation: "invalid"}, false, time.Time{}), | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package controller | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// nextRequeue - predicts duration for next requeue of GardenerCluster CR | ||
func nextRequeue(now, lastSyncTime time.Time, rotationPeriod time.Duration, modifier float64) time.Duration { | ||
rotationPeriodWithModifier := modifier * rotationPeriod.Minutes() | ||
minutesToRequeue := rotationPeriodWithModifier - now.Sub(lastSyncTime).Minutes() | ||
if minutesToRequeue <= 0 { | ||
return time.Duration(rotationPeriodWithModifier * float64(time.Minute)) | ||
} | ||
|
||
return time.Duration(minutesToRequeue * float64(time.Minute)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package controller | ||
|
||
import ( | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("nextRequeueAfter", func() { | ||
var ( | ||
now = time.Now() | ||
newYear, _ = time.Parse(time.RFC3339, "2024-01-01T00:00:01Z00:00") | ||
) | ||
|
||
DescribeTable("should return expected values when", | ||
func(now, lastSyncTime time.Time, rotationPeriod time.Duration, modifier float64, expectedDuration time.Duration) { | ||
result := nextRequeue(now, lastSyncTime, rotationPeriod, modifier) | ||
Expect(result).To(BeNumerically("~", expectedDuration, 1)) | ||
}, | ||
Entry("receives all zero arguments", now, time.Time{}, time.Duration(0), 0.0, time.Duration(0)), | ||
Entry("receives arguments (now, zero, 1[m], 0.95)", now, time.Time{}, time.Minute, 0.95, time.Second*57), | ||
Entry("receives arguments (now, now-30[s], 1[m], 0.95)", now, now.Add(-30*time.Second), time.Minute, 0.95, time.Nanosecond*26999999999), | ||
Entry("receives arguments (now, now-900[s], 1[m], 0.95)", now, now.Add(-900*time.Second), time.Minute, 0.95, time.Second*57), | ||
Entry("receives arguments (now, now, 1[m], 0.95)", now, now, time.Minute, 0.95, time.Second*57), | ||
Entry("receives arguments (newYear, newYear-45[m], 1[h], 0.95)", newYear, newYear.Add(-45*time.Minute), time.Hour, 0.95, time.Nanosecond*719999999999), | ||
) | ||
}) |