Skip to content

Commit

Permalink
Added dockerBuildStandard test
Browse files Browse the repository at this point in the history
  • Loading branch information
flounderpinto committed Dec 14, 2023
1 parent 7c4c410 commit 4da8c8e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/dockerBuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ CODE_DIR="${CODE_DIR:-/opt/code}"
function dockerBuildUsage
{
echo $'Usage:'
echo $'\tdockerBuild.sh dockerBuild -e dockerRegistryName -r dockerRepoName -c buildContextDir -d dockerFile [-g gitRepoDir] [-p platform]... [-t tag]... [-b buildArg]... [-a arg]... [-h]'
echo $'\tdockerBuild.sh dockerBuild -e dockerRegistryName -r dockerRepoName -c buildContextDir -f dockerFile [-g gitRepoDir] [-p platform]... [-t tag]... [-b buildArg]... [-a arg]... [-h]'
echo $'\t\t-e - Docker registry name.'
echo $'\t\t\te.g. "index.docker.io/my-registry"'
echo $'\t\t-r - Docker repo name.'
echo $'\t\t\te.g. "builder-docker".'
echo $'\t\t-c - Docker build context.'
echo $'\t\t-d - Dockerfile location.'
echo $'\t\t-f - Dockerfile location.'
echo $'\t\t-g - Git repo directory.'
echo $'\t\t-p - Target platform for build.'
echo $'\t\t\tCan be a comma-separated list or defined multiple times. '
Expand Down Expand Up @@ -88,7 +88,7 @@ function dockerBuild
local BUILD_ARGS=()
local ADDITIONAL_BUILD_FLAGS=()

while getopts ":e:r:c:d:g:p:t:b:a:h" opt; do
while getopts ":e:r:c:f:g:p:t:b:a:h" opt; do
case $opt in
e)
DOCKER_REGISTRY="$OPTARG"
Expand All @@ -99,7 +99,7 @@ function dockerBuild
c)
BUILD_CONTEXT_DIR="$OPTARG"
;;
d)
f)
DOCKER_FILE="$OPTARG"
;;
g)
Expand Down Expand Up @@ -244,7 +244,7 @@ function dockerBuild
# always the same. This is a shortcut for calling dockerBuild()
function dockerBuildStandard
{
dockerBuild -c "$CODE_DIR" -d "$CODE_DIR/docker/Dockerfile" -g "$CODE_DIR" "$@"
dockerBuild -c "$CODE_DIR" -f "$CODE_DIR/docker/Dockerfile" -g "$CODE_DIR" "$@"
}

#Allows function calls based on arguments passed to the script
Expand Down
74 changes: 50 additions & 24 deletions test/unit/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,50 @@ source ../../src/dockerBuild.sh
# Begin Mocks
# These methods override methods by the same name in the dockerBuild.sh script
# ------------------
function getGitVersion
function getGitVersion()
{
echo "mocked-git-version"
}
export -f getGitVersion

function getGitBranch
function getGitBranch()
{
echo "mocked-git-branch"
}
export -f getGitBranch

function createBuilder
function createBuilder()
{
echo "createBuilder" >> "$OUTPUT_FILE"
}
export -f createBuilder

function removeBuilder
function removeBuilder()
{
echo "removeBuilder" >> "$OUTPUT_FILE"
}
export -f removeBuilder

function build
function build()
{
echo "$1" >> "$OUTPUT_FILE"
}
export -f build


function oneTimeSetUp()
{
echo "oneTimeSetUp"
}
# ------------------
# End Mocks
# ------------------

function oneTimeTearDown()
{
echo "oneTimeTearDown"
rm -f "$OUTPUT_FILE"
}
# ------------------
# End Mocks
# ------------------

testSingleArgs()
function testSingleArgs()
{
local E="docker-registry"
local R="docker-repo"
local C="build-context-dir"
local D="dockerfile"
local F="dockerfile"
local G="git-dir"
local P="target"
local T="tag"
Expand All @@ -72,7 +65,7 @@ testSingleArgs()
-e "$E" \
-r "$R" \
-c "$C" \
-d "$D" \
-f "$F" \
-g "$G" \
-p "$P" \
-t "$T" \
Expand All @@ -85,12 +78,12 @@ testSingleArgs()
echo ""
}

testMultipleArgs()
function testMultipleArgs()
{
local E="docker-registry"
local R="docker-repo"
local C="build-context-dir"
local D="dockerfile"
local F="dockerfile"
local G="git-dir"
local P1="platform1"
local P2="platform2"
Expand All @@ -107,7 +100,7 @@ testMultipleArgs()
-e "$E" \
-r "$R" \
-c "$C" \
-d "$D" \
-f "$F" \
-g "$G" \
-p "$P1" \
-p "$P2" \
Expand All @@ -124,12 +117,12 @@ testMultipleArgs()
echo ""
}

testMultipleArgsCommaSeparated()
function testMultipleArgsCommaSeparated()
{
local E="docker-registry"
local R="docker-repo"
local C="build-context-dir"
local D="dockerfile"
local F="dockerfile"
local G="git-dir"
local P="platform1,platform2"
local T1="tag1"
Expand All @@ -144,7 +137,7 @@ testMultipleArgsCommaSeparated()
-e "$E" \
-r "$R" \
-c "$C" \
-d "$D" \
-f "$F" \
-g "$G" \
-p "$P" \
-t "$T1" \
Expand All @@ -159,6 +152,39 @@ testMultipleArgsCommaSeparated()
echo ""
}

function testdockerBuildStandard()
{
local E="docker-registry"
local R="docker-repo"
local P1="platform1"
local P2="platform2"
local T1="tag1"
local T2="tag2"
local B1="build-arg1"
local B2="build-arg2"
local A1="--arg1 on"
local A2="--arg2 off"

rm -f "$OUTPUT_FILE"

stdout=$(dockerBuildStandard \
-e "$E" \
-r "$R" \
-p "$P1" \
-p "$P2" \
-t "$T1" \
-t "$T2" \
-b "$B1" \
-b "$B2" \
-a "$A1" \
-a "$A2")
echo "$stdout"
buildCmd=$(<"$OUTPUT_FILE")
expected=$(<"$SCRIPT_DIR/expected/buildStandard")
assertEquals "$expected" "$buildCmd"
echo ""
}

# Load and run shUnit2.
# shellcheck disable=SC1091
. shunit2

0 comments on commit 4da8c8e

Please sign in to comment.