Skip to content

Commit f447b6b

Browse files
authored
Merge pull request #2822 from jongwooo/inject-a-fake-clock-during-testing-gwctl-get-gateways
Inject a fake clock during testing `gwctl get gateways`
2 parents da26d60 + 2edc120 commit f447b6b

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

gwctl/pkg/cmd/get/get.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222

2323
"github.com/spf13/cobra"
2424

25+
"k8s.io/utils/clock"
26+
2527
"sigs.k8s.io/gateway-api/gwctl/pkg/cmd/utils"
2628
"sigs.k8s.io/gateway-api/gwctl/pkg/printer"
2729
"sigs.k8s.io/gateway-api/gwctl/pkg/resourcediscovery"
@@ -60,7 +62,8 @@ func runGet(args []string, params *utils.CmdParams, flags *getFlags) {
6062
K8sClients: params.K8sClients,
6163
PolicyManager: params.PolicyManager,
6264
}
63-
gwPrinter := &printer.GatewaysPrinter{Out: params.Out}
65+
realClock := clock.RealClock{}
66+
gwPrinter := &printer.GatewaysPrinter{Out: params.Out, Clock: realClock}
6467
policiesPrinter := &printer.PoliciesPrinter{Out: params.Out}
6568
httpRoutesPrinter := &printer.HTTPRoutesPrinter{Out: params.Out}
6669

gwctl/pkg/printer/gateways.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ import (
2121
"io"
2222
"strings"
2323
"text/tabwriter"
24-
"time"
25-
26-
"sigs.k8s.io/yaml"
2724

2825
"sigs.k8s.io/gateway-api/gwctl/pkg/policymanager"
2926
"sigs.k8s.io/gateway-api/gwctl/pkg/resourcediscovery"
30-
27+
"sigs.k8s.io/yaml"
28+
3129
"k8s.io/apimachinery/pkg/util/duration"
30+
"k8s.io/utils/clock"
3231
)
3332

3433
type GatewaysPrinter struct {
35-
Out io.Writer
34+
Out io.Writer
35+
Clock clock.Clock
3636
}
3737

3838
type gatewayDescribeView struct {
@@ -74,7 +74,7 @@ func (gp *GatewaysPrinter) Print(resourceModel *resourcediscovery.ResourceModel)
7474
}
7575
}
7676

77-
age := duration.HumanDuration(time.Since(gatewayNode.Gateway.GetCreationTimestamp().Time))
77+
age := duration.HumanDuration(gp.Clock.Since(gatewayNode.Gateway.GetCreationTimestamp().Time))
7878

7979
row := []string{
8080
gatewayNode.Gateway.GetName(),

gwctl/pkg/printer/gateways_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2828
"k8s.io/apimachinery/pkg/runtime"
29+
testingclock "k8s.io/utils/clock/testing"
2930

3031
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
3132
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
@@ -35,6 +36,7 @@ import (
3536
)
3637

3738
func TestGatewaysPrinter_Print(t *testing.T) {
39+
fakeClock := testingclock.NewFakeClock(time.Now())
3840
objects := []runtime.Object{
3941
&gatewayv1.GatewayClass{
4042
ObjectMeta: metav1.ObjectMeta{
@@ -50,7 +52,7 @@ func TestGatewaysPrinter_Print(t *testing.T) {
5052
ObjectMeta: metav1.ObjectMeta{
5153
Name: "foo-gateway",
5254
CreationTimestamp: metav1.Time{
53-
Time: time.Now().Add(-time.Second),
55+
Time: fakeClock.Now().Add(-time.Second),
5456
},
5557
},
5658
Spec: gatewayv1.GatewaySpec{
@@ -90,7 +92,8 @@ func TestGatewaysPrinter_Print(t *testing.T) {
9092
}
9193

9294
gp := &GatewaysPrinter{
93-
Out: params.Out,
95+
Out: params.Out,
96+
Clock: fakeClock,
9497
}
9598
gp.Print(resourceModel)
9699

@@ -106,6 +109,7 @@ foo-gateway foo-gatewayclass 10.0.0.1 80 True 1s
106109
}
107110

108111
func TestGatewaysPrinter_PrintDescribeView(t *testing.T) {
112+
fakeClock := testingclock.NewFakeClock(time.Now())
109113
objects := []runtime.Object{
110114
&gatewayv1.GatewayClass{
111115
ObjectMeta: metav1.ObjectMeta{
@@ -240,7 +244,8 @@ func TestGatewaysPrinter_PrintDescribeView(t *testing.T) {
240244
}
241245

242246
gp := &GatewaysPrinter{
243-
Out: params.Out,
247+
Out: params.Out,
248+
Clock: fakeClock,
244249
}
245250
gp.PrintDescribeView(resourceModel)
246251

0 commit comments

Comments
 (0)