github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/kubernetes/manifest/manifests_test.go (about) 1 /* 2 Copyright 2019 The Skaffold 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 manifest 18 19 import ( 20 "bytes" 21 "testing" 22 23 "github.com/GoogleContainerTools/skaffold/testutil" 24 ) 25 26 func TestLoad(t *testing.T) { 27 tests := []struct { 28 name string 29 input string 30 expected []string 31 }{ 32 {name: "empty", input: "", expected: []string{}}, 33 {name: "single doc", input: "a: b", expected: []string{"a: b\n"}}, // note lf introduced 34 {name: "multiple docs", input: "a: b\n---\nc: d", expected: []string{"a: b\n", "c: d\n"}}, 35 } 36 for _, test := range tests { 37 testutil.Run(t, test.name, func(t *testutil.T) { 38 result, err := Load(bytes.NewReader([]byte(test.input))) 39 40 t.CheckError(false, err) 41 t.CheckDeepEqual(len(test.expected), len(result)) 42 for i := range test.expected { 43 t.CheckDeepEqual(test.expected[i], string(result[i])) 44 } 45 }) 46 } 47 } 48 49 const pod1 = `apiVersion: v1 50 kind: Pod 51 metadata: 52 name: leeroy-web 53 spec: 54 containers: 55 - name: leeroy-web 56 image: leeroy-web` 57 58 const pod2 = `apiVersion: v1 59 kind: Pod 60 metadata: 61 name: leeroy-app 62 spec: 63 containers: 64 - name: leeroy-app 65 image: leeroy-app` 66 67 const clusterRole = `aggregationRule: {} 68 apiVersion: v1 69 kind: ClusterRole` 70 71 const podUnordered = `kind: Pod 72 metadata: 73 name: leeroy-web 74 apiVersion: v1 75 spec: 76 containers: 77 - name: leeroy-web 78 image: leeroy-web` 79 80 const roleBinding = `apiVersion: rbac.authorization.k8s.io/v1 81 kind: RoleBinding 82 subjects: 83 - kind: ServiceAccount 84 name: default 85 namespace: default` 86 87 const service = `apiVersion: v1 88 kind: Service 89 metadata: 90 name: my-app 91 spec: 92 selector: 93 app: my-app` 94 95 func TestEmpty(t *testing.T) { 96 var manifests ManifestList 97 98 testutil.CheckDeepEqual(t, 0, len(manifests)) 99 100 manifests.Append(nil) 101 102 testutil.CheckDeepEqual(t, 1, len(manifests)) 103 } 104 105 func TestAppendSingle(t *testing.T) { 106 var manifests ManifestList 107 108 manifests.Append([]byte(pod1)) 109 110 testutil.CheckDeepEqual(t, 1, len(manifests)) 111 testutil.CheckDeepEqual(t, pod1, string(manifests[0])) 112 } 113 114 func TestAppendUnordered(t *testing.T) { 115 var manifests ManifestList 116 117 manifests.Append([]byte(podUnordered)) 118 119 testutil.CheckDeepEqual(t, 1, len(manifests)) 120 testutil.CheckDeepEqual(t, podUnordered, string(manifests[0])) 121 } 122 123 func TestAppendWithSeparators(t *testing.T) { 124 var manifests ManifestList 125 126 manifests.Append([]byte(pod1 + "\n---\n" + pod2 + "\n---\n" + podUnordered)) 127 128 testutil.CheckDeepEqual(t, 3, len(manifests)) 129 testutil.CheckDeepEqual(t, pod1, string(manifests[0])) 130 testutil.CheckDeepEqual(t, pod2, string(manifests[1])) 131 testutil.CheckDeepEqual(t, podUnordered, string(manifests[2])) 132 } 133 134 func TestAppendWithoutSeparators(t *testing.T) { 135 var manifests ManifestList 136 137 manifests.Append([]byte(pod1 + "\n" + pod2 + "\n" + clusterRole)) 138 139 testutil.CheckDeepEqual(t, 3, len(manifests)) 140 testutil.CheckDeepEqual(t, pod1, string(manifests[0])) 141 testutil.CheckDeepEqual(t, pod2, string(manifests[1])) 142 testutil.CheckDeepEqual(t, clusterRole, string(manifests[2])) 143 } 144 145 func TestAppendDifferentApiVersion(t *testing.T) { 146 var manifests ManifestList 147 148 manifests.Append([]byte("apiVersion: v1\napiVersion: v2")) 149 150 testutil.CheckDeepEqual(t, 2, len(manifests)) 151 testutil.CheckDeepEqual(t, "apiVersion: v1", string(manifests[0])) 152 testutil.CheckDeepEqual(t, "apiVersion: v2", string(manifests[1])) 153 } 154 155 func TestAppendServiceAndRoleBinding(t *testing.T) { 156 var manifests ManifestList 157 158 manifests.Append([]byte(roleBinding + "\n" + service)) 159 160 testutil.CheckDeepEqual(t, 2, len(manifests)) 161 testutil.CheckDeepEqual(t, roleBinding, string(manifests[0])) 162 testutil.CheckDeepEqual(t, service, string(manifests[1])) 163 testutil.CheckDeepEqual(t, manifests.String(), roleBinding+"\n---\n"+service) 164 }