6db541a472118807a01283d8fe5ce61fd1ba9b72
[multicloud/k8s.git] / src / k8splugin / internal / app / client_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 app
15
16 import (
17         "encoding/base64"
18         "io/ioutil"
19         "os"
20         "plugin"
21         "reflect"
22         "testing"
23
24         utils "github.com/onap/multicloud-k8s/src/k8splugin/internal"
25         "github.com/onap/multicloud-k8s/src/k8splugin/internal/connection"
26         "github.com/onap/multicloud-k8s/src/k8splugin/internal/db"
27         "github.com/onap/multicloud-k8s/src/k8splugin/internal/helm"
28
29         pkgerrors "github.com/pkg/errors"
30         "k8s.io/apimachinery/pkg/runtime/schema"
31         "k8s.io/client-go/kubernetes"
32 )
33
34 func LoadMockPlugins(krdLoadedPlugins map[string]*plugin.Plugin) error {
35         if _, err := os.Stat("../../mock_files/mock_plugins/mockplugin.so"); os.IsNotExist(err) {
36                 return pkgerrors.New("mockplugin.so does not exist. Please compile mockplugin.go to generate")
37         }
38
39         mockPlugin, err := plugin.Open("../../mock_files/mock_plugins/mockplugin.so")
40         if err != nil {
41                 return pkgerrors.Wrap(err, "Opening mock plugins")
42         }
43
44         krdLoadedPlugins["namespace"] = mockPlugin
45         krdLoadedPlugins["generic"] = mockPlugin
46         krdLoadedPlugins["service"] = mockPlugin
47
48         return nil
49 }
50
51 func TestInit(t *testing.T) {
52         t.Run("Successfully create Kube Client", func(t *testing.T) {
53                 // Load the mock kube config file into memory
54                 fd, err := ioutil.ReadFile("../../mock_files/mock_configs/mock_kube_config")
55                 if err != nil {
56                         t.Fatal("Unable to read mock_kube_config")
57                 }
58
59                 fdbase64 := base64.StdEncoding.EncodeToString(fd)
60
61                 // Create mock db with connectivity information in it
62                 db.DBconn = &db.MockDB{
63                         Items: map[string]map[string][]byte{
64                                 connection.ConnectionKey{CloudRegion: "mock_connection"}.String(): {
65                                         "metadata": []byte(
66                                                 "{\"cloud-region\":\"mock_connection\"," +
67                                                         "\"cloud-owner\":\"mock_owner\"," +
68                                                         "\"kubeconfig\": \"" + fdbase64 + "\"}"),
69                                 },
70                         },
71                 }
72
73                 kubeClient := KubernetesClient{}
74                 // Refer to the connection via its name
75                 err = kubeClient.Init("mock_connection", "abcdefg")
76                 if err != nil {
77                         t.Fatalf("TestGetKubeClient returned an error (%s)", err)
78                 }
79
80                 name := reflect.TypeOf(kubeClient.clientSet).Elem().Name()
81                 if name != "Clientset" {
82                         t.Fatalf("TestGetKubeClient returned :\n result=%v\n expected=%v", name, "Clientset")
83                 }
84
85         })
86 }
87
88 func TestCreateResources(t *testing.T) {
89         oldkrdPluginData := utils.LoadedPlugins
90
91         defer func() {
92                 utils.LoadedPlugins = oldkrdPluginData
93         }()
94
95         err := LoadMockPlugins(utils.LoadedPlugins)
96         if err != nil {
97                 t.Fatalf("LoadMockPlugins returned an error (%s)", err)
98         }
99
100         k8 := KubernetesClient{
101                 clientSet: &kubernetes.Clientset{},
102         }
103
104         t.Run("Successfully delete resources", func(t *testing.T) {
105                 data := []helm.KubernetesResourceTemplate{
106                         {
107                                 GVK: schema.GroupVersionKind{
108                                         Group:   "apps",
109                                         Version: "v1",
110                                         Kind:    "Deployment"},
111                                 FilePath: "../../mock_files/mock_yamls/deployment.yaml",
112                         },
113                         {
114                                 GVK: schema.GroupVersionKind{
115                                         Group:   "",
116                                         Version: "v1",
117                                         Kind:    "Service"},
118                                 FilePath: "../../mock_files/mock_yamls/service.yaml",
119                         },
120                 }
121
122                 _, err := k8.createResources(data, "testnamespace")
123                 if err != nil {
124                         t.Fatalf("TestCreateResources returned an error (%s)", err)
125                 }
126         })
127 }
128
129 func TestDeleteResources(t *testing.T) {
130         oldkrdPluginData := utils.LoadedPlugins
131
132         defer func() {
133                 utils.LoadedPlugins = oldkrdPluginData
134         }()
135
136         err := LoadMockPlugins(utils.LoadedPlugins)
137         if err != nil {
138                 t.Fatalf("LoadMockPlugins returned an error (%s)", err)
139         }
140
141         k8 := KubernetesClient{
142                 clientSet: &kubernetes.Clientset{},
143         }
144
145         t.Run("Successfully delete resources", func(t *testing.T) {
146                 data := []helm.KubernetesResource{
147                         {
148                                 GVK: schema.GroupVersionKind{
149                                         Group:   "apps",
150                                         Version: "v1",
151                                         Kind:    "Deployment"},
152                                 Name: "deployment-1",
153                         },
154                         {
155                                 GVK: schema.GroupVersionKind{
156                                         Group:   "apps",
157                                         Version: "v1",
158                                         Kind:    "Deployment"},
159                                 Name: "deployment-2",
160                         },
161                         {
162                                 GVK: schema.GroupVersionKind{
163                                         Group:   "",
164                                         Version: "v1",
165                                         Kind:    "Service"},
166                                 Name: "service-1",
167                         },
168                         {
169                                 GVK: schema.GroupVersionKind{
170                                         Group:   "",
171                                         Version: "v1",
172                                         Kind:    "Service"},
173                                 Name: "service-2",
174                         },
175                 }
176
177                 err := k8.deleteResources(data, "test")
178                 if err != nil {
179                         t.Fatalf("TestCreateVNF returned an error (%s)", err)
180                 }
181         })
182 }