-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-built-container
executable file
·216 lines (180 loc) · 4.88 KB
/
test-built-container
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/bash
##################
# Constants {{{1 #
##################
PROG_NAME=$(basename $0)
VERSION=1.0
YES=yes
#########################
# Global variables {{{1 #
#########################
DEBUG=0
CONTAINER_IMAGE=
#CMDS_FILE=test_cmds.txt
CMDS_FILE=
TESTDATA_DIR=
REPO="BioContainers/containers"
GITHUB_STATUS_TOKEN=$GITHUB_AUTH_TOKEN
COMMIT=$GIT_COMMIT
PR_ID=$PULL_REQUEST_ID
SOFTWARE=$CONTAINER
HDR1="Accept: application/vnd.github.v3+json"
HDR2="Authorization: token $GITHUB_STATUS_TOKEN"
###################
# Print help {{{1 #
###################
function print_help {
echo "Usage: $PROG_NAME [options] container_image"
echo
echo " -t, --test-cmds Path to test-cmds.txt file (and its associated test files)."
echo " -g, --debug Debug mode."
echo " -h, --help Print this help message."
}
############
# Msg {{{1 #
############
function msg {
local tag=$1
shift
local code_level=1
is_numeric=$(echo $1 | grep '^[0-9]*$')
if [ -n "$is_numeric" ] ; then
code_level=$1
shift
fi
local msg=$1
# Check tag
if [ -z "$tag" -o \( "$tag" != INFO -a "$tag" != DEBUG -a "$tag" != ERROR \) ] ; then
echo "ERROR: Unvalid message tag \"$tag\"." >&2
exit 999
fi
# Print message
[ "$tag" = DEBUG -a "$DEBUG" -lt "$code_level" ] || echo "$tag: $msg" >&2
# Exit
[ $tag = ERROR ] && exit $code_level
}
#################
# Requires {{{1 #
#################
function requires {
local prg=$1
[ -n "$(which $prg)" ] || msg ERROR "Cannot find $prg. Please install required package."
}
##################
# Read args {{{1 #
##################
function read_args {
local args="$*" # save arguments for debugging purpose
# Read options
while true ; do
shift_count=1
case $1 in
-g|--debug) DEBUG=$((DEBUG + 1)) ;;
-h|--help) print_help ; exit 0 ;;
-t|--test-cmds) shift; CMDS_FILE=$1 ;;
-|--|--*) msg ERROR "Illegal option $1." ;;
-?) msg ERROR "Unknown option $1." ;;
-[^-]*) split_opt=$(echo $1 | sed 's/^-//' | sed 's/\([a-zA-Z]\)/ -\1/g') ; set -- $1$split_opt "${@:2}" ;;
*) break
esac
shift $shift_count
done
shift $((OPTIND - 1))
# Read remaining arguments
[ $# -eq 1 ] || msg ERROR "You must set one, and only one, container image."
CONTAINER_IMAGE=$1
# Debug
msg DEBUG 1 "Arguments are : $args"
}
########################
# Test container {{{ 1 #
########################
function test_container {
local entrypoint=$1
local args=$2
local mountpath=$3
local entrypoint_arg=
[ -z "$entrypoint" ] || entrypoint_arg="--entrypoint=$1"
echo "Running the following command:"
echo "docker run --rm -v $mountpath:/biocontainers $entrypoint_arg $CONTAINER_IMAGE $args"
docker run --rm -v $mountpath:/biocontainers $entrypoint_arg $CONTAINER_IMAGE $args
#echo "Command $?"
return $?
}
################
# Send comment #
################
function send_comment {
local comment=$1
local githuburl="https://api.github.com/repos/$REPO/commits/$COMMIT/comments"
local header1=$HDR1
if [ -n "$PR_ID" ] ; then
header1="Accept:application/vnd.github.v3.raw+json"
githuburl="https://api.github.com/repos/$REPO/issues/$PR_ID/comments"
fi
#echo "Cmd sent:"
#echo "curl -H '$header1' -H '$HDR2' -d '{\"body\": \"$comment\"}' $githuburl"
curl -H "$header1" \
-H "$HDR2" \
-d "{\"body\": \"$comment\"}" \
"$githuburl"
}
###############
# Send status #
###############
function send_status {
local software=$1
local status=$2
local msg=$3
local json="{\"description\": \"$msg\",\"state\": \"$status\",\"context\": \"biocontainers/status/test/$SOFTWARE\"}"
local githuburl="https://api.github.com/repos/$REPO/statuses/$COMMIT"
case "$status" in
"s" | "success")
status="success"
;;
"f" | "failure")
status="failure"
;;
"n" | "none")
status="pending";;
*)
msg ERROR "Unknown test status: $status"
return 1;;
esac
#echo "Curl command:"
# echo "curl -H '$HDR1' -H '$HDR2' -d '$json' $githuburl"
curl -H "$HDR1" \
-H "$HDR2" \
-d "$json" \
"$githuburl"
}
#############
# MAIN {{{1 #
#############
# Requirements
requires docker
# Read arguments
read_args "$@"
# Test
if [ -f "$CMDS_FILE" ] ; then
testdatapath=$(dirname $(realpath "$CMDS_FILE"))
#echo "Dir to mount: $testdatapath"
while read line ; do
has_entrypoint=$(echo $line | grep '^[^-]')
if [ -n "$has_entrypoint" ] ; then
entrypoint=$(echo $line | sed 's/^\([^ ]*\).*$/\1/')
args=$(echo $line | sed 's/^[^ ]*\(.*\)$/\1/')
else
entrypoint=
args=$line
fi
if ! test_container "$entrypoint" "$args" $testdatapath; then
send_status $CONTAINER_IMAGE "failure" "Testing failed on command: $line"
exit 1
fi
done <"$CMDS_FILE"
send_status $CONTAINER_IMAGE "success" "All tests successful"
else
#echo "No $CMDS_FILE (test file) present, skipping tests"
send_comment "No $CMDS_FILE (test file) present, skipping tests"
fi