From 0e398dd9ebd19424790bd2ea6abc69fb60f28324 Mon Sep 17 00:00:00 2001 From: Yonah Dissen <47282577+yonahd@users.noreply.github.com> Date: Tue, 20 Aug 2024 22:19:44 +0300 Subject: [PATCH] Tests: add tests for multi functionality (#351) * tests: add tests for multi functionality * tests: add tests for multi functionality * tests: add tests for multi functionality - lint fix --------- Co-authored-by: Yonah Dissen --- pkg/kor/multi_test.go | 108 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 pkg/kor/multi_test.go diff --git a/pkg/kor/multi_test.go b/pkg/kor/multi_test.go new file mode 100644 index 00000000..3c4828f2 --- /dev/null +++ b/pkg/kor/multi_test.go @@ -0,0 +1,108 @@ +package kor + +import ( + "context" + "encoding/json" + "reflect" + "testing" + + corev1 "k8s.io/api/core/v1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes/fake" + + "github.com/yonahd/kor/pkg/common" + "github.com/yonahd/kor/pkg/filters" +) + +func createTestMultiResources(t *testing.T) *fake.Clientset { + clientset := fake.NewClientset() + + _, err := clientset.CoreV1().Namespaces().Create(context.TODO(), &corev1.Namespace{ + ObjectMeta: v1.ObjectMeta{Name: testNamespace}, + }, v1.CreateOptions{}) + + if err != nil { + t.Fatalf("Error creating namespace %s: %v", testNamespace, err) + } + + deployment1 := CreateTestDeployment(testNamespace, "test-deployment1", 0, AppLabels) + _, err = clientset.AppsV1().Deployments(testNamespace).Create(context.TODO(), deployment1, v1.CreateOptions{}) + if err != nil { + t.Fatalf("Error creating fake deployment: %v", err) + } + + configmap1 := CreateTestConfigmap(testNamespace, "configmap-1", AppLabels) + _, err = clientset.CoreV1().ConfigMaps(testNamespace).Create(context.TODO(), configmap1, v1.CreateOptions{}) + if err != nil { + t.Fatalf("Error creating fake configmap: %v", err) + } + + return clientset + +} + +func TestRetrieveNamespaceDiff(t *testing.T) { + clientset := createTestMultiResources(t) + resourceList := []string{"cm", "pdb", "deployment"} + filterOpts := &filters.Options{} + + namespaceDiff := retrieveNamespaceDiffs(clientset, testNamespace, resourceList, filterOpts) + + if len(namespaceDiff) != 3 { + t.Fatalf("Expected 3 diffs, got %d", len(namespaceDiff)) + } + + if namespaceDiff[0].resourceType != "ConfigMap" && namespaceDiff[0].diff[0].Name != "configmap-1" { + t.Fatalf("Expected configmap-1, got %s", namespaceDiff[0].diff[0].Name) + } + + if namespaceDiff[1].resourceType != "Pdb" && namespaceDiff[1].diff != nil { + t.Fatalf("Expected nil, got %s", namespaceDiff[1].diff[0].Name) + } + + if namespaceDiff[2].resourceType != "Deployment" && namespaceDiff[2].diff[0].Name != "test-deployment1" { + t.Fatalf("Expected test-deployment1, got %s", namespaceDiff[2].diff[0].Name) + } + +} + +func TestGetUnusedMulti(t *testing.T) { + clientset := createTestMultiResources(t) + resourceList := "cm,pdb,deployment" + + opts := common.Opts{ + WebhookURL: "", + Channel: "", + Token: "", + DeleteFlag: false, + NoInteractive: true, + GroupBy: "namespace", + } + + output, err := GetUnusedMulti(resourceList, &filters.Options{}, clientset, nil, nil, "json", opts) + + if err != nil { + t.Fatalf("Error calling GetUnusedMulti: %v", err) + } + + expectedOutput := map[string]map[string][]string{ + "test-namespace": { + "ConfigMap": { + "configmap-1", + }, + "Deployment": { + "test-deployment1", + }, + }, + } + + var actualOutput map[string]map[string][]string + if err := json.Unmarshal([]byte(output), &actualOutput); err != nil { + t.Fatalf("Error unmarshaling actual output: %v", err) + } + + if !reflect.DeepEqual(expectedOutput, actualOutput) { + t.Errorf("Expected output does not match \n actualOutput:\n %s \n expectedOutput:\n %s", actualOutput, expectedOutput) + } + +}