Skip to content

Commit

Permalink
Suit testcases for k8sGPT CRDs (#48)
Browse files Browse the repository at this point in the history
Signed-off-by: Aisuko <urakiny@gmail.com>
Co-authored-by: Alex Jones <alexsimonjones@gmail.com>
  • Loading branch information
Aisuko and AlexsJones committed May 2, 2023
1 parent aea19f4 commit c73899b
Show file tree
Hide file tree
Showing 5 changed files with 361 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ config/rendered
*.swp
*.swo
*~
coverage.out
coverage.html
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,10 @@ $(CONTROLLER_GEN): $(LOCALBIN)
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest


# Running code covergae locally, skip "imgaes", "charts" folder output as HTML
.PHONY: coverage
coverage: generate fmt vet manifests ## Run code coverage
go test -v -cover ./... -coverprofile coverage.out | grep -v /chart/ | grep -v /images/ | grep -v /charts/ | grep -v /config/ | grep -v /hack/ | grep -v /test/ | grep -v /vendor/
go tool cover -html=coverage.out -o coverage.html
161 changes: 161 additions & 0 deletions api/v1alpha1/k8sgpt_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
Copyright 2023 K8sGPT Contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

var _ = Describe("The test cases for the K8sGPT CRDs", func() {
// Define utility variables for this test suite.
var (
ctx context.Context
secretRef = SecretRef{
Name: "k8s-gpt-secret",
Key: "k8s-gpt",
}

Kind = "K8sGPT"
Backend = "gpt"
Backend2 = "gpt-2"
BaseUrl = "https://api.k8s-gpt.localhost"
Model = "345M"
Version = "v1alpha1"

Namespace = "k8sGPT"

k8sGPT = K8sGPT{
TypeMeta: metav1.TypeMeta{
Kind: Kind,
},
ObjectMeta: metav1.ObjectMeta{
Name: "k8s-gpt",
Namespace: Namespace,
},
Spec: K8sGPTSpec{
Backend: Backend,
BaseUrl: BaseUrl,
Model: Model,
Secret: &secretRef,
Version: Version,
EnableAI: true,
NoCache: true,
},
}

k8sGPT2 = K8sGPT{
TypeMeta: metav1.TypeMeta{
Kind: Kind,
},
ObjectMeta: metav1.ObjectMeta{
Name: "k8s-gpt-2",
Namespace: Namespace,
},
Spec: K8sGPTSpec{
Backend: Backend2,
BaseUrl: BaseUrl,
Model: Model,
Secret: &secretRef,
Version: Version,
EnableAI: false,
NoCache: false,
},
}

typeNamespace = types.NamespacedName{
Name: "k8s-gpt",
Namespace: Namespace,
}
)
// Setup the test context.
BeforeEach(func() {
ctx = context.Background()
})
// Define utility functions for this test suite.
Context("Creating K8sGPT CRDs", func() {
It("Should create a new K8sGPT CRDs", func() {
By("Creating a new K8sGPT CRDs")
// Create a new K8sGPT CRDs.
Expect(fakeClient.Create(ctx, &k8sGPT)).Should(Succeed())
Expect(fakeClient.Create(ctx, &k8sGPT2)).Should(Succeed())
})

// We can get the k8sGPT CRDs by the name and namespace.
It("Should get the K8sGPT CRDs by the name and namespace", func() {
By("Getting the K8sGPT CRDs by the name and namespace")
// Define the K8sGPT CRDs object.
k8sGPTObject := K8sGPT{}
// Get the K8sGPT CRDs by the name and namespace.
Expect(fakeClient.Get(ctx, typeNamespace, &k8sGPTObject)).Should(Succeed())
// Check the K8sGPT CRDs object's name and the APIVersion.
Expect(k8sGPTObject.Name).Should(Equal("k8s-gpt"))
Expect(k8sGPTObject.APIVersion).Should(Equal(GroupVersion.String()))
Expect(k8sGPTObject.Spec.EnableAI).Should(Equal(true))

//get K8sGPT CRD by resource name
Expect(fakeClient.Get(ctx, types.NamespacedName{Name: "k8s-gpt-2", Namespace: Namespace}, &k8sGPTObject)).Should(Succeed())
})

// Get K8sGPT by list
It("Should get the K8sGPT CRDs by list", func() {
By("Getting the K8sGPT CRDs by list")
// Define the K8sGPT CRDs object.
k8sGPTList := K8sGPTList{}
// Get the K8sGPT CRDs by the name and namespace.
Expect(fakeClient.List(ctx, &k8sGPTList)).Should(Succeed())
// check the number of the list should be equal to 2
Expect(len(k8sGPTList.Items)).Should(Equal(2))
})

// Update the K8sGPT CRDs.
It("Should update the K8sGPT CRDs", func() {
By("Updating the K8sGPT CRDs")
// Define the K8sGPT CRDs object.
k8sGPTObject := K8sGPT{}
// Get the K8sGPT CRDs by the name and namespace.
Expect(fakeClient.Get(ctx, typeNamespace, &k8sGPTObject)).Should(Succeed())
// Update the K8sGPT CRDs.
k8sGPTObject.Spec.EnableAI = false
Expect(fakeClient.Update(ctx, &k8sGPTObject)).Should(Succeed())
// check the K8sGPT CRDs should be equal to false
Expect(k8sGPTObject.Spec.EnableAI).Should(Equal(false))
})

// Delete the K8sGPT CRDs.
It("Should delete the K8sGPT CRDs", func() {
By("Deleting the K8sGPT CRDs")
// Define the K8sGPT CRDs object.
Expect(fakeClient.Delete(ctx, &k8sGPT)).Should(Succeed())

// Check the K8sGPT CRDs by list
By("Checking the K8sGPT CRDs by list")
k8sGPTList := K8sGPTList{}
Expect(fakeClient.List(ctx, &k8sGPTList)).Should(Succeed())
// check the number of the list should be equal to 1
Expect(len(k8sGPTList.Items)).Should(Equal(1))
// check the K8sGPT CRD's name should be equal to "k8s-gpt-2"
Expect(k8sGPTList.Items[0].Name).Should(Equal("k8s-gpt-2"))
// remove the K8sGPT CRDs
Expect(fakeClient.Delete(ctx, &k8sGPT2)).Should(Succeed())
})
})
})
138 changes: 138 additions & 0 deletions api/v1alpha1/result_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
Copyright 2023 K8sGPT Contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var _ = Describe("The test cases for the K8sGPT CRDs result types", func() {
var (
ctx context.Context
Namespace = "k8sGPT"
Kind = "Result"
Unmasked = "This is unmasked"
Masked = "This is masked"
Text = "This is a failure"
Details = "This is a result"
ParentObject = "k8s-gpt"
Name = "result"

sensitive = Sensitive{
Unmasked: Unmasked,
Masked: Masked,
}

// Implement a instance of Failure type
failure = Failure{
Text: Text,
Sensitive: []Sensitive{sensitive},
}

// Implement a instance of ResultSpec type
result = Result{
TypeMeta: metav1.TypeMeta{
Kind: Kind,
},
ObjectMeta: metav1.ObjectMeta{
Name: Name,
Namespace: Namespace,
},
Spec: ResultSpec{
Kind: Kind,
Name: Name,
Error: []Failure{failure},
Details: Details,
ParentObject: ParentObject,
},
}
// Create a Namespace object
typeNamespace = types.NamespacedName{
Name: Name,
Namespace: Namespace,
}
)

BeforeEach(func() {
ctx = context.Background()
})

Context("Create a new Result object", func() {
It("Should create a new Result object", func() {
Expect(fakeClient.Create(ctx, &result)).Should(Succeed())
})
})

// Get the Result object
Context("Get the Result object", func() {
It("Should get the Result object", func() {
Expect(fakeClient.Get(ctx, typeNamespace, &result)).Should(Succeed())
})
// Check the Result object's filed values
It("Should check the Result object's filed values", func() {
Expect(result.Spec.Kind).Should(Equal(Kind))
Expect(result.Spec.Name).Should(Equal(Name))
Expect(result.Spec.Error[0].Text).Should(Equal(Text))
Expect(result.Spec.Details).Should(Equal(Details))
Expect(result.Spec.ParentObject).Should(Equal(ParentObject))
})
})
// Update the Result object
Context("Update the Result object", func() {
It("Should update the Result object", func() {
result.Spec.Details = "This is a new result"
Expect(fakeClient.Update(ctx, &result)).Should(Succeed())
})
// Check the Result object's filed values
It("Should check the Result object's filed values", func() {
Expect(result.Spec.Kind).Should(Equal(Kind))
Expect(result.Spec.Name).Should(Equal(Name))
Expect(result.Spec.Error[0].Text).Should(Equal(Text))
Expect(result.Spec.Details).Should(Equal("This is a new result"))
Expect(result.Spec.ParentObject).Should(Equal(ParentObject))
})
})
// Get the Result object by list
Context("Get the Result object by list", func() {
It("Should get the Result object by list", func() {
resultList := ResultList{}
Expect(fakeClient.List(ctx, &resultList)).Should(Succeed())
})
// Check the length of Result object list
It("Should check the length of Result object list", func() {
Expect(len(result.Spec.Error)).Should(Equal(1))
})
})
// delete the Result object
Context("Delete the Result object", func() {
It("Should delete the Result object", func() {
Expect(fakeClient.Delete(ctx, &result)).Should(Succeed())
})
// Check the Result object has been deleted
It("Should check the Result object has been deleted", func() {
Expect(fakeClient.Get(ctx, typeNamespace, &result)).ShouldNot(Succeed())
})
})
})
53 changes: 53 additions & 0 deletions api/v1alpha1/suit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2023 K8sGPT Contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
runtime "k8s.io/apimachinery/pkg/runtime"
cgScheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

// Entry
func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "K8sgpt CRDs API Suite")
}

// Initial fake client for every cases
var fakeClient client.Client

var _ = BeforeSuite(func() {
By("Bootstrapping test environment")

// Initial the scheme
scheme := runtime.NewScheme()
// For the standard k8s types
_ = cgScheme.AddToScheme(scheme)
err := AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())
// Initial fake client
fakeClient = fake.NewClientBuilder().WithScheme(scheme).Build()
})

0 comments on commit c73899b

Please sign in to comment.