From 0384263a5c802b8d3b937762034cd5c62d180dd7 Mon Sep 17 00:00:00 2001 From: Jeremy PLANCKEEL Date: Mon, 1 Aug 2022 17:40:46 +0200 Subject: [PATCH 1/2] fix(golang): in SetTime replace time.UTC by now.Location() --- pkg/utils/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index adfd13f..2aafec9 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -13,7 +13,7 @@ func SetTime(str string, now time.Time) (time.Time, error) { } hour, min, sec := timeStamp.Clock() - dateTime := time.Date(now.Year(), now.Month(), now.Day(), hour, min, sec, 0, time.UTC) + dateTime := time.Date(now.Year(), now.Month(), now.Day(), hour, min, sec, 0, now.Location()) return dateTime, nil } From 56089994f2d980d1982b7220b1580974abab96c2 Mon Sep 17 00:00:00 2001 From: Jeremy PLANCKEEL Date: Tue, 2 Aug 2022 09:43:36 +0200 Subject: [PATCH 2/2] fix(golang): test change time.utc by time.now.local.location --- pkg/prescaling/get_hpa_test.go | 30 +++++++------- pkg/services/prescaling_event_test.go | 4 +- pkg/utils/utils_test.go | 57 ++++++++++++++------------- 3 files changed, 48 insertions(+), 43 deletions(-) diff --git a/pkg/prescaling/get_hpa_test.go b/pkg/prescaling/get_hpa_test.go index c3dbaa9..8888ce7 100644 --- a/pkg/prescaling/get_hpa_test.go +++ b/pkg/prescaling/get_hpa_test.go @@ -16,8 +16,10 @@ import ( testclientk8s "k8s.io/client-go/kubernetes/fake" ) +var loc = time.Now().Local().Location() + func TestGetHpa(t *testing.T) { - fakeClock := clock.NewFakeClock(time.Date(2022, time.March, 2, 21, 0, 0, 0, time.UTC)) + fakeClock := clock.NewFakeClock(time.Date(2022, time.March, 2, 21, 0, 0, 0, loc)) var client k8s.Client client.Clientset = testclientk8s.NewSimpleClientset( @@ -75,8 +77,8 @@ func TestGetHpa(t *testing.T) { Project: "project-a", Namespace: "default", Deployment: "project-a", - Start: time.Date(2022, time.March, 2, 20, 00, 0, 0, time.UTC), - End: time.Date(2022, time.March, 2, 23, 00, 0, 0, time.UTC), + Start: time.Date(2022, time.March, 2, 20, 00, 0, 0, loc), + End: time.Date(2022, time.March, 2, 23, 00, 0, 0, loc), }, { Replica: 20, @@ -84,8 +86,8 @@ func TestGetHpa(t *testing.T) { Project: "project-b", Namespace: "default", Deployment: "project-b", - Start: time.Date(2022, time.March, 2, 18, 00, 0, 0, time.UTC), - End: time.Date(2022, time.March, 2, 23, 30, 0, 0, time.UTC), + Start: time.Date(2022, time.March, 2, 18, 00, 0, 0, loc), + End: time.Date(2022, time.March, 2, 23, 30, 0, 0, loc), }, } @@ -94,7 +96,7 @@ func TestGetHpa(t *testing.T) { } func TestGetHpaError(t *testing.T) { - fakeClock := clock.NewFakeClock(time.Date(2022, time.March, 2, 21, 0, 0, 0, time.UTC)) + fakeClock := clock.NewFakeClock(time.Date(2022, time.March, 2, 21, 0, 0, 0, loc)) var client k8s.Client client.Clientset = testclientk8s.NewSimpleClientset( @@ -197,8 +199,8 @@ func TestCheckAnnotationsKO(t *testing.T) { name: "OK", prescaling: Hpa{ Replica: 1, - Start: time.Date(2022, time.March, 2, 20, 30, 0, 0, time.UTC), - End: time.Date(2022, time.March, 2, 20, 30, 0, 0, time.UTC), + Start: time.Date(2022, time.March, 2, 20, 30, 0, 0, loc), + End: time.Date(2022, time.March, 2, 20, 30, 0, 0, loc), }, }, { @@ -206,8 +208,8 @@ func TestCheckAnnotationsKO(t *testing.T) { expected: "annotation replica min is misconfigured", prescaling: Hpa{ Replica: 0, - Start: time.Date(2022, time.March, 2, 20, 30, 0, 0, time.UTC), - End: time.Date(2022, time.March, 2, 20, 30, 0, 0, time.UTC), + Start: time.Date(2022, time.March, 2, 20, 30, 0, 0, loc), + End: time.Date(2022, time.March, 2, 20, 30, 0, 0, loc), }, }, { @@ -216,7 +218,7 @@ func TestCheckAnnotationsKO(t *testing.T) { prescaling: Hpa{ Replica: 1, Start: time.Time{}, - End: time.Date(2022, time.March, 2, 20, 30, 0, 0, time.UTC), + End: time.Date(2022, time.March, 2, 20, 30, 0, 0, loc), }, }, { @@ -224,7 +226,7 @@ func TestCheckAnnotationsKO(t *testing.T) { expected: "annotation time start is misconfigured", prescaling: Hpa{ Replica: 1, - Start: time.Date(2022, time.March, 2, 20, 30, 0, 0, time.UTC), + Start: time.Date(2022, time.March, 2, 20, 30, 0, 0, loc), End: time.Time{}, }, }, @@ -250,8 +252,8 @@ func TestCheckAnnotationsOK(t *testing.T) { name: "OK - checkAnnotation return nil", prescaling: Hpa{ Replica: 1, - Start: time.Date(2022, time.March, 2, 20, 30, 0, 0, time.UTC), - End: time.Date(2022, time.March, 2, 20, 30, 0, 0, time.UTC), + Start: time.Date(2022, time.March, 2, 20, 30, 0, 0, loc), + End: time.Date(2022, time.March, 2, 20, 30, 0, 0, loc), }, }, } diff --git a/pkg/services/prescaling_event_test.go b/pkg/services/prescaling_event_test.go index 1f38d87..6b58f0c 100644 --- a/pkg/services/prescaling_event_test.go +++ b/pkg/services/prescaling_event_test.go @@ -14,7 +14,7 @@ import ( func TestNewEventService(t *testing.T) { - fakeClock := testclock.NewFakeClock(time.Date(2022, time.March, 2, 21, 0, 0, 0, time.UTC)) + fakeClock := testclock.NewFakeClock(time.Date(2022, time.March, 2, 21, 0, 0, 0, time.Now().Local().Location())) fakePrescalingEvents := &fake.FakePrescalingEvents{} type args struct { @@ -48,7 +48,7 @@ func TestNewEventService(t *testing.T) { } //func TestPrescalingEventService_Clean(t *testing.T) { -// fakeClock := testclock.NewFakeClock(time.Date(2022, time.March, 2, 21, 0, 0, 0, time.UTC)) +// fakeClock := testclock.NewFakeClock(time.Date(2022, time.March, 2, 21, 0, 0, 0, time.Now().Local().Location())) // fakeEventRepository := fake.FakePrescalingEvents{} // fakeEventRepository.Fake.ReactionChain = []testingFake.Reactor{ // } diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 403d0d5..8390434 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -7,9 +7,12 @@ import ( "github.com/stretchr/testify/assert" ) +var loc = time.Now().Local().Location() + func TestSetTime(t *testing.T) { - faketime := time.Date(2022, time.March, 2, 21, 0, 0, 0, time.UTC) + faketime := time.Date(2022, time.March, 2, 21, 0, 0, 0, loc) + testCases := []struct { name string @@ -19,12 +22,12 @@ func TestSetTime(t *testing.T) { { name: "OK - Test 1", dateStr: "10:00:00", - dateExpected: time.Date(2022, time.March, 2, 10, 0, 0, 0, time.UTC), + dateExpected: time.Date(2022, time.March, 2, 10, 0, 0, 0, loc), }, { name: "OK - Test 2", dateStr: "00:00:00", - dateExpected: time.Date(2022, time.March, 2, 0, 0, 0, 0, time.UTC), + dateExpected: time.Date(2022, time.March, 2, 0, 0, 0, 0, loc), }, } @@ -36,7 +39,7 @@ func TestSetTime(t *testing.T) { func TestSetTimeError(t *testing.T) { - faketime := time.Date(2022, time.March, 2, 21, 0, 0, 0, time.UTC) + faketime := time.Date(2022, time.March, 2, 21, 0, 0, 0, loc) testCases := []struct { name string @@ -76,7 +79,7 @@ func TestSetTimeError(t *testing.T) { func TestInRangeTime(t *testing.T) { - faketime := time.Date(2022, time.March, 2, 21, 0, 0, 0, time.UTC) + faketime := time.Date(2022, time.March, 2, 21, 0, 0, 0, loc) testCases := []struct { expected bool @@ -86,38 +89,38 @@ func TestInRangeTime(t *testing.T) { }{ { name: "OK - is inside the period ", - dateStart: time.Date(2022, time.March, 2, 20, 0, 0, 0, time.UTC), - dateEnd: time.Date(2022, time.March, 2, 22, 0, 0, 0, time.UTC), + dateStart: time.Date(2022, time.March, 2, 20, 0, 0, 0, loc), + dateEnd: time.Date(2022, time.March, 2, 22, 0, 0, 0, loc), expected: true, }, { name: "OK - dateStart and time.Now is Equal", - dateStart: time.Date(2022, time.March, 2, 21, 0, 0, 0, time.UTC), - dateEnd: time.Date(2022, time.March, 2, 22, 0, 0, 0, time.UTC), + dateStart: time.Date(2022, time.March, 2, 21, 0, 0, 0, loc), + dateEnd: time.Date(2022, time.March, 2, 22, 0, 0, 0, loc), expected: true, }, { name: "OK - dateEnd and time.Now is Equal", - dateStart: time.Date(2022, time.March, 2, 20, 0, 0, 0, time.UTC), - dateEnd: time.Date(2022, time.March, 2, 21, 0, 0, 0, time.UTC), + dateStart: time.Date(2022, time.March, 2, 20, 0, 0, 0, loc), + dateEnd: time.Date(2022, time.March, 2, 21, 0, 0, 0, loc), expected: true, }, { name: "OK - dateEnd is after midnight", - dateStart: time.Date(2022, time.March, 2, 20, 0, 0, 0, time.UTC), - dateEnd: time.Date(2022, time.March, 2, 0, 30, 0, 0, time.UTC), + dateStart: time.Date(2022, time.March, 2, 20, 0, 0, 0, loc), + dateEnd: time.Date(2022, time.March, 2, 0, 30, 0, 0, loc), expected: true, }, { name: "KO - dateStart and dateEnd is inverted", - dateStart: time.Date(2022, time.March, 2, 22, 0, 0, 0, time.UTC), - dateEnd: time.Date(2022, time.March, 2, 0, 30, 0, 0, time.UTC), + dateStart: time.Date(2022, time.March, 2, 22, 0, 0, 0, loc), + dateEnd: time.Date(2022, time.March, 2, 0, 30, 0, 0, loc), expected: false, }, { name: "KO - is outside the period", - dateStart: time.Date(2022, time.March, 2, 19, 0, 0, 0, time.UTC), - dateEnd: time.Date(2022, time.March, 2, 20, 30, 0, 0, time.UTC), + dateStart: time.Date(2022, time.March, 2, 19, 0, 0, 0, loc), + dateEnd: time.Date(2022, time.March, 2, 20, 30, 0, 0, loc), expected: false, }, } @@ -138,32 +141,32 @@ func TestDaysBetweenDates(t *testing.T) { }{ { name: "OK - 10d8h", - today: time.Date(2022, time.March, 13, 20, 0, 0, 0, time.UTC), - eventDate: time.Date(2022, time.March, 2, 22, 0, 0, 0, time.UTC), + today: time.Date(2022, time.March, 13, 20, 0, 0, 0, loc), + eventDate: time.Date(2022, time.March, 2, 22, 0, 0, 0, loc), expected: 10, }, { name: "OK - 9d,8h", - today: time.Date(2022, time.March, 12, 20, 0, 0, 0, time.UTC), - eventDate: time.Date(2022, time.March, 2, 22, 0, 0, 0, time.UTC), + today: time.Date(2022, time.March, 12, 20, 0, 0, 0, loc), + eventDate: time.Date(2022, time.March, 2, 22, 0, 0, 0, loc), expected: 9, }, { name: "OK - 10d,1h", - today: time.Date(2022, time.March, 12, 20, 0, 0, 0, time.UTC), - eventDate: time.Date(2022, time.March, 2, 19, 0, 0, 0, time.UTC), + today: time.Date(2022, time.March, 12, 20, 0, 0, 0, loc), + eventDate: time.Date(2022, time.March, 2, 19, 0, 0, 0, loc), expected: 10, }, { name: "OK - 10d,1h", - today: time.Date(2022, time.March, 2, 20, 0, 0, 0, time.UTC), - eventDate: time.Date(2022, time.March, 12, 19, 0, 0, 0, time.UTC), + today: time.Date(2022, time.March, 2, 20, 0, 0, 0, loc), + eventDate: time.Date(2022, time.March, 12, 19, 0, 0, 0, loc), expected: 0, }, { name: "OK - 10d,1h", - today: time.Date(2022, time.March, 2, 20, 0, 0, 0, time.UTC), - eventDate: time.Date(2022, time.March, 2, 19, 0, 0, 0, time.UTC), + today: time.Date(2022, time.March, 2, 20, 0, 0, 0, loc), + eventDate: time.Date(2022, time.March, 2, 19, 0, 0, 0, loc), expected: 0, }, }