Skip to content

Commit 7ef40b2

Browse files
committedSep 25, 2023
Add tests for gatewayclasses, gateways and httproutes
1 parent fa6eba2 commit 7ef40b2

File tree

13 files changed

+616
-24
lines changed

13 files changed

+616
-24
lines changed
 

‎cmd/gwctl/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ func main() {
5555
params := &types.Params{
5656
Client: client,
5757
DC: dc,
58-
PolicyManager: policyManager,
5958
DiscoveryClient: discovery.NewDiscoveryClientForConfigOrDie(restConfig),
59+
PolicyManager: policyManager,
60+
Out: os.Stdout,
6061
}
6162

6263
rootCmd := &cobra.Command{

‎pkg/common/clients.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package common
2+
3+
import (
4+
"testing"
5+
6+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
7+
"k8s.io/apimachinery/pkg/runtime"
8+
"k8s.io/client-go/discovery"
9+
"k8s.io/client-go/dynamic"
10+
fakedynamicclient "k8s.io/client-go/dynamic/fake"
11+
fakeclientset "k8s.io/client-go/kubernetes/fake"
12+
"k8s.io/client-go/kubernetes/scheme"
13+
"sigs.k8s.io/controller-runtime/pkg/client"
14+
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
15+
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
16+
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
17+
)
18+
19+
type FakeClients struct {
20+
Client client.Client
21+
DC dynamic.Interface
22+
DiscoveryClient discovery.DiscoveryInterface
23+
}
24+
25+
func MustClientsForTest(t *testing.T, initRuntimeObjects ...runtime.Object) *FakeClients {
26+
scheme := scheme.Scheme
27+
gatewayv1alpha2.AddToScheme(scheme)
28+
gatewayv1beta1.AddToScheme(scheme)
29+
apiextensionsv1.AddToScheme(scheme)
30+
31+
fakeClient := fakeclient.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(initRuntimeObjects...).Build()
32+
fakeDC := fakedynamicclient.NewSimpleDynamicClient(scheme, initRuntimeObjects...)
33+
fakeDiscoveryClient := fakeclientset.NewSimpleClientset().Discovery()
34+
35+
return &FakeClients{
36+
Client: fakeClient,
37+
DC: fakeDC,
38+
DiscoveryClient: fakeDiscoveryClient,
39+
}
40+
}
41+
42+
func PtrTo[T any](a T) *T {
43+
return &a
44+
}

‎pkg/common/consts.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package common
2+
3+
const (
4+
GatewayPolicyLabelKey = "gateway.networking.k8s.io/policy"
5+
)

‎pkg/common/helpers.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package common
2+
3+
import (
4+
"strings"
5+
6+
"github.com/google/go-cmp/cmp"
7+
)
8+
9+
type YamlString string
10+
11+
var YamlStringTransformer = cmp.Transformer("YamlLines", func(s YamlString) []string {
12+
// Splitting string on new line allows diff to be done for each individual
13+
// line.
14+
lines := strings.Split(string(s), "\n")
15+
16+
// Remove and empty lines from the start and end.
17+
var start, end int
18+
for i := range lines {
19+
if lines[i] != "" {
20+
start = i
21+
break
22+
}
23+
}
24+
for i := len(lines) - 1; i >= 0; i-- {
25+
if lines[i] != "" {
26+
end = i
27+
break
28+
}
29+
}
30+
return lines[start : end+1]
31+
})

‎pkg/policymanager/manager.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,10 @@ func PolicyFromUnstrucutred(u unstructured.Unstructured, policyCRDs map[PolicyCr
207207
Name: string(structuredPolicy.Spec.TargetRef.Name),
208208
Namespace: structuredPolicy.GetNamespace(),
209209
}
210-
if structuredPolicy.Spec.TargetRef.Namespace != nil && *structuredPolicy.Spec.TargetRef.Namespace != "" {
210+
if result.targetRef.Namespace == "default" {
211+
result.targetRef.Namespace = ""
212+
}
213+
if structuredPolicy.Spec.TargetRef.Namespace != nil {
211214
result.targetRef.Namespace = string(*structuredPolicy.Spec.TargetRef.Namespace)
212215
}
213216

@@ -239,6 +242,18 @@ func (p Policy) IsDirect() bool {
239242
}
240243

241244
func (p Policy) IsAttachedTo(objRef ObjRef) bool {
245+
if p.targetRef.Kind == "Namespace" && p.targetRef.Name == "" {
246+
p.targetRef.Name = "default"
247+
}
248+
if objRef.Kind == "Namespace" && objRef.Name == "" {
249+
objRef.Name = "default"
250+
}
251+
if p.targetRef.Kind != "Namespace" && p.targetRef.Namespace == "" {
252+
p.targetRef.Namespace = "default"
253+
}
254+
if objRef.Kind != "Namespace" && objRef.Namespace == "" {
255+
objRef.Namespace = "default"
256+
}
242257
return p.targetRef == objRef
243258
}
244259

‎pkg/resources/gatewayclasses/gatewayclasses.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ func PrintDescribeView(ctx context.Context, params *types.Params, gwClasses []ga
8989
if err != nil {
9090
panic(err)
9191
}
92-
fmt.Print(string(b))
92+
fmt.Fprint(params.Out, string(b))
9393
}
9494

9595
if i+1 != len(gwClasses) {
96-
fmt.Printf("\n\n")
96+
fmt.Fprintf(params.Out, "\n\n")
9797
}
9898
}
9999
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package gatewayclasses
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"testing"
7+
8+
"github.com/gauravkghildiyal/gwctl/pkg/common"
9+
"github.com/gauravkghildiyal/gwctl/pkg/types"
10+
"github.com/google/go-cmp/cmp"
11+
12+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
13+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
15+
"k8s.io/apimachinery/pkg/runtime"
16+
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
17+
)
18+
19+
func TestPrintDescribeView(t *testing.T) {
20+
objects := []runtime.Object{
21+
&gatewayv1beta1.GatewayClass{
22+
ObjectMeta: metav1.ObjectMeta{
23+
Name: "foo-gatewayclass",
24+
},
25+
Spec: gatewayv1beta1.GatewayClassSpec{
26+
ControllerName: "example.net/gateway-controller",
27+
Description: common.PtrTo("random"),
28+
},
29+
},
30+
&apiextensionsv1.CustomResourceDefinition{
31+
ObjectMeta: metav1.ObjectMeta{
32+
Name: "healthcheckpolicies.foo.com",
33+
Labels: map[string]string{
34+
common.GatewayPolicyLabelKey: "true",
35+
},
36+
},
37+
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
38+
Group: "foo.com",
39+
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{{Name: "v1"}},
40+
Names: apiextensionsv1.CustomResourceDefinitionNames{
41+
Plural: "healthcheckpolicies",
42+
Kind: "HealthCheckPolicy",
43+
},
44+
},
45+
},
46+
&unstructured.Unstructured{
47+
Object: map[string]interface{}{
48+
"apiVersion": "foo.com/v1",
49+
"kind": "HealthCheckPolicy",
50+
"metadata": map[string]interface{}{
51+
"name": "policy-name",
52+
},
53+
"spec": map[string]interface{}{
54+
"targetRef": map[string]interface{}{
55+
"group": "gateway.networking.k8s.io",
56+
"kind": "GatewayClass",
57+
"name": "foo-gatewayclass",
58+
},
59+
},
60+
},
61+
},
62+
}
63+
64+
params := types.MustParamsForTest(t, common.MustClientsForTest(t, objects...))
65+
gws, err := List(context.Background(), params)
66+
if err != nil {
67+
t.Fatalf("Failed to List GatewayClasses: %v", err)
68+
}
69+
PrintDescribeView(context.Background(), params, gws)
70+
71+
got := params.Out.(*bytes.Buffer).String()
72+
want := `
73+
Name: foo-gatewayclass
74+
ControllerName: example.net/gateway-controller
75+
Description: random
76+
DirectlyAttachedPolicies:
77+
- Group: foo.com
78+
Kind: HealthCheckPolicy
79+
Name: policy-name
80+
`
81+
if diff := cmp.Diff(common.YamlString(want), common.YamlString(got), common.YamlStringTransformer); diff != "" {
82+
t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff)
83+
}
84+
}

