From 62b7db814933bbefd698aa7a3594e1ae1fcf504d Mon Sep 17 00:00:00 2001 From: Gabe Alford Date: Fri, 9 Feb 2024 13:29:21 -0700 Subject: [PATCH] feat: add some utils tests --- internal/controller/common/utils.go | 2 +- internal/controller/common/utils_test.go | 68 ++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/internal/controller/common/utils.go b/internal/controller/common/utils.go index 71b6839c..9a9782fb 100644 --- a/internal/controller/common/utils.go +++ b/internal/controller/common/utils.go @@ -227,7 +227,7 @@ func GetNamespaceNamesSort(ctx context.Context, cli client.Client) ([]string, er ns := &corev1.NamespaceList{} err := cli.List(ctx, ns) if err != nil { - return nil, err + return []string{}, err } for _, i := range ns.Items { diff --git a/internal/controller/common/utils_test.go b/internal/controller/common/utils_test.go index fd13e042..7e2425d2 100644 --- a/internal/controller/common/utils_test.go +++ b/internal/controller/common/utils_test.go @@ -1,11 +1,79 @@ package common import ( + "context" "testing" "github.com/google/go-cmp/cmp" + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" ) +func getFakeClient(initObjs ...client.Object) (client.WithWatch, error) { + scheme := runtime.NewScheme() + if err := corev1.AddToScheme(scheme); err != nil { + return nil, err + } + if err := appsv1.AddToScheme(scheme); err != nil { + return nil, err + } + // ... + return fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjs...).Build(), nil +} + +func TestGetDeployment(t *testing.T) { + ctx := context.Background() + + fakeClient, err := getFakeClient() + if err != nil { + t.Errorf("getFakeClient() error = %v", err) + } + + testLabel := map[string]string{"testLabel": "test"} + + fakeClient.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test-namespace"}}) + fakeClient.Create(ctx, &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "test-deployment", Namespace: "test-namespace", Labels: testLabel}}) + + want := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "test-deployment", Namespace: "test-namespace", Labels: testLabel, ResourceVersion: "1"}} + got, err := GetDeployment(fakeClient, ctx, "test-namespace", testLabel) + if err != nil { + t.Errorf("GetDeployment() error = %v", err) + } + + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("GetDeployment() mismatch (-want +got):\n%s", diff) + } +} + +func TestGetNamespaceNamesSort(t *testing.T) { + ctx := context.Background() + + fakeClient, err := getFakeClient() + if err != nil { + t.Errorf("getFakeClient() error = %v", err) + } + + fakeClient.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test2"}}) + fakeClient.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "defaultz"}}) + fakeClient.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "kube-systemz"}}) + fakeClient.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "openshift"}}) + fakeClient.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "falcon-system"}}) + + want := []string{"falcon-system", "openshift"} + got, err := GetNamespaceNamesSort(ctx, fakeClient) + if err != nil { + t.Errorf("GetNamespaceNamesSort() error = %v", err) + } + + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("getNamespaceNamesSort() mismatch (-want +got):\n%s", diff) + } +} + func TestOLogMessage(t *testing.T) { want := "test.test" got := oLogMessage("test", "test")