From 2f10fb80a802cd19b8f343fe0fc1ec79f1cfb38a Mon Sep 17 00:00:00 2001 From: decentral1se Date: Wed, 2 Aug 2023 10:47:44 +0200 Subject: [PATCH] wip: automated integration tests --- .drone.yml | 129 +++++++++++++++-------------- cli/app/new.go | 10 +-- tests/integration/Dockerfile | 16 ++++ tests/integration/helpers.sh | 9 ++ tests/integration/integration.bats | 61 ++++++++++++++ 5 files changed, 160 insertions(+), 65 deletions(-) create mode 100644 tests/integration/Dockerfile create mode 100644 tests/integration/helpers.sh create mode 100644 tests/integration/integration.bats diff --git a/.drone.yml b/.drone.yml index 17c563ad..2cff097b 100644 --- a/.drone.yml +++ b/.drone.yml @@ -2,68 +2,77 @@ kind: pipeline name: coopcloud.tech/abra steps: - - name: make check - image: golang:1.20 + # - name: run gofmt + # image: golang:1.20 + # commands: + # - make check + + # - name: build binaries + # image: golang:1.20 + # commands: + # - make check + # depends_on: + # - run gofmt + + # - name: run unit tests + # image: golang:1.20 + # commands: + # - make test + # depends_on: + # - run gofmt + + - name: run integration tests + image: decentral1se/abra-int-test:latest commands: - - make check + - bats tests/integration/integration.bats + # depends_on: + # - run gofmt - - name: make build - image: golang:1.20 - commands: - - make build - depends_on: - - make check + # - name: fetch all tags + # image: docker:git + # commands: + # - git fetch --tags + # depends_on: + # - run gofmt + # - build binaries + # - run unit tests + # - run integration tests + # when: + # event: tag - - name: make test - image: golang:1.20 - commands: - - make test - depends_on: - - make check + # - name: publish release + # image: goreleaser/goreleaser:v1.18.2 + # environment: + # GITEA_TOKEN: + # from_secret: goreleaser_gitea_token + # volumes: + # - name: deps + # path: /go + # commands: + # - goreleaser release + # depends_on: + # - fetch all tags + # when: + # event: tag - - name: fetch - image: docker:git - commands: - - git fetch --tags - depends_on: - - make check - - make build - - make test - when: - event: tag + # - name: publish image + # image: plugins/docker + # settings: + # auto_tag: true + # username: 3wordchant + # password: + # from_secret: git_coopcloud_tech_token_3wc + # repo: git.coopcloud.tech/coop-cloud/abra + # tags: dev + # registry: git.coopcloud.tech + # when: + # event: + # exclude: + # - pull_request + # depends_on: + # - run gofmt + # - build binaries - - name: release - image: goreleaser/goreleaser:v1.18.2 - environment: - GITEA_TOKEN: - from_secret: goreleaser_gitea_token - volumes: - - name: deps - path: /go - commands: - - goreleaser release - depends_on: - - fetch - when: - event: tag - - - name: publish image - image: plugins/docker - settings: - auto_tag: true - username: 3wordchant - password: - from_secret: git_coopcloud_tech_token_3wc - repo: git.coopcloud.tech/coop-cloud/abra - tags: dev - registry: git.coopcloud.tech - when: - event: - exclude: - - pull_request - depends_on: - - make check - -volumes: - - name: deps - temp: {} +# volumes: +# - name: deps +# temp: {} diff --git a/cli/app/new.go b/cli/app/new.go index ac8e194c..9034b3bc 100644 --- a/cli/app/new.go +++ b/cli/app/new.go @@ -93,14 +93,14 @@ var appNewCommand = cli.Command{ logrus.Fatal(err) } - cl, err := client.New(internal.NewAppServer) - if err != nil { - logrus.Fatal(err) - } - var secrets AppSecrets var secretTable *jsontable.JSONTable if internal.Secrets { + cl, err := client.New(internal.NewAppServer) + if err != nil { + logrus.Fatal(err) + } + secrets, err := createSecrets(cl, sanitisedAppName) if err != nil { logrus.Fatal(err) diff --git a/tests/integration/Dockerfile b/tests/integration/Dockerfile new file mode 100644 index 00000000..08a36d82 --- /dev/null +++ b/tests/integration/Dockerfile @@ -0,0 +1,16 @@ +FROM debian:12-slim + +RUN apt update && \ + apt install -y --no-install-recommends \ + bats \ + bats-assert \ + bats-support \ + ca-certificates \ + curl \ + expect \ + git \ + golang \ + make \ + openssl + +RUN curl -fsSL https://get.docker.com | sh diff --git a/tests/integration/helpers.sh b/tests/integration/helpers.sh new file mode 100644 index 00000000..7626fb99 --- /dev/null +++ b/tests/integration/helpers.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +_build_abra() { + if [[ ! -e "$ROOT/abra" ]]; then + cd "$ROOT" && make build-dev + return 0 + fi + return 0 +} diff --git a/tests/integration/integration.bats b/tests/integration/integration.bats new file mode 100644 index 00000000..3ae9a27e --- /dev/null +++ b/tests/integration/integration.bats @@ -0,0 +1,61 @@ +setup() { + load '/usr/lib/bats/bats-support/load' + load '/usr/lib/bats/bats-assert/load' + + # get test binaries on $PATH for testing + DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" + ROOT="$DIR/../.." + + source "$DIR/helpers.sh" + _build_abra +} + +@test "build test abra" { + run $ROOT/abra -h + assert_output --partial 'The Co-op Cloud command-line utility belt' +} + +@test "create new recipe" { + run $ROOT/abra recipe new foobar + assert_output --partial 'Your new foobar recipe has been created' + + mkdir -p "$HOME/.abra/servers/example.com" + if ! grep -R -q "example.com" "$HOME/.docker"; then + docker context create --docker host=ssh://foo@example.com:222 example.com + fi + + run $ROOT/abra app new foobar \ + --no-input \ + --server example.com \ + --domain foobar.example.com + + assert_output --partial 'A new foobar app has been created!' + + # TODO: abra app deploy foobar.example.com + # we need an actual server for this to ensure things work maybe we can get + # a donated server from the comrades for this? + + rm -r "$HOME/.abra/recipes/foobar" + rm -r "$HOME/.abra/servers/example.com" +} + +@test "upgrade and sync a recipe" { + run $ROOT/abra recipe fetch gitea + cd "$HOME/.abra/recipes/gitea" && git checkout "2.3.1+1.20.1-rootless" + + run $ROOT/abra recipe upgrade gitea --offline --machine + assert_output --partial '1.20.2-rootless' + + # TODO: when passing --patch, there is no output so we can't test against + # anything... we should add info logging when automation flags are passed + # so we can assert + run $ROOT/abra recipe upgrade gitea --offline --patch + + run $ROOT/abra recipe sync gitea --offline --patch + assert_output --partial \ + 'synced label coop-cloud.${STACK_NAME}.version=2.3.2+1.20.2-rootless to service app' + + # TODO: assert git output + + cd "$HOME/.abra/recipes/gitea" && git checkout . && git checkout master +}