diff --git a/test/catalogd-e2e/unpack_test.go b/test/catalogd-e2e/unpack_test.go index c16b96dff..4c0ad6c01 100644 --- a/test/catalogd-e2e/unpack_test.go +++ b/test/catalogd-e2e/unpack_test.go @@ -14,6 +14,7 @@ import ( "k8s.io/apimachinery/pkg/types" catalogdv1 "github.com/operator-framework/operator-controller/catalogd/api/v1" + testutils "github.com/operator-framework/operator-controller/test/utils" ) const ( @@ -76,7 +77,7 @@ var _ = Describe("ClusterCatalog Unpacking", func() { Expect(catalog.ObjectMeta.Labels).To(HaveKeyWithValue("olm.operatorframework.io/metadata.name", catalogName)) By("Making sure the catalog content is available via the http server") - actualFBC, err := ReadTestCatalogServerContents(ctx, catalog, c, kubeClient) + actualFBC, err := testutils.ReadTestCatalogServerContents(ctx, catalog, kubeClient) Expect(err).To(Not(HaveOccurred())) expectedFBC, err := os.ReadFile("../../catalogd/testdata/catalogs/test-catalog/expected_all.json") diff --git a/test/catalogd-e2e/util.go b/test/catalogd-e2e/util.go deleted file mode 100644 index 3d3a8a86c..000000000 --- a/test/catalogd-e2e/util.go +++ /dev/null @@ -1,51 +0,0 @@ -package catalogde2e - -import ( - "context" - "fmt" - "io" - "net/url" - "strings" - - "k8s.io/client-go/kubernetes" - "sigs.k8s.io/controller-runtime/pkg/client" - - catalogdv1 "github.com/operator-framework/operator-controller/catalogd/api/v1" -) - -func ReadTestCatalogServerContents(ctx context.Context, catalog *catalogdv1.ClusterCatalog, c client.Client, kubeClient kubernetes.Interface) ([]byte, error) { - if catalog == nil { - return nil, fmt.Errorf("cannot read nil catalog") - } - if catalog.Status.URLs == nil { - return nil, fmt.Errorf("catalog %q has no catalog urls", catalog.Name) - } - url, err := url.Parse(catalog.Status.URLs.Base) - if err != nil { - return nil, fmt.Errorf("error parsing clustercatalog url %q: %v", catalog.Status.URLs.Base, err) - } - // url is expected to be in the format of - // http://{service_name}.{namespace}.svc/catalogs/{catalog_name}/ - // so to get the namespace and name of the service we grab only - // the hostname and split it on the '.' character - ns := strings.Split(url.Hostname(), ".")[1] - name := strings.Split(url.Hostname(), ".")[0] - port := url.Port() - // the ProxyGet() call below needs an explicit port value, so if - // value from url.Port() is empty, we assume port 443. - if port == "" { - if url.Scheme == "https" { - port = "443" - } else { - port = "80" - } - } - resp := kubeClient.CoreV1().Services(ns).ProxyGet(url.Scheme, name, port, url.JoinPath("api", "v1", "all").Path, map[string]string{}) - rc, err := resp.Stream(ctx) - if err != nil { - return nil, err - } - defer rc.Close() - - return io.ReadAll(rc) -} diff --git a/test/catalogd-upgrade-e2e/unpack_test.go b/test/catalogd-upgrade-e2e/unpack_test.go index 1165152f1..07b599afd 100644 --- a/test/catalogd-upgrade-e2e/unpack_test.go +++ b/test/catalogd-upgrade-e2e/unpack_test.go @@ -21,7 +21,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" catalogdv1 "github.com/operator-framework/operator-controller/catalogd/api/v1" - catalogde2e "github.com/operator-framework/operator-controller/test/catalogd-e2e" + testutils "github.com/operator-framework/operator-controller/test/utils" ) var _ = Describe("ClusterCatalog Unpacking", func() { @@ -92,7 +92,7 @@ var _ = Describe("ClusterCatalog Unpacking", func() { By("Making sure the catalog content is available via the http server") Eventually(func(g Gomega) { - actualFBC, err := catalogde2e.ReadTestCatalogServerContents(ctx, catalog, c, kubeClient) + actualFBC, err := testutils.ReadTestCatalogServerContents(ctx, catalog, kubeClient) g.Expect(err).To(Not(HaveOccurred())) g.Expect(cmp.Diff(expectedFBC, actualFBC)).To(BeEmpty()) }).Should(Succeed()) diff --git a/test/utils/utils.go b/test/utils/utils.go index db6d25a7f..09bbf7148 100644 --- a/test/utils/utils.go +++ b/test/utils/utils.go @@ -1,8 +1,17 @@ package utils import ( + "context" + "fmt" + "io" + "net/url" "os/exec" + "strings" "testing" + + "k8s.io/client-go/kubernetes" + + catalogdv1 "github.com/operator-framework/operator-controller/catalogd/api/v1" ) // FindK8sClient returns the first available Kubernetes CLI client from the system, @@ -21,3 +30,40 @@ func FindK8sClient(t *testing.T) string { t.Fatal("k8s client not found") return "" } + +func ReadTestCatalogServerContents(ctx context.Context, catalog *catalogdv1.ClusterCatalog, kubeClient kubernetes.Interface) ([]byte, error) { + if catalog == nil { + return nil, fmt.Errorf("cannot read nil catalog") + } + if catalog.Status.URLs == nil { + return nil, fmt.Errorf("catalog %q has no catalog urls", catalog.Name) + } + url, err := url.Parse(catalog.Status.URLs.Base) + if err != nil { + return nil, fmt.Errorf("error parsing clustercatalog url %q: %v", catalog.Status.URLs.Base, err) + } + // url is expected to be in the format of + // http://{service_name}.{namespace}.svc/catalogs/{catalog_name}/ + // so to get the namespace and name of the service we grab only + // the hostname and split it on the '.' character + ns := strings.Split(url.Hostname(), ".")[1] + name := strings.Split(url.Hostname(), ".")[0] + port := url.Port() + // the ProxyGet() call below needs an explicit port value, so if + // value from url.Port() is empty, we assume port 443. + if port == "" { + if url.Scheme == "https" { + port = "443" + } else { + port = "80" + } + } + resp := kubeClient.CoreV1().Services(ns).ProxyGet(url.Scheme, name, port, url.JoinPath("api", "v1", "all").Path, map[string]string{}) + rc, err := resp.Stream(ctx) + if err != nil { + return nil, err + } + defer rc.Close() + + return io.ReadAll(rc) +}