forked from rpm-software-management/ci-dnf-stack
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdnf-testing.sh
executable file
·200 lines (180 loc) · 5.53 KB
/
dnf-testing.sh
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
#!/bin/bash
set -euo pipefail
PROG_PATH=$(dirname $(readlink -f -- $0))
fatal()
{
printf >&2 "Error: %s\n" "$*"
exit 1
}
show_usage()
{
printf >&2 "Try \`$0 --help' for more information.\n"
exit 1
}
set_devel()
{
devel="$PROG_PATH/dnf-behave-tests/features:/opt/behave/features:Z"
}
set_reserve()
{
PARAM_RESERVE="-r"
PARAM_TTY="-it"
}
set_reserveR()
{
PARAM_RESERVE="-R"
PARAM_TTY="-it"
}
show_help()
{
cat << EOF
$0 - functional tests for DNF.
Usage: $0 [OPTIONS...] {COMMAND}
Options:
-h, --help Show this help
-c, --container IMAGE Specified Image ID or name if do not want to run the last built image
-d, --devel Share local feature/ with docker
-p, --podman Force using podman instead of docker
-r, --reserve Keep bash shell session open after every single test executed
-R, --reserveonfail Keep bash shell session open upon test failure
-t, --tags TAG Pass specific tag to the behave command when running tests
--noxfail Skip tests marked as @xfail (same as --tags ~@xfail)
--usecache Use cache when building the image
Commands:
list List of available functional tests
build [TYPE] Build container with functional tests. The default TYPE is 'local'
run [TEST...] Run all tests. The set of tests can be optionally specified by [TEST...]
shell Run a bash shell session within the container
EOF
exit 0
}
TEMP=$(getopt -n $0 -o hdrpRc:t: -l help,devel,podman,reserve,reserveonfail,noxfail,usecache,container:,tags: -- "$@") || show_usage
eval set -- "$TEMP"
devel=""
IMAGE="dnf-bot/dnf-testing:latest"
PARAM_RESERVE=""
PARAM_TTY=""
PARAM_TAGS=""
BUILD_CACHE="--no-cache"
DOCKER_BIN="sudo docker";
! rpm -q docker &>/dev/null && rpm -q podman &>/dev/null && DOCKER_BIN="podman"
while :; do
case "$1" in
--) shift; break;;
-h|--help) show_help;;
-d|--devel) set_devel; shift;;
-c|--container) IMAGE=$2; shift 2;;
-p|--podman) DOCKER_BIN="podman"; shift;;
-r|--reserve) set_reserve; shift;;
-R|--reserveonfail) set_reserveR; shift;;
-t|--tags) PARAM_TAGS="$PARAM_TAGS --tags $2"; shift 2;;
--noxfail) PARAM_TAGS="$PARAM_TAGS --tags ~@xfail"; shift;;
--usecache) BUILD_CACHE=""; shift;;
*) fatal "Non-implemented option: $1"
esac
done
action=
for arg; do
case "$arg" in
list) action="list";;
build) action="build";;
run) action="run";;
shell) action="shell";;
*) fatal "Unknown argument: $arg";;
esac
shift
break
done
[ "$action" != "" ] || fatal "Specify command to do."
if [ "$action" = "run" ]; then
TESTS=()
for arg; do
TESTS+=( $arg )
shift
done
elif [ "$action" = "build" ]; then
type="local"
for arg; do
case "$arg" in
jjb) type="jjb";;
local) type="local";;
side-tag) type="side-tag";;
distro) type="distro";;
"") type="local";;
*) fatal "Unknown argument: $arg";;
esac
shift
done
fi
[ $# -eq 0 ] || fatal "Too many arguments."
gather_tests()
{
$DOCKER_BIN run --rm "$IMAGE" behave --dry-run | grep '^ *Feature:' | sed 's@.*# features/\(.*\):.*$@\1@'
}
list()
{
FEATURES=($(gather_tests))
printf "%s\n" "${FEATURES[@]}"
exit 0
}
[ "$action" = "list" ] && list
build()
{
local output=($($DOCKER_BIN build --build-arg TYPE="$type" \
${BUILD_CACHE} --force-rm -t "$IMAGE" "$PROG_PATH" | \
tee >(cat - >&2) | tail -1))
RET=$?
if [ "$DOCKER_BIN" == "sudo docker" ]; then
if [ ${#output[@]} -eq 3 ] && \
[ "${output[0]}" = "Successfully" ] &&
[ "${output[1]}" = "built" ]; then
printf "%s\n" "${output[2]}"
else
fatal "Failed to parse output."
fi
else
if [ $RET -ne 0 ]; then
fatal "Image build failed."
fi
fi
exit 0
}
[ "$action" = "build" ] && build
run()
{
printf "Packages installed in the container:\n"
$DOCKER_BIN run $PARAM_TTY --rm "$IMAGE" rpm -qa | sort
FEATURES=($(gather_tests))
[ ${#TESTS[@]} -eq 0 ] && TESTS=("${FEATURES[@]}")
local failed=0
local failed_test_name='Failed test(s):'
if [ -z "$devel" ];then
for feature in "${TESTS[@]}"; do
printf "\n$DOCKER_BIN run $PARAM_TTY --rm "$IMAGE" ./launch-test $PARAM_RESERVE $PARAM_TAGS "$feature"\n"
$DOCKER_BIN run $PARAM_TTY --rm "$IMAGE" ./launch-test $PARAM_RESERVE $PARAM_TAGS "$feature" >&2 || \
if [ $? -ne 0 ]; then let ++failed && failed_test_name+=" $feature"; fi
done
else
for feature in "${TESTS[@]}"; do
printf "\n$DOCKER_BIN run $PARAM_TTY --rm --volume "$devel" "$IMAGE" ./launch-test $PARAM_RESERVE $PARAM_TAGS "$feature"\n"
$DOCKER_BIN run $PARAM_TTY --rm --volume "$devel" "$IMAGE" ./launch-test $PARAM_RESERVE $PARAM_TAGS "$feature" >&2 || \
if [ $? -ne 0 ]; then let ++failed && failed_test_name+=" $feature"; fi
done
fi
if [ "$failed" != 0 ]; then
>&2 echo "$failed_test_name"
fi
exit $failed
}
[ "$action" = "run" ] && run
shell()
{
if [ -z "$devel" ];then
printf "\n$DOCKER_BIN run -it --rm "$IMAGE" bash\n"
$DOCKER_BIN run -it --rm "$IMAGE" bash
else
printf "\n$DOCKER_BIN run -it --rm -v "$devel" "$IMAGE" bash\n"
$DOCKER_BIN run -it --rm -v "$devel" "$IMAGE" bash
fi
}
[ "$action" == "shell" ] && shell