Skip to content

Commit

Permalink
(http-server) 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 c6bec94 commit 4c0f6b6
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 16 deletions.
10 changes: 5 additions & 5 deletions pkg/controllers/core/catalog_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import (
"github.com/operator-framework/catalogd/pkg/storage"
)

const fbcDeletionFinalizer = "operator-framework.catalogd/localstore"
const FbcDeletionFinalizer = "operator-framework.catalogd/localstore"

// CatalogReconciler reconciles a Catalog object
type CatalogReconciler struct {
Expand Down Expand Up @@ -83,20 +83,20 @@ func (r *CatalogReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if existingCatsrc.DeletionTimestamp.IsZero() {

Check failure on line 85 in pkg/controllers/core/catalog_controller.go

View workflow job for this annotation

GitHub Actions / lint

`if existingCatsrc.DeletionTimestamp.IsZero()` has complex nested blocks (complexity: 9) (nestif)
if !controllerutil.ContainsFinalizer(&existingCatsrc, fbcDeletionFinalizer) {
controllerutil.AddFinalizer(&existingCatsrc, fbcDeletionFinalizer)
if !controllerutil.ContainsFinalizer(&existingCatsrc, FbcDeletionFinalizer) {
controllerutil.AddFinalizer(&existingCatsrc, FbcDeletionFinalizer)
if err := r.Update(ctx, &existingCatsrc); err != nil {
return ctrl.Result{}, err
}
}
} else {
if controllerutil.ContainsFinalizer(&existingCatsrc, fbcDeletionFinalizer) {
if controllerutil.ContainsFinalizer(&existingCatsrc, FbcDeletionFinalizer) {
err := r.Storage.Delete(existingCatsrc.Name)
if err != nil {
return ctrl.Result{}, err
}
// remove our finalizer from the list and update it.
controllerutil.RemoveFinalizer(&existingCatsrc, fbcDeletionFinalizer)
controllerutil.RemoveFinalizer(&existingCatsrc, FbcDeletionFinalizer)
if err := r.Update(ctx, &existingCatsrc); err != nil {
return ctrl.Result{}, err
}
Expand Down
99 changes: 88 additions & 11 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"
k8sError "k8s.io/apimachinery/pkg/api/errors"

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

View workflow job for this annotation

GitHub Actions / lint

import "k8s.io/apimachinery/pkg/api/errors" imported as "k8sError" but must be "apierrors" according to config (importas)
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -54,7 +56,7 @@ var _ = Describe("Catalogd Controller Test", func() {
mockSource *MockSource
)
BeforeEach(func() {
tmpDir, err := os.MkdirTemp(GinkgoT().TempDir(), "cache")
cacheDir, err := os.MkdirTemp(GinkgoT().TempDir(), "cache")
Expect(err).ToNot(HaveOccurred())
ctx = context.Background()
mockSource = &MockSource{}
Expand All @@ -65,7 +67,7 @@ var _ = Describe("Catalogd Controller Test", func() {
v1alpha1.SourceTypeImage: mockSource,
},
),
Storage: storage.NewStorage(tmpDir, http.NewServeMux()),
Storage: storage.NewStorage(cacheDir, http.NewServeMux()),
}
})

Expand Down Expand Up @@ -103,7 +105,10 @@ var _ = Describe("Catalogd Controller Test", func() {
BeforeEach(func() {
By("initializing cluster state")
catalog = &v1alpha1.Catalog{
ObjectMeta: metav1.ObjectMeta{Name: catalogKey.Name},
ObjectMeta: metav1.ObjectMeta{
Name: catalogKey.Name,
Finalizers: []string{core.FbcDeletionFinalizer},
},
Spec: v1alpha1.CatalogSpec{
Source: v1alpha1.CatalogSource{
Type: "invalid-source",
Expand Down Expand Up @@ -139,7 +144,10 @@ var _ = Describe("Catalogd Controller Test", func() {
BeforeEach(func() {
By("initializing cluster state")
catalog = &v1alpha1.Catalog{
ObjectMeta: metav1.ObjectMeta{Name: catalogKey.Name},
ObjectMeta: metav1.ObjectMeta{
Name: catalogKey.Name,
Finalizers: []string{core.FbcDeletionFinalizer},
},
Spec: v1alpha1.CatalogSpec{
Source: v1alpha1.CatalogSource{
Type: "image",
Expand All @@ -154,7 +162,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 !k8sError.IsNotFound(err) {
return err
}
return nil
}).Should(Succeed())
})

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

It("should store the FBC in 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(Not(HaveOccurred()))
})

When("the catalog is deleted", func() {
BeforeEach(func() {
Eventually(func() error {
err := cl.Delete(ctx, catalog)
if !k8sError.IsNotFound(err) {
return err
}
return nil
}).Should(Succeed())
})
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{
Expand Down Expand Up @@ -341,7 +388,10 @@ var _ = Describe("Catalogd Controller Test", func() {
)
BeforeEach(func() {
tempCatalog = &v1alpha1.Catalog{
ObjectMeta: metav1.ObjectMeta{Name: "tempedout"},
ObjectMeta: metav1.ObjectMeta{
Name: "tempedout",
Finalizers: []string{core.FbcDeletionFinalizer},
},
Spec: v1alpha1.CatalogSpec{
Source: v1alpha1.CatalogSource{
Type: "image",
Expand All @@ -355,14 +405,26 @@ var _ = Describe("Catalogd Controller Test", func() {
tempTestBundleMetaName, _ = k8sutil.MetadataName(fmt.Sprintf("%s-%s", tempCatalog.Name, testBundleName))
tempTestPackageMetaName, _ = k8sutil.MetadataName(fmt.Sprintf("%s-%s", tempCatalog.Name, testPackageName))

Expect(cl.Create(ctx, tempCatalog)).To(Succeed())
Eventually(func() error {
err := cl.Create(ctx, tempCatalog)
if !k8sError.IsAlreadyExists(err) {
return err
}
return nil
}).Should(Succeed())
res, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: "tempedout"}})
Expect(res).To(Equal(ctrl.Result{}))
Expect(err).ToNot(HaveOccurred())
})

AfterEach(func() {
Expect(cl.Delete(ctx, tempCatalog)).NotTo(HaveOccurred())
Eventually(func() error {
err := cl.Delete(ctx, tempCatalog)
if !k8sError.IsNotFound(err) {
return err
}
return nil
}).Should(Succeed())
})

It("should not delete BundleMetadata belonging to a different catalog", func() {
Expand Down Expand Up @@ -454,7 +516,10 @@ var _ = Describe("Catalogd Controller Test", func() {
)
BeforeEach(func() {
tempCatalog = &v1alpha1.Catalog{
ObjectMeta: metav1.ObjectMeta{Name: "tempedout"},
ObjectMeta: metav1.ObjectMeta{
Name: "tempedout",
Finalizers: []string{core.FbcDeletionFinalizer},
},
Spec: v1alpha1.CatalogSpec{
Source: v1alpha1.CatalogSource{
Type: "image",
Expand All @@ -465,14 +530,26 @@ var _ = Describe("Catalogd Controller Test", func() {
},
}

Expect(cl.Create(ctx, tempCatalog)).To(Succeed())
Eventually(func() error {
err := cl.Create(ctx, tempCatalog)
if !k8sError.IsAlreadyExists(err) {
return err
}
return nil
}).Should(Succeed())
res, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: "tempedout"}})
Expect(res).To(Equal(ctrl.Result{}))
Expect(err).ToNot(HaveOccurred())
})

AfterEach(func() {
Expect(cl.Delete(ctx, tempCatalog)).NotTo(HaveOccurred())
Eventually(func() error {
err := cl.Delete(ctx, tempCatalog)
if !k8sError.IsNotFound(err) {
return err
}
return nil
}).Should(Succeed())
})

It("should not delete CatalogMetadata belonging to a different catalog", func() {
Expand Down

0 comments on commit 4c0f6b6

Please sign in to comment.