yonahd / kor

A Golang Tool to discover unused Kubernetes Resources
MIT License
1.04k stars 96 forks source link

Add Test for GetUnusedAll functionality #165

Open yonahd opened 12 months ago

yonahd commented 12 months ago

In order to properly support testing this we need to refactor the tests in the rest of the resources by separating creating the clientset from the resource creation e.g. deployments

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{})
    if err != nil {
        t.Fatalf("Error creating fake deployment: %v", err)
    }

    _, err = clientset.AppsV1().Deployments(testNamespace).Create(context.TODO(), deployment2, v1.CreateOptions{})
    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
}

Once this is done the all test can reuse createTestDeployments(and the same for the rest of the resources)