Skip to content

Commit 6dace7c

Browse files
[Monorepo]: Cleanup : Centralize the utils func under the test/utils directories (#1742)
1 parent aa4cf2e commit 6dace7c

File tree

4 files changed

+50
-54
lines changed

4 files changed

+50
-54
lines changed

test/catalogd-e2e/unpack_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"k8s.io/apimachinery/pkg/types"
1515

1616
catalogdv1 "github.com/operator-framework/operator-controller/catalogd/api/v1"
17+
testutils "github.com/operator-framework/operator-controller/test/utils"
1718
)
1819

1920
const (
@@ -76,7 +77,7 @@ var _ = Describe("ClusterCatalog Unpacking", func() {
7677
Expect(catalog.ObjectMeta.Labels).To(HaveKeyWithValue("olm.operatorframework.io/metadata.name", catalogName))
7778

7879
By("Making sure the catalog content is available via the http server")
79-
actualFBC, err := ReadTestCatalogServerContents(ctx, catalog, c, kubeClient)
80+
actualFBC, err := testutils.ReadTestCatalogServerContents(ctx, catalog, kubeClient)
8081
Expect(err).To(Not(HaveOccurred()))
8182

8283
expectedFBC, err := os.ReadFile("../../catalogd/testdata/catalogs/test-catalog/expected_all.json")

test/catalogd-e2e/util.go

Lines changed: 0 additions & 51 deletions
This file was deleted.

test/catalogd-upgrade-e2e/unpack_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"sigs.k8s.io/controller-runtime/pkg/client"
2222

2323
catalogdv1 "github.com/operator-framework/operator-controller/catalogd/api/v1"
24-
catalogde2e "github.com/operator-framework/operator-controller/test/catalogd-e2e"
24+
testutils "github.com/operator-framework/operator-controller/test/utils"
2525
)
2626

2727
var _ = Describe("ClusterCatalog Unpacking", func() {
@@ -92,7 +92,7 @@ var _ = Describe("ClusterCatalog Unpacking", func() {
9292

9393
By("Making sure the catalog content is available via the http server")
9494
Eventually(func(g Gomega) {
95-
actualFBC, err := catalogde2e.ReadTestCatalogServerContents(ctx, catalog, c, kubeClient)
95+
actualFBC, err := testutils.ReadTestCatalogServerContents(ctx, catalog, kubeClient)
9696
g.Expect(err).To(Not(HaveOccurred()))
9797
g.Expect(cmp.Diff(expectedFBC, actualFBC)).To(BeEmpty())
9898
}).Should(Succeed())

test/utils/utils.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
package utils
22

33
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"net/url"
48
"os/exec"
9+
"strings"
510
"testing"
11+
12+
"k8s.io/client-go/kubernetes"
13+
14+
catalogdv1 "github.com/operator-framework/operator-controller/catalogd/api/v1"
615
)
716

817
// FindK8sClient returns the first available Kubernetes CLI client from the system,
@@ -21,3 +30,40 @@ func FindK8sClient(t *testing.T) string {
2130
t.Fatal("k8s client not found")
2231
return ""
2332
}
33+
34+
func ReadTestCatalogServerContents(ctx context.Context, catalog *catalogdv1.ClusterCatalog, kubeClient kubernetes.Interface) ([]byte, error) {
35+
if catalog == nil {
36+
return nil, fmt.Errorf("cannot read nil catalog")
37+
}
38+
if catalog.Status.URLs == nil {
39+
return nil, fmt.Errorf("catalog %q has no catalog urls", catalog.Name)
40+
}
41+
url, err := url.Parse(catalog.Status.URLs.Base)
42+
if err != nil {
43+
return nil, fmt.Errorf("error parsing clustercatalog url %q: %v", catalog.Status.URLs.Base, err)
44+
}
45+
// url is expected to be in the format of
46+
// http://{service_name}.{namespace}.svc/catalogs/{catalog_name}/
47+
// so to get the namespace and name of the service we grab only
48+
// the hostname and split it on the '.' character
49+
ns := strings.Split(url.Hostname(), ".")[1]
50+
name := strings.Split(url.Hostname(), ".")[0]
51+
port := url.Port()
52+
// the ProxyGet() call below needs an explicit port value, so if
53+
// value from url.Port() is empty, we assume port 443.
54+
if port == "" {
55+
if url.Scheme == "https" {
56+
port = "443"
57+
} else {
58+
port = "80"
59+
}
60+
}
61+
resp := kubeClient.CoreV1().Services(ns).ProxyGet(url.Scheme, name, port, url.JoinPath("api", "v1", "all").Path, map[string]string{})
62+
rc, err := resp.Stream(ctx)
63+
if err != nil {
64+
return nil, err
65+
}
66+
defer rc.Close()
67+
68+
return io.ReadAll(rc)
69+
}

0 commit comments

Comments
 (0)