From 15f45358a3eba56f48e523c792b158e12aa66d6d Mon Sep 17 00:00:00 2001 From: Damien DUPORTAL Date: Wed, 3 Jan 2018 16:34:49 +0100 Subject: [PATCH] Adding ztombol bats libraries + using multi stage build for dependency resolution Signed-off-by: Damien DUPORTAL --- Dockerfile | 31 +++++++++++++++++++++---------- Makefile | 2 -- package-lock.json | 26 ++++++++++++++++++++++++++ package.json | 14 ++++++++++++++ tests/samples/sample.bats | 13 +++++++++++++ tests/test-bats-dockerimage.bats | 28 +++++++++++++++++++++------- 6 files changed, 95 insertions(+), 19 deletions(-) create mode 100644 package-lock.json create mode 100644 package.json diff --git a/Dockerfile b/Dockerfile index 794ab02..8ef29aa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,31 @@ + +# We use npm for dependency management +FROM node:alpine as dependencies-solver +RUN apk add --no-cache git +COPY package*.json /bats/ +WORKDIR /bats +RUN npm install + +# Minimalistic image FROM alpine:3.7 LABEL Maintainer="Damien DUPORTAL " +ENV BATS_HELPERS_DIR=/opt/bats-helpers -ARG bats_version=0.4.0 -ENV BATS_VERSION=${bats_version} +# Bats +COPY --from=dependencies-solver /bats/node_modules/bats /opt/bats + +# ztombol's bats helpers +COPY --from=dependencies-solver /bats/node_modules/bats-support /opt/bats-helpers/bats-support +COPY --from=dependencies-solver /bats/node_modules/bats-file /opt/bats-helpers/bats-file +COPY --from=dependencies-solver /bats/node_modules/bats-assert /opt/bats-helpers/bats-assert -WORKDIR /tests -RUN apk add --no-cache \ - bash \ - curl \ - && curl -sSL -o "/tmp/v${BATS_VERSION}.tgz" \ - "https://github.com/bats-core/bats-core/archive/v${BATS_VERSION}.tar.gz" \ - && tar -xzf "/tmp/v${BATS_VERSION}.tgz" -C /tmp/ \ - && bash "/tmp/bats-core-${BATS_VERSION}/install.sh" /opt/bats \ +RUN apk add --no-cache bash \ && ln -s /opt/bats/libexec/bats /sbin/bats +WORKDIR /tests + + ENTRYPOINT ["/sbin/bats"] CMD ["-v"] diff --git a/Makefile b/Makefile index 430b648..d206ae2 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,6 @@ export DOCKER_IMAGE_NAME ?= dduportal/bats export DOCKER_IMAGE_TAG ?= $(shell git rev-parse --short HEAD) -export BATS_VERSION ?= 0.4.0 CURRENT_GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD) @@ -11,7 +10,6 @@ all: build test build: docker build \ --tag $(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG) \ - --build-arg BATS_VERSION=$(BATS_VERSION) \ ./ test: diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..635724c --- /dev/null +++ b/package-lock.json @@ -0,0 +1,26 @@ +{ + "name": "dduportal-bats", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "bats": { + "version": "0.4.2", + "resolved": "https://registry.npmjs.org/bats/-/bats-0.4.2.tgz", + "integrity": "sha1-gfIiu8uwg9FSiz6IMKbo8xSHbsY=", + "dev": true + }, + "bats-assert": { + "version": "git+https://github.com/ztombol/bats-assert.git#cf1718da00bf1a7d33186ef13bb21841a77861c6", + "dev": true + }, + "bats-file": { + "version": "git+https://github.com/ztombol/bats-file.git#2fddb2b831d65cdf2e411f3b47f4677fbb15729c", + "dev": true + }, + "bats-support": { + "version": "git+https://github.com/ztombol/bats-support.git#24a72e14349690bcbf7c151b9d2d1cdd32d36eb1", + "dev": true + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..172eb13 --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "dduportal-bats", + "version": "1.0.0", + "description": "This file is use for dependency management only and will never be used on the end Docker image", + "private": "true", + "author": "Damien DUPORTAL ", + "license": "ISC", + "devDependencies": { + "bats": "^0.4.2", + "bats-support": "git+https://github.com/ztombol/bats-support.git#v0.3.0", + "bats-assert": "git+https://github.com/ztombol/bats-assert.git#v0.3.0", + "bats-file": "git+https://github.com/ztombol/bats-file.git#v0.2.0" + } +} diff --git a/tests/samples/sample.bats b/tests/samples/sample.bats index 52fdb8f..ebd8756 100644 --- a/tests/samples/sample.bats +++ b/tests/samples/sample.bats @@ -1,5 +1,18 @@ #!/usr/bin/env bats +load "${BATS_HELPERS_DIR}/bats-support/load.bash" +load "${BATS_HELPERS_DIR}/bats-file/load.bash" +load "${BATS_HELPERS_DIR}/bats-assert/load.bash" + @test "Simple echo test" { echo "Hello Bats" } + +@test "I can use an assert() from helper bats-assert" { + touch '/var/log/test.log' + assert [ -e '/var/log/test.log' ] +} + +@test "I can use an assert_file_exist() from helper bats-file" { + assert_file_exist '/tmp' +} diff --git a/tests/test-bats-dockerimage.bats b/tests/test-bats-dockerimage.bats index 24131e0..a8c940b 100644 --- a/tests/test-bats-dockerimage.bats +++ b/tests/test-bats-dockerimage.bats @@ -1,7 +1,6 @@ #!/usr/bin/env bats -# load "${BATS_LIBS}/bats-support/load.bash" -# load "${BATS_LIBS}/bats-assert/load.bash" +BATS_VERSION=0.4.0 run_command_with_docker() { docker run --rm -t ${CUSTOM_DOCKER_RUN_OPTS} \ @@ -16,6 +15,26 @@ setup() { run_command_with_docker | grep "Bats" | grep "${BATS_VERSION}" } +@test "Environment variable for Bats Helper is set and valid" { + local CUSTOM_DOCKER_RUN_OPTS="--entrypoint bash" + run_command_with_docker -c 'test -d "${BATS_HELPERS_DIR}"' +} + +@test "ztombol's bats-support helpers is installed" { + local CUSTOM_DOCKER_RUN_OPTS="--entrypoint bash" + run_command_with_docker -c 'test -e "${BATS_HELPERS_DIR}/bats-support/load.bash"' +} + +@test "ztombol's bats-file helpers is installed" { + local CUSTOM_DOCKER_RUN_OPTS="--entrypoint bash" + run_command_with_docker -c 'test -e "${BATS_HELPERS_DIR}/bats-file/load.bash"' +} + +@test "ztombol's bats-assert helpers is installed" { + local CUSTOM_DOCKER_RUN_OPTS="--entrypoint bash" + run_command_with_docker -c 'test -e "${BATS_HELPERS_DIR}/bats-assert/load.bash"' +} + @test "Base OS is using Alpine Linux" { local CUSTOM_DOCKER_RUN_OPTS="--entrypoint grep" run_command_with_docker "Alpine" /etc/os-release @@ -30,8 +49,3 @@ setup() { local CUSTOM_DOCKER_RUN_OPTS="--entrypoint which" run_command_with_docker bash } - -@test "Curl is installed" { - local CUSTOM_DOCKER_RUN_OPTS="--entrypoint which" - run_command_with_docker curl -}