diff --git a/boilerplate/_data/last-boilerplate-commit b/boilerplate/_data/last-boilerplate-commit index c727781..c51c578 100644 --- a/boilerplate/_data/last-boilerplate-commit +++ b/boilerplate/_data/last-boilerplate-commit @@ -1 +1 @@ -76942d7dbb2425eb028459cfc5f88e92b6d4d504 +b882991ad169f49cb8e09c2e5fbe8740fb1f9a08 diff --git a/boilerplate/generated-includes.mk b/boilerplate/generated-includes.mk index 78b982f..4e424c9 100644 --- a/boilerplate/generated-includes.mk +++ b/boilerplate/generated-includes.mk @@ -5,3 +5,5 @@ include boilerplate/_lib/boilerplate.mk include boilerplate/openshift/golang-osd-operator/csv-generate/csv-generate.mk include boilerplate/openshift/golang-osd-operator/project.mk include boilerplate/openshift/golang-osd-operator/standard.mk +include boilerplate/openshift/golang-osd-operator-osde2e/project.mk +include boilerplate/openshift/golang-osd-operator-osde2e/standard.mk diff --git a/boilerplate/openshift/golang-osd-operator-osde2e/README.md b/boilerplate/openshift/golang-osd-operator-osde2e/README.md new file mode 100644 index 0000000..498b2f5 --- /dev/null +++ b/boilerplate/openshift/golang-osd-operator-osde2e/README.md @@ -0,0 +1,40 @@ +# Conventions for Ginkgo based e2e tests + +- [Conventions for Ginkgo based e2e tests](#conventions-for-ginkgo-based-e2e-tests) + - [Consuming](#consuming) + - [`make` targets and functions.](#make-targets-and-functions) + - [E2E Test Harness](#e2e-test-harness) + - [Local Testing](#e2e-harness-local-testing) + +## Consuming +Currently, this convention is only intended for OSD operators. To adopt this convention, your `boilerplate/update.cfg` should include: + +``` +openshift/golang-osd-operator-osde2e +``` + +## `make` targets and functions. + +**Note:** Your repository's main `Makefile` needs to be edited to include the +"nexus makefile include": + +``` +include boilerplate/generated-includes.mk +``` + +One of the primary purposes of these `make` targets is to allow you to +standardize your prow and app-sre pipeline configurations using the +following: + +### E2e Test Harness + +| `make` target | Purpose | +|--------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `e2e-harness-generate` | Generate scaffolding for an end to end test harness. The `osde2e/` directory is created where the tests and test runner reside. The harness has access to cloud client and addon passthrough secrets within the test job cluster. Add your operator related ginkgo e2e tests under the `osde2e/_tests.go` file. See [this README](https://github.com/openshift/osde2e-example-test-harness/blob/main/README.md#locally-running-this-example) for more details on test harness. | +| `e2e-harness-build`| Compiles ginkgo tests under osde2e/tests and creates the binary to be used by docker image used by osde2e. | +| `e2e-image-build-push` | Builds osde2e test harness image and pushes to operator's quay repo. Image name is defaulted to -test-harness. Quay repository must be created beforehand. | + +#### E2E Harness Local Testing + +Please follow [this README](https://github.com/openshift/osde2e-example-test-harness/blob/main/README.md#locally-running-this-example) to test your e2e harness with Osde2e locally + diff --git a/boilerplate/openshift/golang-osd-operator-osde2e/e2e-harness-generate.sh b/boilerplate/openshift/golang-osd-operator-osde2e/e2e-harness-generate.sh new file mode 100755 index 0000000..82da26b --- /dev/null +++ b/boilerplate/openshift/golang-osd-operator-osde2e/e2e-harness-generate.sh @@ -0,0 +1,80 @@ +#!/usr/bin/env bash + +set -e + +cmd=${0##*/} + +usage() { + cat < $HARNESS_DIR/Dockerfile + +echo "package osde2etests + +import \"github.com/onsi/ginkgo/v2\" + +var _ = ginkgo.Describe(\"$OPERATOR_NAME\", func() { + // Add your tests +}) +" > ${HARNESS_DIR}/${OPERATOR_HYPHEN_NAME}_tests.go + +echo "// THIS FILE IS GENERATED BY BOILERPLATE. DO NOT EDIT. +//go:build integration +// +build integration + +package osde2etests + +import ( + \"path/filepath\" + \"testing\" + . \"github.com/onsi/ginkgo/v2\" + . \"github.com/onsi/gomega\" +) + +const ( + testResultsDirectory = \"/test-run-results\" + jUnitOutputFilename = \"junit-example-addon.xml\" +) + +// Test entrypoint. osde2e runs this as a test suite on test pod. +func $REPLACE_FUNC(t *testing.T) { + RegisterFailHandler(Fail) + + suiteConfig, reporterConfig := GinkgoConfiguration() + reporterConfig.JUnitReport = filepath.Join(testResultsDirectory, jUnitOutputFilename) + RunSpecs(t, \"$REPLACE_SPECNAME\", suiteConfig, reporterConfig) + +} +" > ${HARNESS_DIR}/${OPERATOR_HYPHEN_NAME}_runner_test.go \ No newline at end of file diff --git a/boilerplate/openshift/golang-osd-operator-osde2e/e2e-image-build-push.sh b/boilerplate/openshift/golang-osd-operator-osde2e/e2e-image-build-push.sh new file mode 100755 index 0000000..b087279 --- /dev/null +++ b/boilerplate/openshift/golang-osd-operator-osde2e/e2e-image-build-push.sh @@ -0,0 +1,53 @@ +#!/usr/bin/env bash + +set -ev + +usage() { + cat < Podman uses --authfile=PATH *after* the `login` subcommand; but +# also accepts REGISTRY_AUTH_FILE from the env. See +# https://www.mankier.com/1/podman-login#Options---authfile=path +export REGISTRY_AUTH_FILE = ${CONTAINER_ENGINE_CONFIG_DIR}/config.json +# If this configuration file doesn't exist, podman will error out. So +# we'll create it if it doesn't exist. +ifeq (,$(wildcard $(REGISTRY_AUTH_FILE))) +$(shell mkdir -p $(CONTAINER_ENGINE_CONFIG_DIR)) +$(shell echo '{}' > $(REGISTRY_AUTH_FILE)) +endif +# ==> Docker uses --config=PATH *before* (any) subcommand; so we'll glue +# that to the CONTAINER_ENGINE variable itself. (NOTE: I tried half a +# dozen other ways to do this. This was the least ugly one that actually +# works.) +ifndef CONTAINER_ENGINE +CONTAINER_ENGINE=$(shell command -v podman 2>/dev/null || echo docker --config=$(CONTAINER_ENGINE_CONFIG_DIR)) +endif + +REGISTRY_USER ?= +REGISTRY_TOKEN ?= + +# TODO: Figure out how to discover this dynamically +OSDE2E_CONVENTION_DIR := boilerplate/openshift/golang-osd-operator-osde2e + +# TODO: figure out how to container-engine-login only once across multiple `make` calls +.PHONY: container-build-push-one +container-build-push-one: container-engine-login + @(if [[ -z "${IMAGE_URI}" ]]; then echo "Must specify IMAGE_URI"; exit 1; fi) + @(if [[ -z "${DOCKERFILE_PATH}" ]]; then echo "Must specify DOCKERFILE_PATH"; exit 1; fi) + ${CONTAINER_ENGINE} build --pull -f $(DOCKERFILE_PATH) -t $(IMAGE_URI) . + ${CONTAINER_ENGINE} push ${IMAGE_URI} + +# log into quay.io +.PHONY: container-engine-login +container-engine-login: + @test "${REGISTRY_USER}" != "" && test "${REGISTRY_TOKEN}" != "" || (echo "REGISTRY_USER and REGISTRY_TOKEN must be defined" && exit 1) + mkdir -p ${CONTAINER_ENGINE_CONFIG_DIR} + @${CONTAINER_ENGINE} login -u="${REGISTRY_USER}" -p="${REGISTRY_TOKEN}" quay.io + +###################### +# Targets used by osde2e test harness +###################### + +# create e2e scaffolding +.PHONY: e2e-harness-generate +e2e-harness-generate: + ${OSDE2E_CONVENTION_DIR}/e2e-harness-generate.sh $(OPERATOR_NAME) $(OSDE2E_CONVENTION_DIR) + +# create binary +.PHONY: e2e-harness-build +e2e-harness-build: GOFLAGS_MOD=-mod=mod +e2e-harness-build: GOENV=GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 GOFLAGS="${GOFLAGS_MOD}" +e2e-harness-build: + go mod tidy + ${GOENV} go test ./osde2e -v -c --tags=integration -o harness.test + +# TODO: Push to a known image tag and commit id +# push harness image +.PHONY: e2e-image-build-push +e2e-image-build-push: + ${OSDE2E_CONVENTION_DIR}/e2e-image-build-push.sh "./osde2e/Dockerfile $(IMAGE_REGISTRY)/$(IMAGE_REPOSITORY)/$(HARNESS_IMAGE_NAME):latest" diff --git a/boilerplate/update.cfg b/boilerplate/update.cfg index c353e73..a23dda8 100644 --- a/boilerplate/update.cfg +++ b/boilerplate/update.cfg @@ -1 +1,2 @@ openshift/golang-osd-operator +openshift/golang-osd-operator-osde2e diff --git a/osde2e/Dockerfile b/osde2e/Dockerfile index b4ccd7e..5d338da 100644 --- a/osde2e/Dockerfile +++ b/osde2e/Dockerfile @@ -1,13 +1,13 @@ + +# THIS FILE IS GENERATED BY BOILERPLATE. DO NOT EDIT. FROM registry.ci.openshift.org/openshift/release:golang-1.18 AS builder ENV PKG=/go/src/github.com/openshift/aws-vpce-operator/ WORKDIR ${PKG} - -# compile test binary COPY . . FROM registry.access.redhat.com/ubi7/ubi-minimal:latest -COPY osde2e.test osde2e.test +COPY ./harness.test harness.test -ENTRYPOINT [ "/osde2e.test" ] \ No newline at end of file +ENTRYPOINT [ "/harness.test" ] diff --git a/osde2e/avo_test_harness_suite_test.go b/osde2e/aws_vpce_operator_runner_test.go similarity index 63% rename from osde2e/avo_test_harness_suite_test.go rename to osde2e/aws_vpce_operator_runner_test.go index f5e5f96..e00e222 100644 --- a/osde2e/avo_test_harness_suite_test.go +++ b/osde2e/aws_vpce_operator_runner_test.go @@ -1,15 +1,14 @@ +// THIS FILE IS GENERATED BY BOILERPLATE. DO NOT EDIT. //go:build integration // +build integration -package exampleaddontestharness +package osde2etests import ( - "path/filepath" - "testing" - . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - _ "github.com/openshift/aws-vpce-operator/osde2e/tests" + "path/filepath" + "testing" ) const ( @@ -17,11 +16,12 @@ const ( jUnitOutputFilename = "junit-example-addon.xml" ) -func TestExampleAddonTestHarness(t *testing.T) { +// Test entrypoint. osde2e runs this as a test suite on test pod. +func AwsVpceOperator(t *testing.T) { RegisterFailHandler(Fail) suiteConfig, reporterConfig := GinkgoConfiguration() reporterConfig.JUnitReport = filepath.Join(testResultsDirectory, jUnitOutputFilename) - RunSpecs(t, "AVO Test Harness", suiteConfig, reporterConfig) + RunSpecs(t, "Aws Vpce Operator", suiteConfig, reporterConfig) } diff --git a/osde2e/aws_vpce_operator_tests.go b/osde2e/aws_vpce_operator_tests.go new file mode 100644 index 0000000..4b62c77 --- /dev/null +++ b/osde2e/aws_vpce_operator_tests.go @@ -0,0 +1,7 @@ +package osde2etests + +import "github.com/onsi/ginkgo/v2" + +var _ = ginkgo.Describe("aws-vpce-operator", func() { + // Add your tests +}) diff --git a/osde2e/harness-build-push.sh b/osde2e/harness-build-push.sh deleted file mode 100755 index e67a396..0000000 --- a/osde2e/harness-build-push.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env bash - -HARNESS_IMAGE="quay.io/app-sre/aws-vpce-operator-test-harness" -# Detect the container engine to use, allowing override from the env -CONTAINER_ENGINE=${CONTAINER_ENGINE:-$(command -v podman || command -v docker || true)} -if [[ -z "$CONTAINER_ENGINE" ]]; then - echo "WARNING: Couldn't find a container engine! Defaulting to docker." - CONTAINER_ENGINE=docker -fi - -${CONTAINER_ENGINE} build --pull osde2e --tag ${HARNESS_IMAGE} -if [ $? -ne 0 ] ; then - echo "docker build failed, exiting..." - exit 1 -fi - -${CONTAINER_ENGINE} push ${HARNESS_IMAGE} diff --git a/osde2e/tests/avo_tests.go b/osde2e/tests/avo_tests.go deleted file mode 100644 index 0d42bbd..0000000 --- a/osde2e/tests/avo_tests.go +++ /dev/null @@ -1,43 +0,0 @@ -package tests - -import ( - "context" - "log" - - "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - _ "k8s.io/client-go/kubernetes" - - "k8s.io/client-go/rest" -) - -var crdName = "" - -var _ = ginkgo.Describe("AVO Tests", func() { - defer ginkgo.GinkgoRecover() - config, err := rest.InClusterConfig() - - if err != nil { - panic(err) - } - - ginkgo.It(" CRD - "+crdName+" - exists", func() { - apiextensions, err := clientset.NewForConfig(config) - Expect(err).NotTo(HaveOccurred()) - - // Make sure the CRD exists - result, err := apiextensions.ApiextensionsV1().CustomResourceDefinitions().Get(context.TODO(), crdName, v1.GetOptions{}) - - if err != nil { - log.Printf("CRD %v not found: %v", crdName, err.Error()) - } else { - log.Printf("CRD %v found: %v", crdName, result) - } - - Expect(err).NotTo(HaveOccurred()) - }, float64(30)) - -})