Skip to content

Commit

Permalink
Replace the bash test script by a ginkgo test suite
Browse files Browse the repository at this point in the history
Signed-off-by: Soule BA <bah.soule@gmail.com>
  • Loading branch information
souleb committed Jun 6, 2024
1 parent d5e19d4 commit d5e7751
Show file tree
Hide file tree
Showing 22 changed files with 279 additions and 108 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/e2e-olm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: e2e-oerator-lifecycle-manager

on:
workflow_dispatch:
inputs:
version:
description: 'Flux operator strict semver e.g. v0.3.0'
required: true

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
- name: Setup Go
uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1
with:
go-version-file: 'go.mod'
cache-dependency-path: |
**/go.sum
**/go.mod
- name: Setup Kubernetes
uses: helm/kind-action@0025e74a8c7512023d06dc019c617aa3cf561fde # v1.10.0
with:
version: v0.23.0
cluster_name: kind
- name: Setup QEMU
uses: docker/setup-qemu-action@68827325e0b33c7199eb31dd4e31fbe9023e06e3 # v3.0.0
- name: Setup Docker Buildx
id: buildx
uses: docker/setup-buildx-action@d70bba72b1f3fd22344832f00baa16ece964efeb # v3.3.0
- name: Run OLM tests
run: |
make test-olm FLUX_OPERATOR_VERSION="${{ github.event.inputs.version }}"
30 changes: 0 additions & 30 deletions .github/workflows/e2e-openshift.yml

This file was deleted.

24 changes: 18 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ IMG ?= ghcr.io/controlplaneio-fluxcd/flux-operator:latest

# FLUX_OPERATOR_VERSION refers to the version of the operator to be tested
# under ./config/operatorhub/flux-operator/<version> directory.
FLUX_OPERATOR_VERSION ?= 0.1.0
FLUX_OPERATOR_VERSION ?= v0.1.0
# OLM_VERSION refers to the version of the Operator Lifecycle Manager to be used.
OLM_VERSION ?= 0.28.0

Expand Down Expand Up @@ -155,11 +155,23 @@ undeploy: kustomize ## Undeploy controller from the K8s cluster specified in ~/.
opm-index:
./config/operatorhub/flux-operator/scripts/opm-index.sh ${FLUX_OPERATOR_VERSION}

.PHONY: test-operatorhub
test-operatorhub: opm-index
yq e -i ".spec.startingCSV=\"flux-operator.v${FLUX_OPERATOR_VERSION}\"" \
./config/operatorhub/flux-operator/test-yamls/004-operator-subscription.yaml
bash -x ./config/operatorhub/flux-operator/scripts/test.sh ${FLUX_OPERATOR_VERSION} ${OLM_VERSION}
.PHONY: test-olm
test-olm: opm-index
yq e -i ".spec.startingCSV=\"flux-operator.${FLUX_OPERATOR_VERSION}\"" \
./config/operatorhub/flux-operator/testdata/004-operator-subscription.yaml
yq e -i ".spec.image=\"ghcr.io/controlplaneio-fluxcd/openshift-flux-operator-index:${FLUX_OPERATOR_VERSION}\"" \
./config/operatorhub/flux-operator/testdata/003-catalog-source.yaml
export OLM_VERSION=${OLM_VERSION} && \
export FLUX_OPERATOR_VERSION=${FLUX_OPERATOR_VERSION} && \
go test ./test/olm/ -v -ginkgo.v

.PHONY: deploy-olm-data
deploy-olm-data:
kubectl apply -k ./config/operatorhub/flux-operator/testdata/

.PHONY: undeploy-olm-data
undeploy-olm-data:
kubectl delete -k ./config/operatorhub/flux-operator/testdata/

##@ Dependencies

Expand Down
4 changes: 2 additions & 2 deletions config/operatorhub/flux-operator/Dockerfile.opm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM alpine:3.20

ARG DOCKER_VERSION=26.1.3
ARG OPM_VERSION=4.15.9
ARG OPM_VERSION=4.15.16
# x86_64 or aarch64
ARG ARCH=x86_64

Expand All @@ -19,4 +19,4 @@ WORKDIR /opt
COPY --from=0 /opt/docker/docker /usr/bin/
COPY --from=0 /opt/opm /opt/

ENTRYPOINT ["/opt/opm"]
ENTRYPOINT ["/opt/opm"]
28 changes: 17 additions & 11 deletions config/operatorhub/flux-operator/scripts/opm-index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set -euo pipefail

VERSION=$1
ARCH=""
DIR="config/operatorhub/flux-operator/"
case $(uname -m) in
x86_64) ARCH="x86_64" ;;
aarch64) ARCH="aarch64" ;;
Expand All @@ -16,22 +17,27 @@ case $(uname -m) in
;;
esac

