istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/util/yml/apply_test.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package yml 16 17 import ( 18 "testing" 19 ) 20 21 func TestApplyNamespace(t *testing.T) { 22 cases := []struct { 23 name string 24 yml string 25 namespace string 26 expected string 27 }{ 28 { 29 "no namespace", 30 `metadata: 31 name: foo`, 32 "default", 33 `metadata: 34 name: foo 35 namespace: default`, 36 }, 37 { 38 "empty namespace", 39 `metadata: 40 name: foo 41 namespace: ""`, 42 "default", 43 `metadata: 44 name: foo 45 namespace: default`, 46 }, 47 { 48 "existing namespace", 49 `metadata: 50 name: foo 51 namespace: bar`, 52 "default", 53 `metadata: 54 name: foo 55 namespace: bar`, 56 }, 57 { 58 "multipart", 59 `apiVersion: v1 60 metadata: 61 name: foo 62 --- 63 apiVersion: v1 64 metadata: 65 name: bar 66 `, 67 "default", 68 `apiVersion: v1 69 metadata: 70 name: foo 71 namespace: default 72 --- 73 apiVersion: v1 74 metadata: 75 name: bar 76 namespace: default`, 77 }, 78 } 79 for _, tt := range cases { 80 t.Run(tt.name, func(t *testing.T) { 81 got, err := ApplyNamespace(tt.yml, tt.namespace) 82 if err != nil { 83 t.Fatal(err) 84 } 85 if got != tt.expected { 86 t.Fatalf("expected '%s', got '%s'", tt.expected, got) 87 } 88 }) 89 } 90 }