Skip to content

Commit 6d3b319

Browse files
committed
Generate a fake clock
1 parent d6fad6e commit 6d3b319

File tree

9 files changed

+128
-53
lines changed

9 files changed

+128
-53
lines changed

internal/status/clock.go

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package status
22

3-
import "time"
3+
import (
4+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
5+
)
6+
7+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Clock
48

59
// Clock returns the current local time.
610
type Clock interface {
7-
Now() time.Time
11+
Now() metav1.Time
812
}
913

1014
// Real clock returns the current local time.
@@ -17,21 +21,6 @@ func NewRealClock() *RealClock {
1721
}
1822

1923
// Now returns the current local time.
20-
func (c *RealClock) Now() time.Time {
21-
return time.Now()
22-
}
23-
24-
// FakeClock allows you to control the returned time.
25-
type FakeClock struct {
26-
time time.Time
27-
}
28-
29-
// NewFakeClock creates a FakeClock. The clock will always return the specified time.
30-
func NewFakeClock(time time.Time) *FakeClock {
31-
return &FakeClock{time: time}
32-
}
33-
34-
// Now is a fake implementation of Now().
35-
func (c FakeClock) Now() time.Time {
36-
return c.time
24+
func (c *RealClock) Now() metav1.Time {
25+
return metav1.Now()
3726
}

internal/status/clock_test.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

internal/status/gateway.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// FIXME(pleshakov): Be compliant with in the Gateway API.
1414
// Currently, we only support simple valid/invalid status per each listener.
1515
// Extend support to cover more cases.
16-
func prepareGatewayStatus(statuses newstate.ListenerStatuses, clock Clock) v1alpha2.GatewayStatus {
16+
func prepareGatewayStatus(statuses newstate.ListenerStatuses, transitionTime metav1.Time) v1alpha2.GatewayStatus {
1717
listenerStatuses := make([]v1alpha2.ListenerStatus, 0, len(statuses))
1818

1919
// FIXME(pleshakov) Maintain the order from the Gateway resource
@@ -41,7 +41,7 @@ func prepareGatewayStatus(statuses newstate.ListenerStatuses, clock Clock) v1alp
4141
Status: metav1.ConditionStatus(status),
4242
// FIXME(pleshakov) Set the observed generation to the last processed generation of the Gateway resource.
4343
ObservedGeneration: 123,
44-
LastTransitionTime: metav1.NewTime(clock.Now()),
44+
LastTransitionTime: transitionTime,
4545
Reason: reason,
4646
Message: "", // FIXME(pleshakov) Come up with a good message
4747
}

internal/status/gateway_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ func TestPrepareGatewayStatus(t *testing.T) {
2323
},
2424
}
2525

26-
expectedTime := time.Now()
27-
clock := NewFakeClock(expectedTime)
26+
transitionTime := metav1.NewTime(time.Now())
2827

2928
expected := v1alpha2.GatewayStatus{
3029
Listeners: []v1alpha2.ListenerStatus{
@@ -41,7 +40,7 @@ func TestPrepareGatewayStatus(t *testing.T) {
4140
Type: string(v1alpha2.ListenerConditionReady),
4241
Status: "False",
4342
ObservedGeneration: 123,
44-
LastTransitionTime: metav1.Time{Time: expectedTime},
43+
LastTransitionTime: transitionTime,
4544
Reason: string(v1alpha2.ListenerReasonInvalid),
4645
},
4746
},
@@ -59,15 +58,15 @@ func TestPrepareGatewayStatus(t *testing.T) {
5958
Type: string(v1alpha2.ListenerConditionReady),
6059
Status: "True",
6160
ObservedGeneration: 123,
62-
LastTransitionTime: metav1.Time{Time: expectedTime},
61+
LastTransitionTime: transitionTime,
6362
Reason: string(v1alpha2.ListenerReasonReady),
6463
},
6564
},
6665
},
6766
},
6867
}
6968

70-
result := prepareGatewayStatus(statuses, clock)
69+
result := prepareGatewayStatus(statuses, transitionTime)
7170
if diff := cmp.Diff(expected, result); diff != "" {
7271
t.Errorf("prepareGatewayStatus() mismatch (-want +got):\n%s", diff)
7372
}