list=""
for i in $(ls -d ./config/operatorhub/flux-operator/${VERSION}/ | xargs -I{} basename {}); do
# docker build and push individual bundles
docker build -t ghcr.io/controlplaneio-fluxcd/openshift-flux-operator-catalog:bundle-v"${i}" \
-f ./config/operatorhub/flux-operator/bundle.Dockerfile config/operatorhub/flux-operator/"${i}"
list="$list,ghcr.io/controlplaneio-fluxcd/openshift-flux-operator-catalog:bundle-v$i"
done
if [ ! -d "${DIR}/${VERSION}" ]; then
echo "Version ${VERSION} does not exist"
exit 1
fi

docker build -t opm --build-arg ARCH=$ARCH -f ./config/operatorhub/flux-operator/Dockerfile.opm .
# docker build and push individual bundles
docker build -t ghcr.io/controlplaneio-fluxcd/openshift-flux-operator-catalog:bundle-"${VERSION}" \
-f "${DIR}/bundle.Dockerfile" "${DIR}/${VERSION}"
docker push ghcr.io/controlplaneio-fluxcd/openshift-flux-operator-catalog:bundle-"${VERSION}"

docker build -t opm --build-arg ARCH=$ARCH -f "${DIR}/Dockerfile.opm" .

list=${list:1} # remove first comma
docker run --rm -it \
--privileged \
-v /var/lib/docker:/var/lib/docker \
-v /var/run/docker.sock:/var/run/docker.sock \
opm:latest index add \
--container-tool docker \
--bundles "$list" \
--tag ghcr.io/controlplaneio-fluxcd/openshift-flux-operator-index:latest
--bundles ghcr.io/controlplaneio-fluxcd/openshift-flux-operator-catalog:bundle-"${VERSION}" \
--tag ghcr.io/controlplaneio-fluxcd/openshift-flux-operator-index:${VERSION}

#push index
docker push ghcr.io/controlplaneio-fluxcd/openshift-flux-operator-index:${VERSION}

