From 68b22b943a93f9f7604f228c9a7d996c2a4ac997 Mon Sep 17 00:00:00 2001 From: Yonah Dissen Date: Tue, 26 Dec 2023 22:36:47 +0200 Subject: [PATCH 1/4] restructure tests --- pkg/kor/configmaps_test.go | 36 +++++++++++++++++++-------------- pkg/kor/deployments_test.go | 34 +++++++++++++++++-------------- pkg/kor/hpas_test.go | 35 +++++++++++++++++++------------- pkg/kor/ingresses_test.go | 35 +++++++++++++++++++------------- pkg/kor/jobs_test.go | 40 ++++++++++++++++++++++--------------- pkg/kor/pdbs_test.go | 33 ++++++++++++++++++------------ pkg/kor/pods_test.go | 33 ++++++++++++++++++------------ pkg/kor/pv_test.go | 17 ++++++++++------ pkg/kor/pvc_test.go | 35 +++++++++++++++++++------------- 9 files changed, 178 insertions(+), 120 deletions(-) diff --git a/pkg/kor/configmaps_test.go b/pkg/kor/configmaps_test.go index 6930b22c..6fbb6e03 100644 --- a/pkg/kor/configmaps_test.go +++ b/pkg/kor/configmaps_test.go @@ -14,16 +14,7 @@ import ( "k8s.io/client-go/kubernetes/scheme" ) -func createTestConfigmaps(t *testing.T) *fake.Clientset { - clientset := fake.NewSimpleClientset() - - _, err := clientset.CoreV1().Namespaces().Create(context.TODO(), &corev1.Namespace{ - ObjectMeta: metav1.ObjectMeta{Name: testNamespace}, - }, metav1.CreateOptions{}) - - if err != nil { - t.Fatalf("Error creating namespace %s: %v", testNamespace, err) - } +func createTestConfigmaps(clientset *fake.Clientset, t *testing.T) { configmap1 := CreateTestConfigmap(testNamespace, "configmap-1") configmap2 := CreateTestConfigmap(testNamespace, "configmap-2") @@ -60,7 +51,7 @@ func createTestConfigmaps(t *testing.T) *fake.Clientset { }, } - _, err = clientset.CoreV1().ConfigMaps(testNamespace).Create(context.TODO(), configmap1, metav1.CreateOptions{}) + _, err := clientset.CoreV1().ConfigMaps(testNamespace).Create(context.TODO(), configmap1, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake configmap: %v", err) } @@ -95,11 +86,26 @@ func createTestConfigmaps(t *testing.T) *fake.Clientset { t.Fatalf("Error creating fake pod: %v", err) } +} + +func createTestConfigmapsClient(t *testing.T) *fake.Clientset { + clientset := fake.NewSimpleClientset() + + _, err := clientset.CoreV1().Namespaces().Create(context.TODO(), &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{Name: testNamespace}, + }, metav1.CreateOptions{}) + + if err != nil { + t.Fatalf("Error creating namespace %s: %v", testNamespace, err) + } + + createTestConfigmaps(clientset, t) + return clientset } func TestRetrieveConfigMapNames(t *testing.T) { - clientset := createTestConfigmaps(t) + clientset := createTestConfigmapsClient(t) configMapNames, err := retrieveConfigMapNames(clientset, testNamespace, &FilterOptions{}) @@ -114,7 +120,7 @@ func TestRetrieveConfigMapNames(t *testing.T) { } func TestProcessNamespaceCM(t *testing.T) { - clientset := createTestConfigmaps(t) + clientset := createTestConfigmapsClient(t) diff, err := processNamespaceCM(clientset, testNamespace, &FilterOptions{}) if err != nil { @@ -128,7 +134,7 @@ func TestProcessNamespaceCM(t *testing.T) { } func TestRetrieveUsedCM(t *testing.T) { - clientset := createTestConfigmaps(t) + clientset := createTestConfigmapsClient(t) volumesCM, envCM, envFromCM, envFromContainerCM, envFromInitContainerCM, err := retrieveUsedCM(clientset, testNamespace) @@ -164,7 +170,7 @@ func TestRetrieveUsedCM(t *testing.T) { } func TestGetUnusedConfigmapsStructured(t *testing.T) { - clientset := createTestConfigmaps(t) + clientset := createTestConfigmapsClient(t) includeExcludeLists := IncludeExcludeLists{ IncludeListStr: "", diff --git a/pkg/kor/deployments_test.go b/pkg/kor/deployments_test.go index fae808fa..738d0c93 100644 --- a/pkg/kor/deployments_test.go +++ b/pkg/kor/deployments_test.go @@ -8,28 +8,18 @@ import ( appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes/scheme" ) -func createTestDeployments(t *testing.T) *fake.Clientset { - clientset := fake.NewSimpleClientset() - - _, 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) - } - +func createTestDeployments(clientset *fake.Clientset, t *testing.T) { appLabels := map[string]string{} deployment1 := CreateTestDeployment(testNamespace, "test-deployment1", 0, appLabels) deployment2 := CreateTestDeployment(testNamespace, "test-deployment2", 1, appLabels) - _, err = clientset.AppsV1().Deployments(testNamespace).Create(context.TODO(), deployment1, v1.CreateOptions{}) + _, err := clientset.AppsV1().Deployments(testNamespace).Create(context.TODO(), deployment1, v1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake deployment: %v", err) } @@ -38,12 +28,26 @@ func createTestDeployments(t *testing.T) *fake.Clientset { if err != nil { t.Fatalf("Error creating fake deployment: %v", err) } +} + +func createTestDeploymentsClient(t *testing.T) *fake.Clientset { + clientset := fake.NewSimpleClientset() + + _, 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) + } + + createTestDeployments(clientset, t) return clientset } func TestProcessNamespaceDeployments(t *testing.T) { - clientset := createTestDeployments(t) + clientset := createTestDeploymentsClient(t) deploymentsWithoutReplicas, err := ProcessNamespaceDeployments(clientset, testNamespace, &FilterOptions{}) if err != nil { @@ -60,7 +64,7 @@ func TestProcessNamespaceDeployments(t *testing.T) { } func TestGetUnusedDeploymentsStructured(t *testing.T) { - clientset := createTestDeployments(t) + clientset := createTestDeploymentsClient(t) includeExcludeLists := IncludeExcludeLists{ IncludeListStr: "", diff --git a/pkg/kor/hpas_test.go b/pkg/kor/hpas_test.go index 14ec5518..68103c5e 100644 --- a/pkg/kor/hpas_test.go +++ b/pkg/kor/hpas_test.go @@ -8,22 +8,13 @@ import ( appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes/scheme" ) -func createTestHpas(t *testing.T) *fake.Clientset { - clientset := fake.NewSimpleClientset() - - _, 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) - } +func createTestHpas(clientset *fake.Clientset, t *testing.T) *fake.Clientset { deploymentName := "test-deployment" appLabels := map[string]string{} @@ -32,7 +23,7 @@ func createTestHpas(t *testing.T) *fake.Clientset { hpa1 := CreateTestHpa(testNamespace, "test-hpa1", deploymentName, 1, 1) hpa2 := CreateTestHpa(testNamespace, "test-hpa2", "non-existing-deployment", 1, 1) - _, err = clientset.AppsV1().Deployments(testNamespace).Create(context.TODO(), deployment1, v1.CreateOptions{}) + _, err := clientset.AppsV1().Deployments(testNamespace).Create(context.TODO(), deployment1, v1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake deployment: %v", err) } @@ -50,8 +41,24 @@ func createTestHpas(t *testing.T) *fake.Clientset { return clientset } +func createTestHpaClient(t *testing.T) *fake.Clientset { + clientset := fake.NewSimpleClientset() + + _, 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) + } + + createTestHpas(clientset, t) + + return clientset +} + func TestExtractUnusedHpas(t *testing.T) { - clientset := createTestHpas(t) + clientset := createTestHpaClient(t) unusedHpas, err := extractUnusedHpas(clientset, testNamespace, &FilterOptions{}) if err != nil { @@ -68,7 +75,7 @@ func TestExtractUnusedHpas(t *testing.T) { } func TestGetUnusedHpasStructured(t *testing.T) { - clientset := createTestHpas(t) + clientset := createTestHpaClient(t) includeExcludeLists := IncludeExcludeLists{ IncludeListStr: "", diff --git a/pkg/kor/ingresses_test.go b/pkg/kor/ingresses_test.go index 39e8fdb3..5b6f67af 100644 --- a/pkg/kor/ingresses_test.go +++ b/pkg/kor/ingresses_test.go @@ -8,28 +8,19 @@ import ( appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes/scheme" ) -func createTestIngresses(t *testing.T) *fake.Clientset { - clientset := fake.NewSimpleClientset() - - _, 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) - } +func createTestIngresses(clientset *fake.Clientset, t *testing.T) *fake.Clientset { service1 := CreateTestService(testNamespace, "my-service-1") ingress1 := CreateTestIngress(testNamespace, "test-ingress-1", "my-service-1", "test-secret") ingress2 := CreateTestIngress(testNamespace, "test-ingress-2", "my-service-2", "test-secret") - _, err = clientset.CoreV1().Services(testNamespace).Create(context.TODO(), service1, v1.CreateOptions{}) + _, err := clientset.CoreV1().Services(testNamespace).Create(context.TODO(), service1, v1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "Service", err) } @@ -45,8 +36,24 @@ func createTestIngresses(t *testing.T) *fake.Clientset { return clientset } +func createTestIngressClient(t *testing.T) *fake.Clientset { + clientset := fake.NewSimpleClientset() + + _, 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) + } + + createTestIngresses(clientset, t) + + return clientset +} + func TestRetrieveUsedIngress(t *testing.T) { - clientset := createTestIngresses(t) + clientset := createTestIngressClient(t) usedIngresses, err := retrieveUsedIngress(clientset, testNamespace, &FilterOptions{}) if err != nil { @@ -72,7 +79,7 @@ func contains(slice []string, item string) bool { } func TestGetUnusedIngressesStructured(t *testing.T) { - clientset := createTestIngresses(t) + clientset := createTestIngressClient(t) includeExcludeLists := IncludeExcludeLists{ IncludeListStr: "", diff --git a/pkg/kor/jobs_test.go b/pkg/kor/jobs_test.go index d6498bf0..5d297684 100644 --- a/pkg/kor/jobs_test.go +++ b/pkg/kor/jobs_test.go @@ -3,6 +3,10 @@ package kor import ( "context" "encoding/json" + "reflect" + "testing" + "time" + appsv1 "k8s.io/api/apps/v1" batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" @@ -10,21 +14,9 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes/scheme" - "reflect" - "testing" - "time" ) -func createTestJobs(t *testing.T) *fake.Clientset { - clientset := fake.NewSimpleClientset() - - _, 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) - } +func createTestJobs(clientset *fake.Clientset, t *testing.T) *fake.Clientset { job1 := CreateTestJob(testNamespace, "test-job1", &batchv1.JobStatus{ Succeeded: 0, @@ -36,7 +28,7 @@ func createTestJobs(t *testing.T) *fake.Clientset { CompletionTime: &v1.Time{Time: time.Now()}, }) - _, err = clientset.BatchV1().Jobs(testNamespace).Create(context.TODO(), job1, v1.CreateOptions{}) + _, err := clientset.BatchV1().Jobs(testNamespace).Create(context.TODO(), job1, v1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake job: %v", err) } @@ -48,8 +40,24 @@ func createTestJobs(t *testing.T) *fake.Clientset { return clientset } +func createTestJobsClient(t *testing.T) *fake.Clientset { + clientset := fake.NewSimpleClientset() + + _, 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) + } + + createTestJobs(clientset, t) + + return clientset +} + func TestProcessNamespaceJobs(t *testing.T) { - clientset := createTestJobs(t) + clientset := createTestJobsClient(t) completedJobs, err := ProcessNamespaceJobs(clientset, testNamespace, &FilterOptions{}) if err != nil { @@ -66,7 +74,7 @@ func TestProcessNamespaceJobs(t *testing.T) { } func TestGetUnusedJobsStructured(t *testing.T) { - clientset := createTestJobs(t) + clientset := createTestJobsClient(t) includeExcludeLists := IncludeExcludeLists{ IncludeListStr: "", diff --git a/pkg/kor/pdbs_test.go b/pkg/kor/pdbs_test.go index 785aacc0..a398b6d9 100644 --- a/pkg/kor/pdbs_test.go +++ b/pkg/kor/pdbs_test.go @@ -11,16 +11,7 @@ import ( "k8s.io/client-go/kubernetes/fake" ) -func createTestPdbs(t *testing.T) *fake.Clientset { - clientset := fake.NewSimpleClientset() - - _, 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) - } +func createTestPdbs(clientset *fake.Clientset, t *testing.T) *fake.Clientset { appLabels1 := map[string]string{ "app": "my-app", @@ -30,7 +21,7 @@ func createTestPdbs(t *testing.T) *fake.Clientset { pdb1 := CreateTestPdb(testNamespace, "test-pdb1", appLabels1) pdb2 := CreateTestPdb(testNamespace, "test-pdb2", appLabels1) pdb3 := CreateTestPdb(testNamespace, "test-pdb3", appLabels2) - _, err = clientset.PolicyV1().PodDisruptionBudgets(testNamespace).Create(context.TODO(), pdb1, v1.CreateOptions{}) + _, err := clientset.PolicyV1().PodDisruptionBudgets(testNamespace).Create(context.TODO(), pdb1, v1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "Pdb", err) } @@ -60,8 +51,24 @@ func createTestPdbs(t *testing.T) *fake.Clientset { return clientset } +func createTestPdbsClient(t *testing.T) *fake.Clientset { + clientset := fake.NewSimpleClientset() + + _, 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) + } + + createTestPdbs(clientset, t) + + return clientset +} + func TestProcessNamespacePdbs(t *testing.T) { - clientset := createTestPdbs(t) + clientset := createTestPdbsClient(t) unusedPdbs, err := processNamespacePdbs(clientset, testNamespace, &FilterOptions{}) if err != nil { @@ -78,7 +85,7 @@ func TestProcessNamespacePdbs(t *testing.T) { } func TestGetUnusedPdbsStructured(t *testing.T) { - clientset := createTestPdbs(t) + clientset := createTestPdbsClient(t) includeExcludeLists := IncludeExcludeLists{ IncludeListStr: "", diff --git a/pkg/kor/pods_test.go b/pkg/kor/pods_test.go index 0fc0cb76..03da8272 100644 --- a/pkg/kor/pods_test.go +++ b/pkg/kor/pods_test.go @@ -14,16 +14,7 @@ import ( "k8s.io/client-go/kubernetes/scheme" ) -func createTestPods(t *testing.T) *fake.Clientset { - clientset := fake.NewSimpleClientset() - - _, 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) - } +func createTestPods(clientset *fake.Clientset, t *testing.T) *fake.Clientset { pod1 := CreateTestPod(testNamespace, "pod-1", "", nil) pod1.Status = corev1.PodStatus{ @@ -72,7 +63,7 @@ func createTestPods(t *testing.T) *fake.Clientset { // Add test pods to the clientset for _, pod := range pods { - _, err = clientset.CoreV1().Pods(pod.Namespace).Create(context.TODO(), pod, v1.CreateOptions{}) + _, err := clientset.CoreV1().Pods(pod.Namespace).Create(context.TODO(), pod, v1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake pod: %v", err) } @@ -81,8 +72,24 @@ func createTestPods(t *testing.T) *fake.Clientset { return clientset } +func createTestPodsClient(t *testing.T) *fake.Clientset { + clientset := fake.NewSimpleClientset() + + _, 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) + } + + createTestPods(clientset, t) + + return clientset +} + func TestProcessNamespacePods(t *testing.T) { - clientset := createTestPods(t) + clientset := createTestPodsClient(t) evictedPods, err := ProcessNamespacePods(clientset, testNamespace, &FilterOptions{}) if err != nil { t.Errorf("Unexpected error: %v", err) @@ -102,7 +109,7 @@ func TestProcessNamespacePods(t *testing.T) { } func TestGetUnusedPodsStructured(t *testing.T) { - clientset := createTestPods(t) + clientset := createTestPodsClient(t) includeExcludeLists := IncludeExcludeLists{ IncludeListStr: "", diff --git a/pkg/kor/pv_test.go b/pkg/kor/pv_test.go index 97f119b1..7fdb07f1 100644 --- a/pkg/kor/pv_test.go +++ b/pkg/kor/pv_test.go @@ -10,8 +10,7 @@ import ( "k8s.io/client-go/kubernetes/fake" ) -func createTestPvs(t *testing.T) *fake.Clientset { - clientset := fake.NewSimpleClientset() +func createTestPvs(clientset *fake.Clientset, t *testing.T) *fake.Clientset { pv1 := CreateTestPv("test-pv1", "Bound") pv2 := CreateTestPv("test-pv2", "Available") @@ -28,8 +27,16 @@ func createTestPvs(t *testing.T) *fake.Clientset { return clientset } +func createTestPvsClient(t *testing.T) *fake.Clientset { + clientset := fake.NewSimpleClientset() + + createTestPvs(clientset, t) + + return clientset +} + func TestProcessPvs(t *testing.T) { - clientset := createTestPvs(t) + clientset := createTestPvsClient(t) usedPvs, err := processPvs(clientset, &FilterOptions{}) if err != nil { t.Errorf("Expected no error, got %v", err) @@ -45,7 +52,7 @@ func TestProcessPvs(t *testing.T) { } func TestGetUnusedPvs(t *testing.T) { - clientset := createTestPvs(t) + clientset := createTestPvsClient(t) opts := Opts{ WebhookURL: "", @@ -75,5 +82,3 @@ func TestGetUnusedPvs(t *testing.T) { t.Errorf("Expected output does not match actual output") } } - - diff --git a/pkg/kor/pvc_test.go b/pkg/kor/pvc_test.go index 9b0124a6..4b27d227 100644 --- a/pkg/kor/pvc_test.go +++ b/pkg/kor/pvc_test.go @@ -14,21 +14,12 @@ import ( "k8s.io/client-go/kubernetes/scheme" ) -func createTestPvcs(t *testing.T) *fake.Clientset { - clientset := fake.NewSimpleClientset() +func createTestPvcs(clientset *fake.Clientset, t *testing.T) *fake.Clientset { var volumeList []corev1.Volume - _, 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) - } - pvc1 := CreateTestPvc(testNamespace, "test-pvc1") pvc2 := CreateTestPvc(testNamespace, "test-pvc2") - _, err = clientset.CoreV1().PersistentVolumeClaims(testNamespace).Create(context.TODO(), pvc1, v1.CreateOptions{}) + _, err := clientset.CoreV1().PersistentVolumeClaims(testNamespace).Create(context.TODO(), pvc1, v1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "Pvc", err) } @@ -50,8 +41,24 @@ func createTestPvcs(t *testing.T) *fake.Clientset { return clientset } +func createTestPvcsClient(t *testing.T) *fake.Clientset { + clientset := fake.NewSimpleClientset() + + _, 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) + } + + createTestPvcs(clientset, t) + + return clientset +} + func TestRetreiveUsedPvcs(t *testing.T) { - clientset := createTestPvcs(t) + clientset := createTestPvcsClient(t) usedPvcs, err := retreiveUsedPvcs(clientset, testNamespace) if err != nil { t.Errorf("Expected no error, got %v", err) @@ -67,7 +74,7 @@ func TestRetreiveUsedPvcs(t *testing.T) { } func TestProcessNamespacePvcs(t *testing.T) { - clientset := createTestPvcs(t) + clientset := createTestPvcsClient(t) usedPvcs, err := processNamespacePvcs(clientset, testNamespace, &FilterOptions{}) if err != nil { t.Errorf("Expected no error, got %v", err) @@ -83,7 +90,7 @@ func TestProcessNamespacePvcs(t *testing.T) { } func TestGetUnusedPvcsStructured(t *testing.T) { - clientset := createTestPvcs(t) + clientset := createTestPvcsClient(t) includeExcludeLists := IncludeExcludeLists{ IncludeListStr: "", From 5da6a76afa011b8dad19312385744c91a611f366 Mon Sep 17 00:00:00 2001 From: Yonah Dissen Date: Wed, 27 Dec 2023 21:33:56 +0200 Subject: [PATCH 2/4] split resource creation from client --- pkg/kor/roles_test.go | 44 +++++++++++++---------- pkg/kor/secrets_test.go | 63 ++++++++++++++++++--------------- pkg/kor/serviceaccounts_test.go | 59 ++++++++++++++++-------------- pkg/kor/services_test.go | 37 +++++++++++-------- pkg/kor/statefulsets_test.go | 37 +++++++++++-------- 5 files changed, 138 insertions(+), 102 deletions(-) diff --git a/pkg/kor/roles_test.go b/pkg/kor/roles_test.go index 3a963029..11ecb809 100644 --- a/pkg/kor/roles_test.go +++ b/pkg/kor/roles_test.go @@ -8,46 +8,54 @@ import ( appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes/scheme" ) -func createTestRoles(t *testing.T) *fake.Clientset { - clientset := fake.NewSimpleClientset() - - _, 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) - } +func createTestRoles(clientset *fake.Clientset, t *testing.T) *fake.Clientset { role1 := CreateTestRole(testNamespace, "test-role1") role2 := CreateTestRole(testNamespace, "test-role2") - _, err = clientset.RbacV1().Roles(testNamespace).Create(context.TODO(), role1, v1.CreateOptions{}) + _, err := clientset.RbacV1().Roles(testNamespace).Create(context.TODO(), role1, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "Role", err) } - _, err = clientset.RbacV1().Roles(testNamespace).Create(context.TODO(), role2, v1.CreateOptions{}) + _, err = clientset.RbacV1().Roles(testNamespace).Create(context.TODO(), role2, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "Role", err) } testRoleRef := CreateTestRoleRef("test-role1") testRoleBinding := CreateTestRoleBinding(testNamespace, "test-rb", "test-sa", testRoleRef) - _, err = clientset.RbacV1().RoleBindings(testNamespace).Create(context.TODO(), testRoleBinding, v1.CreateOptions{}) + _, err = clientset.RbacV1().RoleBindings(testNamespace).Create(context.TODO(), testRoleBinding, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "Role", err) } return clientset } + +func createTestRolesClient(t *testing.T) *fake.Clientset { + clientset := fake.NewSimpleClientset() + + _, err := clientset.CoreV1().Namespaces().Create(context.TODO(), &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{Name: testNamespace}, + }, metav1.CreateOptions{}) + + if err != nil { + t.Fatalf("Error creating namespace %s: %v", testNamespace, err) + } + + createTestRoles(clientset, t) + + return clientset +} + func TestRetrieveUsedRoles(t *testing.T) { - clientset := createTestRoles(t) + clientset := createTestRolesClient(t) usedRoles, err := retrieveUsedRoles(clientset, testNamespace, &FilterOptions{}) if err != nil { @@ -64,7 +72,7 @@ func TestRetrieveUsedRoles(t *testing.T) { } func TestRetrieveRoleNames(t *testing.T) { - clientset := createTestRoles(t) + clientset := createTestRolesClient(t) allRoles, err := retrieveRoleNames(clientset, testNamespace) if err != nil { t.Errorf("Expected no error, got %v", err) @@ -76,7 +84,7 @@ func TestRetrieveRoleNames(t *testing.T) { } func TestProcessNamespaceRoles(t *testing.T) { - clientset := createTestRoles(t) + clientset := createTestRolesClient(t) unusedRoles, err := processNamespaceRoles(clientset, testNamespace, &FilterOptions{}) if err != nil { @@ -93,7 +101,7 @@ func TestProcessNamespaceRoles(t *testing.T) { } func TestGetUnusedRolesStructured(t *testing.T) { - clientset := createTestRoles(t) + clientset := createTestRolesClient(t) includeExcludeLists := IncludeExcludeLists{ IncludeListStr: "", diff --git a/pkg/kor/secrets_test.go b/pkg/kor/secrets_test.go index 1814ba4b..2e96be83 100644 --- a/pkg/kor/secrets_test.go +++ b/pkg/kor/secrets_test.go @@ -8,22 +8,13 @@ import ( appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes/scheme" ) -func createTestSecrets(t *testing.T) *fake.Clientset { - clientset := fake.NewSimpleClientset() - - _, 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) - } +func createTestSecrets(clientset *fake.Clientset, t *testing.T) *fake.Clientset { secret1 := CreateTestSecret(testNamespace, "test-secret1") secret2 := CreateTestSecret(testNamespace, "test-secret2") @@ -70,47 +61,47 @@ func createTestSecrets(t *testing.T) *fake.Clientset { {Name: secret2.ObjectMeta.Name}, } - _, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), pod1, v1.CreateOptions{}) + _, err := clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), pod1, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake pod: %v", err) } - _, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), pod2, v1.CreateOptions{}) + _, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), pod2, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake pod: %v", err) } - _, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), pod3, v1.CreateOptions{}) + _, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), pod3, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake pod: %v", err) } - _, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), pod4, v1.CreateOptions{}) + _, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), pod4, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake pod: %v", err) } - _, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), pod5, v1.CreateOptions{}) + _, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), pod5, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake pod: %v", err) } - _, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), pod6, v1.CreateOptions{}) + _, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), pod6, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake pod: %v", err) } - _, err = clientset.CoreV1().Secrets(testNamespace).Create(context.TODO(), secret1, v1.CreateOptions{}) + _, err = clientset.CoreV1().Secrets(testNamespace).Create(context.TODO(), secret1, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "Secret", err) } - _, err = clientset.CoreV1().Secrets(testNamespace).Create(context.TODO(), secret2, v1.CreateOptions{}) + _, err = clientset.CoreV1().Secrets(testNamespace).Create(context.TODO(), secret2, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "Secret", err) } - _, err = clientset.CoreV1().Secrets(testNamespace).Create(context.TODO(), secret3, v1.CreateOptions{}) + _, err = clientset.CoreV1().Secrets(testNamespace).Create(context.TODO(), secret3, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "Secret", err) } @@ -118,6 +109,22 @@ func createTestSecrets(t *testing.T) *fake.Clientset { return clientset } +func createTestSecretsClient(t *testing.T) *fake.Clientset { + clientset := fake.NewSimpleClientset() + + _, err := clientset.CoreV1().Namespaces().Create(context.TODO(), &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{Name: testNamespace}, + }, metav1.CreateOptions{}) + + if err != nil { + t.Fatalf("Error creating namespace %s: %v", testNamespace, err) + } + + createTestSecrets(clientset, t) + + return clientset +} + func TestRetrieveIngressTLS(t *testing.T) { clientset := fake.NewSimpleClientset() @@ -125,17 +132,17 @@ func TestRetrieveIngressTLS(t *testing.T) { secret1 := CreateTestSecret(testNamespace, "test-secret1") secret2 := CreateTestSecret(testNamespace, "test-secret2") - _, err := clientset.NetworkingV1().Ingresses(testNamespace).Create(context.TODO(), ingress1, v1.CreateOptions{}) + _, err := clientset.NetworkingV1().Ingresses(testNamespace).Create(context.TODO(), ingress1, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "Ingress", err) } - _, err = clientset.CoreV1().Secrets(testNamespace).Create(context.TODO(), secret1, v1.CreateOptions{}) + _, err = clientset.CoreV1().Secrets(testNamespace).Create(context.TODO(), secret1, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "Secret", err) } - _, err = clientset.CoreV1().Secrets(testNamespace).Create(context.TODO(), secret2, v1.CreateOptions{}) + _, err = clientset.CoreV1().Secrets(testNamespace).Create(context.TODO(), secret2, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "Secret", err) } @@ -156,7 +163,7 @@ func TestRetrieveIngressTLS(t *testing.T) { } func TestRetrieveUsedSecret(t *testing.T) { - clientset := createTestSecrets(t) + clientset := createTestSecretsClient(t) envSecrets, envSecrets2, volumeSecrets, initContainerEnvSecrets, pullSecrets, _, err := retrieveUsedSecret(clientset, testNamespace) if err != nil { @@ -196,12 +203,12 @@ func TestRetrieveSecretNames(t *testing.T) { secret1 := CreateTestSecret(testNamespace, "secret-1") secret2 := CreateTestSecret(testNamespace, "secret-2") - _, err := clientset.CoreV1().Secrets(testNamespace).Create(context.TODO(), secret1, v1.CreateOptions{}) + _, err := clientset.CoreV1().Secrets(testNamespace).Create(context.TODO(), secret1, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake secret: %v", err) } - _, err = clientset.CoreV1().Secrets(testNamespace).Create(context.TODO(), secret2, v1.CreateOptions{}) + _, err = clientset.CoreV1().Secrets(testNamespace).Create(context.TODO(), secret2, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake secret: %v", err) } @@ -219,7 +226,7 @@ func TestRetrieveSecretNames(t *testing.T) { } func TestProcessNamespaceSecret(t *testing.T) { - clientset := createTestSecrets(t) + clientset := createTestSecretsClient(t) unusedSecrets, err := processNamespaceSecret(clientset, testNamespace, &FilterOptions{}) if err != nil { @@ -237,7 +244,7 @@ func TestProcessNamespaceSecret(t *testing.T) { } func TestGetUnusedSecretsStructured(t *testing.T) { - clientset := createTestSecrets(t) + clientset := createTestSecretsClient(t) includeExcludeLists := IncludeExcludeLists{ IncludeListStr: "", diff --git a/pkg/kor/serviceaccounts_test.go b/pkg/kor/serviceaccounts_test.go index 91c65425..ed0c857a 100644 --- a/pkg/kor/serviceaccounts_test.go +++ b/pkg/kor/serviceaccounts_test.go @@ -8,43 +8,50 @@ import ( appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes/scheme" ) -func createTestServiceAccounts(t *testing.T) *fake.Clientset { - - clientset := fake.NewSimpleClientset() - - _, 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) - } +func createTestServiceAccounts(clientset *fake.Clientset, t *testing.T) *fake.Clientset { sa1 := CreateTestServiceAccount(testNamespace, "test-sa1") sa2 := CreateTestServiceAccount(testNamespace, "test-sa2") - _, err = clientset.CoreV1().ServiceAccounts(testNamespace).Create(context.TODO(), sa1, v1.CreateOptions{}) + _, err := clientset.CoreV1().ServiceAccounts(testNamespace).Create(context.TODO(), sa1, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "ServiceAccount", err) } - _, err = clientset.CoreV1().ServiceAccounts(testNamespace).Create(context.TODO(), sa2, v1.CreateOptions{}) + _, err = clientset.CoreV1().ServiceAccounts(testNamespace).Create(context.TODO(), sa2, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "ServiceAccount", err) } return clientset } + +func createTestServiceAccountsClient(t *testing.T) *fake.Clientset { + clientset := fake.NewSimpleClientset() + + _, err := clientset.CoreV1().Namespaces().Create(context.TODO(), &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{Name: testNamespace}, + }, metav1.CreateOptions{}) + + if err != nil { + t.Fatalf("Error creating namespace %s: %v", testNamespace, err) + } + + createTestServiceAccounts(clientset, t) + + return clientset +} + func TestGetServiceAccountsFromClusterRoleBindings(t *testing.T) { - clientset := createTestServiceAccounts(t) + clientset := createTestServiceAccountsClient(t) clusterRoleBinding1 := CreateTestClusterRoleBinding(testNamespace, "test-crb1", "test-sa1") - _, err := clientset.RbacV1().ClusterRoleBindings().Create(context.TODO(), clusterRoleBinding1, v1.CreateOptions{}) + _, err := clientset.RbacV1().ClusterRoleBindings().Create(context.TODO(), clusterRoleBinding1, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "clusterRoleBinding", err) } @@ -65,11 +72,11 @@ func TestGetServiceAccountsFromClusterRoleBindings(t *testing.T) { } func TestGetServiceAccountsFromRoleBindings(t *testing.T) { - clientset := createTestServiceAccounts(t) + clientset := createTestServiceAccountsClient(t) testRoleRef := CreateTestRoleRef("test-role") roleBinding1 := CreateTestRoleBinding(testNamespace, "test-crb1", "test-sa1", testRoleRef) - _, err := clientset.RbacV1().RoleBindings(testNamespace).Create(context.TODO(), roleBinding1, v1.CreateOptions{}) + _, err := clientset.RbacV1().RoleBindings(testNamespace).Create(context.TODO(), roleBinding1, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "roleBinding", err) } @@ -90,13 +97,13 @@ func TestGetServiceAccountsFromRoleBindings(t *testing.T) { func TestRetrieveUsedSA(t *testing.T) { var volumeList []corev1.Volume - clientset := createTestServiceAccounts(t) + clientset := createTestServiceAccountsClient(t) testVolume := CreateTestVolume("test-volume1", "test-pvc") volumeList = append(volumeList, *testVolume) podWithSA := CreateTestPod(testNamespace, "test-pod1", "test-sa1", volumeList) - _, err := clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), podWithSA, v1.CreateOptions{}) + _, err := clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), podWithSA, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "Pod", err) } @@ -116,7 +123,7 @@ func TestRetrieveUsedSA(t *testing.T) { } func TestRetrieveServiceAccountNames(t *testing.T) { - clientset := createTestServiceAccounts(t) + clientset := createTestServiceAccountsClient(t) serviceAccountNames, err := retrieveServiceAccountNames(clientset, testNamespace, &FilterOptions{}) if err != nil { t.Fatalf("Expected no error, got %v", err) @@ -127,7 +134,7 @@ func TestRetrieveServiceAccountNames(t *testing.T) { } func TestProcessNamespaceSA(t *testing.T) { - clientset := createTestServiceAccounts(t) + clientset := createTestServiceAccountsClient(t) var volumeList []corev1.Volume testVolume := CreateTestVolume("test-volume1", "test-pvc") @@ -136,13 +143,13 @@ func TestProcessNamespaceSA(t *testing.T) { testRoleRef := CreateTestRoleRef("test-role") roleBinding1 := CreateTestRoleBinding(testNamespace, "test-crb1", "test-sa1", testRoleRef) - _, err := clientset.RbacV1().RoleBindings(testNamespace).Create(context.TODO(), roleBinding1, v1.CreateOptions{}) + _, err := clientset.RbacV1().RoleBindings(testNamespace).Create(context.TODO(), roleBinding1, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "roleBinding", err) } podWithSA := CreateTestPod(testNamespace, "test-pod1", "test-sa1", volumeList) - _, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), podWithSA, v1.CreateOptions{}) + _, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), podWithSA, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "Pod", err) } @@ -162,10 +169,10 @@ func TestProcessNamespaceSA(t *testing.T) { } func TestGetUnusedServiceAccountsStructured(t *testing.T) { - clientset := createTestServiceAccounts(t) + clientset := createTestServiceAccountsClient(t) clusterRoleBinding1 := CreateTestClusterRoleBinding(testNamespace, "test-crb1", "test-sa1") - _, err := clientset.RbacV1().ClusterRoleBindings().Create(context.TODO(), clusterRoleBinding1, v1.CreateOptions{}) + _, err := clientset.RbacV1().ClusterRoleBindings().Create(context.TODO(), clusterRoleBinding1, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "clusterRoleBinding", err) } diff --git a/pkg/kor/services_test.go b/pkg/kor/services_test.go index e7f3b758..1263bbac 100644 --- a/pkg/kor/services_test.go +++ b/pkg/kor/services_test.go @@ -8,31 +8,22 @@ import ( appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes/scheme" ) -func createTestServices(t *testing.T) *fake.Clientset { - clientset := fake.NewSimpleClientset() - - _, 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) - } +func createTestServices(clientset *fake.Clientset, t *testing.T) *fake.Clientset { endpoint1 := CreateTestEndpoint(testNamespace, "test-endpoint1", 0) endpoint2 := CreateTestEndpoint(testNamespace, "test-endpoint2", 1) - _, err = clientset.CoreV1().Endpoints(testNamespace).Create(context.TODO(), endpoint1, v1.CreateOptions{}) + _, err := clientset.CoreV1().Endpoints(testNamespace).Create(context.TODO(), endpoint1, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake endpoint: %v", err) } - _, err = clientset.CoreV1().Endpoints(testNamespace).Create(context.TODO(), endpoint2, v1.CreateOptions{}) + _, err = clientset.CoreV1().Endpoints(testNamespace).Create(context.TODO(), endpoint2, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake endpoint: %v", err) } @@ -40,8 +31,24 @@ func createTestServices(t *testing.T) *fake.Clientset { return clientset } +func createTestServicesClient(t *testing.T) *fake.Clientset { + clientset := fake.NewSimpleClientset() + + _, err := clientset.CoreV1().Namespaces().Create(context.TODO(), &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{Name: testNamespace}, + }, metav1.CreateOptions{}) + + if err != nil { + t.Fatalf("Error creating namespace %s: %v", testNamespace, err) + } + + createTestServices(clientset, t) + + return clientset +} + func TestGetEndpointsWithoutSubsets(t *testing.T) { - clientset := createTestServices(t) + clientset := createTestServicesClient(t) servicesWithoutEndpoints, err := ProcessNamespaceServices(clientset, testNamespace, &FilterOptions{}) if err != nil { @@ -58,7 +65,7 @@ func TestGetEndpointsWithoutSubsets(t *testing.T) { } func TestGetUnusedServicesStructured(t *testing.T) { - clientset := createTestServices(t) + clientset := createTestServicesClient(t) includeExcludeLists := IncludeExcludeLists{ IncludeListStr: testNamespace, diff --git a/pkg/kor/statefulsets_test.go b/pkg/kor/statefulsets_test.go index c6c1fe21..bc29706f 100644 --- a/pkg/kor/statefulsets_test.go +++ b/pkg/kor/statefulsets_test.go @@ -8,33 +8,24 @@ import ( appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes/scheme" ) -func createTestStatefulSets(t *testing.T) *fake.Clientset { - clientset := fake.NewSimpleClientset() - - _, 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) - } +func createTestStatefulSets(clientset *fake.Clientset, t *testing.T) *fake.Clientset { appLabels := map[string]string{} sts1 := CreateTestStatefulSet(testNamespace, "test-sts1", 0, appLabels) sts2 := CreateTestStatefulSet(testNamespace, "test-sts2", 1, appLabels) - _, err = clientset.AppsV1().StatefulSets(testNamespace).Create(context.TODO(), sts1, v1.CreateOptions{}) + _, err := clientset.AppsV1().StatefulSets(testNamespace).Create(context.TODO(), sts1, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "statefulSet", err) } - _, err = clientset.AppsV1().StatefulSets(testNamespace).Create(context.TODO(), sts2, v1.CreateOptions{}) + _, err = clientset.AppsV1().StatefulSets(testNamespace).Create(context.TODO(), sts2, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "statefulSet", err) } @@ -42,8 +33,24 @@ func createTestStatefulSets(t *testing.T) *fake.Clientset { return clientset } +func createTestStatefulSetsClient(t *testing.T) *fake.Clientset { + clientset := fake.NewSimpleClientset() + + _, err := clientset.CoreV1().Namespaces().Create(context.TODO(), &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{Name: testNamespace}, + }, metav1.CreateOptions{}) + + if err != nil { + t.Fatalf("Error creating namespace %s: %v", testNamespace, err) + } + + createTestStatefulSets(clientset, t) + + return clientset +} + func TestProcessNamespaceStatefulSets(t *testing.T) { - clientset := createTestStatefulSets(t) + clientset := createTestStatefulSetsClient(t) statefulSetsWithoutReplicas, err := ProcessNamespaceStatefulSets(clientset, testNamespace, &FilterOptions{}) if err != nil { @@ -60,7 +67,7 @@ func TestProcessNamespaceStatefulSets(t *testing.T) { } func TestGetUnusedStatefulSetsStructured(t *testing.T) { - clientset := createTestStatefulSets(t) + clientset := createTestStatefulSetsClient(t) includeExcludeLists := IncludeExcludeLists{ IncludeListStr: "", From 8a830c456540c946eebe6e9bc838207742226101 Mon Sep 17 00:00:00 2001 From: Yonah Dissen Date: Wed, 27 Dec 2023 21:40:22 +0200 Subject: [PATCH 3/4] split resource creation from client --- pkg/kor/replicaset_test.go | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/pkg/kor/replicaset_test.go b/pkg/kor/replicaset_test.go index 8e8b2e19..c85aa5fd 100644 --- a/pkg/kor/replicaset_test.go +++ b/pkg/kor/replicaset_test.go @@ -5,7 +5,7 @@ import ( "encoding/json" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes/scheme" @@ -13,17 +13,7 @@ import ( "testing" ) -func createTestReplicaSets(t *testing.T) *fake.Clientset { - clientset := fake.NewSimpleClientset() - - _, 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) - } - +func createTestReplicaSets(clientset *fake.Clientset, t *testing.T) *fake.Clientset { var count1 int32 = 1 replicaSet1 := CreateTestReplicaSet(testNamespace, "test-replicaSet1", &count1, &appsv1.ReplicaSetStatus{ Replicas: count1, @@ -39,20 +29,36 @@ func createTestReplicaSets(t *testing.T) *fake.Clientset { FullyLabeledReplicas: count2, }) - _, err = clientset.AppsV1().ReplicaSets(testNamespace).Create(context.TODO(), replicaSet1, v1.CreateOptions{}) + _, err := clientset.AppsV1().ReplicaSets(testNamespace).Create(context.TODO(), replicaSet1, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake replicaSet: %v", err) } - _, err = clientset.AppsV1().ReplicaSets(testNamespace).Create(context.TODO(), replicaSet2, v1.CreateOptions{}) + _, err = clientset.AppsV1().ReplicaSets(testNamespace).Create(context.TODO(), replicaSet2, metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake replicaSet: %v", err) } return clientset } +func createTestReplicaSetsClient(t *testing.T) *fake.Clientset { + clientset := fake.NewSimpleClientset() + + _, err := clientset.CoreV1().Namespaces().Create(context.TODO(), &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{Name: testNamespace}, + }, metav1.CreateOptions{}) + + if err != nil { + t.Fatalf("Error creating namespace %s: %v", testNamespace, err) + } + + createTestReplicaSets(clientset, t) + + return clientset +} + func TestProcessNamespaceReplicaSets(t *testing.T) { - clientset := createTestReplicaSets(t) + clientset := createTestReplicaSetsClient(t) includeExcludeLists := IncludeExcludeLists{ IncludeListStr: "", From ddd28517755641e7e637f212bd8d7b1998d38fda Mon Sep 17 00:00:00 2001 From: Yonah Dissen Date: Thu, 4 Jan 2024 20:46:32 +0200 Subject: [PATCH 4/4] fix some tests --- pkg/kor/all_test.go | 82 ++++++++++++++++++++++++++++++++++++++ pkg/kor/configmaps_test.go | 8 ++-- pkg/kor/pdbs_test.go | 4 +- pkg/kor/secrets_test.go | 12 +++--- 4 files changed, 94 insertions(+), 12 deletions(-) create mode 100644 pkg/kor/all_test.go diff --git a/pkg/kor/all_test.go b/pkg/kor/all_test.go new file mode 100644 index 00000000..7211160f --- /dev/null +++ b/pkg/kor/all_test.go @@ -0,0 +1,82 @@ +package kor + +/* +import ( + "context" + "encoding/json" + "reflect" + "testing" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes/fake" +) + +func createTestAllClient(t *testing.T) *fake.Clientset { + clientset := fake.NewSimpleClientset() + + _, err := clientset.CoreV1().Namespaces().Create(context.TODO(), &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{Name: testNamespace}, + }, metav1.CreateOptions{}) + + if err != nil { + t.Fatalf("Error creating namespace %s: %v", testNamespace, err) + } + + createTestConfigmaps(clientset, t) + createTestDeployments(clientset, t) + createTestHpas(clientset, t) + createTestIngresses(clientset, t) + createTestJobs(clientset, t) + createTestPdbs(clientset, t) + createTestPods(clientset, t) + createTestPvs(clientset, t) + createTestPvcs(clientset, t) + createTestReplicaSets(clientset, t) + createTestRoles(clientset, t) + createTestSecrets(clientset, t) + createTestServiceAccounts(clientset, t) + createTestServices(clientset, t) + createTestStatefulSets(clientset, t) + + return clientset +} + +func TestGetUnusedAll(t *testing.T) { + clientset := createTestAllClient(t) + + includeExcludeLists := IncludeExcludeLists{ + IncludeListStr: "", + ExcludeListStr: "", + } + + opts := Opts{ + WebhookURL: "", + Channel: "", + Token: "", + DeleteFlag: false, + NoInteractive: true, + } + + output, err := GetUnusedAll(includeExcludeLists, &FilterOptions{}, clientset, "", "", "json", opts) + if err != nil { + t.Fatalf("Error calling GetUnusedAll: %v", err) + } + + expectedOutput := map[string]map[string][]string{ + testNamespace: { + "ConfigMap": {"configmap-3"}, + }, + } + + 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 actual output") + } +} + +*/ diff --git a/pkg/kor/configmaps_test.go b/pkg/kor/configmaps_test.go index 6fbb6e03..5ac35b07 100644 --- a/pkg/kor/configmaps_test.go +++ b/pkg/kor/configmaps_test.go @@ -20,11 +20,11 @@ func createTestConfigmaps(clientset *fake.Clientset, t *testing.T) { configmap2 := CreateTestConfigmap(testNamespace, "configmap-2") configmap3 := CreateTestConfigmap(testNamespace, "configmap-3") - pod1 := CreateTestPod(testNamespace, "pod-1", "", []corev1.Volume{ + pod1 := CreateTestPod(testNamespace, "cm-pod-1", "", []corev1.Volume{ {Name: "vol-1", VolumeSource: corev1.VolumeSource{ConfigMap: &corev1.ConfigMapVolumeSource{LocalObjectReference: corev1.LocalObjectReference{Name: configmap1.ObjectMeta.Name}}}}, }) - pod2 := CreateTestPod(testNamespace, "pod-2", "", nil) + pod2 := CreateTestPod(testNamespace, "cm-pod-2", "", nil) pod2.Spec.Containers = []corev1.Container{ { Env: []corev1.EnvVar{ @@ -33,7 +33,7 @@ func createTestConfigmaps(clientset *fake.Clientset, t *testing.T) { }, } - pod3 := CreateTestPod(testNamespace, "pod-3", "", nil) + pod3 := CreateTestPod(testNamespace, "cm-pod-3", "", nil) pod3.Spec.Containers = []corev1.Container{ { EnvFrom: []corev1.EnvFromSource{ @@ -42,7 +42,7 @@ func createTestConfigmaps(clientset *fake.Clientset, t *testing.T) { }, } - pod4 := CreateTestPod(testNamespace, "pod-4", "", nil) + pod4 := CreateTestPod(testNamespace, "cm-pod-4", "", nil) pod4.Spec.InitContainers = []corev1.Container{ { Env: []corev1.EnvVar{ diff --git a/pkg/kor/pdbs_test.go b/pkg/kor/pdbs_test.go index a398b6d9..5c5de176 100644 --- a/pkg/kor/pdbs_test.go +++ b/pkg/kor/pdbs_test.go @@ -36,13 +36,13 @@ func createTestPdbs(clientset *fake.Clientset, t *testing.T) *fake.Clientset { t.Fatalf("Error creating fake %s: %v", "Pdb", err) } - deployment1 := CreateTestDeployment(testNamespace, "test-deployment2", 1, appLabels1) + deployment1 := CreateTestDeployment(testNamespace, "pdb-test-deploy2", 1, appLabels1) _, err = clientset.AppsV1().Deployments(testNamespace).Create(context.TODO(), deployment1, v1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake deployment: %v", err) } - sts1 := CreateTestStatefulSet(testNamespace, "test-sts2", 1, appLabels1) + sts1 := CreateTestStatefulSet(testNamespace, "pdb-test-sts2", 1, appLabels1) _, err = clientset.AppsV1().StatefulSets(testNamespace).Create(context.TODO(), sts1, v1.CreateOptions{}) if err != nil { t.Fatalf("Error creating fake %s: %v", "StatefulSet", err) diff --git a/pkg/kor/secrets_test.go b/pkg/kor/secrets_test.go index 2e96be83..58064c73 100644 --- a/pkg/kor/secrets_test.go +++ b/pkg/kor/secrets_test.go @@ -20,15 +20,15 @@ func createTestSecrets(clientset *fake.Clientset, t *testing.T) *fake.Clientset secret2 := CreateTestSecret(testNamespace, "test-secret2") secret3 := CreateTestSecret(testNamespace, "test-secret3") - pod1 := CreateTestPod(testNamespace, "pod-1", "", []corev1.Volume{ + pod1 := CreateTestPod(testNamespace, "sec-pod-1", "", []corev1.Volume{ {Name: "vol-1", VolumeSource: corev1.VolumeSource{Secret: &corev1.SecretVolumeSource{SecretName: "test-secret1"}}}, }) - pod2 := CreateTestPod(testNamespace, "pod-2", "", []corev1.Volume{ + pod2 := CreateTestPod(testNamespace, "sec-pod-2", "", []corev1.Volume{ {Name: "vol-2", VolumeSource: corev1.VolumeSource{Secret: &corev1.SecretVolumeSource{SecretName: "test-secret2"}}}, }) - pod3 := CreateTestPod(testNamespace, "pod-3", "", nil) + pod3 := CreateTestPod(testNamespace, "sec-pod-3", "", nil) pod3.Spec.Containers = []corev1.Container{ { Env: []corev1.EnvVar{ @@ -37,7 +37,7 @@ func createTestSecrets(clientset *fake.Clientset, t *testing.T) *fake.Clientset }, } - pod4 := CreateTestPod(testNamespace, "pod-4", "", nil) + pod4 := CreateTestPod(testNamespace, "sec-pod-4", "", nil) pod4.Spec.Containers = []corev1.Container{ { EnvFrom: []corev1.EnvFromSource{ @@ -46,7 +46,7 @@ func createTestSecrets(clientset *fake.Clientset, t *testing.T) *fake.Clientset }, } - pod5 := CreateTestPod(testNamespace, "pod-5", "", nil) + pod5 := CreateTestPod(testNamespace, "sec-pod-5", "", nil) pod5.Spec.InitContainers = []corev1.Container{ { Env: []corev1.EnvVar{ @@ -55,7 +55,7 @@ func createTestSecrets(clientset *fake.Clientset, t *testing.T) *fake.Clientset }, } - pod6 := CreateTestPod(testNamespace, "pod-6", "", nil) + pod6 := CreateTestPod(testNamespace, "sec-pod-6", "", nil) pod6.Spec.ImagePullSecrets = []corev1.LocalObjectReference{ {Name: secret1.ObjectMeta.Name}, {Name: secret2.ObjectMeta.Name},