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 fc81961 commit 3496d8a
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 @@ -46,7 +46,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 @@ -82,20 +82,20 @@ func (r *CatalogReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if existingCatsrc.DeletionTimestamp.IsZero() {
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"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -53,7 +55,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 @@ -64,7 +66,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 @@ -102,7 +104,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 @@ -138,7 +143,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 @@ -153,7 +161,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 @@ -277,6 +291,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 @@ -352,7 +399,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 @@ -366,14 +416,26 @@ var _ = Describe("Catalogd Controller Test", func() {
tempTestBundleMetaName = fmt.Sprintf("%s-%s", tempCatalog.Name, testBundleName)
tempTestPackageMetaName = 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 @@ -465,7 +527,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 @@ -476,14 +541,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 3496d8a

Please sign in to comment.