Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safeguard against missing catalog sources #666

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ var _ = SynchronizedBeforeSuite(func() {
Expect(err).ToNot(HaveOccurred())
}

if !globalhelper.IsKindCluster() {
By("Check if catalog sources are available")
err = globalhelper.ValidateCatalogSources()
Expect(err).ToNot(HaveOccurred(), "All necessary catalog sources are not available")
}
}, func() {})

var _ = SynchronizedAfterSuite(func() {}, func() {
Expand Down
53 changes: 53 additions & 0 deletions tests/globalhelper/catalogsources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package globalhelper

import (
"context"
"fmt"

v1alpha1typed "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned/typed/operators/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
CatalogSourceNamespace = "openshift-marketplace"
)

func ValidateCatalogSources() error {
return validateCatalogSources(GetAPIClient().OperatorsV1alpha1Interface)
}

func validateCatalogSources(opclient v1alpha1typed.OperatorsV1alpha1Interface) error {
validCatalogSources := []string{"certified-operators", "community-operators", "redhat-operators", "redhat-marketplace"}

catalogSources, err := opclient.CatalogSources(CatalogSourceNamespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
return err
}

if len(catalogSources.Items) == 0 {
return fmt.Errorf("no catalog sources found")
}

var foundCatalogSources []string
for _, catalogSource := range catalogSources.Items {
foundCatalogSources = append(foundCatalogSources, catalogSource.Name)
}

for _, validCatalogSource := range validCatalogSources {
if !contains(foundCatalogSources, validCatalogSource) {
return fmt.Errorf("catalog source %s not found", validCatalogSource)
}
}

return nil
}

func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}

return false
}
1 change: 1 addition & 0 deletions tests/globalhelper/catalogsources_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package globalhelper
7 changes: 7 additions & 0 deletions tests/operator/operator_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ var _ = SynchronizedBeforeSuite(func() {
[]string{},
[]string{}, globalhelper.GetConfiguration().General.TnfConfigDir)
Expect(err).ToNot(HaveOccurred())

// Safeguard against running the operator tests on a cluster without catalog sources
if !globalhelper.IsKindCluster() {
By("Check if catalog sources are available")
err = globalhelper.ValidateCatalogSources()
Expect(err).ToNot(HaveOccurred(), "All necessary catalog sources are not available")
}
}, func() {})

var _ = SynchronizedAfterSuite(func() {}, func() {
Expand Down
Loading