|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +dir="$(dirname "$(readlink -f "$BASH_SOURCE")")" |
| 5 | + |
| 6 | +self="$(basename "$0")" |
| 7 | + |
| 8 | +usage() { |
| 9 | + cat <<EOUSAGE |
| 10 | +
|
| 11 | +usage: $self [-t test ...] image:tag [...] |
| 12 | + ie: $self debian:wheezy |
| 13 | + $self -t utc python:3 |
| 14 | + $self -t utc python:3 -t python-hy |
| 15 | +
|
| 16 | +This script processes the specified Docker images to test their running |
| 17 | +environments. |
| 18 | +EOUSAGE |
| 19 | +} |
| 20 | + |
| 21 | +# arg handling |
| 22 | +opts="$(getopt -o 'ht:c:?' --long 'dry-run,help,test:,config:' -- "$@" || { usage >&2 && false; })" |
| 23 | +eval set -- "$opts" |
| 24 | + |
| 25 | +declare -A argTests=() |
| 26 | +declare -a configs=() |
| 27 | +dryRun= |
| 28 | +while true; do |
| 29 | + flag=$1 |
| 30 | + shift |
| 31 | + case "$flag" in |
| 32 | + --dry-run) dryRun=1 ;; |
| 33 | + --help|-h|'-?') usage && exit 0 ;; |
| 34 | + --test|-t) argTests["$1"]=1 && shift ;; |
| 35 | + --config|-c) configs+=("$(readlink -f "$1")") && shift ;; |
| 36 | + --) break ;; |
| 37 | + *) |
| 38 | + { |
| 39 | + echo "error: unknown flag: $flag" |
| 40 | + usage |
| 41 | + } >&2 |
| 42 | + exit 1 |
| 43 | + ;; |
| 44 | + esac |
| 45 | +done |
| 46 | + |
| 47 | +if [ $# -eq 0 ]; then |
| 48 | + usage >&2 |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +# declare configuration variables |
| 53 | +declare -a globalTests=() |
| 54 | +declare -A testAlias=() |
| 55 | +declare -A imageTests=() |
| 56 | +declare -A globalExcludeTests=() |
| 57 | +declare -A explicitTests=() |
| 58 | + |
| 59 | +# if there are no user-specified configs, use the default config |
| 60 | +if [ ${#configs} -eq 0 ]; then |
| 61 | + configs+=("$dir/config.sh") |
| 62 | +fi |
| 63 | + |
| 64 | +# load the configs |
| 65 | +declare -A testPaths=() |
| 66 | +for conf in "${configs[@]}"; do |
| 67 | + . "$conf" |
| 68 | + |
| 69 | + # Determine the full path to any newly-declared tests |
| 70 | + confDir="$(dirname "$conf")" |
| 71 | + |
| 72 | + for testName in ${globalTests[@]} ${imageTests[@]}; do |
| 73 | + [ "${testPaths[$testName]}" ] && continue |
| 74 | + |
| 75 | + if [ -d "$confDir/tests/$testName" ]; then |
| 76 | + # Test directory found relative to the conf file |
| 77 | + testPaths[$testName]="$confDir/tests/$testName" |
| 78 | + elif [ -d "$dir/tests/$testName" ]; then |
| 79 | + # Test directory found in the main tests/ directory |
| 80 | + testPaths[$testName]="$dir/tests/$testName" |
| 81 | + fi |
| 82 | + done |
| 83 | +done |
| 84 | + |
| 85 | +didFail= |
| 86 | +for dockerImage in "$@"; do |
| 87 | + echo "testing $dockerImage" |
| 88 | + |
| 89 | + if ! docker inspect "$dockerImage" &> /dev/null; then |
| 90 | + echo $'\timage does not exist!' |
| 91 | + didFail=1 |
| 92 | + continue |
| 93 | + fi |
| 94 | + |
| 95 | + repo="${dockerImage%:*}" |
| 96 | + tagVar="${dockerImage#*:}" |
| 97 | + #version="${tagVar%-*}" |
| 98 | + variant="${tagVar##*-}" |
| 99 | + |
| 100 | + testRepo=$repo |
| 101 | + [ -z "${testAlias[$repo]}" ] || testRepo="${testAlias[$repo]}" |
| 102 | + |
| 103 | + explicitVariant= |
| 104 | + if [ \ |
| 105 | + "${explicitTests[:$variant]}" \ |
| 106 | + -o "${explicitTests[$repo:$variant]}" \ |
| 107 | + -o "${explicitTests[$testRepo:$variant]}" \ |
| 108 | + ]; then |
| 109 | + explicitVariant=1 |
| 110 | + fi |
| 111 | + |
| 112 | + testCandidates=() |
| 113 | + if [ -z "$explicitVariant" ]; then |
| 114 | + testCandidates+=( "${globalTests[@]}" ) |
| 115 | + fi |
| 116 | + testCandidates+=( |
| 117 | + ${imageTests[:$variant]} |
| 118 | + ) |
| 119 | + if [ -z "$explicitVariant" ]; then |
| 120 | + testCandidates+=( |
| 121 | + ${imageTests[$testRepo]} |
| 122 | + ) |
| 123 | + fi |
| 124 | + testCandidates+=( |
| 125 | + ${imageTests[$testRepo:$variant]} |
| 126 | + ) |
| 127 | + if [ "$testRepo" != "$repo" ]; then |
| 128 | + if [ -z "$explicitVariant" ]; then |
| 129 | + testCandidates+=( |
| 130 | + ${imageTests[$repo]} |
| 131 | + ) |
| 132 | + fi |
| 133 | + testCandidates+=( |
| 134 | + ${imageTests[$repo:$variant]} |
| 135 | + ) |
| 136 | + fi |
| 137 | + |
| 138 | + tests=() |
| 139 | + for t in "${testCandidates[@]}"; do |
| 140 | + if [ ${#argTests[@]} -gt 0 -a -z "${argTests[$t]}" ]; then |
| 141 | + # skipping due to -t |
| 142 | + continue |
| 143 | + fi |
| 144 | + |
| 145 | + if [ \ |
| 146 | + ! -z "${globalExcludeTests[${testRepo}_$t]}" \ |
| 147 | + -o ! -z "${globalExcludeTests[${testRepo}:${variant}_$t]}" \ |
| 148 | + -o ! -z "${globalExcludeTests[:${variant}_$t]}" \ |
| 149 | + -o ! -z "${globalExcludeTests[${repo}_$t]}" \ |
| 150 | + -o ! -z "${globalExcludeTests[${repo}:${variant}_$t]}" \ |
| 151 | + -o ! -z "${globalExcludeTests[:${variant}_$t]}" \ |
| 152 | + ]; then |
| 153 | + # skipping due to exclude |
| 154 | + continue |
| 155 | + fi |
| 156 | + |
| 157 | + tests+=( "$t" ) |
| 158 | + done |
| 159 | + |
| 160 | + currentTest=0 |
| 161 | + totalTest="${#tests[@]}" |
| 162 | + for t in "${tests[@]}"; do |
| 163 | + (( currentTest+=1 )) |
| 164 | + echo -ne "\t'$t' [$currentTest/$totalTest]..." |
| 165 | + |
| 166 | + # run test against dockerImage here |
| 167 | + # find the script for the test |
| 168 | + scriptDir="${testPaths[$t]}" |
| 169 | + if [ -d "$scriptDir" ]; then |
| 170 | + script="$scriptDir/run.sh" |
| 171 | + if [ -x "$script" -a ! -d "$script" ]; then |
| 172 | + # TODO dryRun logic |
| 173 | + if output="$("$script" $dockerImage)"; then |
| 174 | + if [ -f "$scriptDir/expected-std-out.txt" ] && ! d="$(echo "$output" | diff -u "$scriptDir/expected-std-out.txt" - 2>/dev/null)"; then |
| 175 | + echo 'failed; unexpected output:' |
| 176 | + echo "$d" |
| 177 | + didFail=1 |
| 178 | + else |
| 179 | + echo 'passed' |
| 180 | + fi |
| 181 | + else |
| 182 | + echo 'failed' |
| 183 | + didFail=1 |
| 184 | + fi |
| 185 | + else |
| 186 | + echo "skipping" |
| 187 | + echo >&2 "error: $script missing, not executable or is a directory" |
| 188 | + didFail=1 |
| 189 | + continue |
| 190 | + fi |
| 191 | + else |
| 192 | + echo "skipping" |
| 193 | + echo >&2 "error: unable to locate test '$t'" |
| 194 | + didFail=1 |
| 195 | + continue |
| 196 | + fi |
| 197 | + done |
| 198 | +done |
| 199 | + |
| 200 | +if [ "$didFail" ]; then |
| 201 | + exit 1 |
| 202 | +fi |
0 commit comments