Skip to content

Commit

Permalink
fix(golang): in SetTime replace time.UTC by now.Location()
Browse files Browse the repository at this point in the history
  • Loading branch information
jplanckeel authored Aug 2, 2022
2 parents 526e4e6 + 5608999 commit 481f9e8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 44 deletions.
30 changes: 16 additions & 14 deletions pkg/prescaling/get_hpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -75,17 +77,17 @@ 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,
CurrentReplicas: 2,
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),
},
}

Expand All @@ -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(
Expand Down Expand Up @@ -197,17 +199,17 @@ 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),
},
},
{
name: "KO - Replica is null",
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),
},
},
{
Expand All @@ -216,15 +218,15 @@ 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),
},
},
{
name: "KO - End is null",
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{},
},
},
Expand All @@ -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),
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/prescaling_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
// }
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
57 changes: 30 additions & 27 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
},
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
},
}
Expand All @@ -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,
},
}
Expand Down

0 comments on commit 481f9e8

Please sign in to comment.