Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: added test case for integration and utils package #352

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions pkg/integrations/integrations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package integrations

import (
"context"
"testing"

"github.com/k8sgpt-ai/k8sgpt-operator/api/v1alpha1"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func Test_NewIntegrations(t *testing.T) {
// Create a fake client
cl := fake.NewClientBuilder().Build()

// Call the function with the fake client and a context
integrations, err := NewIntegrations(cl, context.Background())

// Check that the function returned a non-nil Integrations object and no error
assert.NotNil(t, integrations)
assert.NoError(t, err)
}

func Test_BackstageLabel(t *testing.T) {
testKind := &unstructured.Unstructured{}
testKind.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "test-kind"})
testKind.SetName("test")
testKind.SetNamespace("default")
testKind.SetLabels(map[string]string{backstageLabelKey: "test-value"})

// Create a fake client
cl := fake.NewClientBuilder().WithObjects(testKind).Build()
// Create a fake RESTMapper
restMapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{})
restMapper.Add(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "test-kind"}, meta.RESTScopeNamespace)

// Create an Integrations object with the fake client and a context
i := &Integrations{
client: cl,
ctx: context.Background(),
restMapper: restMapper,
}

testCases := []struct {
name string
result v1alpha1.ResultSpec
expected map[string]string
}{
{
name: "non-existing kind",
result: v1alpha1.ResultSpec{
Name: "default/test",
},
expected: map[string]string{},
},
{
name: "existing kind and name with slash",
result: v1alpha1.ResultSpec{
Name: "",
Kind: "test-kind",
},
expected: map[string]string{},
},
{
name: "existing kind and name with slash",
result: v1alpha1.ResultSpec{
Name: "default/test",
Kind: "test-kind",
},
expected: map[string]string{backstageLabelKey: "test-value"},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
labels := i.BackstageLabel(tc.result)
assert.Equal(t, tc.expected, labels)
})
}
}
func Test_BackstageLabelNotExist(t *testing.T) {
testKind := &unstructured.Unstructured{}
testKind.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "test-kind"})
testKind.SetName("test")
testKind.SetNamespace("default")

// Create a fake client
cl := fake.NewClientBuilder().WithObjects(testKind).Build()
// Create a fake RESTMapper
restMapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{})
restMapper.Add(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "test-kind"}, meta.RESTScopeNamespace)

// Create an Integrations object with the fake client and a context
i := &Integrations{
client: cl,
ctx: context.Background(),
restMapper: restMapper,
}

labels := i.BackstageLabel(v1alpha1.ResultSpec{
Name: "default/test",
Kind: "test-kind",
})
assert.Equal(t, map[string]string{"backstage.io/kubernetes-id": ""}, labels)
}
62 changes: 62 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package utils

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_ContainsString(t *testing.T) {
tests := []struct {
name string
slice []string
s string
expected bool
}{
{
name: "Contains string",
slice: []string{"apple", "banana", "cherry"},
s: "banana",
expected: true,
},
{
name: "Does not contain string",
slice: []string{"apple", "banana", "cherry"},
s: "grape",
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ContainsString(tt.slice, tt.s)
assert.Equal(t, tt.expected, result)
})
}
}

func Test_PtrBool(t *testing.T) {
tests := []struct {
name string
b bool
expected *bool
}{
{
name: "Pointer to true",
b: true,
expected: PtrBool(true),
},
{
name: "Pointer to false",
b: false,
expected: PtrBool(false),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := PtrBool(tt.b)
assert.Equal(t, tt.expected, result)
})
}
}
Loading