From 6aabcbd257f61c077c9909ae4447f045f1e9098f Mon Sep 17 00:00:00 2001 From: Konrad Bańka Date: Thu, 10 Sep 2020 16:19:02 +0200 Subject: Provide tests for nested labels provisioning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue-ID: MULTICLOUD-1179 Signed-off-by: Konrad Bańka Change-Id: I2f8facefc5299408a47dd9f8bdba9410aa8171de --- src/k8splugin/internal/plugin/helpers_test.go | 83 ++++++++++++++++++++++ src/k8splugin/mock_files/mock_yamls/configmap.yaml | 6 ++ src/k8splugin/plugins/generic/plugin.go | 2 +- 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 src/k8splugin/internal/plugin/helpers_test.go create mode 100644 src/k8splugin/mock_files/mock_yamls/configmap.yaml diff --git a/src/k8splugin/internal/plugin/helpers_test.go b/src/k8splugin/internal/plugin/helpers_test.go new file mode 100644 index 00000000..b968072f --- /dev/null +++ b/src/k8splugin/internal/plugin/helpers_test.go @@ -0,0 +1,83 @@ +/* + * Copyright © 2020 Samsung Electronics + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package plugin + +import ( + "testing" + + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + + utils "github.com/onap/multicloud-k8s/src/k8splugin/internal" + "github.com/onap/multicloud-k8s/src/k8splugin/internal/config" +) + +func TestTagPodsIfPresent(t *testing.T) { + + testCases := []struct{ + testName string + inputUnstructSrc string + valueToTag string + shouldFailBeforeCheck bool //This flag provides information if .spec.template.metadata.labels path should be reachable or not + }{ + { + testName: "Resource with no child PodTemplateSpec", + inputUnstructSrc: "../../mock_files/mock_yamls/configmap.yaml", + valueToTag: "test1", + shouldFailBeforeCheck: true, + }, + { + testName: "Deployment with PodTemplateSpec", + inputUnstructSrc: "../../mock_files/mock_yamls/deployment.yaml", + valueToTag: "test2", + shouldFailBeforeCheck: false, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.testName, func(t *testing.T){ + holderUnstr := new(unstructured.Unstructured) + _, err := utils.DecodeYAML(testCase.inputUnstructSrc, holderUnstr) + if err != nil { + t.Fatal("Couldn't decode Yaml:", err) + } + TagPodsIfPresent(holderUnstr, testCase.valueToTag) + t.Log(holderUnstr) + var labelsFinder map[string]interface{} = holderUnstr.Object + var ok bool + for _, key := range []string{"spec", "template", "metadata", "labels"} { + labelsFinder, ok = labelsFinder[key].(map[string]interface{}) + if !ok { + if testCase.shouldFailBeforeCheck { + return + } else { + t.Fatalf("Error converting %s to map", key) + } + } + } + if testCase.shouldFailBeforeCheck { + t.Fatal("Error, nested element '.spec.template.metadata.labels' shouldn't be reachable") + } + label, ok := labelsFinder[config.GetConfiguration().KubernetesLabelName].(string) + if !ok { + t.Fatalf("Error extracting string label '%s'", config.GetConfiguration().KubernetesLabelName) + } + if label != testCase.valueToTag { + t.Fatalf("Error, expected label '%s' but received '%s'", testCase.valueToTag, label) + } + }) + } +} diff --git a/src/k8splugin/mock_files/mock_yamls/configmap.yaml b/src/k8splugin/mock_files/mock_yamls/configmap.yaml new file mode 100644 index 00000000..8a1e14c5 --- /dev/null +++ b/src/k8splugin/mock_files/mock_yamls/configmap.yaml @@ -0,0 +1,6 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: mock-configmap +data: + key1: value1 diff --git a/src/k8splugin/plugins/generic/plugin.go b/src/k8splugin/plugins/generic/plugin.go index aa503097..8cbfcdf5 100644 --- a/src/k8splugin/plugins/generic/plugin.go +++ b/src/k8splugin/plugins/generic/plugin.go @@ -35,7 +35,7 @@ var ExportedVariable genericPlugin type genericPlugin struct { } -// Create deployment object in a specific Kubernetes cluster +// Create generic object in a specific Kubernetes cluster func (g genericPlugin) Create(yamlFilePath string, namespace string, client plugin.KubernetesConnector) (string, error) { if namespace == "" { namespace = "default" -- cgit 1.2.3-korg