From 5587848de6590f3dcbbbd83181672759d363c62d Mon Sep 17 00:00:00 2001 From: Franco Bruno Lavayen Date: Thu, 7 Jul 2022 18:18:56 -0300 Subject: [PATCH] [CONSUL-183] Create Bats Dockerfile (#8) --- .../envoy/Dockerfile-bats-core-windows | 15 ++++++ .../connect/envoy/Dockerfile-bats-windows | 9 ++++ .../envoy/case-dummy-bats/dummy-function.bash | 4 ++ .../connect/envoy/case-dummy-bats/setup.sh | 1 + .../connect/envoy/case-dummy-bats/vars.sh | 2 + .../envoy/case-dummy-bats/verify_1.bats | 9 ++++ .../envoy/case-dummy-bats/verify_2.bats | 30 ++++++++++++ .../connect/envoy/docker.windows.md | 48 +++++++++++++++---- 8 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 test/integration/connect/envoy/Dockerfile-bats-core-windows create mode 100644 test/integration/connect/envoy/Dockerfile-bats-windows create mode 100644 test/integration/connect/envoy/case-dummy-bats/dummy-function.bash create mode 100644 test/integration/connect/envoy/case-dummy-bats/setup.sh create mode 100644 test/integration/connect/envoy/case-dummy-bats/vars.sh create mode 100644 test/integration/connect/envoy/case-dummy-bats/verify_1.bats create mode 100644 test/integration/connect/envoy/case-dummy-bats/verify_2.bats diff --git a/test/integration/connect/envoy/Dockerfile-bats-core-windows b/test/integration/connect/envoy/Dockerfile-bats-core-windows new file mode 100644 index 000000000000..b587093c20f9 --- /dev/null +++ b/test/integration/connect/envoy/Dockerfile-bats-core-windows @@ -0,0 +1,15 @@ +FROM mcr.microsoft.com/windows/servercore:1809 + +RUN ["powershell", "Set-ExecutionPolicy", "Bypass", "-Scope", "Process", "-Force;"] +RUN ["powershell", "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"] + +RUN choco install git.install -yf + +ENV BATS_URL=https://github.com/bats-core/bats-core/archive/refs/tags/v1.7.0.zip +RUN curl %BATS_URL% -L -o bats.zip + +RUN mkdir bats-core +RUN tar -xf bats.zip -C bats-core --strip-components=1 +RUN cd "C:\\Program Files\\Git\\bin" && bash.exe -c "/c/bats-core/install.sh /c/bats" + +ENTRYPOINT ["C:\\Program Files\\Git\\bin\\bash.exe", "C:\\bats\\bin\\bats"] \ No newline at end of file diff --git a/test/integration/connect/envoy/Dockerfile-bats-windows b/test/integration/connect/envoy/Dockerfile-bats-windows new file mode 100644 index 000000000000..1cf4ee73fc14 --- /dev/null +++ b/test/integration/connect/envoy/Dockerfile-bats-windows @@ -0,0 +1,9 @@ +FROM docker.mirror.hashicorp.services/windows/fortio AS fortio + +FROM docker.mirror.hashicorp.services/windows/bats:1.7.0 + +RUN choco install openssl -yf +RUN choco install jq -yf + +COPY --from=fortio C:\\fortio C:\\fortio +ENV PATH C:\\fortio;%PATH% \ No newline at end of file diff --git a/test/integration/connect/envoy/case-dummy-bats/dummy-function.bash b/test/integration/connect/envoy/case-dummy-bats/dummy-function.bash new file mode 100644 index 000000000000..bce0a9361bd3 --- /dev/null +++ b/test/integration/connect/envoy/case-dummy-bats/dummy-function.bash @@ -0,0 +1,4 @@ +function dummyFunction { + local LOCAL_VAR=$1 + echo $LOCAL_VAR $COMMON_VAR +} diff --git a/test/integration/connect/envoy/case-dummy-bats/setup.sh b/test/integration/connect/envoy/case-dummy-bats/setup.sh new file mode 100644 index 000000000000..6683bf561f49 --- /dev/null +++ b/test/integration/connect/envoy/case-dummy-bats/setup.sh @@ -0,0 +1 @@ +echo $1 >> $2 diff --git a/test/integration/connect/envoy/case-dummy-bats/vars.sh b/test/integration/connect/envoy/case-dummy-bats/vars.sh new file mode 100644 index 000000000000..ef12c16d938a --- /dev/null +++ b/test/integration/connect/envoy/case-dummy-bats/vars.sh @@ -0,0 +1,2 @@ +COMMON_VAR="Common variable" +TXT_FILE_NAME="file.txt" diff --git a/test/integration/connect/envoy/case-dummy-bats/verify_1.bats b/test/integration/connect/envoy/case-dummy-bats/verify_1.bats new file mode 100644 index 000000000000..31007243da01 --- /dev/null +++ b/test/integration/connect/envoy/case-dummy-bats/verify_1.bats @@ -0,0 +1,9 @@ +@test "Basic Test 1" { + result=4 + [ "$result" -eq 4 ] +} + +@test "Basic Test 2" { + result=10 + [ "$result" -eq 10 ] +} diff --git a/test/integration/connect/envoy/case-dummy-bats/verify_2.bats b/test/integration/connect/envoy/case-dummy-bats/verify_2.bats new file mode 100644 index 000000000000..11b4274abf82 --- /dev/null +++ b/test/integration/connect/envoy/case-dummy-bats/verify_2.bats @@ -0,0 +1,30 @@ +#!/usr/bin/env bats + +load dummy-function + +setup() { + source $(pwd)/workdir/vars.sh + source $(pwd)/workdir/setup.sh "Content of the created setup.txt file in setup.sh" $TXT_FILE_NAME +} + +teardown() { + rm $TXT_FILE_NAME +} + +@test "Test with dummyFunction invoked" { + FIRST_ARG="First Argument" + + run dummyFunction "$FIRST_ARG" + + [ $status -eq 0 ] + [ -n "$output" ] # Not empty + [ "$output" = "$FIRST_ARG $COMMON_VAR" ] +} + +@test "Test skipped" { + skip + + run not_existing_function + + [ "$status" -eq 100000 ] +} diff --git a/test/integration/connect/envoy/docker.windows.md b/test/integration/connect/envoy/docker.windows.md index 74fb882c18d1..71b80fa3a8c4 100644 --- a/test/integration/connect/envoy/docker.windows.md +++ b/test/integration/connect/envoy/docker.windows.md @@ -6,6 +6,7 @@ - [Dockerfile-test-sds-server-windows](#dockerfile-test-sds-server-windows) - [Dockerfile-fortio-windows](#dockerfile-fortio-windows) - [Dockerfile-socat-windows](#dockerfile-socat-windows) +- [Dockerfile-bats-windows](#dockerfile-bats-windows) ## About this File @@ -16,7 +17,7 @@ In this file you will find which Dockerfiles are needed to run the Envoy integra This file sole purpose is to build the test-sds-server executable using Go. To do so, we use an official [golang image](https://hub.docker.com/_/golang/) provided in docker hub with Windows nano server. To build this image you need to run the following command on your terminal: -```Powershell +```shell docker build -t test-sds-server -f Dockerfile-test-sds-server-windows test-sds-server ``` @@ -24,13 +25,13 @@ This is the same command used in run-tests.sh You can test the built file by running the following command: -```Powershell +```shell docker run --rm -p 1234:1234 --name test-sds-server test-sds-server ``` If everything works properly you should get the following output: -```Powershell +```shell 20XX-XX-XXTXX:XX:XX.XXX-XXX [INFO] Loaded cert from file: name=ca-root 20XX-XX-XXTXX:XX:XX.XXX-XXX [INFO] Loaded cert from file: name=foo.example.com 20XX-XX-XXTXX:XX:XX.XXX-XXX [INFO] Loaded cert from file: name=wildcard.ingress.consul @@ -43,7 +44,7 @@ If everything works properly you should get the following output: This file sole purpose is to build the custom Fortio image for Windows OS. To do this, the official [windows/nanoserver image](https://hub.docker.com/_/microsoft-windows-nanoserver) is used as base image. To build this image you need to run the following command on your terminal: -```Powershell +```shell docker build -t fortio . -f Dockerfile-fortio-windows ``` @@ -51,7 +52,7 @@ This is the same command used in run-tests.sh You can test the built file by running the following command: -```Powershell +```shell docker run --rm -p 8080:8080 --name fortio fortio ``` @@ -67,18 +68,49 @@ The compiled windows version of Socat can be found in the repository [https://gi To build this image you need to run the following command on your terminal: -```Powershell +```shell docker build -t socat -f Dockerfile-socat-windows . ``` You can test the built file by running the following command: -```Powershell +```shell docker run --rm --name socat socat ``` If everything works properly you should get the following output: -```Powershell +```shell 20XX/XX/XX XX:XX:XX socat[1292] E exactly 2 addresses required (there are 0); use option "-h" for help ``` + +## Dockerfile-bats-windows + +This file sole purpose is to build the custom Bats image for Windows OS. To do this, the official [windows/servercore image](https://hub.docker.com/_/microsoft-windows-servercore) is used as base image. +To build this image you need to run the following command on your terminal: + +```shell +docker build -t bats-verify . -f Dockerfile-bats-windows +``` + +This is the same command used in run-tests.sh + +You can test the built file by running the following command: + +```shell +docker run --rm --name bats-verify -v $(pwd -W)/case-dummy-bats:C:\\workdir bats-verify --pretty /c/workdir/*.bats +``` + +If everything works properly you should see the result of the dummy test executed as is displayed below + +```shell +$ docker run --rm --name bats-verify -v $(pwd -W)/case-dummy-bats:C:\\workdir bats-verify --pretty /c/workdir/*.bats +verify_1.bats + ✔ Basic Test 1 + ✔ Basic Test 2 +verify_2.bats + ✔ Test with dummyFunction invoked + - Test skipped (skipped) + +4 tests, 0 failures, 1 skipped +```