Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Anik <anikbhattacharya93@gmail.com>
  • Loading branch information
anik120 committed Aug 14, 2023
1 parent 96b62e8 commit c512ed8
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions pkg/controllers/core/catalog_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"fmt"
"net/http"
"os"
"path/filepath"
"testing/fstest"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/format"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -154,7 +156,13 @@ var _ = Describe("Catalogd Controller Test", func() {

AfterEach(func() {
By("tearing down cluster state")
Expect(cl.Delete(ctx, catalog)).To(Succeed())
Eventually(func() error {
err := cl.Delete(ctx, catalog)
if !apierrors.IsNotFound(err) {
return err
}
return nil
}).Should(Succeed())
})

When("unpacker returns source.Result with state == 'Pending'", func() {
Expand Down Expand Up @@ -278,11 +286,48 @@ var _ = Describe("Catalogd Controller Test", func() {
Expect(cond.Status).To(Equal(metav1.ConditionTrue))
})

When("HTTPServer feature gate is enabled", func() {
BeforeEach(func() {
Expect(features.CatalogdFeatureGate.SetFromMap(map[string]bool{
string(features.PackagesBundleMetadataAPIs): false,
string(features.CatalogMetadataAPI): false,
string(features.HTTPServer): true,
})).NotTo(HaveOccurred())

// reconcile
res, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: catalogKey})
Expect(res).To(Equal(ctrl.Result{}))
Expect(err).ToNot(HaveOccurred())
})
It("should store the FBC in the cache directory", func() {
fbcFile := filepath.Join(reconciler.Storage.RootDirectory, fmt.Sprintf("%s.json", catalog.Name))
_, err := os.Stat(fbcFile)
Expect(err).To(Not(HaveOccurred()))
})

When("the catalog is deleted", func() {
BeforeEach(func() {
Eventually(cl.Delete(ctx, catalog)).Should(Succeed())

Check failure on line 310 in pkg/controllers/core/catalog_controller_test.go

View workflow job for this annotation

GitHub Actions / lint

ginkgo-linter: use a function call in Eventually. This actually checks nothing, because Eventually receives the function returned value, instead of function itself, and this value is never changed; consider using `Eventually(cl.Delete).WithArguments(ctx, catalog).Should(Succeed())` instead (ginkgolinter)
})
It("should delete the FBC from the cache directory", func() {
// reconcile
res, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: catalogKey})
Expect(res).To(Equal(ctrl.Result{}))
Expect(err).ToNot(HaveOccurred())

fbcFile := filepath.Join(reconciler.Storage.RootDirectory, fmt.Sprintf("%s.json", catalog.Name))
_, err = os.Stat(fbcFile)
Expect(err).To(HaveOccurred())
Expect(os.IsNotExist(err)).To(BeTrue())
})
})
})
When("PackagesBundleMetadataAPIs feature gate is enabled", func() {
BeforeEach(func() {
Expect(features.CatalogdFeatureGate.SetFromMap(map[string]bool{
string(features.PackagesBundleMetadataAPIs): true,
string(features.CatalogMetadataAPI): false,
string(features.HTTPServer): false,
})).NotTo(HaveOccurred())

// reconcile
Expand Down Expand Up @@ -417,7 +462,9 @@ var _ = Describe("Catalogd Controller Test", func() {
When("the CatalogMetadataAPI feature gate is enabled", func() {
BeforeEach(func() {
Expect(features.CatalogdFeatureGate.SetFromMap(map[string]bool{
string(features.CatalogMetadataAPI): true,
string(features.CatalogMetadataAPI): true,
string(features.PackagesBundleMetadataAPIs): false,
string(features.HTTPServer): false,
})).NotTo(HaveOccurred())

// reconcile
Expand Down

0 comments on commit c512ed8

Please sign in to comment.