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