@@ -19,21 +19,205 @@ package printer
19
19
import (
20
20
"bytes"
21
21
"testing"
22
+ "time"
23
+
24
+ gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
25
+ gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
22
26
23
27
"github.com/google/go-cmp/cmp"
24
28
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
25
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
30
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27
31
"k8s.io/apimachinery/pkg/runtime"
28
- gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
29
- gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
32
+ testingclock "k8s.io/utils/clock/testing"
30
33
31
34
"sigs.k8s.io/gateway-api/gwctl/pkg/cmd/utils"
32
35
"sigs.k8s.io/gateway-api/gwctl/pkg/common"
33
36
"sigs.k8s.io/gateway-api/gwctl/pkg/resourcediscovery"
34
37
)
35
38
39
+ func TestHTTPRoutesPrinter_Print (t * testing.T ) {
40
+ fakeClock := testingclock .NewFakeClock (time .Now ())
41
+ objects := []runtime.Object {
42
+ & gatewayv1.GatewayClass {
43
+ ObjectMeta : metav1.ObjectMeta {
44
+ Name : "demo-gatewayclass-1" ,
45
+ },
46
+ Spec : gatewayv1.GatewayClassSpec {
47
+ ControllerName : "example.net/gateway-controller" ,
48
+ Description : common .PtrTo ("random" ),
49
+ },
50
+ },
51
+ & gatewayv1.GatewayClass {
52
+ ObjectMeta : metav1.ObjectMeta {
53
+ Name : "demo-gatewayclass-2" ,
54
+ },
55
+ Spec : gatewayv1.GatewayClassSpec {
56
+ ControllerName : "example.net/gateway-controller" ,
57
+ Description : common .PtrTo ("random" ),
58
+ },
59
+ },
60
+ & gatewayv1.Gateway {
61
+ ObjectMeta : metav1.ObjectMeta {
62
+ Name : "demo-gateway-1" ,
63
+ Namespace : "default" ,
64
+ },
65
+ Spec : gatewayv1.GatewaySpec {
66
+ GatewayClassName : "demo-gatewayclass-1" ,
67
+ },
68
+ },
69
+ & gatewayv1.Gateway {
70
+ ObjectMeta : metav1.ObjectMeta {
71
+ Name : "demo-gateway-2" ,
72
+ Namespace : "ns2" ,
73
+ },
74
+ Spec : gatewayv1.GatewaySpec {
75
+ GatewayClassName : "demo-gatewayclass-2" ,
76
+ },
77
+ },
78
+ & gatewayv1.Gateway {
79
+ ObjectMeta : metav1.ObjectMeta {
80
+ Name : "demo-gateway-200" ,
81
+ Namespace : "default" ,
82
+ },
83
+ Spec : gatewayv1.GatewaySpec {
84
+ GatewayClassName : "demo-gatewayclass-1" ,
85
+ },
86
+ },
87
+ & gatewayv1.Gateway {
88
+ ObjectMeta : metav1.ObjectMeta {
89
+ Name : "demo-gateway-345" ,
90
+ Namespace : "ns1" ,
91
+ },
92
+ Spec : gatewayv1.GatewaySpec {
93
+ GatewayClassName : "demo-gatewayclass-2" ,
94
+ },
95
+ },
96
+ & gatewayv1.HTTPRoute {
97
+ ObjectMeta : metav1.ObjectMeta {
98
+ Name : "foo-httproute-1" ,
99
+ Namespace : "default" ,
100
+ CreationTimestamp : metav1.Time {
101
+ Time : fakeClock .Now ().Add (- 24 * time .Hour ),
102
+ },
103
+ },
104
+ Spec : gatewayv1.HTTPRouteSpec {
105
+ Hostnames : []gatewayv1.Hostname {"example.com" , "example2.com" , "example3.com" },
106
+ CommonRouteSpec : gatewayv1.CommonRouteSpec {
107
+ ParentRefs : []gatewayv1.ParentReference {
108
+ {
109
+ Kind : common .PtrTo (gatewayv1 .Kind ("Gateway" )),
110
+ Group : common .PtrTo (gatewayv1 .Group ("gateway.networking.k8s.io" )),
111
+ Namespace : common .PtrTo (gatewayv1 .Namespace ("ns2" )),
112
+ Name : "demo-gateway-2" ,
113
+ },
114
+ },
115
+ },
116
+ },
117
+ },
118
+ & gatewayv1.HTTPRoute {
119
+ ObjectMeta : metav1.ObjectMeta {
120
+ Name : "qmn-httproute-100" ,
121
+ Namespace : "default" ,
122
+ CreationTimestamp : metav1.Time {
123
+ Time : fakeClock .Now ().Add (- 11 * time .Hour ),
124
+ },
125
+ },
126
+ Spec : gatewayv1.HTTPRouteSpec {
127
+ Hostnames : []gatewayv1.Hostname {"example.com" },
128
+ CommonRouteSpec : gatewayv1.CommonRouteSpec {
129
+ ParentRefs : []gatewayv1.ParentReference {
130
+ {
131
+ Kind : common .PtrTo (gatewayv1 .Kind ("Gateway" )),
132
+ Group : common .PtrTo (gatewayv1 .Group ("gateway.networking.k8s.io" )),
133
+ Name : "demo-gateway-1" ,
134
+ },
135
+ {
136
+ Kind : common .PtrTo (gatewayv1 .Kind ("Gateway" )),
137
+ Group : common .PtrTo (gatewayv1 .Group ("gateway.networking.k8s.io" )),
138
+ Name : "demo-gateway-200" ,
139
+ },
140
+ },
141
+ },
142
+ },
143
+ },
144
+ & gatewayv1.HTTPRoute {
145
+ ObjectMeta : metav1.ObjectMeta {
146
+ Name : "bar-route-21" ,
147
+ Namespace : "ns1" ,
148
+ CreationTimestamp : metav1.Time {
149
+ Time : fakeClock .Now ().Add (- 9 * time .Hour ),
150
+ },
151
+ },
152
+ Spec : gatewayv1.HTTPRouteSpec {
153
+ Hostnames : []gatewayv1.Hostname {"foo.com" , "bar.com" , "example.com" , "example2.com" , "example3.com" , "example4.com" , "example5.com" },
154
+ CommonRouteSpec : gatewayv1.CommonRouteSpec {
155
+ ParentRefs : []gatewayv1.ParentReference {
156
+ {
157
+ Kind : common .PtrTo (gatewayv1 .Kind ("Gateway" )),
158
+ Group : common .PtrTo (gatewayv1 .Group ("gateway.networking.k8s.io" )),
159
+ Namespace : common .PtrTo (gatewayv1 .Namespace ("default" )),
160
+ Name : "demo-gateway-200" ,
161
+ },
162
+ },
163
+ },
164
+ },
165
+ },
166
+ & gatewayv1.HTTPRoute {
167
+ ObjectMeta : metav1.ObjectMeta {
168
+ Name : "bax-httproute-18777" ,
169
+ Namespace : "ns2" ,
170
+ CreationTimestamp : metav1.Time {
171
+ Time : fakeClock .Now ().Add (- 5 * time .Minute ),
172
+ },
173
+ },
174
+ Spec : gatewayv1.HTTPRouteSpec {
175
+ CommonRouteSpec : gatewayv1.CommonRouteSpec {
176
+ ParentRefs : []gatewayv1.ParentReference {
177
+ {
178
+ Kind : common .PtrTo (gatewayv1 .Kind ("Gateway" )),
179
+ Group : common .PtrTo (gatewayv1 .Group ("gateway.networking.k8s.io" )),
180
+ Namespace : common .PtrTo (gatewayv1 .Namespace ("ns1" )),
181
+ Name : "demo-gateway-345" ,
182
+ },
183
+ },
184
+ },
185
+ },
186
+ },
187
+ }
188
+
189
+ params := utils .MustParamsForTest (t , common .MustClientsForTest (t , objects ... ))
190
+ discoverer := resourcediscovery.Discoverer {
191
+ K8sClients : params .K8sClients ,
192
+ PolicyManager : params .PolicyManager ,
193
+ }
194
+ resourceModel , err := discoverer .DiscoverResourcesForHTTPRoute (resourcediscovery.Filter {})
195
+ if err != nil {
196
+ t .Fatalf ("Failed to construct resourceModel: %v" , resourceModel )
197
+ }
198
+
199
+ hp := & HTTPRoutesPrinter {
200
+ Out : params .Out ,
201
+ Clock : fakeClock ,
202
+ }
203
+
204
+ hp .Print (resourceModel )
205
+
206
+ got := params .Out .(* bytes.Buffer ).String ()
207
+ want := `
208
+ NAMESPACE NAME HOSTNAMES PARENT REFS AGE
209
+ default foo-httproute-1 example.com,example2.com + 1 more 1 24h
210
+ default qmn-httproute-100 example.com 2 11h
211
+ ns1 bar-route-21 foo.com,bar.com + 5 more 1 9h
212
+ ns2 bax-httproute-18777 None 1 5m
213
+ `
214
+ if diff := cmp .Diff (common .YamlString (want ), common .YamlString (got ), common .YamlStringTransformer ); diff != "" {
215
+ t .Errorf ("Unexpected diff\n got=\n %v\n want=\n %v\n diff (-want +got)=\n %v" , got , want , diff )
216
+ }
217
+ }
218
+
36
219
func TestHTTPRoutesPrinter_PrintDescribeView (t * testing.T ) {
220
+ fakeClock := testingclock .NewFakeClock (time .Now ())
37
221
objects := []runtime.Object {
38
222
& gatewayv1.GatewayClass {
39
223
ObjectMeta : metav1.ObjectMeta {
@@ -202,7 +386,8 @@ func TestHTTPRoutesPrinter_PrintDescribeView(t *testing.T) {
202
386
}
203
387
204
388
hp := & HTTPRoutesPrinter {
205
- Out : params .Out ,
389
+ Out : params .Out ,
390
+ Clock : fakeClock ,
206
391
}
207
392
hp .PrintDescribeView (resourceModel )
208
393
0 commit comments