@@ -19,19 +19,23 @@ package printer
19
19
import (
20
20
"bytes"
21
21
"testing"
22
+ "time"
22
23
23
24
"github.com/google/go-cmp/cmp"
25
+
24
26
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
25
27
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
28
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27
29
"k8s.io/apimachinery/pkg/runtime"
28
- gatewayv1alpha2 "sigs. k8s.io/gateway-api/apis/v1alpha2 "
30
+ testingclock " k8s.io/utils/clock/testing "
29
31
32
+ gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
30
33
"sigs.k8s.io/gateway-api/gwctl/pkg/cmd/utils"
31
34
"sigs.k8s.io/gateway-api/gwctl/pkg/common"
32
35
)
33
36
34
37
func TestPoliciesPrinter_Print_And_PrintDescribeView (t * testing.T ) {
38
+ fakeClock := testingclock .NewFakeClock (time .Now ())
35
39
objects := []runtime.Object {
36
40
& apiextensionsv1.CustomResourceDefinition {
37
41
ObjectMeta : metav1.ObjectMeta {
@@ -55,7 +59,8 @@ func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
55
59
"apiVersion" : "foo.com/v1" ,
56
60
"kind" : "HealthCheckPolicy" ,
57
61
"metadata" : map [string ]interface {}{
58
- "name" : "health-check-gatewayclass" ,
62
+ "name" : "health-check-gatewayclass" ,
63
+ "creationTimestamp" : fakeClock .Now ().Add (- 6 * 24 * time .Hour ).Format (time .RFC3339 ),
59
64
},
60
65
"spec" : map [string ]interface {}{
61
66
"override" : map [string ]interface {}{
@@ -80,7 +85,8 @@ func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
80
85
"apiVersion" : "foo.com/v1" ,
81
86
"kind" : "HealthCheckPolicy" ,
82
87
"metadata" : map [string ]interface {}{
83
- "name" : "health-check-gateway" ,
88
+ "name" : "health-check-gateway" ,
89
+ "creationTimestamp" : fakeClock .Now ().Add (- 20 * 24 * time .Hour ).Format (time .RFC3339 ),
84
90
},
85
91
"spec" : map [string ]interface {}{
86
92
"override" : map [string ]interface {}{
@@ -122,7 +128,8 @@ func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
122
128
"apiVersion" : "bar.com/v1" ,
123
129
"kind" : "TimeoutPolicy" ,
124
130
"metadata" : map [string ]interface {}{
125
- "name" : "timeout-policy-namespace" ,
131
+ "name" : "timeout-policy-namespace" ,
132
+ "creationTimestamp" : fakeClock .Now ().Add (- 5 * time .Minute ).Format (time .RFC3339 ),
126
133
},
127
134
"spec" : map [string ]interface {}{
128
135
"condition" : "path=/abc" ,
@@ -139,7 +146,8 @@ func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
139
146
"apiVersion" : "bar.com/v1" ,
140
147
"kind" : "TimeoutPolicy" ,
141
148
"metadata" : map [string ]interface {}{
142
- "name" : "timeout-policy-httproute" ,
149
+ "name" : "timeout-policy-httproute" ,
150
+ "creationTimestamp" : fakeClock .Now ().Add (- 13 * time .Minute ).Format (time .RFC3339 ),
143
151
},
144
152
"spec" : map [string ]interface {}{
145
153
"condition" : "path=/def" ,
@@ -157,17 +165,18 @@ func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
157
165
params := utils .MustParamsForTest (t , common .MustClientsForTest (t , objects ... ))
158
166
159
167
pp := & PoliciesPrinter {
160
- Out : & bytes.Buffer {},
168
+ Out : & bytes.Buffer {},
169
+ Clock : fakeClock ,
161
170
}
162
171
163
172
pp .Print (params .PolicyManager .GetPolicies ())
164
173
got := pp .Out .(* bytes.Buffer ).String ()
165
174
want := `
166
- POLICY NAME POLICY KIND TARGET NAME TARGET KIND POLICY TYPE
167
- health-check-gateway HealthCheckPolicy foo-gateway Gateway Inherited
168
- health-check-gatewayclass HealthCheckPolicy foo-gatewayclass GatewayClass Inherited
169
- timeout-policy-httproute TimeoutPolicy foo-httproute HTTPRoute Direct
170
- timeout-policy-namespace TimeoutPolicy default Namespace Direct
175
+ NAME KIND TARGET NAME TARGET KIND POLICY TYPE AGE
176
+ health-check-gateway HealthCheckPolicy.foo.com foo-gateway Gateway Inherited 20d
177
+ health-check-gatewayclass HealthCheckPolicy.foo.com foo-gatewayclass GatewayClass Inherited 6d
178
+ timeout-policy-httproute TimeoutPolicy.bar.com foo-httproute HTTPRoute Direct 13m
179
+ timeout-policy-namespace TimeoutPolicy.bar.com default Namespace Direct 5m
171
180
`
172
181
if diff := cmp .Diff (common .YamlString (want ), common .YamlString (got ), common .YamlStringTransformer ); diff != "" {
173
182
t .Errorf ("Print: Unexpected diff\n got=\n %v\n want=\n %v\n diff (-want +got)=\n %v" , got , want , diff )
0 commit comments