From fe255dd3c89c95e793a39c0f8daedf8aede98273 Mon Sep 17 00:00:00 2001 From: Arnaud Tincelin Date: Thu, 9 Sep 2021 10:14:46 +0200 Subject: [PATCH] Run tests inside the cluster --- .github/kustomize/job.yaml | 54 ++++++++++++++++++++++++++++ .github/kustomize/kustomization.yaml | 20 +++++++++++ .github/workflows/ci-pipeline.yaml | 35 ++++++++++++------ builder/Dockerfile.tests | 14 ++++++++ go.mod | 4 +-- 5 files changed, 115 insertions(+), 12 deletions(-) create mode 100644 .github/kustomize/job.yaml create mode 100644 .github/kustomize/kustomization.yaml create mode 100644 builder/Dockerfile.tests diff --git a/.github/kustomize/job.yaml b/.github/kustomize/job.yaml new file mode 100644 index 00000000..7f61c83f --- /dev/null +++ b/.github/kustomize/job.yaml @@ -0,0 +1,54 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: aks-periscope + labels: + app: aks-periscope +spec: + backoffLimit: 4 + template: + metadata: + labels: + app: aks-periscope + spec: + restartPolicy: Never + containers: + - name: go-test-runner + image: go-test-runner + envFrom: + - configMapRef: + name: containerlogs-config + - configMapRef: + name: kubeobjects-config + - configMapRef: + name: nodelogs-config + volumeMounts: + - name: varlog + mountPath: /var/log + - name: resolvlog + mountPath: /run/systemd/resolve + - name: etcvmlog + mountPath: /etchostlogs + - name: runner + mountPath: /runner + resources: + requests: + memory: "500Mi" + cpu: "250m" + limits: + memory: "2000Mi" + cpu: "1000m" + volumes: + - name: varlog + hostPath: + path: /var/log + - name: resolvlog + hostPath: + path: /run/systemd/resolve + - name: etcvmlog + hostPath: + path: /etc + - name: runner + hostPath: + path: /runner + type: Directory diff --git a/.github/kustomize/kustomization.yaml b/.github/kustomize/kustomization.yaml new file mode 100644 index 00000000..56b3d576 --- /dev/null +++ b/.github/kustomize/kustomization.yaml @@ -0,0 +1,20 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization + +namespace: aks-periscope + +resources: +- job.yaml +- ../../deployment/namespace.yaml +- ../../deployment/cluster-role.yaml +- ../../deployment/cluster-role-binding.yaml +- ../../deployment/config-map.yaml +- ../../deployment/crd.yaml +- ../../deployment/service-account.yaml + +patches: +- path: patch-command.yaml + target: + kind: Job + name: aks-periscope + version: batch/v1 diff --git a/.github/workflows/ci-pipeline.yaml b/.github/workflows/ci-pipeline.yaml index c3add35c..b85574a4 100644 --- a/.github/workflows/ci-pipeline.yaml +++ b/.github/workflows/ci-pipeline.yaml @@ -68,7 +68,13 @@ jobs: nodes: - role: control-plane - role: worker + extraMounts: + - hostPath: ${PWD} + containerPath: /runner - role: worker + extraMounts: + - hostPath: ${PWD} + containerPath: /runner containerdConfigPatches: - |- [plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:5000"] @@ -98,8 +104,8 @@ jobs: with: context: . push: true - tags: localhost:5000/periscope:foo - file: ./builder/Dockerfile + tags: localhost:5000/go-test-runner:latest + file: ./builder/Dockerfile.tests - name: Deploy dummy helm chart for tests run: | helm repo add bitnami https://charts.bitnami.com/bitnami @@ -107,14 +113,23 @@ jobs: helm install happy-panda bitnami/wordpress --namespace default - name: Deploy AKS Periscope run: | - (cd ./deployment && kustomize edit set image aksrepos.azurecr.io/staging/aks-periscope=localhost:5000/periscope:foo) - kubectl apply -f <(kustomize build ./deployment) - kubectl -n aks-periscope describe ds aks-periscope - kubectl -n aks-periscope wait po --all --for condition=ready --timeout=60s - - name: Go tests - run: go test -v -race -coverprofile=coverage.out -covermode=atomic ./... - - name: Upload coverage to Codecov - run: bash <(curl -s https://codecov.io/bash) -C $(Build.SourceVersion) + cd ./.github/kustomize + + cat <>patch-command.yaml + - op: add + path: '/spec/template/spec/containers/0/command' + value: [ "go", "test", "-v", "-coverprofile=/runner/coverage.out", "-covermode=atomic", "./..." ] + EOF + + cat patch-command.yaml + + kustomize edit set image go-test-runner=localhost:5000/go-test-runner:latest + + kubectl apply -f <(kustomize build --load-restrictor LoadRestrictionsNone) + + kubectl -n aks-periscope wait job aks-periscope --for condition=complete --timeout=120s + - name: Upload coverage to codecov + run: bash <(curl -s https://codecov.io/bash) - name: Stop kind run: kind delete cluster continue-on-error: false diff --git a/builder/Dockerfile.tests b/builder/Dockerfile.tests new file mode 100644 index 00000000..5258b729 --- /dev/null +++ b/builder/Dockerfile.tests @@ -0,0 +1,14 @@ +FROM golang:alpine AS builder + +ENV GO111MODULE=on \ + CGO_ENABLED=0 \ + GOOS=linux \ + GOARCH=amd64 + +WORKDIR /build + +COPY go.mod . +COPY go.sum . +RUN go mod download + +COPY . . diff --git a/go.mod b/go.mod index 85ff0480..e4f102a6 100644 --- a/go.mod +++ b/go.mod @@ -6,9 +6,9 @@ require ( github.com/Azure/azure-storage-blob-go v0.7.0 github.com/Azure/go-autorest/autorest/adal v0.9.14 // indirect github.com/kr/pretty v0.2.1 // indirect - github.com/onsi/gomega v1.13.0 + github.com/onsi/gomega v1.13.0 // indirect helm.sh/helm/v3 v3.6.3 - k8s.io/api v0.21.3 // indirect + k8s.io/api v0.21.3 k8s.io/apimachinery v0.21.3 k8s.io/cli-runtime v0.21.3 k8s.io/client-go v0.21.3