Skip to content

Commit

Permalink
add test structured
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonah Dissen committed Sep 19, 2023
1 parent ec470f6 commit 36b289c
Show file tree
Hide file tree
Showing 15 changed files with 311 additions and 40 deletions.
4 changes: 2 additions & 2 deletions pkg/kor/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func getUnusedPdbs(clientset kubernetes.Interface, namespace string) ResourceDif
return namespacePdbDiff
}

func GetUnusedAll(includeExcludeLists IncludeExcludeLists, clientset *kubernetes.Clientset) {
func GetUnusedAll(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface) {
namespaces := SetNamespaceList(includeExcludeLists, clientset)
for _, namespace := range namespaces {
var allDiffs []ResourceDiff
Expand Down Expand Up @@ -150,7 +150,7 @@ func GetUnusedAll(includeExcludeLists IncludeExcludeLists, clientset *kubernetes
}
}

func GetUnusedAllStructured(includeExcludeLists IncludeExcludeLists, clientset *kubernetes.Clientset, outputFormat string) (string, error) {
func GetUnusedAllStructured(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, outputFormat string) (string, error) {
namespaces := SetNamespaceList(includeExcludeLists, clientset)

// Create the JSON response object
Expand Down
4 changes: 2 additions & 2 deletions pkg/kor/hpas.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func processNamespaceHpas(clientset kubernetes.Interface, namespace string) ([]s
return unusedHpas, nil
}

func GetUnusedHpas(includeExcludeLists IncludeExcludeLists, clientset *kubernetes.Clientset) {
func GetUnusedHpas(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface) {
namespaces := SetNamespaceList(includeExcludeLists, clientset)

for _, namespace := range namespaces {
Expand All @@ -95,7 +95,7 @@ func GetUnusedHpas(includeExcludeLists IncludeExcludeLists, clientset *kubernete

}

func GetUnusedHpasStructured(includeExcludeLists IncludeExcludeLists, clientset *kubernetes.Clientset, outputFormat string) (string, error) {
func GetUnusedHpasStructured(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, outputFormat string) (string, error) {
namespaces := SetNamespaceList(includeExcludeLists, clientset)
response := make(map[string]map[string][]string)

Expand Down
53 changes: 48 additions & 5 deletions pkg/kor/hpas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,37 @@ package kor

import (
"context"
"encoding/json"
"reflect"
"testing"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/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 TestExtractUnusedHpas(t *testing.T) {
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)
}

deploymentName := "test-deployment"
appLabels := map[string]string{}

// Create a Deployment without replicas for testing
deployment1 := CreateTestDeployment(testNamespace, deploymentName, 1, appLabels)
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)
}
Expand All @@ -36,7 +47,12 @@ func TestExtractUnusedHpas(t *testing.T) {
t.Fatalf("Error creating fake Hpa: %v", err)
}

// Test the getDeploymentsWithoutReplicas function
return clientset
}

func TestExtractUnusedHpas(t *testing.T) {
clientset := createTestHpas(t)

unusedHpas, err := extractUnusedHpas(clientset, testNamespace)
if err != nil {
t.Errorf("Expected no error, got %v", err)
Expand All @@ -51,7 +67,34 @@ func TestExtractUnusedHpas(t *testing.T) {
}
}

// Initialize the Kubernetes API scheme
func TestGetUnusedHpasStructured(t *testing.T) {
clientset := createTestHpas(t)

includeExcludeLists := IncludeExcludeLists{
IncludeListStr: "",
ExcludeListStr: "",
}

output, err := GetUnusedHpasStructured(includeExcludeLists, clientset, "json")
if err != nil {
t.Fatalf("Error calling GetUnusedHpasStructured: %v", err)
}

expectedOutput := map[string]map[string][]string{
testNamespace: {
"Hpa": {"test-hpa2"},
},
}

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")
}
}
func init() {
scheme.Scheme = runtime.NewScheme()
_ = appsv1.AddToScheme(scheme.Scheme)
Expand Down
4 changes: 2 additions & 2 deletions pkg/kor/ingresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func processNamespaceIngresses(clientset kubernetes.Interface, namespace string)

}

func GetUnusedIngresses(includeExcludeLists IncludeExcludeLists, clientset *kubernetes.Clientset) {
func GetUnusedIngresses(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface) {
namespaces := SetNamespaceList(includeExcludeLists, clientset)

for _, namespace := range namespaces {
Expand All @@ -103,7 +103,7 @@ func GetUnusedIngresses(includeExcludeLists IncludeExcludeLists, clientset *kube
}
}

func GetUnusedIngressesStructured(includeExcludeLists IncludeExcludeLists, clientset *kubernetes.Clientset, outputFormat string) (string, error) {
func GetUnusedIngressesStructured(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, outputFormat string) (string, error) {
namespaces := SetNamespaceList(includeExcludeLists, clientset)
response := make(map[string]map[string][]string)

Expand Down
50 changes: 44 additions & 6 deletions pkg/kor/ingresses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@ package kor

import (
"context"
"encoding/json"
"reflect"
"testing"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/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 TestRetrieveUsedIngress(t *testing.T) {
func createTestIngresses(t *testing.T) *fake.Clientset {
clientset := fake.NewSimpleClientset()

// Create a fake Ingress with multiple rules and backends for testing
_, err := clientset.CoreV1().Namespaces().Create(context.TODO(), &corev1.Namespace{
ObjectMeta: v1.ObjectMeta{Name: testNamespace},
}, v1.CreateOptions{})

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")

// Create the Ingresses in the fake clientset
_, 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)
}
Expand All @@ -33,7 +38,12 @@ func TestRetrieveUsedIngress(t *testing.T) {
t.Fatalf("Error creating fake %s: %v", "Ingress", err)
}

// Test the retrieveUsedIngress function
return clientset
}

func TestRetrieveUsedIngress(t *testing.T) {
clientset := createTestIngresses(t)

usedIngresses, err := retrieveUsedIngress(clientset, testNamespace)
if err != nil {
t.Errorf("Expected no error, got %v", err)
Expand All @@ -57,7 +67,35 @@ func contains(slice []string, item string) bool {
return false
}

// Initialize the Kubernetes API scheme
func TestGetUnusedIngressesStructured(t *testing.T) {
clientset := createTestIngresses(t)

includeExcludeLists := IncludeExcludeLists{
IncludeListStr: "",
ExcludeListStr: "",
}

output, err := GetUnusedIngressesStructured(includeExcludeLists, clientset, "json")
if err != nil {
t.Fatalf("Error calling GetUnusedIngressesStructured: %v", err)
}

expectedOutput := map[string]map[string][]string{
testNamespace: {
"Ingresses": {"test-ingress-2"},
},
}

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")
}
}

func init() {
scheme.Scheme = runtime.NewScheme()
_ = appsv1.AddToScheme(scheme.Scheme)
Expand Down
4 changes: 2 additions & 2 deletions pkg/kor/pdbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func processNamespacePdbs(clientset kubernetes.Interface, namespace string) ([]s
return unusedPdbs, nil
}

func GetUnusedPdbs(includeExcludeLists IncludeExcludeLists, clientset *kubernetes.Clientset) {
func GetUnusedPdbs(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface) {
namespaces := SetNamespaceList(includeExcludeLists, clientset)

for _, namespace := range namespaces {
Expand All @@ -63,7 +63,7 @@ func GetUnusedPdbs(includeExcludeLists IncludeExcludeLists, clientset *kubernete
}
}

func GetUnusedPdbsStructured(includeExcludeLists IncludeExcludeLists, clientset *kubernetes.Clientset, outputFormat string) (string, error) {
func GetUnusedPdbsStructured(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, outputFormat string) (string, error) {
namespaces := SetNamespaceList(includeExcludeLists, clientset)
response := make(map[string]map[string][]string)

Expand Down
47 changes: 45 additions & 2 deletions pkg/kor/pdbs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ 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"
)

func TestProcessNamespacePdbs(t *testing.T) {
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{})

appLabels1 := map[string]string{
"app": "my-app",
}
Expand All @@ -18,7 +26,7 @@ func TestProcessNamespacePdbs(t *testing.T) {
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)
}
Expand All @@ -45,6 +53,12 @@ func TestProcessNamespacePdbs(t *testing.T) {
t.Fatalf("Error creating fake %s: %v", "StatefulSet", err)
}

return clientset
}

func TestProcessNamespacePdbs(t *testing.T) {
clientset := createTestPdbs(t)

unusedPdbs, err := processNamespacePdbs(clientset, testNamespace)
if err != nil {
t.Errorf("Expected no error, got %v", err)
Expand All @@ -58,3 +72,32 @@ func TestProcessNamespacePdbs(t *testing.T) {
t.Errorf("Expected 'test-pdb3', got %s", unusedPdbs[0])
}
}

func TestGetUnusedPdbsStructured(t *testing.T) {
clientset := createTestPdbs(t)

includeExcludeLists := IncludeExcludeLists{
IncludeListStr: "",
ExcludeListStr: "",
}

output, err := GetUnusedPdbsStructured(includeExcludeLists, clientset, "json")
if err != nil {
t.Fatalf("Error calling GetUnusedPdbsStructured: %v", err)
}

expectedOutput := map[string]map[string][]string{
testNamespace: {
"Pdb": {"test-pdb3"},
},
}

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")
}
}
4 changes: 2 additions & 2 deletions pkg/kor/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func processNamespacePvcs(clientset kubernetes.Interface, namespace string) ([]s
return diff, nil
}

func GetUnusedPvcs(includeExcludeLists IncludeExcludeLists, clientset *kubernetes.Clientset) {
func GetUnusedPvcs(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface) {
namespaces := SetNamespaceList(includeExcludeLists, clientset)

for _, namespace := range namespaces {
Expand All @@ -69,7 +69,7 @@ func GetUnusedPvcs(includeExcludeLists IncludeExcludeLists, clientset *kubernete

}

func GetUnusedPvcsStructured(includeExcludeLists IncludeExcludeLists, clientset *kubernetes.Clientset, outputFormat string) (string, error) {
func GetUnusedPvcsStructured(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, outputFormat string) (string, error) {
namespaces := SetNamespaceList(includeExcludeLists, clientset)
response := make(map[string]map[string][]string)

Expand Down
Loading

0 comments on commit 36b289c

Please sign in to comment.