sigs.k8s.io/gateway-api@v1.0.0/conformance/utils/kubernetes/apply_test.go (about) 1 /* 2 Copyright 2022 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 kubernetes 18 19 import ( 20 "strings" 21 "testing" 22 23 "github.com/stretchr/testify/require" 24 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 25 "k8s.io/apimachinery/pkg/util/yaml" 26 27 _ "sigs.k8s.io/gateway-api/conformance/utils/flags" 28 ) 29 30 func TestPrepareResources(t *testing.T) { 31 tests := []struct { 32 name string 33 given string 34 expected []unstructured.Unstructured 35 applier Applier 36 }{{ 37 name: "empty namespace labels", 38 applier: Applier{}, 39 given: ` 40 apiVersion: v1 41 kind: Namespace 42 metadata: 43 name: test 44 `, 45 expected: []unstructured.Unstructured{{ 46 Object: map[string]interface{}{ 47 "apiVersion": "v1", 48 "kind": "Namespace", 49 "metadata": map[string]interface{}{ 50 "name": "test", 51 }, 52 }, 53 }}, 54 }, { 55 name: "simple namespace labels", 56 applier: Applier{ 57 NamespaceLabels: map[string]string{ 58 "test": "false", 59 }, 60 }, 61 given: ` 62 apiVersion: v1 63 kind: Namespace 64 metadata: 65 name: test 66 `, 67 expected: []unstructured.Unstructured{{ 68 Object: map[string]interface{}{ 69 "apiVersion": "v1", 70 "kind": "Namespace", 71 "metadata": map[string]interface{}{ 72 "name": "test", 73 "labels": map[string]interface{}{ 74 "test": "false", 75 }, 76 }, 77 }, 78 }}, 79 }, { 80 name: "overwrite namespace labels", 81 applier: Applier{ 82 NamespaceLabels: map[string]string{ 83 "test": "true", 84 }, 85 }, 86 given: ` 87 apiVersion: v1 88 kind: Namespace 89 metadata: 90 name: test 91 labels: 92 test: 'false' 93 `, 94 expected: []unstructured.Unstructured{{ 95 Object: map[string]interface{}{ 96 "apiVersion": "v1", 97 "kind": "Namespace", 98 "metadata": map[string]interface{}{ 99 "name": "test", 100 "labels": map[string]interface{}{ 101 "test": "true", 102 }, 103 }, 104 }, 105 }}, 106 }, { 107 name: "setting the gatewayClassName", 108 applier: Applier{}, 109 given: ` 110 apiVersion: gateway.networking.k8s.io/v1beta1 111 kind: Gateway 112 metadata: 113 name: test 114 spec: 115 gatewayClassName: {GATEWAY_CLASS_NAME} 116 listeners: 117 - name: http 118 port: 80 119 protocol: HTTP 120 allowedRoutes: 121 namespaces: 122 from: Same 123 `, 124 expected: []unstructured.Unstructured{{ 125 Object: map[string]interface{}{ 126 "apiVersion": "gateway.networking.k8s.io/v1beta1", 127 "kind": "Gateway", 128 "metadata": map[string]interface{}{ 129 "name": "test", 130 }, 131 "spec": map[string]interface{}{ 132 "gatewayClassName": "test-class", 133 "listeners": []interface{}{ 134 map[string]interface{}{ 135 "name": "http", 136 "port": int64(80), 137 "protocol": "HTTP", 138 "allowedRoutes": map[string]interface{}{ 139 "namespaces": map[string]interface{}{ 140 "from": "Same", 141 }, 142 }, 143 }, 144 }, 145 }, 146 }, 147 }}, 148 }, { 149 name: "setting the controllerName for a GatewayClass", 150 applier: Applier{}, 151 given: ` 152 apiVersion: gateway.networking.k8s.io/v1beta1 153 kind: GatewayClass 154 metadata: 155 name: test 156 spec: 157 controllerName: {GATEWAY_CONTROLLER_NAME} 158 `, 159 expected: []unstructured.Unstructured{{ 160 Object: map[string]interface{}{ 161 "apiVersion": "gateway.networking.k8s.io/v1beta1", 162 "kind": "GatewayClass", 163 "metadata": map[string]interface{}{ 164 "name": "test", 165 }, 166 "spec": map[string]interface{}{ 167 "controllerName": "test-controller", 168 }, 169 }, 170 }}, 171 }} 172 173 for _, tc := range tests { 174 t.Run(tc.name, func(t *testing.T) { 175 decoder := yaml.NewYAMLOrJSONDecoder(strings.NewReader(tc.given), 4096) 176 177 tc.applier.GatewayClass = "test-class" 178 tc.applier.ControllerName = "test-controller" 179 resources, err := tc.applier.prepareResources(t, decoder) 180 181 require.NoError(t, err, "unexpected error preparing resources") 182 require.EqualValues(t, tc.expected, resources) 183 }) 184 } 185 }