istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/pkg/tpath/util_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 tpath 16 17 import ( 18 "errors" 19 "testing" 20 ) 21 22 func TestAddSpecRoot(t *testing.T) { 23 tests := []struct { 24 desc string 25 in string 26 expect string 27 err error 28 }{ 29 { 30 desc: "empty", 31 in: ``, 32 expect: `spec: {} 33 `, 34 err: nil, 35 }, 36 { 37 desc: "add-root", 38 in: ` 39 a: va 40 b: foo`, 41 expect: `spec: 42 a: va 43 b: foo 44 `, 45 err: nil, 46 }, 47 { 48 desc: "err", 49 in: `i can't be yaml, can I?`, 50 expect: ``, 51 err: errors.New(""), 52 }, 53 } 54 55 for _, tt := range tests { 56 t.Run(tt.desc, func(t *testing.T) { 57 if got, err := AddSpecRoot(tt.in); got != tt.expect || 58 ((err != nil && tt.err == nil) || (err == nil && tt.err != nil)) { 59 t.Errorf("%s AddSpecRoot(%s) => %s, want %s", tt.desc, tt.in, got, tt.expect) 60 } 61 }) 62 } 63 } 64 65 func TestGetConfigSubtree(t *testing.T) { 66 tests := []struct { 67 desc string 68 manifest string 69 path string 70 expect string 71 err bool 72 }{ 73 { 74 desc: "empty", 75 manifest: ``, 76 path: ``, 77 expect: `{} 78 `, 79 err: false, 80 }, 81 { 82 desc: "subtree", 83 manifest: ` 84 a: 85 b: 86 - name: n1 87 value: v2 88 - list: 89 - v1 90 - v2 91 - v3_regex 92 name: n2 93 `, 94 path: `a`, 95 expect: `b: 96 - name: n1 97 value: v2 98 - list: 99 - v1 100 - v2 101 - v3_regex 102 name: n2 103 `, 104 err: false, 105 }, 106 { 107 desc: "err", 108 manifest: "not-yaml", 109 path: "not-subnode", 110 expect: ``, 111 err: true, 112 }, 113 } 114 115 for _, tt := range tests { 116 t.Run(tt.desc, func(t *testing.T) { 117 if got, err := GetConfigSubtree(tt.manifest, tt.path); got != tt.expect || (err == nil) == tt.err { 118 t.Errorf("%s GetConfigSubtree(%s, %s) => %s, want %s", tt.desc, tt.manifest, tt.path, got, tt.expect) 119 } 120 }) 121 } 122 }