diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml new file mode 100644 index 0000000..cf6ee7c --- /dev/null +++ b/.github/workflows/e2e.yaml @@ -0,0 +1,36 @@ +name: "End-to-end tests" + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + e2e-tests: + name: End-to-end tests + runs-on: ubuntu-20.04 + strategy: + matrix: + kube-version: + - "1.21" + - "1.22" + - "1.23" + + steps: + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.17 + + - name: Check out code into the Go module directory + uses: actions/checkout@v2.4.0 + + - name: "install kuttl" + run: ./hack/install-kuttl.sh + + - name: "run tests" + env: + KUBE_VERSION: ${{ matrix.kube-version }} + run: make prepare-e2e e2e KUBE_VERSION=$KUBE_VERSION diff --git a/Makefile b/Makefile index 22616df..45c0d8b 100644 --- a/Makefile +++ b/Makefile @@ -55,6 +55,10 @@ SHELL = /usr/bin/env bash -o pipefail all: build +## Kind Test +KUBE_VERSION ?= 1.21 +KIND_CONFIG ?= kind-$(KUBE_VERSION).yaml + ##@ General # The help target prints out all targets with their descriptions organized @@ -105,6 +109,77 @@ sample_deployment: delete_sample_deployment: kubectl delete -f config/samples/sample-deployment.yaml +# end-to-tests +# go-get-tool will 'go get' any package $2 and install it to $1. +PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) +define go-get-tool +@[ -f $(1) ] || { \ +set -e ;\ +TMP_DIR=$$(mktemp -d) ;\ +cd $$TMP_DIR ;\ +go mod init tmp ;\ +echo "Downloading $(2)" ;\ +go get -d $(2)@$(3) ;\ +GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\ +rm -rf $$TMP_DIR ;\ +} +endef + +.PHONY: kuttl +kuttl: +ifeq (, $(shell which kubectl-kuttl)) + echo ${PATH} + ls -l /usr/local/bin + which kubectl-kuttl + @{ \ + set -e ;\ + echo "" ;\ + echo "ERROR: kuttl not found." ;\ + echo "Please check https://kuttl.dev/docs/cli.html for installation instructions and try again." ;\ + echo "" ;\ + exit 1 ;\ + } +else +KUTTL=$(shell which kubectl-kuttl) +endif + +.PHONY: kind +kind: +ifeq (, $(shell which kind)) + @{ \ + set -e ;\ + echo "" ;\ + echo "ERROR: kind not found." ;\ + echo "Please check https://kind.sigs.k8s.io/docs/user/quick-start/#installation for installation instructions and try again." ;\ + echo "" ;\ + exit 1 ;\ + } +else +KIND=$(shell which kind) +endif + +.PHONY: set-image-controller +set-image-controller: manifests kustomize + cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} + +.PHONY: container +container: + podman build -t ${IMG} . + +.PHONY: start-kind +start-kind: + kind create cluster --config $(KIND_CONFIG) + +.PHONY: e2e +e2e: + $(KUTTL) test + +.PHONY: prepare-e2e +prepare-e2e: kuttl set-image-controller container start-kind + mkdir -p tests/_build/crds tests/_build/manifests + $(KUSTOMIZE) build config/default -o tests/_build/manifests/snoopy-operator.yaml + $(KUSTOMIZE) build config/crd -o tests/_build/crds/ + ##@ Build build: generate fmt vet ## Build manager binary. go build -o bin/manager main.go diff --git a/hack/install-kuttl.sh b/hack/install-kuttl.sh new file mode 100755 index 0000000..783a451 --- /dev/null +++ b/hack/install-kuttl.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +sudo curl -Lo /usr/local/bin/kubectl-kuttl https://github.com/kudobuilder/kuttl/releases/download/v0.11.1/kubectl-kuttl_0.11.1_linux_x86_64 +sudo chmod +x /usr/local/bin/kubectl-kuttl +export PATH=$PATH:/usr/local/bin \ No newline at end of file diff --git a/kind-1.21.yaml b/kind-1.21.yaml new file mode 100644 index 0000000..1bcf4f9 --- /dev/null +++ b/kind-1.21.yaml @@ -0,0 +1,12 @@ +kind: Cluster +apiVersion: kind.x-k8s.io/v1alpha4 +nodes: +- role: control-plane + image: quay.io/fennec-project/kindnode-crio:1.21@sha256:3c43afca813cf724e02efe70bf0ee0ceee498b49ee746781f6951db5bafb6b39 + kubeadmConfigPatches: + - | + kind: InitConfiguration + nodeRegistration: + criSocket: unix:///run/crio/crio.sock + kubeletExtraArgs: + cgroup-driver: cgroupfs \ No newline at end of file diff --git a/kind-1.22.yaml b/kind-1.22.yaml new file mode 100644 index 0000000..1250212 --- /dev/null +++ b/kind-1.22.yaml @@ -0,0 +1,12 @@ +kind: Cluster +apiVersion: kind.x-k8s.io/v1alpha4 +nodes: +- role: control-plane + image: quay.io/fennec-project/kindnode-crio:1.22@sha256:46948065311fd050ae9fb5bf7af76674ad167d9f1a41fba0460bc1467424abc2 + kubeadmConfigPatches: + - | + kind: InitConfiguration + nodeRegistration: + criSocket: unix:///run/crio/crio.sock + kubeletExtraArgs: + cgroup-driver: cgroupfs \ No newline at end of file diff --git a/kind-1.23.yaml b/kind-1.23.yaml new file mode 100644 index 0000000..d9ebb41 --- /dev/null +++ b/kind-1.23.yaml @@ -0,0 +1,12 @@ +kind: Cluster +apiVersion: kind.x-k8s.io/v1alpha4 +nodes: +- role: control-plane + image: quay.io/fennec-project/kindnode-crio:1.23@sha256:76548d7eff138dae6dd9c29bfc55a836fb2ed87a19f234f7d3e6c9f8bb0eb71c + kubeadmConfigPatches: + - | + kind: InitConfiguration + nodeRegistration: + criSocket: unix:///run/crio/crio.sock + kubeletExtraArgs: + cgroup-driver: cgroupfs \ No newline at end of file diff --git a/kuttl-test.yaml b/kuttl-test.yaml new file mode 100644 index 0000000..471ef2d --- /dev/null +++ b/kuttl-test.yaml @@ -0,0 +1,23 @@ +apiVersion: kuttl.dev/v1beta1 +kind: TestSuite +crdDir: ./tests/_build/crds/ +artifactsDir: ./tests/_build/artifacts/ +kindContainers: + - local/snoopy-operator:e2e +commands: + - command: kubectl apply -f ./tests/_build/manifests/snoopy-operator.yaml + - command: kubectl get pods -n snoopy-operator + - command: sleep 5s + - command: kubectl get pods -n snoopy-operator + - command: sleep 10s + - command: kubectl get pods -n snoopy-operator + - command: sleep 10s + - command: kubectl get pods -n snoopy-operator + - command: sleep 10s + - command: kubectl get pods -n snoopy-operator + - command: sleep 10s + - command: kubectl wait --timeout=5m --for=condition=available deployment snoopy-operator -n snoopy-operator + - command: sleep 5s +testDirs: + - ./tests/e2e/ +timeout: 300 diff --git a/tests/e2e/snoopyjob-tcpdump/00-assert.yaml b/tests/e2e/snoopyjob-tcpdump/00-assert.yaml new file mode 100644 index 0000000..35d9e37 --- /dev/null +++ b/tests/e2e/snoopyjob-tcpdump/00-assert.yaml @@ -0,0 +1,13 @@ +apiVersion: job.fennecproject.io/v1alpha1 +kind: SnoopyJob +metadata: + name: snoopy-samplejob +spec: + args: -i eth0 -U -w - + command: tcpdump + labelSelector: + networkMonitor: "true" + targetNamespace: cnf-telco + timer: 30s +status: + cronJobList: \ No newline at end of file diff --git a/tests/e2e/snoopyjob-tcpdump/00-install.yaml b/tests/e2e/snoopyjob-tcpdump/00-install.yaml new file mode 100644 index 0000000..5fb69b0 --- /dev/null +++ b/tests/e2e/snoopyjob-tcpdump/00-install.yaml @@ -0,0 +1,14 @@ +apiVersion: job.fennecproject.io/v1alpha1 +kind: SnoopyJob +metadata: + name: snoopy-samplejob +spec: + command: "tcpdump" + args: "-n -i eth0" + labelSelector: { + networkMonitor: "true", + } + targetNamespace: cnf-telco + timer: "30s" + dataServiceIP: "snoopy-data-svc.snoopy-operator.svc.cluster.local" + dataServicePort: "51001" \ No newline at end of file