2 * Copyright © 2020 Samsung Electronics
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24 utils "github.com/onap/multicloud-k8s/src/k8splugin/internal"
25 "github.com/onap/multicloud-k8s/src/k8splugin/internal/config"
28 func TestTagPodsIfPresent(t *testing.T) {
30 testCases := []struct{
32 inputUnstructSrc string
34 shouldFailBeforeCheck bool //This flag provides information if .spec.template.metadata.labels path should be reachable or not
37 testName: "Resource with no child PodTemplateSpec",
38 inputUnstructSrc: "../../mock_files/mock_yamls/configmap.yaml",
40 shouldFailBeforeCheck: true,
43 testName: "Deployment with PodTemplateSpec",
44 inputUnstructSrc: "../../mock_files/mock_yamls/deployment.yaml",
46 shouldFailBeforeCheck: false,
50 for _, testCase := range testCases {
51 t.Run(testCase.testName, func(t *testing.T){
52 holderUnstr := new(unstructured.Unstructured)
53 _, err := utils.DecodeYAML(testCase.inputUnstructSrc, holderUnstr)
55 t.Fatal("Couldn't decode Yaml:", err)
57 TagPodsIfPresent(holderUnstr, testCase.valueToTag)
59 var labelsFinder map[string]interface{} = holderUnstr.Object
61 for _, key := range []string{"spec", "template", "metadata", "labels"} {
62 labelsFinder, ok = labelsFinder[key].(map[string]interface{})
64 if testCase.shouldFailBeforeCheck {
67 t.Fatalf("Error converting %s to map", key)
71 if testCase.shouldFailBeforeCheck {
72 t.Fatal("Error, nested element '.spec.template.metadata.labels' shouldn't be reachable")
74 label, ok := labelsFinder[config.GetConfiguration().KubernetesLabelName].(string)
76 t.Fatalf("Error extracting string label '%s'", config.GetConfiguration().KubernetesLabelName)
78 if label != testCase.valueToTag {
79 t.Fatalf("Error, expected label '%s' but received '%s'", testCase.valueToTag, label)