aboutsummaryrefslogtreecommitdiffstats
path: root/src/k8splugin/internal/app/client_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/k8splugin/internal/app/client_test.go')
-rw-r--r--src/k8splugin/internal/app/client_test.go92
1 files changed, 88 insertions, 4 deletions
diff --git a/src/k8splugin/internal/app/client_test.go b/src/k8splugin/internal/app/client_test.go
index 71892725..5999cfa0 100644
--- a/src/k8splugin/internal/app/client_test.go
+++ b/src/k8splugin/internal/app/client_test.go
@@ -14,21 +14,105 @@ limitations under the License.
package app
import (
+ "os"
+ "plugin"
"reflect"
"testing"
+
+ utils "k8splugin/internal"
+
+ pkgerrors "github.com/pkg/errors"
+ "k8s.io/client-go/kubernetes"
)
-func TestGetKubeClient(t *testing.T) {
+func LoadMockPlugins(krdLoadedPlugins map[string]*plugin.Plugin) error {
+ if _, err := os.Stat("../../mock_files/mock_plugins/mockplugin.so"); os.IsNotExist(err) {
+ return pkgerrors.New("mockplugin.so does not exist. Please compile mockplugin.go to generate")
+ }
+
+ mockPlugin, err := plugin.Open("../../mock_files/mock_plugins/mockplugin.so")
+ if err != nil {
+ return pkgerrors.Wrap(err, "Opening mock plugins")
+ }
+
+ krdLoadedPlugins["namespace"] = mockPlugin
+ krdLoadedPlugins["deployment"] = mockPlugin
+ krdLoadedPlugins["service"] = mockPlugin
+
+ return nil
+}
+
+func TestInit(t *testing.T) {
t.Run("Successfully create Kube Client", func(t *testing.T) {
- clientset, err := GetKubeClient("../../mock_files/mock_configs/mock_config")
+ kubeClient := kubernetesClient{}
+ err := kubeClient.init("../../mock_files/mock_configs/mock_config")
if err != nil {
t.Fatalf("TestGetKubeClient returned an error (%s)", err)
}
- if reflect.TypeOf(clientset).Name() != "Clientset" {
- t.Fatalf("TestGetKubeClient returned :\n result=%v\n expected=%v", clientset, "Clientset")
+ name := reflect.TypeOf(kubeClient.clientSet).Elem().Name()
+ if name != "Clientset" {
+ t.Fatalf("TestGetKubeClient returned :\n result=%v\n expected=%v", name, "Clientset")
}
})
}
+
+func TestCreateResources(t *testing.T) {
+ oldkrdPluginData := utils.LoadedPlugins
+
+ defer func() {
+ utils.LoadedPlugins = oldkrdPluginData
+ }()
+
+ err := LoadMockPlugins(utils.LoadedPlugins)
+ if err != nil {
+ t.Fatalf("LoadMockPlugins returned an error (%s)", err)
+ }
+
+ k8 := kubernetesClient{
+ clientSet: &kubernetes.Clientset{},
+ }
+
+ t.Run("Successfully delete resources", func(t *testing.T) {
+ data := map[string][]string{
+ "Deployment": []string{"../../mock_files/mock_yamls/deployment.yaml"},
+ "Service": []string{"../../mock_files/mock_yamls/service.yaml"},
+ }
+
+ _, err := k8.createResources(data, "testnamespace")
+ if err != nil {
+ t.Fatalf("TestCreateResources returned an error (%s)", err)
+ }
+ })
+}
+
+func TestDeleteResources(t *testing.T) {
+ oldkrdPluginData := utils.LoadedPlugins
+
+ defer func() {
+ utils.LoadedPlugins = oldkrdPluginData
+ }()
+
+ err := LoadMockPlugins(utils.LoadedPlugins)
+ if err != nil {
+ t.Fatalf("LoadMockPlugins returned an error (%s)", err)
+ }
+
+ k8 := kubernetesClient{
+ clientSet: &kubernetes.Clientset{},
+ }
+
+ t.Run("Successfully delete resources", func(t *testing.T) {
+ data := map[string][]string{
+ "Deployment": []string{"deployment-1", "deployment-2"},
+ "Service": []string{"service-1", "service-2"},
+ }
+
+ err := k8.deleteResources(data, "test")
+ if err != nil {
+ t.Fatalf("TestCreateVNF returned an error (%s)", err)
+ }
+ })
+}