Skip to content

Commit

Permalink
Adding ztombol bats libraries + using multi stage build for dependenc…
Browse files Browse the repository at this point in the history
…y resolution

Signed-off-by: Damien DUPORTAL <[email protected]>
  • Loading branch information
dduportal committed Jan 3, 2018
1 parent e89b388 commit 15f4535
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 19 deletions.
31 changes: 21 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"
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"]
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -11,7 +10,6 @@ all: build test
build:
docker build \
--tag $(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG) \
--build-arg BATS_VERSION=$(BATS_VERSION) \
./

test:
Expand Down
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"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"
}
}
13 changes: 13 additions & 0 deletions tests/samples/sample.bats
Original file line number Diff line number Diff line change
@@ -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'
}
28 changes: 21 additions & 7 deletions tests/test-bats-dockerimage.bats
Original file line number Diff line number Diff line change
@@ -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} \
Expand All @@ -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
Expand All @@ -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
}

0 comments on commit 15f4535

Please sign in to comment.