|
| 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 | +} |
0 commit comments