internal/status/httproute.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func prepareHTTPRouteStatus(
1818
status newstate.HTTPRouteStatus,
1919
gwNsName types.NamespacedName,
2020
gatewayCtlrName string,
21-
clock Clock,
21+
transitionTime metav1.Time,
2222
) v1alpha2.HTTPRouteStatus {
2323
parents := make([]v1alpha2.RouteParentStatus, 0, len(status.ParentStatuses))
2424

@@ -56,7 +56,7 @@ func prepareHTTPRouteStatus(
5656
Status: metav1.ConditionStatus(status),
5757
// FIXME(pleshakov) Set the observed generation to the last processed generation of the HTTPRoute resource.
5858
ObservedGeneration: 123,
59-
LastTransitionTime: metav1.NewTime(clock.Now()),
59+
LastTransitionTime: transitionTime,
6060
Reason: reason,
6161
Message: "", // FIXME(pleshakov): Figure out a good message
6262
},

internal/status/httproute_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ func TestPrepareHTTPRouteStatus(t *testing.T) {
2828
gwNsName := types.NamespacedName{Namespace: "test", Name: "gateway"}
2929
gatewayCtlrName := "test.example.com"
3030

31-
expectedTime := time.Now()
32-
clock := NewFakeClock(expectedTime)
31+
transitionTime := metav1.NewTime(time.Now())
3332

3433
expected := v1alpha2.HTTPRouteStatus{
3534
RouteStatus: v1alpha2.RouteStatus{
@@ -46,7 +45,7 @@ func TestPrepareHTTPRouteStatus(t *testing.T) {
4645
Type: string(v1alpha2.ConditionRouteAccepted),
4746
Status: "True",
4847
ObservedGeneration: 123,
49-
LastTransitionTime: metav1.Time{Time: expectedTime},
48+
LastTransitionTime: transitionTime,
5049
Reason: "Attached",
5150
},
5251
},
@@ -63,7 +62,7 @@ func TestPrepareHTTPRouteStatus(t *testing.T) {
6362
Type: string(v1alpha2.ConditionRouteAccepted),
6463
Status: "False",
6564
ObservedGeneration: 123,
66-
LastTransitionTime: metav1.Time{Time: expectedTime},
65+
LastTransitionTime: transitionTime,
6766
Reason: "Not attached",
6867
},
6968
},
@@ -72,7 +71,7 @@ func TestPrepareHTTPRouteStatus(t *testing.T) {
7271
},
7372
}
7473

75-
result := prepareHTTPRouteStatus(status, gwNsName, gatewayCtlrName, clock)
74+
result := prepareHTTPRouteStatus(status, gwNsName, gatewayCtlrName, transitionTime)
7675
if diff := cmp.Diff(expected, result); diff != "" {
7776
t.Errorf("prepareHTTPRouteStatus() mismatch (-want +got):\n%s", diff)
7877
}

internal/status/statusfakes/fake_clock.go

Lines changed: 103 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/status/updater.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (upd *updaterImpl) Update(ctx context.Context, statuses newstate.Statuses)
8888

8989
upd.update(ctx, upd.gwNsName, &v1alpha2.Gateway{}, func(object client.Object) {
9090
gw := object.(*v1alpha2.Gateway)
91-
gw.Status = prepareGatewayStatus(statuses.ListenerStatuses, upd.clock)
91+
gw.Status = prepareGatewayStatus(statuses.ListenerStatuses, upd.clock.Now())
9292
})
9393

9494
for nsname, rs := range statuses.HTTPRouteStatuses {
@@ -100,7 +100,7 @@ func (upd *updaterImpl) Update(ctx context.Context, statuses newstate.Statuses)
100100

101101
upd.update(ctx, nsname, &v1alpha2.HTTPRoute{}, func(object client.Object) {
102102
hr := object.(*v1alpha2.HTTPRoute)
103-
hr.Status = prepareHTTPRouteStatus(rs, upd.gwNsName, upd.gatewayCtlrName, upd.clock)
103+
hr.Status = prepareHTTPRouteStatus(rs, upd.gwNsName, upd.gatewayCtlrName, upd.clock.Now())
104104
})
105105
}
106106
}

internal/status/updater_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ import (
1818
"github.com/nginxinc/nginx-kubernetes-gateway/internal/helpers"
1919
"github.com/nginxinc/nginx-kubernetes-gateway/internal/newstate"
2020
"github.com/nginxinc/nginx-kubernetes-gateway/internal/status"
21+
"github.com/nginxinc/nginx-kubernetes-gateway/internal/status/statusfakes"
2122
)
2223

2324
var _ = Describe("Updater", func() {
2425
var (
2526
updater status.Updater
2627
client client.Client
2728
fakeClockTime metav1.Time
28-
fakeClock *status.FakeClock
2929
gatewayCtrlName string
3030
gwNsName types.NamespacedName
3131
)
@@ -43,7 +43,8 @@ var _ = Describe("Updater", func() {
4343
// We need to remove it, because updating the status in the FakeClient and then getting the resource back
4444
// involves encoding and decoding the resource to/from JSON, which removes the monotonic clock reading.
4545
fakeClockTime = metav1.NewTime(time.Now()).Rfc3339Copy()
46-
fakeClock = status.NewFakeClock(fakeClockTime.Time)
46+
fakeClock := &statusfakes.FakeClock{}
47+
fakeClock.NowReturns(fakeClockTime)
4748

4849
gatewayCtrlName = "test.example.com"
4950
gwNsName = types.NamespacedName{Namespace: "test", Name: "gateway"}

0 commit comments

Comments
 (0)