‎pkg/resources/gateways/gateways.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ func PrintDescribeView(ctx context.Context, params *types.Params, gws []gatewayv
172172
if err != nil {
173173
panic(err)
174174
}
175-
fmt.Print(string(b))
175+
fmt.Fprint(params.Out, string(b))
176176
}
177177

178178
if i+1 != len(gws) {
179-
fmt.Printf("\n\n")
179+
fmt.Fprintf(params.Out, "\n\n")
180180
}
181181
}
182182
}
+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package gateways
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"testing"
7+
8+
"github.com/gauravkghildiyal/gwctl/pkg/common"
9+
"github.com/gauravkghildiyal/gwctl/pkg/types"
10+
"github.com/google/go-cmp/cmp"
11+
12+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
13+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
15+
"k8s.io/apimachinery/pkg/runtime"
16+
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
17+
)
18+
19+
func TestPrintDescribeView(t *testing.T) {
20+
objects := []runtime.Object{
21+
&gatewayv1beta1.GatewayClass{
22+
ObjectMeta: metav1.ObjectMeta{
23+
Name: "foo-gatewayclass",
24+
},
25+
Spec: gatewayv1beta1.GatewayClassSpec{
26+
ControllerName: "example.net/gateway-controller",
27+
Description: common.PtrTo("random"),
28+
},
29+
},
30+
31+
&gatewayv1beta1.Gateway{
32+
ObjectMeta: metav1.ObjectMeta{
33+
Name: "foo-gateway",
34+
},
35+
Spec: gatewayv1beta1.GatewaySpec{
36+
GatewayClassName: "foo-gatewayclass",
37+
},
38+
},
39+
40+
&apiextensionsv1.CustomResourceDefinition{
41+
ObjectMeta: metav1.ObjectMeta{
42+
Name: "healthcheckpolicies.foo.com",
43+
Labels: map[string]string{
44+
common.GatewayPolicyLabelKey: "inherited",
45+
},
46+
},
47+
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
48+
Scope: apiextensionsv1.ClusterScoped,
49+
Group: "foo.com",
50+
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{{Name: "v1"}},
51+
Names: apiextensionsv1.CustomResourceDefinitionNames{
52+
Plural: "healthcheckpolicies",
53+
Kind: "HealthCheckPolicy",
54+
},
55+
},
56+
},
57+
&unstructured.Unstructured{
58+
Object: map[string]interface{}{
59+
"apiVersion": "foo.com/v1",
60+
"kind": "HealthCheckPolicy",
61+
"metadata": map[string]interface{}{
62+
"name": "health-check-gatewayclass",
63+
},
64+
"spec": map[string]interface{}{
65+
"override": map[string]interface{}{
66+
"key1": "value-parent-1",
67+
"key3": "value-parent-3",
68+
"key5": "value-parent-5",
69+
},
70+
"default": map[string]interface{}{
71+
"key2": "value-parent-2",
72+
"key4": "value-parent-4",
73+
},
74+
"targetRef": map[string]interface{}{
75+
"group": "gateway.networking.k8s.io",
76+
"kind": "GatewayClass",
77+
"name": "foo-gatewayclass",
78+
},
79+
},
80+
},
81+
},
82+
&unstructured.Unstructured{
83+
Object: map[string]interface{}{
84+
"apiVersion": "foo.com/v1",
85+
"kind": "HealthCheckPolicy",
86+
"metadata": map[string]interface{}{
87+
"name": "health-check-gateway",
88+
},
89+
"spec": map[string]interface{}{
90+
"override": map[string]interface{}{
91+
"key1": "value-child-1",
92+
},
93+
"default": map[string]interface{}{
94+
"key2": "value-child-2",
95+
"key5": "value-child-5",
96+
},
97+
"targetRef": map[string]interface{}{
98+
"group": "gateway.networking.k8s.io",
99+
"kind": "Gateway",
100+
"name": "foo-gateway",
101+
"namespace": "default",
102+
},
103+
},
104+
},
105+
},
106+
107+
&apiextensionsv1.CustomResourceDefinition{
108+
ObjectMeta: metav1.ObjectMeta{
109+
Name: "timeoutpolicies.bar.com",
110+
Labels: map[string]string{
111+
common.GatewayPolicyLabelKey: "direct",
112+
},
113+
},
114+
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
115+
Scope: apiextensionsv1.ClusterScoped,
116+
Group: "bar.com",
117+
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{{Name: "v1"}},
118+
Names: apiextensionsv1.CustomResourceDefinitionNames{
119+
Plural: "timeoutpolicies",
120+
Kind: "TimeoutPolicy",
121+
},
122+
},
123+
},
124+
&unstructured.Unstructured{
125+
Object: map[string]interface{}{
126+
"apiVersion": "bar.com/v1",
127+
"kind": "TimeoutPolicy",
128+
"metadata": map[string]interface{}{
129+
"name": "timeout-policy-namespace",
130+
},
131+
"spec": map[string]interface{}{
132+
"condition": "path=/abc",
133+
"seconds": int64(30),
134+
"targetRef": map[string]interface{}{
135+
"kind": "Namespace",
136+
"name": "default",
137+
},
138+
},
139+
},
140+
},
141+
}
142+
143+
params := types.MustParamsForTest(t, common.MustClientsForTest(t, objects...))
144+
gws, err := List(context.Background(), params, "")
145+
if err != nil {
146+
t.Fatalf("Failed to List Gateways: %v", err)
147+
}
148+
PrintDescribeView(context.Background(), params, gws)
149+
150+
got := params.Out.(*bytes.Buffer).String()
151+
want := `
152+
Name: foo-gateway
153+
GatewayClass: foo-gatewayclass
154+
AllPolicies:
155+
- Group: foo.com
156+
Kind: HealthCheckPolicy
157+
Name: health-check-gateway
158+
- Group: bar.com
159+
Kind: TimeoutPolicy
160+
Name: timeout-policy-namespace
161+
- Group: foo.com
162+
Kind: HealthCheckPolicy
163+
Name: health-check-gatewayclass
164+
EffectivePolicies:
165+
HealthCheckPolicy.foo.com:
166+
key1: value-parent-1
167+
key2: value-child-2
168+
key3: value-parent-3
169+
key4: value-parent-4
170+
key5: value-parent-5
171+
TimeoutPolicy.bar.com:
172+
condition: path=/abc
173+
seconds: 30
174+
`
175+
if diff := cmp.Diff(common.YamlString(want), common.YamlString(got), common.YamlStringTransformer); diff != "" {
176+
t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff)
177+
}
178+
}

