Skip to content

Commit

Permalink
feat: add some utils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
redhatrises committed Feb 9, 2024
1 parent ecb5f5d commit 62b7db8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/controller/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
68 changes: 68 additions & 0 deletions internal/controller/common/utils_test.go
Original file line number Diff line number Diff line change
@@ -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"}})

Check failure on line 38 in internal/controller/common/utils_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest, 1.20.x)

Error return value of `fakeClient.Create` is not checked (errcheck)
fakeClient.Create(ctx, &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "test-deployment", Namespace: "test-namespace", Labels: testLabel}})

Check failure on line 39 in internal/controller/common/utils_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest, 1.20.x)

Error return value of `fakeClient.Create` is not checked (errcheck)

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

Check failure on line 60 in internal/controller/common/utils_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest, 1.20.x)

Error return value of `fakeClient.Create` is not checked (errcheck)
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")
Expand Down

0 comments on commit 62b7db8

Please sign in to comment.