@@ -19,21 +19,115 @@ 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"
30
+ testingclock "k8s.io/utils/clock/testing"
31
+
28
32
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
29
33
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
30
-
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 TestGatewayClassesPrinter_Print (t * testing.T ) {
40
+ fakeClock := testingclock .NewFakeClock (time .Now ())
41
+ objects := []runtime.Object {
42
+ & gatewayv1.GatewayClass {
43
+ ObjectMeta : metav1.ObjectMeta {
44
+ Name : "bar-com-internal-gateway-class" ,
45
+ CreationTimestamp : metav1.Time {
46
+ Time : fakeClock .Now ().Add (- 365 * 24 * time .Hour ),
47
+ },
48
+ },
49
+ Spec : gatewayv1.GatewayClassSpec {
50
+ ControllerName : "bar.baz/internal-gateway-class" ,
51
+ },
52
+ Status : gatewayv1.GatewayClassStatus {
53
+ Conditions : []metav1.Condition {
54
+ {
55
+ Type : "Accepted" ,
56
+ Status : "True" ,
57
+ },
58
+ },
59
+ },
60
+ },
61
+ & gatewayv1.GatewayClass {
62
+ ObjectMeta : metav1.ObjectMeta {
63
+ Name : "foo-com-external-gateway-class" ,
64
+ CreationTimestamp : metav1.Time {
65
+ Time : fakeClock .Now ().Add (- 100 * 24 * time .Hour ),
66
+ },
67
+ },
68
+ Spec : gatewayv1.GatewayClassSpec {
69
+ ControllerName : "foo.com/external-gateway-class" ,
70
+ },
71
+ Status : gatewayv1.GatewayClassStatus {
72
+ Conditions : []metav1.Condition {
73
+ {
74
+ Type : "Accepted" ,
75
+ Status : "False" ,
76
+ },
77
+ },
78
+ },
79
+ },
80
+ & gatewayv1.GatewayClass {
81
+ ObjectMeta : metav1.ObjectMeta {
82
+ Name : "foo-com-internal-gateway-class" ,
83
+ CreationTimestamp : metav1.Time {
84
+ Time : fakeClock .Now ().Add (- 24 * time .Minute ),
85
+ },
86
+ },
87
+ Spec : gatewayv1.GatewayClassSpec {
88
+ ControllerName : "foo.com/internal-gateway-class" ,
89
+ },
90
+ Status : gatewayv1.GatewayClassStatus {
91
+ Conditions : []metav1.Condition {
92
+ {
93
+ Type : "Accepted" ,
94
+ Status : "Unknown" ,
95
+ },
96
+ },
97
+ },
98
+ },
99
+ }
100
+
101
+ params := utils .MustParamsForTest (t , common .MustClientsForTest (t , objects ... ))
102
+ discoverer := resourcediscovery.Discoverer {
103
+ K8sClients : params .K8sClients ,
104
+ PolicyManager : params .PolicyManager ,
105
+ }
106
+ resourceModel , err := discoverer .DiscoverResourcesForGatewayClass (resourcediscovery.Filter {})
107
+ if err != nil {
108
+ t .Fatalf ("Failed to construct resourceModel: %v" , resourceModel )
109
+ }
110
+
111
+ gcp := & GatewayClassesPrinter {
112
+ Out : params .Out ,
113
+ Clock : fakeClock ,
114
+ }
115
+ gcp .Print (resourceModel )
116
+
117
+ got := params .Out .(* bytes.Buffer ).String ()
118
+ want := `
119
+ NAME CONTROLLER ACCEPTED AGE
120
+ bar-com-internal-gateway-class bar.baz/internal-gateway-class True 365d
121
+ foo-com-external-gateway-class foo.com/external-gateway-class False 100d
122
+ foo-com-internal-gateway-class foo.com/internal-gateway-class Unknown 24m
123
+ `
124
+ if diff := cmp .Diff (common .YamlString (want ), common .YamlString (got ), common .YamlStringTransformer ); diff != "" {
125
+ t .Errorf ("Unexpected diff\n got=\n %v\n want=\n %v\n diff (-want +got)=\n %v" , got , want , diff )
126
+ }
127
+ }
128
+
36
129
func TestGatewayClassesPrinter_PrintDescribeView (t * testing.T ) {
130
+ fakeClock := testingclock .NewFakeClock (time .Now ())
37
131
objects := []runtime.Object {
38
132
& gatewayv1.GatewayClass {
39
133
ObjectMeta : metav1.ObjectMeta {
@@ -89,7 +183,8 @@ func TestGatewayClassesPrinter_PrintDescribeView(t *testing.T) {
89
183
}
90
184
91
185
gcp := & GatewayClassesPrinter {
92
- Out : params .Out ,
186
+ Out : params .Out ,
187
+ Clock : fakeClock ,
93
188
}
94
189
gcp .PrintDescribeView (resourceModel )
95
190
0 commit comments