38 changes: 0 additions & 38 deletions config/operatorhub/flux-operator/scripts/test.sh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ spec:
description: custom
publisher: custom
sourceType: grpc
image: ghcr.io/controlplaneio-fluxcd/openshift-flux-operator-index:latest
image: ghcr.io/controlplaneio-fluxcd/openshift-flux-operator-index:v0.3.0
updateStrategy:
registryPoll:
interval: 30m
7 changes: 7 additions & 0 deletions config/operatorhub/flux-operator/testdata/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- 001-flux-system-ns.yaml
- 002-flux-system-og.yaml
- 003-catalog-source.yaml
- 004-operator-subscription.yaml
12 changes: 6 additions & 6 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ var _ = BeforeSuite(func() {

By("building the flux-operator image")
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", image))
_, err = Run(cmd)
_, err = Run(cmd, "/test/e2e")
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("loading the flux-operator image on Kind")
err = LoadImageToKindClusterWithName(image)
err = LoadImageToKindClusterWithName(image, "/test/e2e")
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("deploying flux-operator")
cmd = exec.Command("make", "deploy", fmt.Sprintf("IMG=%s", image))
_, err = Run(cmd)
_, err = Run(cmd, "/test/e2e")
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("validating that the flux-operator pod is running as expected")
Expand All @@ -49,7 +49,7 @@ var _ = BeforeSuite(func() {
"-n", namespace,
)

podOutput, err := Run(cmd)
podOutput, err := Run(cmd, "/test/e2e")
ExpectWithOffset(2, err).NotTo(HaveOccurred())
podNames := GetNonEmptyLines(string(podOutput))
if len(podNames) != 1 {
Expand All @@ -63,7 +63,7 @@ var _ = BeforeSuite(func() {
"pods", controllerPodName, "-o", "jsonpath={.status.phase}",
"-n", namespace,
)
status, err := Run(cmd)
status, err := Run(cmd, "/test/e2e")
ExpectWithOffset(2, err).NotTo(HaveOccurred())
if string(status) != "Running" {
return fmt.Errorf("flux-operator pod in %s status", status)
Expand All @@ -77,6 +77,6 @@ var _ = BeforeSuite(func() {
var _ = AfterSuite(func() {
By("uninstalling flux-operator")
cmd := exec.Command("make", "undeploy")
_, err := Run(cmd)
_, err := Run(cmd, "/test/e2e")
Expect(err).NotTo(HaveOccurred())
})
16 changes: 8 additions & 8 deletions test/e2e/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ var _ = Describe("FluxInstance", Ordered, func() {
cmd := exec.Command("kubectl", "apply",
"-k", "config/samples", "-n", namespace,
)
_, err := Run(cmd)
_, err := Run(cmd, "/test/e2e")
ExpectWithOffset(2, err).NotTo(HaveOccurred())

cmd = exec.Command("kubectl", "wait", "FluxInstance/flux", "-n", namespace,
"--for=condition=Ready", "--timeout=5m",
)
_, err = Run(cmd)
_, err = Run(cmd, "/test/e2e")
ExpectWithOffset(2, err).NotTo(HaveOccurred())
return nil
}
Expand All @@ -40,19 +40,19 @@ var _ = Describe("FluxInstance", Ordered, func() {
cmd := exec.Command("kubectl", "-n", namespace, "patch", "FluxInstance/flux",
"--type=json", `-p=[{"op": "replace", "path": "/spec/cluster/multitenant", "value":true}]`,
)
_, err := Run(cmd)
_, err := Run(cmd, "/test/e2e")
ExpectWithOffset(2, err).NotTo(HaveOccurred())

cmd = exec.Command("kubectl", "wait", "FluxInstance/flux", "-n", namespace,
"--for=condition=Ready", "--timeout=5m",
)
_, err = Run(cmd)
_, err = Run(cmd, "/test/e2e")
ExpectWithOffset(2, err).NotTo(HaveOccurred())

cmd = exec.Command("kubectl", "get", "deploy/kustomize-controller",
"-n", namespace, "-o=yaml",
)
output, err := Run(cmd)
output, err := Run(cmd, "/test/e2e")
ExpectWithOffset(2, err).NotTo(HaveOccurred())
ExpectWithOffset(2, output).To(ContainSubstring("no-cross-namespace-refs=true"))

Expand All @@ -67,16 +67,16 @@ var _ = Describe("FluxInstance", Ordered, func() {
By("delete FluxInstance")
cmd := exec.Command("kubectl", "delete", "-k", "config/samples",
"--timeout=30s", "-n", namespace)
_, err := Run(cmd)
_, err := Run(cmd, "/test/e2e")
Expect(err).NotTo(HaveOccurred())
By("source-controller deleted")
cmd = exec.Command("kubectl", "get", "deploy/source-controller", "-n", namespace)
_, err = Run(cmd)
_, err = Run(cmd, "/test/e2e")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("not found"))
By("namespace exists")
cmd = exec.Command("kubectl", "get", "ns", namespace)
_, err = Run(cmd)
_, err = Run(cmd, "/test/e2e")
Expect(err).NotTo(HaveOccurred())
})
})
Expand Down
14 changes: 8 additions & 6 deletions test/e2e/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
)

// Run executes the provided command within this context
func Run(cmd *exec.Cmd) ([]byte, error) {
dir, _ := GetProjectDir()
func Run(cmd *exec.Cmd, dirPattern string) ([]byte, error) {
dir, _ := GetProjectDir(dirPattern)
cmd.Dir = dir

if err := os.Chdir(cmd.Dir); err != nil {
Expand All @@ -33,14 +33,14 @@ func Run(cmd *exec.Cmd) ([]byte, error) {
}

// LoadImageToKindClusterWithName loads a local docker image to the kind cluster
func LoadImageToKindClusterWithName(name string) error {
func LoadImageToKindClusterWithName(name, dirPattern string) error {
cluster := "kind"
if v, ok := os.LookupEnv("KIND_CLUSTER"); ok {
cluster = v
}
kindOptions := []string{"load", "docker-image", name, "--name", cluster}
cmd := exec.Command("kind", kindOptions...)
_, err := Run(cmd)
_, err := Run(cmd, dirPattern)
return err
}

Expand All @@ -59,11 +59,13 @@ func GetNonEmptyLines(output string) []string {
}

// GetProjectDir will return the directory where the project is
func GetProjectDir() (string, error) {
// The pattern is used to remove the last part of the path
// e.g. /home/user/go/src/github.com/flux-operator/test/e2e -> /home/user/go/src/github.com/flux-operator
func GetProjectDir(pattern string) (string, error) {
wd, err := os.Getwd()
if err != nil {
return wd, err
}
wd = strings.Replace(wd, "/test/e2e", "", -1)
wd = strings.Replace(wd, pattern, "", -1)
return wd, nil
}
16 changes: 16 additions & 0 deletions test/olm/e2e_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package e2eolm

import (
"fmt"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

// Run e2e tests using the Ginkgo runner.
func TestE2E(t *testing.T) {
RegisterFailHandler(Fail)
fmt.Fprintf(GinkgoWriter, "Starting flux-operator olm e2e suite\n")
RunSpecs(t, "olm e2e suite")
}
Loading

0 comments on commit d5e7751

Please sign in to comment.