diff --git a/README.md b/README.md index 9e0d3d6..8bda43e 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,8 @@ To see all rules supported by the makefile, run `make help` To run integration tests for the operator, run `make test-integration`. -The `oc` executable must be accessible. +One of the `oc` or `kubectl` executables must be accessible. If both are present in your path, `oc` will be used, except if you +define the environment variable `K8S_CLI` with the command you prefer to use. By default, the tests will use the default image for the operator, `quay.io/devfile/registry-operator:next`. diff --git a/tests/integration/pkg/client/client.go b/tests/integration/pkg/client/client.go index 565aeb2..2026341 100644 --- a/tests/integration/pkg/client/client.go +++ b/tests/integration/pkg/client/client.go @@ -17,8 +17,10 @@ package client import ( + "errors" "fmt" "os" + "os/exec" "k8s.io/client-go/kubernetes" "sigs.k8s.io/controller-runtime/pkg/client" @@ -28,6 +30,7 @@ import ( type K8sClient struct { kubeClient *kubernetes.Clientset controllerClient client.Client + cli string } // NewK8sClient creates kubernetes client wrapper with helper functions and direct access to k8s go client @@ -49,6 +52,12 @@ func NewK8sClient() (*K8sClient, error) { } h := &K8sClient{kubeClient: kubeClient, controllerClient: controllerClient} + + h.cli, err = findCLI() + if err != nil { + fmt.Println("failed to find oc or kubectl cli") + os.Exit(1) + } return h, nil } @@ -56,3 +65,20 @@ func NewK8sClient() (*K8sClient, error) { func (c *K8sClient) Kube() kubernetes.Interface { return c.kubeClient } + +// findCLI returns the first found CLI compatible with oc/kubectl +func findCLI() (string, error) { + selected := os.Getenv("K8S_CLI") + if selected != "" { + return selected, nil + } + for _, cli := range []string{"oc", "kubectl"} { + _, err := exec.LookPath(cli) + if err != nil { + continue + } + return cli, nil + } + + return "", errors.New("no oc/kubectl CLI found") +} diff --git a/tests/integration/pkg/client/oc.go b/tests/integration/pkg/client/oc.go index 6716655..ee990c3 100644 --- a/tests/integration/pkg/client/oc.go +++ b/tests/integration/pkg/client/oc.go @@ -24,9 +24,9 @@ import ( "github.com/devfile/registry-operator/tests/integration/pkg/config" ) -// OcApplyResource applies resources on the cluster, corresponding to the specified file(s) -func (w *K8sClient) OcApplyResource(filePath string) (err error) { - cmd := exec.Command("oc", "apply", "--namespace", config.Namespace, "-f", filePath) +// ApplyResource applies resources on the cluster, corresponding to the specified file(s) +func (w *K8sClient) ApplyResource(filePath string) (err error) { + cmd := exec.Command(w.cli, "apply", "--namespace", config.Namespace, "-f", filePath) outBytes, err := cmd.CombinedOutput() output := string(outBytes) if err != nil && !strings.Contains(output, "AlreadyExists") { @@ -35,9 +35,9 @@ func (w *K8sClient) OcApplyResource(filePath string) (err error) { return err } -// OcDeleteResource deletes the resources from the cluster that the specified file(s) correspond to -func (w *K8sClient) OcDeleteResource(filePath string) (err error) { - cmd := exec.Command("oc", "delete", "--namespace", config.Namespace, "-f", filePath) +// DeleteResource deletes the resources from the cluster that the specified file(s) correspond to +func (w *K8sClient) DeleteResource(filePath string) (err error) { + cmd := exec.Command(w.cli, "delete", "--namespace", config.Namespace, "-f", filePath) outBytes, err := cmd.CombinedOutput() output := string(outBytes) if err != nil && !strings.Contains(output, "AlreadyExists") { @@ -48,7 +48,7 @@ func (w *K8sClient) OcDeleteResource(filePath string) (err error) { // CurlEndpointInContainer execs into the given container in the pod and uses curl to hit the specified endpoint func (w *K8sClient) CurlEndpointInContainer(pod string, container string, endpoint string) (string, error) { - cmd := exec.Command("oc", "exec", pod, "--namespace", config.Namespace, "-c", container, "--", "curl", endpoint) + cmd := exec.Command(w.cli, "exec", pod, "--namespace", config.Namespace, "-c", container, "--", "curl", endpoint) outBytes, err := cmd.CombinedOutput() output := string(outBytes) return output, err diff --git a/tests/integration/pkg/tests/devfileregistry_tests.go b/tests/integration/pkg/tests/devfileregistry_tests.go index 2fe3603..8e18ba9 100644 --- a/tests/integration/pkg/tests/devfileregistry_tests.go +++ b/tests/integration/pkg/tests/devfileregistry_tests.go @@ -37,7 +37,7 @@ var _ = ginkgo.Describe("[Create Devfile Registry resource]", func() { label := "devfileregistry_cr=" + crName // Deploy the devfileregistry resource for this test case and wait for the pod to be running - err := K8sClient.OcApplyResource("tests/integration/examples/create/devfileregistry.yaml") + err := K8sClient.ApplyResource("tests/integration/examples/create/devfileregistry.yaml") if err != nil { ginkgo.Fail("Failed to create devfileregistry instance: " + err.Error()) return @@ -82,7 +82,7 @@ var _ = ginkgo.Describe("[Create Devfile Registry resource]", func() { }) var _ = ginkgo.AfterEach(func() { - K8sClient.OcDeleteResource("tests/integration/examples/create/devfileregistry.yaml") + K8sClient.DeleteResource("tests/integration/examples/create/devfileregistry.yaml") }) }) @@ -92,7 +92,7 @@ var _ = ginkgo.Describe("[Create Devfile Registry resource with TLS enabled]", f label := "devfileregistry_cr=" + crName // Deploy the devfileregistry resource for this test case and wait for the pod to be running - err := K8sClient.OcApplyResource("tests/integration/examples/create/devfileregistry-tls.yaml") + err := K8sClient.ApplyResource("tests/integration/examples/create/devfileregistry-tls.yaml") if err != nil { ginkgo.Fail("Failed to create devfileregistry instance: " + err.Error()) return @@ -117,7 +117,7 @@ var _ = ginkgo.Describe("[Create Devfile Registry resource with TLS enabled]", f }) var _ = ginkgo.AfterEach(func() { - K8sClient.OcDeleteResource("tests/integration/examples/create/devfileregistry-tls.yaml") + K8sClient.DeleteResource("tests/integration/examples/create/devfileregistry-tls.yaml") }) }) @@ -127,7 +127,7 @@ var _ = ginkgo.Describe("[Create Devfile Registry resource with headless enabled label := "devfileregistry_cr=" + crName // Deploy the devfileregistry resource for this test case and wait for the pod to be running - err := K8sClient.OcApplyResource("tests/integration/examples/create/devfileregistry-headless.yaml") + err := K8sClient.ApplyResource("tests/integration/examples/create/devfileregistry-headless.yaml") if err != nil { ginkgo.Fail("Failed to create devfileregistry instance: " + err.Error()) return @@ -159,7 +159,7 @@ var _ = ginkgo.Describe("[Create Devfile Registry resource with headless enabled }) var _ = ginkgo.AfterEach(func() { - K8sClient.OcDeleteResource("tests/integration/examples/create/devfileregistry-headless.yaml") + K8sClient.DeleteResource("tests/integration/examples/create/devfileregistry-headless.yaml") }) }) @@ -169,7 +169,7 @@ var _ = ginkgo.Describe("[Update Devfile Registry resource]", func() { label := "devfileregistry_cr=" + crName // Deploy the devfileregistry resource for this test case and wait for the pod to be running - err := K8sClient.OcApplyResource("tests/integration/examples/update/devfileregistry-old.yaml") + err := K8sClient.ApplyResource("tests/integration/examples/update/devfileregistry-old.yaml") if err != nil { ginkgo.Fail("Failed to create devfileregistry instance: " + err.Error()) return @@ -189,7 +189,7 @@ var _ = ginkgo.Describe("[Update Devfile Registry resource]", func() { // Update the devfileregistry resource for this test case fmt.Printf("Applying update...") - err = K8sClient.OcApplyResource("tests/integration/examples/update/devfileregistry-new.yaml") + err = K8sClient.ApplyResource("tests/integration/examples/update/devfileregistry-new.yaml") if err != nil { ginkgo.Fail("Failed to create devfileregistry instance: " + err.Error()) return @@ -225,7 +225,7 @@ var _ = ginkgo.Describe("[Update Devfile Registry resource]", func() { }) var _ = ginkgo.AfterEach(func() { - K8sClient.OcDeleteResource("tests/integration/examples/update/devfileregistry-new.yaml") + K8sClient.DeleteResource("tests/integration/examples/update/devfileregistry-new.yaml") }) })