Use bats to clean up tests and remove complexity

This commit is contained in:
Lars Gohr
2019-09-07 20:41:35 +02:00
parent bbd985b7af
commit 91a3945f7b
3 changed files with 185 additions and 263 deletions

View File

@@ -15,11 +15,12 @@ ADD entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"] ENTRYPOINT ["/entrypoint.sh"]
FROM runtime as test FROM runtime as test
ADD test.sh /test.sh RUN apk add --no-cache coreutils bats ncurses
ADD test.bats /test.bats
ADD stub.sh /fake_bin/docker ADD stub.sh /fake_bin/docker
ADD mock.sh /fake_bin/date ADD mock.sh /fake_bin/date
# Use mock instead of real docker # Use mock instead of real docker
ENV PATH="/fake_bin:usr/bin:/bin" ENV PATH="/fake_bin:usr/bin:/bin"
RUN /test.sh RUN /test.bats
FROM runtime FROM runtime

182
test.bats Executable file
View File

@@ -0,0 +1,182 @@
#!/usr/bin/env bats
setup(){
export GITHUB_REF='refs/heads/master'
export INPUT_USERNAME='USERNAME'
export INPUT_PASSWORD='PASSWORD'
export INPUT_NAME='my/repository'
}
teardown() {
unset INPUT_SNAPSHOT
unset INPUT_DOCKERFILE
unset INPUT_REGISTRY
unset INPUT_CACHE
unset GITHUB_SHA
}
@test "it pushes master branch to latest" {
export GITHUB_REF='refs/heads/master'
run /entrypoint.sh
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: build -t my/repository:latest .
Called mock with: push my/repository:latest
Called mock with: logout"
[ "$output" = "$expected" ]
}
@test "it pushes branch as name of the branch" {
export GITHUB_REF='refs/heads/myBranch'
run /entrypoint.sh
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: build -t my/repository:myBranch .
Called mock with: push my/repository:myBranch
Called mock with: logout"
[ "$output" = "$expected" ]
}
@test "it pushes tags to latest" {
export GITHUB_REF='refs/tags/myRelease'
run /entrypoint.sh
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: build -t my/repository:latest .
Called mock with: push my/repository:latest
Called mock with: logout"
[ "$output" = "$expected" ]
}
@test "it pushes specific Dockerfile to latest" {
export INPUT_DOCKERFILE='MyDockerFileName'
run /entrypoint.sh
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: build -f MyDockerFileName -t my/repository:latest .
Called mock with: push my/repository:latest
Called mock with: logout"
[ "$output" = "$expected" ]
}
@test "it pushes branch by sha and date in addition" {
export INPUT_SNAPSHOT='true'
export GITHUB_SHA='12169ed809255604e557a82617264e9c373faca7'
export MOCK_DATE='197001010101'
run /entrypoint.sh
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: build -t my/repository:latest -t my/repository:19700101010112169e .
Called mock with: push my/repository:latest
Called mock with: push my/repository:19700101010112169e
Called mock with: logout"
[ "$output" = "$expected" ]
}
@test "it caches image from former build and uses it for snapshot" {
export GITHUB_SHA='12169ed809255604e557a82617264e9c373faca7'
export MOCK_DATE='197001010101'
export INPUT_SNAPSHOT='true'
export INPUT_CACHE='true'
run /entrypoint.sh
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: pull my/repository:latest
Called mock with: build --cache-from my/repository:latest -t my/repository:latest -t my/repository:19700101010112169e .
Called mock with: push my/repository:latest
Called mock with: push my/repository:19700101010112169e
Called mock with: logout"
[ "$output" = "$expected" ]
}
@test "it pushes branch by sha and date with specific Dockerfile" {
export GITHUB_SHA='12169ed809255604e557a82617264e9c373faca7'
export MOCK_DATE='197001010101'
export INPUT_SNAPSHOT='true'
export INPUT_DOCKERFILE='MyDockerFileName'
run /entrypoint.sh
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: build -f MyDockerFileName -t my/repository:latest -t my/repository:19700101010112169e .
Called mock with: push my/repository:latest
Called mock with: push my/repository:19700101010112169e
Called mock with: logout"
[ "$output" = "$expected" ]
}
@test "it caches image from former build and uses it for snapshot with specific Dockerfile" {
export GITHUB_SHA='12169ed809255604e557a82617264e9c373faca7'
export MOCK_DATE='197001010101'
export INPUT_SNAPSHOT='true'
export INPUT_CACHE='true'
export INPUT_DOCKERFILE='MyDockerFileName'
run /entrypoint.sh
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: pull my/repository:latest
Called mock with: build -f MyDockerFileName --cache-from my/repository:latest -t my/repository:latest -t my/repository:19700101010112169e .
Called mock with: push my/repository:latest
Called mock with: push my/repository:19700101010112169e
Called mock with: logout"
[ "$output" = "$expected" ]
}
@test "it performs a login to another registry" {
export INPUT_REGISTRY='https://myRegistry'
run /entrypoint.sh
local expected="Called mock with: login -u USERNAME --password-stdin https://myRegistry
Called mock with: build -t my/repository:latest .
Called mock with: push my/repository:latest
Called mock with: logout"
[ "$output" = "$expected" ]
}
@test "it caches the image from a former build" {
export INPUT_CACHE='true'
run /entrypoint.sh
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: pull my/repository:latest
Called mock with: build --cache-from my/repository:latest -t my/repository:latest .
Called mock with: push my/repository:latest
Called mock with: logout"
[ "$output" = "$expected" ]
}
@test "it errors when with.name was not set" {
unset INPUT_NAME
run /entrypoint.sh
local expected="Unable to find the repository name. Did you set with.name?"
[ "$output" = "$expected" ]
}
@test "it errors when with.username was not set" {
unset INPUT_USERNAME
run /entrypoint.sh
local expected="Unable to find the username. Did you set with.username?"
[ "$output" = "$expected" ]
}
@test "it errors when with.password was not set" {
unset INPUT_PASSWORD
run /entrypoint.sh
local expected="Unable to find the password. Did you set with.password?"
[ "$output" = "$expected" ]
}

