|
2 | 2 |
|
3 | 3 | # Usage: run.sh [--unsafe] |
4 | 4 | # |
5 | | -# Must be run from the tests/reactor directory. |
| 5 | +# Runs all the reactor tests, see run_test.sh, and prints a summary of their |
| 6 | +# results. The output of a test is only printed if it failed. They are also |
| 7 | +# run by "make check". |
6 | 8 | # |
7 | 9 | # Tests in an unsafe/ directory modify the system they are running on, so they |
8 | | -# are only run with --unsafe. |
| 10 | +# are skipped unless run with --unsafe (or UNSAFE_TESTS=1). |
9 | 11 | # |
10 | 12 | # The binaries are taken from $CF_BINDIR if it is set, otherwise from the |
11 | 13 | # CFEngine build tree this directory is part of if they are built, otherwise |
12 | 14 | # from /var/cfengine/bin. |
13 | 15 |
|
14 | | -fail=0 |
15 | | - |
16 | | -unsafe=0 |
17 | 16 | if [ "$1" = "--unsafe" ]; then |
18 | | - unsafe=1 |
| 17 | + UNSAFE_TESTS=1 |
19 | 18 | fi |
20 | | - |
21 | | -banner() { |
22 | | - name=" $(basename "$(dirname "$1")")/$(basename "$1") " |
23 | | - width=72 |
24 | | - left=$(( (width - ${#name}) / 2 )) |
25 | | - right=$(( width - ${#name} - left )) |
26 | | - [ "$left" -lt 0 ] && left=0 |
27 | | - [ "$right" -lt 0 ] && right=0 |
28 | | - printf '%s%s%s\n' \ |
29 | | - "$(printf "%${left}s" '' | tr ' ' '=')" \ |
30 | | - "$name" \ |
31 | | - "$(printf "%${right}s" '' | tr ' ' '=')" |
32 | | -} |
33 | | - |
34 | | -error() { |
35 | | - echo "test '$1' FAILED" >&2 |
36 | | - fail=1 |
37 | | -} |
38 | | - |
39 | | -# Print the path of binary $1, see the usage above. |
40 | | -find_binary() { |
41 | | - if [ -n "$CF_BINDIR" ]; then |
42 | | - echo "$CF_BINDIR/$1" |
43 | | - elif [ -x "$PWD/../../$1/$1" ]; then |
44 | | - # in-tree build, may be a libtool wrapper script |
45 | | - echo "$PWD/../../$1/$1" |
46 | | - else |
47 | | - echo "/var/cfengine/bin/$1" |
48 | | - fi |
| 19 | +export UNSAFE_TESTS |
| 20 | + |
| 21 | +dir=$(cd "$(dirname "$0")" && pwd) |
| 22 | + |
| 23 | +# for now() |
| 24 | +. "$dir/lib.sh" |
| 25 | + |
| 26 | +passed=0 |
| 27 | +failed=0 |
| 28 | +skipped=0 |
| 29 | +total=0 |
| 30 | +failures="" |
| 31 | +suite_start=$(now) |
| 32 | + |
| 33 | +# Run test $1 and print its result, with its output if it failed |
| 34 | +run_test() { |
| 35 | + name=${1#"$dir/tests/"} |
| 36 | + name=${name%.sh} |
| 37 | + total=$((total + 1)) |
| 38 | + |
| 39 | + start=$(now) |
| 40 | + output=$(sh "$dir/run_test.sh" "$1" 2>&1) |
| 41 | + result=$? |
| 42 | + elapsed=$(($(now) - start)) |
| 43 | + |
| 44 | + case $result in |
| 45 | + 0) |
| 46 | + passed=$((passed + 1)) |
| 47 | + echo "--- PASS: $name (${elapsed}s)" |
| 48 | + ;; |
| 49 | + 77) |
| 50 | + skipped=$((skipped + 1)) |
| 51 | + echo "--- SKIP: $name (${elapsed}s)" |
| 52 | + ;; |
| 53 | + *) |
| 54 | + failed=$((failed + 1)) |
| 55 | + failures="$failures $name |
| 56 | +" |
| 57 | + echo "--- FAIL: $name (${elapsed}s)" |
| 58 | + echo "$output" |
| 59 | + echo "---" |
| 60 | + ;; |
| 61 | + esac |
49 | 62 | } |
50 | 63 |
|
51 | | -CF_REACTOR=$(find_binary cf-reactor) |
52 | | -CF_PROMISES=$(find_binary cf-promises) |
53 | | -export CF_REACTOR |
54 | | - |
55 | | -for b in "$CF_REACTOR" "$CF_PROMISES"; do |
56 | | - if [ ! -x "$b" ]; then |
57 | | - echo "'$b' not found, build CFEngine or set CF_BINDIR" >&2 |
58 | | - exit 2 |
59 | | - fi |
60 | | -done |
61 | | -echo "Using $CF_REACTOR and $CF_PROMISES" |
62 | | - |
63 | | -for t in "$PWD"/tests/*/*.sh "$PWD"/tests/*/unsafe/*.sh; do |
| 64 | +for t in "$dir"/tests/*/*.sh "$dir"/tests/*/unsafe/*.sh; do |
64 | 65 | # skip the pattern itself if there are no unsafe tests |
65 | 66 | [ -f "$t" ] || continue |
66 | | - case "$t" in |
67 | | - */unsafe/*) [ "$unsafe" = 1 ] || continue ;; |
68 | | - esac |
69 | | - |
70 | | - banner "$t" |
71 | | - |
72 | | - # create throwaway work dir |
73 | | - WORKDIR=$(mktemp -d) |
74 | | - mkdir -p "$WORKDIR/bin" "$WORKDIR/tmp" |
75 | | - ln -s "$CF_PROMISES" "$WORKDIR/bin/cf-promises" |
76 | | - |
77 | | - # Run a copy of the policy, keeping its path relative to lib.cf, as |
78 | | - # CFEngine refuses to read policy files writable by group or others, which |
79 | | - # those of a checkout made with a umask of 002 are |
80 | | - rel=${t#"$PWD"/} |
81 | | - policy="$WORKDIR/policy/${rel%.sh}.cf" |
82 | | - mkdir -p "$(dirname "$policy")" |
83 | | - cp lib.cf "$WORKDIR/policy/" |
84 | | - cp "${t%.sh}.cf" "$policy" |
85 | | - chmod 600 "$WORKDIR/policy/lib.cf" "$policy" |
86 | | - |
87 | | - CFENGINE_TEST_OVERRIDE_WORKDIR="$WORKDIR" |
88 | | - LOG="$WORKDIR/reactor.log" |
89 | | - PROMISE_NAME=$(basename "$t" .sh) |
90 | | - export WORKDIR CFENGINE_TEST_OVERRIDE_WORKDIR LOG PROMISE_NAME |
91 | | - |
92 | | - sh "$t" "$policy" || error "$t" |
93 | | - |
94 | | - rm -rf "$WORKDIR" |
| 67 | + run_test "$t" |
95 | 68 | done |
96 | 69 |
|
97 | | -exit $fail |
| 70 | +echo "" |
| 71 | +echo "==============================" |
| 72 | +echo "Test Results: $passed passed, $failed failed, $skipped skipped (total: $total, $(($(now) - suite_start))s)" |
| 73 | +if [ "$failed" -gt 0 ]; then |
| 74 | + echo "" |
| 75 | + echo "Failed tests:" |
| 76 | + printf '%s' "$failures" |
| 77 | + exit 1 |
| 78 | +fi |
| 79 | +echo "==============================" |
| 80 | +echo "All reactor tests passed" |
0 commit comments