From 62ddaf9e23cadeeaf5551776afa39e2952cd41d7 Mon Sep 17 00:00:00 2001 From: Manuel Vogel Date: Sat, 1 Jun 2019 12:01:42 +0200 Subject: [PATCH] Refactors test setup (#156) * refactors test setup according to the postgres provider * updates readme for new test setup --- GNUmakefile | 8 +++- README.md | 15 +++++++ scripts/runAccTests.sh | 83 -------------------------------------- scripts/testacc_cleanup.sh | 22 ++++++++++ scripts/testacc_full.sh | 29 +++++++++++++ scripts/testacc_setup.sh | 40 ++++++++++++++++++ 6 files changed, 113 insertions(+), 84 deletions(-) delete mode 100755 scripts/runAccTests.sh create mode 100755 scripts/testacc_cleanup.sh create mode 100755 scripts/testacc_full.sh create mode 100755 scripts/testacc_setup.sh diff --git a/GNUmakefile b/GNUmakefile index db163619..02da3223 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -13,8 +13,14 @@ test: fmtcheck echo $(TEST) | \ xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4 +testacc_setup: fmtcheck + @sh -c "'$(CURDIR)/scripts/testacc_setup.sh'" + testacc: fmtcheck - @sh -c "'$(CURDIR)/scripts/runAccTests.sh'" + @sh -c "'$(CURDIR)/scripts/testacc_full.sh'" + +testacc_cleanup: fmtcheck + @sh -c "'$(CURDIR)/scripts/testacc_cleanup.sh'" compile: fmtcheck @sh -c "'$(CURDIR)/scripts/compile.sh'" diff --git a/README.md b/README.md index 9eae9dc6..4011f32e 100644 --- a/README.md +++ b/README.md @@ -62,9 +62,24 @@ In order to run the full suite of Acceptance tests, run `make testacc`. $ make testacc ``` +In order to run only single Acceptance tests, execute the following steps: + +```sh +# setup the testing environment +$ make testacc_setup + +# run single tests +TF_LOG=INFO TF_ACC=1 go test -v ./docker -run ^TestAccDockerImage_data_private_config_file$ -timeout 360s + +# cleanup the local testing resources +$ make testacc_cleanup +``` + In order to extend the provider and test it with `terraform`, build the provider as mentioned above with ```sh $ make build +# or a specific version +$ go build -o terraform-provider-docker_v1.2.0_x4 ``` Remove an explicit version of the provider you develop, because `terraform` will fetch diff --git a/scripts/runAccTests.sh b/scripts/runAccTests.sh deleted file mode 100755 index 8e53b468..00000000 --- a/scripts/runAccTests.sh +++ /dev/null @@ -1,83 +0,0 @@ -#!/bin/bash -set -e - -log() { - echo "####################" - echo "## -> $1 " - echo "####################" -} - -setup() { - # Create self signed certs - mkdir -p "$(pwd)"/scripts/testing/certs - openssl req \ - -newkey rsa:2048 \ - -nodes \ - -x509 \ - -days 365 \ - -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=127.0.0.1" \ - -keyout "$(pwd)"/scripts/testing/certs/registry_auth.key \ - -out "$(pwd)"/scripts/testing/certs/registry_auth.crt - # Create auth - mkdir -p "$(pwd)"/scripts/testing/auth - # Start registry - docker run --rm --entrypoint htpasswd registry:2 -Bbn testuser testpwd > "$(pwd)"/scripts/testing/auth/htpasswd - docker run -d -p 15000:5000 --rm --name private_registry \ - -v "$(pwd)"/scripts/testing/auth:/auth \ - -e "REGISTRY_AUTH=htpasswd" \ - -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \ - -e "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" \ - -v "$(pwd)"/scripts/testing/certs:/certs \ - -e "REGISTRY_HTTP_TLS_CERTIFICATE=/certs/registry_auth.crt" \ - -e "REGISTRY_HTTP_TLS_KEY=/certs/registry_auth.key" \ - registry:2 - # wait a bit for travis... - sleep 5 - # Login to private registry - docker login -u testuser -p testpwd 127.0.0.1:15000 - # Build private images - for i in $(seq 1 3); do - docker build -t tftest-service --build-arg JS_FILE_PATH=server_v${i}.js "$(pwd)"/scripts/testing -f "$(pwd)"/scripts/testing/Dockerfile - docker tag tftest-service 127.0.0.1:15000/tftest-service:v${i} - docker push 127.0.0.1:15000/tftest-service:v${i} - docker tag tftest-service 127.0.0.1:15000/tftest-service - docker push 127.0.0.1:15000/tftest-service - done - # Remove images from host machine before starting the tests - for i in $(docker images -aq 127.0.0.1:15000/tftest-service); do docker rmi -f "$i"; done -} - -run() { - go clean -testcache - TF_ACC=1 go test ./docker -v -timeout 120m - - # for a single test comment the previous line and uncomment the next line - #TF_LOG=DEBUG TF_ACC=1 go test -v ./docker -run ^TestAccDockerService_updateMultiplePropertiesConverge$ -timeout 360s - - # keep the return value for the scripts to fail and clean properly - return $? -} - -cleanup() { - echo "### unsetted env ###" - for p in $(docker container ls -f 'name=private_registry' -q); do docker stop $p; done - echo "### stopped private registry ###" - rm -f "$(pwd)"/scripts/testing/auth/htpasswd - rm -f "$(pwd)"/scripts/testing/certs/registry_auth.* - echo "### removed auth and certs ###" - for resource in "container" "volume"; do - for r in $(docker $resource ls -f 'name=tftest-' -q); do docker $resource rm -f "$r"; done - echo "### removed $resource ###" - done - for resource in "config" "secret" "network"; do - for r in $(docker $resource ls -f 'name=tftest-' -q); do docker $resource rm "$r"; done - echo "### removed $resource ###" - done - for i in $(docker images -aq 127.0.0.1:15000/tftest-service); do docker rmi -f "$i"; done - echo "### removed service images ###" -} - -## main -log "setup" && setup -log "run" && run || (log "cleanup" && cleanup && exit 1) -log "cleanup" && cleanup diff --git a/scripts/testacc_cleanup.sh b/scripts/testacc_cleanup.sh new file mode 100755 index 00000000..f8a477bd --- /dev/null +++ b/scripts/testacc_cleanup.sh @@ -0,0 +1,22 @@ +#!/bin/bash +set -e + +for p in $(docker container ls -f 'name=private_registry' -q); do docker stop $p; done +echo "### stopped private registry ###" + +rm -f "$(pwd)"/scripts/testing/auth/htpasswd +rm -f "$(pwd)"/scripts/testing/certs/registry_auth.* +echo "### removed auth and certs ###" + +for resource in "container" "volume"; do + for r in $(docker $resource ls -f 'name=tftest-' -q); do docker $resource rm -f "$r"; done + echo "### removed $resource ###" +done + +for resource in "config" "secret" "network"; do + for r in $(docker $resource ls -f 'name=tftest-' -q); do docker $resource rm "$r"; done + echo "### removed $resource ###" +done + +for i in $(docker images -aq 127.0.0.1:15000/tftest-service); do docker rmi -f "$i"; done +echo "### removed service images ###" diff --git a/scripts/testacc_full.sh b/scripts/testacc_full.sh new file mode 100755 index 00000000..c24735e6 --- /dev/null +++ b/scripts/testacc_full.sh @@ -0,0 +1,29 @@ +#!/bin/bash +set -e + +log() { + echo "####################" + echo "## -> $1 " + echo "####################" +} + +setup() { + sh "$(pwd)"/scripts/testacc_setup.sh +} + +run() { + go clean -testcache + TF_ACC=1 go test ./docker -v -timeout 120m + + # keep the return value for the scripts to fail and clean properly + return $? +} + +cleanup() { + sh "$(pwd)"/scripts/testacc_cleanup.sh +} + +## main +log "setup" && setup +log "run" && run || (log "cleanup" && cleanup && exit 1) +log "cleanup" && cleanup diff --git a/scripts/testacc_setup.sh b/scripts/testacc_setup.sh new file mode 100755 index 00000000..34766bab --- /dev/null +++ b/scripts/testacc_setup.sh @@ -0,0 +1,40 @@ +#!/bin/bash +set -e + +# Create self signed certs +mkdir -p "$(pwd)"/scripts/testing/certs +openssl req \ + -newkey rsa:2048 \ + -nodes \ + -x509 \ + -days 365 \ + -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=127.0.0.1" \ + -keyout "$(pwd)"/scripts/testing/certs/registry_auth.key \ + -out "$(pwd)"/scripts/testing/certs/registry_auth.crt +# Create auth +mkdir -p "$(pwd)"/scripts/testing/auth +# Start registry +docker run --rm --entrypoint htpasswd registry:2 -Bbn testuser testpwd > "$(pwd)"/scripts/testing/auth/htpasswd +docker run -d -p 15000:5000 --rm --name private_registry \ + -v "$(pwd)"/scripts/testing/auth:/auth \ + -e "REGISTRY_AUTH=htpasswd" \ + -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \ + -e "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" \ + -v "$(pwd)"/scripts/testing/certs:/certs \ + -e "REGISTRY_HTTP_TLS_CERTIFICATE=/certs/registry_auth.crt" \ + -e "REGISTRY_HTTP_TLS_KEY=/certs/registry_auth.key" \ + registry:2 +# wait a bit for travis... +sleep 5 +# Login to private registry +docker login -u testuser -p testpwd 127.0.0.1:15000 +# Build private images +for i in $(seq 1 3); do + docker build -t tftest-service --build-arg JS_FILE_PATH=server_v${i}.js "$(pwd)"/scripts/testing -f "$(pwd)"/scripts/testing/Dockerfile + docker tag tftest-service 127.0.0.1:15000/tftest-service:v${i} + docker push 127.0.0.1:15000/tftest-service:v${i} + docker tag tftest-service 127.0.0.1:15000/tftest-service + docker push 127.0.0.1:15000/tftest-service +done +# Remove images from host machine before starting the tests +for i in $(docker images -aq 127.0.0.1:15000/tftest-service); do docker rmi -f "$i"; done