261
test.sh
View File

@@ -1,261 +0,0 @@
#!/bin/sh -e
function cleanEnvironment() {
unset INPUT_SNAPSHOT
unset INPUT_DOCKERFILE
unset INPUT_REGISTRY
unset INPUT_CACHE
unset GITHUB_SHA
}
function itPushesMasterBranchToLatest() {
export GITHUB_REF='refs/heads/master'
export INPUT_USERNAME='USERNAME'
export INPUT_PASSWORD='PASSWORD'
export INPUT_NAME='my/repository'
local result=$(exec /entrypoint.sh)
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: build -t my/repository:latest .
Called mock with: push my/repository:latest
Called mock with: logout"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
exit 1
fi
}
function itPushesBranchAsNameOfTheBranch() {
export GITHUB_REF='refs/heads/myBranch'
export INPUT_USERNAME='USERNAME'
export INPUT_PASSWORD='PASSWORD'
export INPUT_NAME='my/repository'
local result=$(exec /entrypoint.sh)
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: build -t my/repository:myBranch .
Called mock with: push my/repository:myBranch
Called mock with: logout"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
exit 1
fi
}
function itPushesReleasesToLatest() {
export GITHUB_REF='refs/tags/myRelease'
export INPUT_USERNAME='USERNAME'
export INPUT_PASSWORD='PASSWORD'
export INPUT_NAME='my/repository'
local result=$(exec /entrypoint.sh)
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: build -t my/repository:latest .
Called mock with: push my/repository:latest
Called mock with: logout"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
exit 1
fi
}
function itPushesSpecificDockerfileReleasesToLatest() {
export GITHUB_REF='refs/tags/myRelease'
export INPUT_DOCKERFILE='MyDockerFileName'
export INPUT_USERNAME='USERNAME'
export INPUT_PASSWORD='PASSWORD'
export INPUT_NAME='my/repository'
local result=$(exec /entrypoint.sh)
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: build -f MyDockerFileName -t my/repository:latest .
Called mock with: push my/repository:latest
Called mock with: logout"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
exit 1
fi
cleanEnvironment
}
function itPushesBranchByShaAndDateInAddition() {
export GITHUB_REF='refs/tags/myRelease'
export INPUT_SNAPSHOT='true'
export GITHUB_SHA='12169ed809255604e557a82617264e9c373faca7'
export MOCK_DATE='197001010101'
export INPUT_USERNAME='USERNAME'
export INPUT_PASSWORD='PASSWORD'
export INPUT_NAME='my/repository'
local result=$(exec /entrypoint.sh)
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: build -t my/repository:latest -t my/repository:19700101010112169e .
Called mock with: push my/repository:latest
Called mock with: push my/repository:19700101010112169e
Called mock with: logout"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
exit 1
fi
cleanEnvironment
}
function itCachesImageFromFormerBuildAndUsesItForSnapshotIfConfigured() {
export GITHUB_REF='refs/tags/myRelease'
export INPUT_USERNAME='USERNAME'
export INPUT_PASSWORD='PASSWORD'
export INPUT_NAME='my/repository'
export GITHUB_SHA='12169ed809255604e557a82617264e9c373faca7'
export MOCK_DATE='197001010101'
export INPUT_SNAPSHOT='true'
export INPUT_CACHE='true'
local result=$(exec /entrypoint.sh)
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: pull my/repository:latest
Called mock with: build --cache-from my/repository:latest -t my/repository:latest -t my/repository:19700101010112169e .
Called mock with: push my/repository:latest
Called mock with: push my/repository:19700101010112169e
Called mock with: logout"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
exit 1
fi
cleanEnvironment
}
function itPushesBranchByShaAndDateInAdditionWithSpecificDockerfile() {
export GITHUB_REF='refs/tags/myRelease'
export INPUT_SNAPSHOT='true'
export INPUT_DOCKERFILE='MyDockerFileName'
export GITHUB_SHA='12169ed809255604e557a82617264e9c373faca7'
export MOCK_DATE='197001010101'
export INPUT_USERNAME='USERNAME'
export INPUT_PASSWORD='PASSWORD'
export INPUT_NAME='my/repository'
local result=$(exec /entrypoint.sh)
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: build -f MyDockerFileName -t my/repository:latest -t my/repository:19700101010112169e .
Called mock with: push my/repository:latest
Called mock with: push my/repository:19700101010112169e
Called mock with: logout"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
exit 1
fi
cleanEnvironment
}
function itCachesImageFromFormerBuildAndUsesItForSnapshotIfConfiguredIsOkWithSpecificDockerfile() {
export GITHUB_REF='refs/tags/myRelease'
export INPUT_USERNAME='USERNAME'
export INPUT_PASSWORD='PASSWORD'
export INPUT_NAME='my/repository'
export GITHUB_SHA='12169ed809255604e557a82617264e9c373faca7'
export MOCK_DATE='197001010101'
export INPUT_SNAPSHOT='true'
export INPUT_CACHE='true'
export INPUT_DOCKERFILE='MyDockerFileName'
local result=$(exec /entrypoint.sh)
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: pull my/repository:latest
Called mock with: build -f MyDockerFileName --cache-from my/repository:latest -t my/repository:latest -t my/repository:19700101010112169e .
Called mock with: push my/repository:latest
Called mock with: push my/repository:19700101010112169e
Called mock with: logout"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
exit 1
fi
cleanEnvironment
}
function itLogsIntoAnotherRegistryIfConfigured() {
export GITHUB_REF='refs/tags/myRelease'
export INPUT_USERNAME='USERNAME'
export INPUT_PASSWORD='PASSWORD'
export INPUT_REGISTRY='https://myRegistry'
export INPUT_NAME='my/repository'
local result=$(exec /entrypoint.sh)
local expected="Called mock with: login -u USERNAME --password-stdin https://myRegistry
Called mock with: build -t my/repository:latest .
Called mock with: push my/repository:latest
Called mock with: logout"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
exit 1
fi
cleanEnvironment
}
function itCachesImageFromFormerBuildIfConfigured() {
export GITHUB_REF='refs/tags/myRelease'
export INPUT_USERNAME='USERNAME'
export INPUT_PASSWORD='PASSWORD'
export INPUT_NAME='my/repository'
export INPUT_CACHE='true'
local result=$(exec /entrypoint.sh)
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: pull my/repository:latest
Called mock with: build --cache-from my/repository:latest -t my/repository:latest .
Called mock with: push my/repository:latest
Called mock with: logout"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
exit 1
fi
cleanEnvironment
}
function itErrorsWhenNameWasNotSet() {
unset INPUT_NAME
local result=$(exec /entrypoint.sh)
local expected="Unable to find the repository name. Did you set with.name?"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
exit 1
fi
}
function itErrorsWhenUsernameWasNotSet() {
export INPUT_NAME='my/repository'
unset INPUT_USERNAME
local result=$(exec /entrypoint.sh)
local expected="Unable to find the username. Did you set with.username?"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
exit 1
fi
}
function itErrorsWhenPasswordWasNotSet() {
export INPUT_NAME='my/repository'
export INPUT_USERNAME='USERNAME'
unset INPUT_PASSWORD
local result=$(exec /entrypoint.sh)
local expected="Unable to find the password. Did you set with.password?"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
exit 1
fi
}
itPushesMasterBranchToLatest
itPushesBranchAsNameOfTheBranch
itPushesReleasesToLatest
itPushesSpecificDockerfileReleasesToLatest
itPushesBranchByShaAndDateInAddition
itCachesImageFromFormerBuildAndUsesItForSnapshotIfConfigured
itPushesBranchByShaAndDateInAdditionWithSpecificDockerfile
itLogsIntoAnotherRegistryIfConfigured
itCachesImageFromFormerBuildIfConfigured
itErrorsWhenNameWasNotSet
itErrorsWhenUsernameWasNotSet
itErrorsWhenPasswordWasNotSet