From ca0bc342b46966960172172aeadfb5720f28f5c1 Mon Sep 17 00:00:00 2001 From: Bryce Palmer Date: Wed, 8 Nov 2023 12:24:54 -0500 Subject: [PATCH] :seedling: replace e2e Job with client-go ProxyGet() call (#210) (e2e): replace e2e Job with client-go ProxyGet() call Signed-off-by: Bryce Palmer --- test/e2e/e2e_suite_test.go | 9 +++-- test/e2e/unpack_test.go | 69 ++++++++++++-------------------------- 2 files changed, 26 insertions(+), 52 deletions(-) diff --git a/test/e2e/e2e_suite_test.go b/test/e2e/e2e_suite_test.go index 4945d4c3..db15ffec 100644 --- a/test/e2e/e2e_suite_test.go +++ b/test/e2e/e2e_suite_test.go @@ -16,11 +16,10 @@ import ( ) var ( - cfg *rest.Config - c client.Client - err error - defaultSystemNamespace = "catalogd-system" - kubeClient kubernetes.Interface + cfg *rest.Config + c client.Client + err error + kubeClient kubernetes.Interface ) func TestE2E(t *testing.T) { diff --git a/test/e2e/unpack_test.go b/test/e2e/unpack_test.go index 6c27dc4a..b2a63db4 100644 --- a/test/e2e/unpack_test.go +++ b/test/e2e/unpack_test.go @@ -3,18 +3,17 @@ package e2e import ( "context" "io" + "net/url" "os" + "strings" "github.com/google/go-cmp/cmp" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - batchv1 "k8s.io/api/batch/v1" - corev1 "k8s.io/api/core/v1" "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" - "sigs.k8s.io/controller-runtime/pkg/client" catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1" ) @@ -43,7 +42,6 @@ var _ = Describe("Catalog Unpacking", func() { var ( ctx context.Context catalog *catalogd.Catalog - job batchv1.Job ) When("A Catalog is created", func() { BeforeEach(func() { @@ -82,57 +80,34 @@ var _ = Describe("Catalog Unpacking", func() { By("Making sure the catalog content is available via the http server") err = c.Get(ctx, types.NamespacedName{Name: catalog.Name}, catalog) - Expect(err).ToNot(HaveOccurred()) - job = batchv1.Job{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-svr-job", - Namespace: defaultSystemNamespace, - }, - Spec: batchv1.JobSpec{ - Template: corev1.PodTemplateSpec{ - Spec: corev1.PodSpec{ - Containers: []corev1.Container{ - { - Name: "test-svr", - Image: "curlimages/curl", - Command: []string{"sh", "-c", "curl --silent --show-error --location -o - " + catalog.Status.ContentURL}, - }, - }, - RestartPolicy: "Never", - }, - }, - }, + url, err := url.Parse(catalog.Status.ContentURL) + Expect(err).NotTo(HaveOccurred()) + Expect(url).NotTo(BeNil()) + // url is expected to be in the format of + // http://{service_name}.{namespace}.svc/{catalog_name}/all.json + // 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 80. + if port == "" { + port = "80" } - err = c.Create(ctx, &job) - Expect(err).ToNot(HaveOccurred()) - Eventually(func() (bool, error) { - err = c.Get(ctx, types.NamespacedName{Name: "test-svr-job", Namespace: defaultSystemNamespace}, &job) - if err != nil { - return false, err - } - return job.Status.CompletionTime != nil && job.Status.Succeeded == 1, err - }).Should(BeTrue()) - pods := &corev1.PodList{} - Eventually(func() (bool, error) { - err := c.List(context.Background(), pods, client.MatchingLabels{"job-name": "test-svr-job"}) - if err != nil { - return false, err - } - return len(pods.Items) == 1, nil - }).Should(BeTrue()) + resp := kubeClient.CoreV1().Services(ns).ProxyGet(url.Scheme, name, port, url.Path, map[string]string{}) + rc, err := resp.Stream(ctx) + Expect(err).NotTo(HaveOccurred()) + defer rc.Close() expectedFBC, err := os.ReadFile("../../testdata/catalogs/test-catalog/expected_all.json") Expect(err).To(Not(HaveOccurred())) - // Get logs of the Pod - pod := pods.Items[0] - logReader, err := kubeClient.CoreV1().Pods(defaultSystemNamespace).GetLogs(pod.Name, &corev1.PodLogOptions{}).Stream(context.Background()) - Expect(err).To(Not(HaveOccurred())) - actualFBC, err := io.ReadAll(logReader) + + actualFBC, err := io.ReadAll(rc) Expect(err).To(Not(HaveOccurred())) Expect(cmp.Diff(expectedFBC, actualFBC)).To(BeEmpty()) }) AfterEach(func() { - Expect(c.Delete(ctx, &job)).To(Succeed()) Expect(c.Delete(ctx, catalog)).To(Succeed()) Eventually(func(g Gomega) { err = c.Get(ctx, types.NamespacedName{Name: catalog.Name}, &catalogd.Catalog{})