Add support for parsing profile yaml files
[multicloud/k8s.git] / src / k8splugin / internal / app / vnfhelper_test.go
1 // +build integration
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 app
17
18 import (
19         "io/ioutil"
20         "log"
21         "os"
22         "plugin"
23         "testing"
24
25         yaml "gopkg.in/yaml.v2"
26         "k8s.io/client-go/kubernetes"
27
28         pkgerrors "github.com/pkg/errors"
29
30         utils "k8splugin/internal"
31 )
32
33 func LoadMockPlugins(krdLoadedPlugins *map[string]*plugin.Plugin) error {
34         if _, err := os.Stat("../../mock_files/mock_plugins/mockplugin.so"); os.IsNotExist(err) {
35                 return pkgerrors.New("mockplugin.so does not exist. Please compile mockplugin.go to generate")
36         }
37
38         mockPlugin, err := plugin.Open("../../mock_files/mock_plugins/mockplugin.so")
39         if err != nil {
40                 return pkgerrors.Cause(err)
41         }
42
43         (*krdLoadedPlugins)["namespace"] = mockPlugin
44         (*krdLoadedPlugins)["deployment"] = mockPlugin
45         (*krdLoadedPlugins)["service"] = mockPlugin
46
47         return nil
48 }
49
50 func TestCreateVNF(t *testing.T) {
51         oldkrdPluginData := utils.LoadedPlugins
52         oldReadMetadataFile := ReadMetadataFile
53
54         defer func() {
55                 utils.LoadedPlugins = oldkrdPluginData
56                 ReadMetadataFile = oldReadMetadataFile
57         }()
58
59         err := LoadMockPlugins(&utils.LoadedPlugins)
60         if err != nil {
61                 t.Fatalf("TestCreateVNF returned an error (%s)", err)
62         }
63
64         ReadMetadataFile = func(yamlFilePath string) (MetadataFile, error) {
65                 var seqFile MetadataFile
66
67                 if _, err := os.Stat(yamlFilePath); err == nil {
68                         rawBytes, err := ioutil.ReadFile("../../mock_files/mock_yamls/metadata.yaml")
69                         if err != nil {
70                                 return seqFile, pkgerrors.Wrap(err, "Metadata YAML file read error")
71                         }
72
73                         err = yaml.Unmarshal(rawBytes, &seqFile)
74                         if err != nil {
75                                 return seqFile, pkgerrors.Wrap(err, "Metadata YAML file unmarshall error")
76                         }
77                 }
78
79                 return seqFile, nil
80         }
81
82         kubeclient := kubernetes.Clientset{}
83
84         t.Run("Successfully create VNF", func(t *testing.T) {
85                 externaluuid, data, err := CreateVNF("uuid", "cloudregion1", "test", &kubeclient)
86                 if err != nil {
87                         t.Fatalf("TestCreateVNF returned an error (%s)", err)
88                 }
89
90                 log.Println(externaluuid)
91
92                 if data == nil {
93                         t.Fatalf("TestCreateVNF returned empty data (%s)", data)
94                 }
95         })
96
97 }
98
99 func TestDeleteVNF(t *testing.T) {
100         oldkrdPluginData := utils.LoadedPlugins
101
102         defer func() {
103                 utils.LoadedPlugins = oldkrdPluginData
104         }()
105
106         err := LoadMockPlugins(&utils.LoadedPlugins)
107         if err != nil {
108                 t.Fatalf("TestCreateVNF returned an error (%s)", err)
109         }
110
111         kubeclient := kubernetes.Clientset{}
112
113         t.Run("Successfully delete VNF", func(t *testing.T) {
114                 data := map[string][]string{
115                         "deployment": []string{"cloud1-default-uuid-sisedeploy"},
116                         "service":    []string{"cloud1-default-uuid-sisesvc"},
117                 }
118
119                 err := DestroyVNF(data, "test", &kubeclient)
120                 if err != nil {
121                         t.Fatalf("TestCreateVNF returned an error (%s)", err)
122                 }
123         })
124 }
125
126 func TestReadMetadataFile(t *testing.T) {
127         t.Run("Successfully read Metadata YAML file", func(t *testing.T) {
128                 _, err := ReadMetadataFile("../../mock_files//mock_yamls/metadata.yaml")
129                 if err != nil {
130                         t.Fatalf("TestReadMetadataFile returned an error (%s)", err)
131                 }
132         })
133 }