Update status check endpoint 
[multicloud/k8s.git] / src / k8splugin / internal / utils / utils_test.go
1 /*
2 Copyright 2018 Intel Corporation.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6     http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software
8 distributed under the License is distributed on an "AS IS" BASIS,
9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 See the License for the specific language governing permissions and
11 limitations under the License.
12 */
13
14 package utils
15
16 import (
17         "strings"
18         "testing"
19
20         appsV1 "k8s.io/api/apps/v1"
21         coreV1 "k8s.io/api/core/v1"
22         metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23         "k8s.io/apimachinery/pkg/runtime"
24 )
25
26 func TestDecodeYAML(t *testing.T) {
27         testCases := []struct {
28                 label          string
29                 input          string
30                 expectedResult runtime.Object
31                 expectedError  string
32         }{
33                 {
34                         label:         "Fail to read non-existing YAML file",
35                         input:         "unexisting-file.yaml",
36                         expectedError: "not found",
37                 },
38                 {
39                         label:         "Fail to read invalid YAML format",
40                         input:         "./utils_test.go",
41                         expectedError: "mapping values are not allowed in this contex",
42                 },
43                 {
44                         label: "Successfully read YAML file",
45                         input: "../../mock_files/mock_yamls/deployment.yaml",
46                         expectedResult: &appsV1.Deployment{
47                                 ObjectMeta: metaV1.ObjectMeta{
48                                         Name: "mock-deployment",
49                                 },
50                                 Spec: appsV1.DeploymentSpec{
51                                         Template: coreV1.PodTemplateSpec{
52                                                 ObjectMeta: metaV1.ObjectMeta{
53                                                         Labels: map[string]string{"app": "sise"},
54                                                 },
55                                                 Spec: coreV1.PodSpec{
56                                                         Containers: []coreV1.Container{
57                                                                 coreV1.Container{
58                                                                         Name:  "sise",
59                                                                         Image: "mhausenblas/simpleservice:0.5.0",
60                                                                 },
61                                                         },
62                                                 },
63                                         },
64                                 },
65                         },
66                 },
67         }
68
69         for _, testCase := range testCases {
70                 t.Run(testCase.label, func(t *testing.T) {
71                         result, err := DecodeYAML(testCase.input, nil)
72                         if err != nil {
73                                 if testCase.expectedError == "" {
74                                         t.Fatalf("Decode YAML method return an un-expected (%s)", err)
75                                 }
76                                 if !strings.Contains(string(err.Error()), testCase.expectedError) {
77                                         t.Fatalf("Decode YAML method returned an error (%s)", err)
78                                 }
79                         } else {
80                                 if testCase.expectedError != "" && testCase.expectedResult == nil {
81                                         t.Fatalf("Decode YAML method was expecting \"%s\" error message", testCase.expectedError)
82                                 }
83                                 if result == nil {
84                                         t.Fatal("Decode YAML method returned nil result")
85                                 }
86                                 // if !reflect.DeepEqual(testCase.expectedResult, result) {
87
88                                 //      t.Fatalf("Decode YAML method returned: \n%v\n and it was expected: \n%v", result, testCase.expectedResult)
89                                 // }
90                         }
91                 })
92         }
93 }