‎pkg/resources/httproutes/httproutes.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ func GetEffectivePolicies(ctx context.Context, params *types.Params, namespace,
9090
if gatewayRef.Namespace != nil {
9191
ns = string(*gatewayRef.Namespace)
9292
}
93+
if ns == "" {
94+
ns = "default"
95+
}
9396

9497
gatewayPoliciesByKind, err := gateways.GetEffectivePolicies(ctx, params, string(ns), string(gatewayRef.Name))
9598
if err != nil {
@@ -181,11 +184,11 @@ func PrintDescribeView(ctx context.Context, params *types.Params, httpRoutes []g
181184
if err != nil {
182185
panic(err)
183186
}
184-
fmt.Print(string(b))
187+
fmt.Fprint(params.Out, string(b))
185188
}
186189

187190
if i+1 != len(httpRoutes) {
188-
fmt.Printf("\n\n")
191+
fmt.Fprintf(params.Out, "\n\n")
189192
}
190193
}
191194
}
+210
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
package httproutes
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"testing"
7+
8+
"github.com/gauravkghildiyal/gwctl/pkg/common"
9+
"github.com/gauravkghildiyal/gwctl/pkg/types"
10+
"github.com/google/go-cmp/cmp"
11+
12+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
13+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
15+
"k8s.io/apimachinery/pkg/runtime"
16+
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
17+
)
18+
19+
func TestPrintDescribeView(t *testing.T) {
20+
objects := []runtime.Object{
21+
&gatewayv1beta1.GatewayClass{
22+
ObjectMeta: metav1.ObjectMeta{
23+
Name: "foo-gatewayclass",
24+
},
25+
Spec: gatewayv1beta1.GatewayClassSpec{
26+
ControllerName: "example.net/gateway-controller",
27+
Description: common.PtrTo("random"),
28+
},
29+
},
30+
&unstructured.Unstructured{
31+
Object: map[string]interface{}{
32+
"apiVersion": "foo.com/v1",
33+
"kind": "HealthCheckPolicy",
34+
"metadata": map[string]interface{}{
35+
"name": "health-check-gatewayclass",
36+
},
37+
"spec": map[string]interface{}{
38+
"override": map[string]interface{}{
39+
"key1": "value-parent-1",
40+
"key3": "value-parent-3",
41+
"key5": "value-parent-5",
42+
},
43+
"default": map[string]interface{}{
44+
"key2": "value-parent-2",
45+
"key4": "value-parent-4",
46+
},
47+
"targetRef": map[string]interface{}{
48+
"group": "gateway.networking.k8s.io",
49+
"kind": "GatewayClass",
50+
"name": "foo-gatewayclass",
51+
},
52+
},
53+
},
54+
},
55+
56+
&gatewayv1beta1.Gateway{
57+
ObjectMeta: metav1.ObjectMeta{
58+
Name: "foo-gateway",
59+
Namespace: "default",
60+
},
61+
Spec: gatewayv1beta1.GatewaySpec{
62+
GatewayClassName: "foo-gatewayclass",
63+
},
64+
},
65+
&unstructured.Unstructured{
66+
Object: map[string]interface{}{
67+
"apiVersion": "foo.com/v1",
68+
"kind": "HealthCheckPolicy",
69+
"metadata": map[string]interface{}{
70+
"name": "health-check-gateway",
71+
},
72+
"spec": map[string]interface{}{
73+
"override": map[string]interface{}{
74+
"key1": "value-child-1",
75+
},
76+
"default": map[string]interface{}{
77+
"key2": "value-child-2",
78+
"key5": "value-child-5",
79+
},
80+
"targetRef": map[string]interface{}{
81+
"group": "gateway.networking.k8s.io",
82+
"kind": "Gateway",
83+
"name": "foo-gateway",
84+
"namespace": "default",
85+
},
86+
},
87+
},
88+
},
89+
90+
&gatewayv1beta1.HTTPRoute{
91+
ObjectMeta: metav1.ObjectMeta{
92+
Name: "foo-httproute",
93+
},
94+
Spec: gatewayv1beta1.HTTPRouteSpec{
95+
CommonRouteSpec: gatewayv1beta1.CommonRouteSpec{
96+
ParentRefs: []gatewayv1beta1.ParentReference{{
97+
Kind: common.PtrTo(gatewayv1beta1.Kind("Gateway")),
98+
Group: common.PtrTo(gatewayv1beta1.Group("gateway.networking.k8s.io")),
99+
Name: "foo-gateway",
100+
}},
101+
},
102+
},
103+
},
104+
&unstructured.Unstructured{
105+
Object: map[string]interface{}{
106+
"apiVersion": "bar.com/v1",
107+
"kind": "TimeoutPolicy",
108+
"metadata": map[string]interface{}{
109+
"name": "timeout-policy-httproute",
110+
},
111+
"spec": map[string]interface{}{
112+
"condition": "path=/def",
113+
"seconds": int64(60),
114+
"targetRef": map[string]interface{}{
115+
"group": "gateway.networking.k8s.io",
116+
"kind": "HTTPRoute",
117+
"name": "foo-httproute",
118+
},
119+
},
120+
},
121+
},
122+
123+
&apiextensionsv1.CustomResourceDefinition{
124+
ObjectMeta: metav1.ObjectMeta{
125+
Name: "healthcheckpolicies.foo.com",
126+
Labels: map[string]string{
127+
common.GatewayPolicyLabelKey: "inherited",
128+
},
129+
},
130+
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
131+
Scope: apiextensionsv1.ClusterScoped,
132+
Group: "foo.com",
133+
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{{Name: "v1"}},
134+
Names: apiextensionsv1.CustomResourceDefinitionNames{
135+
Plural: "healthcheckpolicies",
136+
Kind: "HealthCheckPolicy",
137+
},
138+
},
139+
},
140+
141+
&apiextensionsv1.CustomResourceDefinition{
142+
ObjectMeta: metav1.ObjectMeta{
143+
Name: "timeoutpolicies.bar.com",
144+
Labels: map[string]string{
145+
common.GatewayPolicyLabelKey: "direct",
146+
},
147+
},
148+
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
149+
Scope: apiextensionsv1.ClusterScoped,
150+
Group: "bar.com",
151+
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{{Name: "v1"}},
152+
Names: apiextensionsv1.CustomResourceDefinitionNames{
153+
Plural: "timeoutpolicies",
154+
Kind: "TimeoutPolicy",
155+
},
156+
},
157+
},
158+
&unstructured.Unstructured{
159+
Object: map[string]interface{}{
160+
"apiVersion": "bar.com/v1",
161+
"kind": "TimeoutPolicy",
162+
"metadata": map[string]interface{}{
163+
"name": "timeout-policy-namespace",
164+
},
165+
"spec": map[string]interface{}{
166+
"condition": "path=/abc",
167+
"seconds": int64(30),
168+
"targetRef": map[string]interface{}{
169+
"kind": "Namespace",
170+
"name": "default",
171+
},
172+
},
173+
},
174+
},
175+
}
176+
177+
params := types.MustParamsForTest(t, common.MustClientsForTest(t, objects...))
178+
httpRoutes, err := List(context.Background(), params, "")
179+
if err != nil {
180+
t.Fatalf("Failed to List HTTPRoutes: %v", err)
181+
}
182+
PrintDescribeView(context.Background(), params, httpRoutes)
183+
184+
got := params.Out.(*bytes.Buffer).String()
185+
want := `
186+
Name: foo-httproute
187+
ParentRefs:
188+
- group: gateway.networking.k8s.io
189+
kind: Gateway
190+
name: foo-gateway
191+
DirectlyAttachedPolicies:
192+
- Group: bar.com
193+
Kind: TimeoutPolicy
194+
Name: timeout-policy-httproute
195+
EffectivePolicies:
196+
default/foo-gateway:
197+
HealthCheckPolicy.foo.com:
198+
key1: value-parent-1
199+
key2: value-child-2
200+
key3: value-parent-3
201+
key4: value-parent-4
202+
key5: value-parent-5
203+
TimeoutPolicy.bar.com:
204+
condition: path=/def
205+
seconds: 60
206+
`
207+
if diff := cmp.Diff(common.YamlString(want), common.YamlString(got), common.YamlStringTransformer); diff != "" {
208+
t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff)
209+
}
210+
}

