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 bddb98c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (

"github.com/operator-framework/catalogd/internal/source"
"github.com/operator-framework/catalogd/internal/version"
catlaogserver "github.com/operator-framework/catalogd/pkg/catalogserver"
catalogserver "github.com/operator-framework/catalogd/pkg/catalogserver"
corecontrollers "github.com/operator-framework/catalogd/pkg/controllers/core"
"github.com/operator-framework/catalogd/pkg/features"
"github.com/operator-framework/catalogd/pkg/profile"
Expand Down Expand Up @@ -122,7 +122,7 @@ func main() {
os.Exit(1)
}

catalogServer := catlaogserver.NewServer(storageDir, serverPort)
catalogServer := catalogserver.NewServer(storageDir, serverPort)
if err := mgr.Add(catalogServer); err != nil {
setupLog.Error(err, "unable to start catalog server")
os.Exit(1)
Expand Down
33 changes: 12 additions & 21 deletions pkg/controllers/core/catalog_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,19 @@ func (r *CatalogReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
if err := r.Client.Get(ctx, req.NamespacedName, &existingCatsrc); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if features.CatalogdFeatureGate.Enabled(features.HTTPServer) {
if existingCatsrc.DeletionTimestamp.IsZero() {
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) {
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)
if err := r.Update(ctx, &existingCatsrc); err != nil {
return ctrl.Result{}, err
}
}
return ctrl.Result{}, nil
if features.CatalogdFeatureGate.Enabled(features.HTTPServer) && existingCatsrc.DeletionTimestamp.IsZero() && !controllerutil.ContainsFinalizer(&existingCatsrc, fbcDeletionFinalizer) {
controllerutil.AddFinalizer(&existingCatsrc, fbcDeletionFinalizer)
if err := r.Update(ctx, &existingCatsrc); err != nil {
return ctrl.Result{}, err

Check warning on line 88 in pkg/controllers/core/catalog_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/core/catalog_controller.go#L88

Added line #L88 was not covered by tests
}
}
if features.CatalogdFeatureGate.Enabled(features.HTTPServer) && !existingCatsrc.DeletionTimestamp.IsZero() && controllerutil.ContainsFinalizer(&existingCatsrc, fbcDeletionFinalizer) {
if err := r.Storage.Delete(existingCatsrc.Name); err != nil {
return ctrl.Result{}, err

Check warning on line 93 in pkg/controllers/core/catalog_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/core/catalog_controller.go#L93

Added line #L93 was not covered by tests
}
controllerutil.RemoveFinalizer(&existingCatsrc, fbcDeletionFinalizer)
err := r.Update(ctx, &existingCatsrc)
return ctrl.Result{}, err
}
reconciledCatsrc := existingCatsrc.DeepCopy()
res, reconcileErr := r.reconcile(ctx, reconciledCatsrc)
Expand Down
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() {
Expect(cl.Delete(ctx, catalog)).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{
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 bddb98c

Please sign in to comment.