|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package printer |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/google/go-cmp/cmp" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 28 | + "k8s.io/apimachinery/pkg/runtime" |
| 29 | + gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" |
| 30 | + "sigs.k8s.io/gateway-api/gwctl/pkg/utils" |
| 31 | + "sigs.k8s.io/gateway-api/gwctl/pkg/common" |
| 32 | + "sigs.k8s.io/gateway-api/gwctl/pkg/resourcediscovery" |
| 33 | +) |
| 34 | + |
| 35 | +func TestNamespacePrinter_PrintDescribeView(t *testing.T) { |
| 36 | + objects := []runtime.Object{ |
| 37 | + // Defining a Namespace called development |
| 38 | + &corev1.Namespace{ |
| 39 | + ObjectMeta: metav1.ObjectMeta{ |
| 40 | + Name: "development", |
| 41 | + Labels: map[string]string{ |
| 42 | + "type": "test-namespace", |
| 43 | + }, |
| 44 | + Annotations: map[string]string{ |
| 45 | + "test-annotation": "development-annotation", |
| 46 | + }, |
| 47 | + }, |
| 48 | + Status: corev1.NamespaceStatus{ |
| 49 | + Phase: corev1.NamespaceActive, |
| 50 | + }, |
| 51 | + }, |
| 52 | + |
| 53 | + // CRD and definition for HealthCheckPolicy attached to development namespace |
| 54 | + &apiextensionsv1.CustomResourceDefinition{ |
| 55 | + ObjectMeta: metav1.ObjectMeta{ |
| 56 | + Name: "healthcheckpolicies.foo.com", |
| 57 | + Labels: map[string]string{ |
| 58 | + gatewayv1alpha2.PolicyLabelKey: "inherited", |
| 59 | + }, |
| 60 | + }, |
| 61 | + Spec: apiextensionsv1.CustomResourceDefinitionSpec{ |
| 62 | + Scope: apiextensionsv1.ClusterScoped, |
| 63 | + Group: "foo.com", |
| 64 | + Versions: []apiextensionsv1.CustomResourceDefinitionVersion{{Name: "v1"}}, |
| 65 | + Names: apiextensionsv1.CustomResourceDefinitionNames{ |
| 66 | + Plural: "healthcheckpolicies", |
| 67 | + Kind: "HealthCheckPolicy", |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + &unstructured.Unstructured{ |
| 72 | + Object: map[string]interface{}{ |
| 73 | + "apiVersion": "foo.com/v1", |
| 74 | + "kind": "HealthCheckPolicy", |
| 75 | + "metadata": map[string]interface{}{ |
| 76 | + "name": "health-check-gatewayclass", |
| 77 | + }, |
| 78 | + "spec": map[string]interface{}{ |
| 79 | + "override": map[string]interface{}{ |
| 80 | + "key1": "value-parent-1", |
| 81 | + "key3": "value-parent-3", |
| 82 | + "key5": "value-parent-5", |
| 83 | + }, |
| 84 | + "default": map[string]interface{}{ |
| 85 | + "key2": "value-parent-2", |
| 86 | + "key4": "value-parent-4", |
| 87 | + }, |
| 88 | + "targetRef": map[string]interface{}{ |
| 89 | + "kind": "Namespace", |
| 90 | + "name": "development", |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + |
| 96 | + // Defining a Namespace called production |
| 97 | + &corev1.Namespace{ |
| 98 | + ObjectMeta: metav1.ObjectMeta{ |
| 99 | + Name: "production", |
| 100 | + Labels: map[string]string{ |
| 101 | + "type": "production-namespace", |
| 102 | + }, |
| 103 | + }, |
| 104 | + Status: corev1.NamespaceStatus{ |
| 105 | + Phase: corev1.NamespaceActive, |
| 106 | + }, |
| 107 | + }, |
| 108 | + // CRD and definition for TimeoutPolicy attached to default namespace |
| 109 | + &apiextensionsv1.CustomResourceDefinition{ |
| 110 | + ObjectMeta: metav1.ObjectMeta{ |
| 111 | + Name: "timeoutpolicies.bar.com", |
| 112 | + Labels: map[string]string{ |
| 113 | + gatewayv1alpha2.PolicyLabelKey: "direct", |
| 114 | + }, |
| 115 | + }, |
| 116 | + Spec: apiextensionsv1.CustomResourceDefinitionSpec{ |
| 117 | + Scope: apiextensionsv1.ClusterScoped, |
| 118 | + Group: "bar.com", |
| 119 | + Versions: []apiextensionsv1.CustomResourceDefinitionVersion{{Name: "v1"}}, |
| 120 | + Names: apiextensionsv1.CustomResourceDefinitionNames{ |
| 121 | + Plural: "timeoutpolicies", |
| 122 | + Kind: "TimeoutPolicy", |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + &unstructured.Unstructured{ |
| 127 | + Object: map[string]interface{}{ |
| 128 | + "apiVersion": "bar.com/v1", |
| 129 | + "kind": "TimeoutPolicy", |
| 130 | + "metadata": map[string]interface{}{ |
| 131 | + "name": "timeout-policy-namespace", |
| 132 | + }, |
| 133 | + "spec": map[string]interface{}{ |
| 134 | + "condition": "path=/abc", |
| 135 | + "seconds": int64(30), |
| 136 | + "targetRef": map[string]interface{}{ |
| 137 | + "kind": "Namespace", |
| 138 | + "name": "production", |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + } |
| 144 | + |
| 145 | + params := utils.MustParamsForTest(t, common.MustClientsForTest(t, objects...)) |
| 146 | + discoverer := resourcediscovery.Discoverer{ |
| 147 | + K8sClients: params.K8sClients, |
| 148 | + PolicyManager: params.PolicyManager, |
| 149 | + } |
| 150 | + resourceModel, err := discoverer.DiscoverResourcesForNamespace(resourcediscovery.Filter{}) |
| 151 | + if err != nil { |
| 152 | + t.Fatalf("Failed to construct resourceModel: %v", resourceModel) |
| 153 | + } |
| 154 | + |
| 155 | + nsp := &NamespacesPrinter{ |
| 156 | + Out: params.Out, |
| 157 | + } |
| 158 | + nsp.PrintDescribeView(resourceModel) |
| 159 | + |
| 160 | + got := params.Out.(*bytes.Buffer).String() |
| 161 | + want := ` |
| 162 | +Name: development |
| 163 | +Annotations: |
| 164 | + test-annotation: development-annotation |
| 165 | +Labels: |
| 166 | + type: test-namespace |
| 167 | +Status: Active |
| 168 | +DirectlyAttachedPolicies: |
| 169 | +- Group: foo.com |
| 170 | + Kind: HealthCheckPolicy |
| 171 | + Name: health-check-gatewayclass |
| 172 | +
|
| 173 | +
|
| 174 | +Name: production |
| 175 | +Labels: |
| 176 | + type: production-namespace |
| 177 | +Status: Active |
| 178 | +DirectlyAttachedPolicies: |
| 179 | +- Group: bar.com |
| 180 | + Kind: TimeoutPolicy |
| 181 | + Name: timeout-policy-namespace |
| 182 | +` |
| 183 | + if diff := cmp.Diff(common.YamlString(want), common.YamlString(got), common.YamlStringTransformer); diff != "" { |
| 184 | + t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff) |
| 185 | + } |
| 186 | + |
| 187 | +} |
0 commit comments