Skip to content

Commit

Permalink
Detect and use oc / kubectl for integration tests (#53)
Browse files Browse the repository at this point in the history
* Detect and use oc / kubectl for integration tests

Signed-off-by: Philippe Martin <[email protected]>

* Use K8S_CLI first, then look in path instead of running version command

Signed-off-by: Philippe Martin <[email protected]>

* Document

Signed-off-by: Philippe Martin <[email protected]>

---------

Signed-off-by: Philippe Martin <[email protected]>
  • Loading branch information
feloy authored Oct 12, 2023
1 parent cb48fbd commit 3801183
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
26 changes: 26 additions & 0 deletions tests/integration/pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package client

import (
"errors"
"fmt"
"os"
"os/exec"

"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -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
Expand All @@ -49,10 +52,33 @@ 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
}

// Kube returns the clientset for Kubernetes upstream.
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")
}
14 changes: 7 additions & 7 deletions tests/integration/pkg/client/oc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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") {
Expand All @@ -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
Expand Down
18 changes: 9 additions & 9 deletions tests/integration/pkg/tests/devfileregistry_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
})
})

Expand All @@ -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
Expand All @@ -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")
})
})

Expand All @@ -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
Expand Down Expand Up @@ -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")
})
})

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
})
})

Expand Down

0 comments on commit 3801183

Please sign in to comment.