-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
1 changed file
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
|
||
} |