github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/kubernetes/manifest/labels_test.go (about) 1 /* 2 Copyright 2020 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 "testing" 21 22 "k8s.io/apimachinery/pkg/runtime/schema" 23 24 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" 25 "github.com/GoogleContainerTools/skaffold/testutil" 26 ) 27 28 func TestSetLabels(t *testing.T) { 29 manifests := ManifestList{[]byte(` 30 apiVersion: v1 31 kind: Pod 32 metadata: 33 name: getting-started 34 spec: 35 containers: 36 - image: gcr.io/k8s-skaffold/example 37 name: example 38 `)} 39 40 expected := ManifestList{[]byte(` 41 apiVersion: v1 42 kind: Pod 43 metadata: 44 labels: 45 key1: value1 46 key2: value2 47 name: getting-started 48 spec: 49 containers: 50 - image: gcr.io/k8s-skaffold/example 51 name: example 52 `)} 53 54 resultManifest, err := manifests.SetLabels(map[string]string{ 55 "key1": "value1", 56 "key2": "value2", 57 }, NewResourceSelectorLabels(TransformAllowlist, TransformDenylist)) 58 59 testutil.CheckErrorAndDeepEqual(t, false, err, expected.String(), resultManifest.String(), testutil.YamlObj(t)) 60 } 61 62 func TestAddLabels(t *testing.T) { 63 manifests := ManifestList{[]byte(` 64 apiVersion: v1 65 kind: Pod 66 metadata: 67 labels: 68 key0: value0 69 name: getting-started 70 spec: 71 containers: 72 - image: gcr.io/k8s-skaffold/example 73 name: example 74 `)} 75 76 expected := ManifestList{[]byte(` 77 apiVersion: v1 78 kind: Pod 79 metadata: 80 labels: 81 key0: value0 82 key1: value1 83 key2: value2 84 name: getting-started 85 spec: 86 containers: 87 - image: gcr.io/k8s-skaffold/example 88 name: example 89 `)} 90 91 resultManifest, err := manifests.SetLabels(map[string]string{ 92 "key0": "should-be-ignored", 93 "key1": "value1", 94 "key2": "value2", 95 }, NewResourceSelectorLabels(TransformAllowlist, TransformDenylist)) 96 97 testutil.CheckErrorAndDeepEqual(t, false, err, expected.String(), resultManifest.String(), testutil.YamlObj(t)) 98 } 99 100 func TestSetNoLabel(t *testing.T) { 101 manifests := ManifestList{[]byte(` 102 apiVersion: v1 103 kind: Pod 104 metadata: 105 name: getting-started 106 spec: 107 containers: 108 - image: gcr.io/k8s-skaffold/example 109 name: example 110 `)} 111 112 expected := ManifestList{[]byte(` 113 apiVersion: v1 114 kind: Pod 115 metadata: 116 name: getting-started 117 spec: 118 containers: 119 - image: gcr.io/k8s-skaffold/example 120 name: example 121 `)} 122 123 resultManifest, err := manifests.SetLabels(nil, NewResourceSelectorLabels(TransformAllowlist, TransformDenylist)) 124 125 testutil.CheckErrorAndDeepEqual(t, false, err, expected.String(), resultManifest.String()) 126 } 127 128 func TestSetNoLabelWhenTypeUnexpected(t *testing.T) { 129 manifests := ManifestList{[]byte(` 130 apiVersion: v1 131 kind: Pod 132 metadata: 3 133 `), 134 []byte(` 135 apiVersion: v1 136 kind: Pod 137 metadata: 138 labels: 3 139 name: getting-started 140 `)} 141 142 resultManifest, err := manifests.SetLabels(map[string]string{"key0": "value0"}, NewResourceSelectorLabels(TransformAllowlist, TransformDenylist)) 143 144 testutil.CheckErrorAndDeepEqual(t, false, err, manifests.String(), resultManifest.String()) 145 } 146 147 func TestSetLabelInCRDSchema(t *testing.T) { 148 manifests := ManifestList{[]byte(`apiVersion: apiextensions.k8s.io/v1beta1 149 kind: CustomResourceDefinition 150 metadata: 151 name: mykind.mygroup.org 152 spec: 153 group: mygroup.org 154 names: 155 kind: MyKind 156 validation: 157 openAPIV3Schema: 158 properties: 159 apiVersion: 160 type: string 161 kind: 162 type: string 163 metadata: 164 type: object`)} 165 166 expected := ManifestList{[]byte(`apiVersion: apiextensions.k8s.io/v1beta1 167 kind: CustomResourceDefinition 168 metadata: 169 labels: 170 key0: value0 171 key1: value1 172 name: mykind.mygroup.org 173 spec: 174 group: mygroup.org 175 names: 176 kind: MyKind 177 validation: 178 openAPIV3Schema: 179 properties: 180 apiVersion: 181 type: string 182 kind: 183 type: string 184 metadata: 185 type: object`)} 186 187 resultManifest, err := manifests.SetLabels(map[string]string{ 188 "key0": "value0", 189 "key1": "value1", 190 }, NewResourceSelectorLabels(map[schema.GroupKind]latest.ResourceFilter{ 191 {Group: "apiextensions.k8s.io", Kind: "CustomResourceDefinition"}: { 192 GroupKind: "CustomResourceDefinition.apiextensions.k8s.io", 193 Image: []string{".*"}, 194 Labels: []string{".metadata.labels"}, 195 }, 196 }, nil)) 197 198 testutil.CheckErrorAndDeepEqual(t, false, err, expected.String(), resultManifest.String()) 199 }