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.
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"
29 pkgerrors "github.com/pkg/errors"
30 "k8s.io/apimachinery/pkg/runtime/schema"
31 "k8s.io/client-go/kubernetes"
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")
39 mockPlugin, err := plugin.Open("../../mock_files/mock_plugins/mockplugin.so")
41 return pkgerrors.Wrap(err, "Opening mock plugins")
44 krdLoadedPlugins["namespace"] = mockPlugin
45 krdLoadedPlugins["generic"] = mockPlugin
46 krdLoadedPlugins["service"] = mockPlugin
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")
56 t.Fatal("Unable to read mock_kube_config")
59 fdbase64 := base64.StdEncoding.EncodeToString(fd)
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(): {
66 "{\"cloud-region\":\"mock_connection\"," +
67 "\"cloud-owner\":\"mock_owner\"," +
68 "\"kubeconfig\": \"" + fdbase64 + "\"}"),
73 kubeClient := KubernetesClient{}
74 // Refer to the connection via its name
75 err = kubeClient.Init("mock_connection", "abcdefg")
77 t.Fatalf("TestGetKubeClient returned an error (%s)", err)
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")
88 func TestCreateResources(t *testing.T) {
89 oldkrdPluginData := utils.LoadedPlugins
92 utils.LoadedPlugins = oldkrdPluginData
95 err := LoadMockPlugins(utils.LoadedPlugins)
97 t.Fatalf("LoadMockPlugins returned an error (%s)", err)
100 k8 := KubernetesClient{
101 clientSet: &kubernetes.Clientset{},
104 t.Run("Successfully delete resources", func(t *testing.T) {
105 data := []helm.KubernetesResourceTemplate{
107 GVK: schema.GroupVersionKind{
111 FilePath: "../../mock_files/mock_yamls/deployment.yaml",
114 GVK: schema.GroupVersionKind{
118 FilePath: "../../mock_files/mock_yamls/service.yaml",
122 _, err := k8.createResources(data, "testnamespace")
124 t.Fatalf("TestCreateResources returned an error (%s)", err)
129 func TestDeleteResources(t *testing.T) {
130 oldkrdPluginData := utils.LoadedPlugins
133 utils.LoadedPlugins = oldkrdPluginData
136 err := LoadMockPlugins(utils.LoadedPlugins)
138 t.Fatalf("LoadMockPlugins returned an error (%s)", err)
141 k8 := KubernetesClient{
142 clientSet: &kubernetes.Clientset{},
145 t.Run("Successfully delete resources", func(t *testing.T) {
146 data := []helm.KubernetesResource{
148 GVK: schema.GroupVersionKind{
152 Name: "deployment-1",
155 GVK: schema.GroupVersionKind{
159 Name: "deployment-2",
162 GVK: schema.GroupVersionKind{
169 GVK: schema.GroupVersionKind{
177 err := k8.deleteResources(data, "test")
179 t.Fatalf("TestCreateVNF returned an error (%s)", err)