‎pkg/types/params.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package types
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"io"
7+
"testing"
8+
9+
"k8s.io/client-go/discovery"
10+
"k8s.io/client-go/dynamic"
11+
"sigs.k8s.io/controller-runtime/pkg/client"
12+
13+
"github.com/gauravkghildiyal/gwctl/pkg/common"
14+
"github.com/gauravkghildiyal/gwctl/pkg/policymanager"
15+
)
16+
17+
type Params struct {
18+
Client client.Client
19+
DC dynamic.Interface
20+
DiscoveryClient discovery.DiscoveryInterface
21+
PolicyManager *policymanager.PolicyManager
22+
Out io.Writer
23+
}
24+
25+
func MustParamsForTest(t *testing.T, fakeClients *common.FakeClients) *Params {
26+
policyManager := policymanager.New(fakeClients.DC)
27+
if err := policyManager.Init(context.Background()); err != nil {
28+
t.Fatalf("failed to initialize PolicyManager: %v", err)
29+
}
30+
return &Params{
31+
Client: fakeClients.Client,
32+
DC: fakeClients.DC,
33+
DiscoveryClient: fakeClients.DiscoveryClient,
34+
PolicyManager: policyManager,
35+
Out: &bytes.Buffer{},
36+
}
37+
}

‎pkg/types/types.go

-16
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.