b968072f4284c9ea78d429729240c3fb7cbfea18
[multicloud/k8s.git] / src / k8splugin / internal / plugin / helpers_test.go
1 /*
2  * Copyright © 2020 Samsung Electronics
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 plugin
18
19 import (
20         "testing"
21
22         "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
23
24         utils "github.com/onap/multicloud-k8s/src/k8splugin/internal"
25         "github.com/onap/multicloud-k8s/src/k8splugin/internal/config"
26 )
27
28 func TestTagPodsIfPresent(t *testing.T) {
29
30   testCases := []struct{
31     testName                string
32     inputUnstructSrc        string
33     valueToTag              string
34     shouldFailBeforeCheck   bool //This flag provides information if .spec.template.metadata.labels path should be reachable or not
35   }{
36     {
37       testName:               "Resource with no child PodTemplateSpec",
38       inputUnstructSrc:       "../../mock_files/mock_yamls/configmap.yaml",
39       valueToTag:             "test1",
40       shouldFailBeforeCheck:  true,
41     },
42     {
43       testName:               "Deployment with PodTemplateSpec",
44       inputUnstructSrc:       "../../mock_files/mock_yamls/deployment.yaml",
45       valueToTag:             "test2",
46       shouldFailBeforeCheck:  false,
47     },
48   }
49
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)
54       if err != nil {
55         t.Fatal("Couldn't decode Yaml:", err)
56       }
57       TagPodsIfPresent(holderUnstr, testCase.valueToTag)
58       t.Log(holderUnstr)
59       var labelsFinder map[string]interface{} = holderUnstr.Object
60       var ok bool
61       for _, key := range []string{"spec", "template", "metadata", "labels"} {
62         labelsFinder, ok = labelsFinder[key].(map[string]interface{})
63         if !ok {
64             if testCase.shouldFailBeforeCheck {
65               return
66             } else {
67               t.Fatalf("Error converting %s to map", key)
68             }
69         }
70       }
71       if testCase.shouldFailBeforeCheck {
72         t.Fatal("Error, nested element '.spec.template.metadata.labels' shouldn't be reachable")
73       }
74       label, ok := labelsFinder[config.GetConfiguration().KubernetesLabelName].(string)
75       if !ok {
76         t.Fatalf("Error extracting string label '%s'", config.GetConfiguration().KubernetesLabelName)
77       }
78       if label != testCase.valueToTag {
79         t.Fatalf("Error, expected label '%s' but received '%s'", testCase.valueToTag, label)
80       }
81     })
82   }
83 }