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.
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"
26 func TestDecodeYAML(t *testing.T) {
27 testCases := []struct {
30 expectedResult runtime.Object
34 label: "Fail to read non-existing YAML file",
35 input: "unexisting-file.yaml",
36 expectedError: "not found",
39 label: "Fail to read invalid YAML format",
40 input: "./utils_test.go",
41 expectedError: "mapping values are not allowed in this contex",
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",
50 Spec: appsV1.DeploymentSpec{
51 Template: coreV1.PodTemplateSpec{
52 ObjectMeta: metaV1.ObjectMeta{
53 Labels: map[string]string{"app": "sise"},
56 Containers: []coreV1.Container{
59 Image: "mhausenblas/simpleservice:0.5.0",
69 for _, testCase := range testCases {
70 t.Run(testCase.label, func(t *testing.T) {
71 result, err := DecodeYAML(testCase.input, nil)
73 if testCase.expectedError == "" {
74 t.Fatalf("Decode YAML method return an un-expected (%s)", err)
76 if !strings.Contains(string(err.Error()), testCase.expectedError) {
77 t.Fatalf("Decode YAML method returned an error (%s)", err)
80 if testCase.expectedError != "" && testCase.expectedResult == nil {
81 t.Fatalf("Decode YAML method was expecting \"%s\" error message", testCase.expectedError)
84 t.Fatal("Decode YAML method returned nil result")
86 // if !reflect.DeepEqual(testCase.expectedResult, result) {
88 // t.Fatalf("Decode YAML method returned: \n%v\n and it was expected: \n%v", result, testCase.expectedResult)