File: TEST-02-UNITTESTS.sh

package info (click to toggle)
systemd-udeb 259-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 104,120 kB
  • sloc: ansic: 726,480; xml: 121,118; python: 35,852; sh: 33,447; cpp: 946; awk: 102; makefile: 89; lisp: 13; sed: 1
file content (133 lines) | stat: -rwxr-xr-x 3,550 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later
set -eux
set -o pipefail

# shellcheck source=test/units/util.sh
. "$(dirname "$0")"/util.sh

if ! systemd-detect-virt -qc && [[ "${TEST_CMDLINE_NEWLINE:-}" != bar ]]; then
    cat /proc/cmdline
    echo >&2 "Expected TEST_CMDLINE_NEWLINE=bar from the kernel command line"
    exit 1
fi

if built_with_musl; then
    SYSTEMD_LIBC=musl
else
    SYSTEMD_LIBC=glibc
fi
export SYSTEMD_LIBC

if [[ -z "${TEST_MATCH_SUBTEST:-}" ]]; then
    # If we're running with TEST_PREFER_NSPAWN=1 limit the set of tests we run
    # in QEMU to only those that can't run in a container to avoid running
    # the same tests again in a, most likely, very slow environment
    if ! systemd-detect-virt -qc && [[ "${TEST_PREFER_NSPAWN:-0}" -ne 0 ]]; then
        TEST_MATCH_SUBTEST="test-loop-block"
    else
        TEST_MATCH_SUBTEST="test-*"
    fi
fi

NPROC=$(nproc)
MAX_QUEUE_SIZE=${NPROC:-2}

# Reset state
rm -fv /failed /skipped /testok
touch /lock

if ! systemd-detect-virt -qc; then
    # Make sure ping works for unprivileged users (for test-bpf-firewall)
    sysctl net.ipv4.ping_group_range="0 2147483647"
fi

# Disable firewalld and friends to make them not disturb test-firewall-util
systemctl disable --now firewalld.service || :
systemctl disable --now iptables.service || :
systemctl disable --now ip6tables.service || :

# Check & report test results
# Arguments:
#   $1: test path
run_test() {
    if [[ $# -ne 1 ]]; then
        echo >&2 "run_test: missing arguments"
        exit 1
    fi

    local test="$1"
    local name="${test##*/}"
    local environment=

    echo "Executing test $name as unit $name.service"

    case "$name" in
        test-journal-flush)
            environment="SYSTEMD_LOG_LEVEL=info"
            ;;
        test-journal-verify)
            environment="SYSTEMD_LOG_LEVEL=crit"
            ;;
    esac

    systemd-run \
        --quiet \
        --property Delegate=1 \
        --property EnvironmentFile=-/usr/lib/systemd/systemd-asan-env \
        --property "Environment=$environment" \
        --setenv SYSTEMD_LIBC \
        --unit="$name" \
        --wait "$test" && ret=0 || ret=$?

    exec {LOCK_FD}> /lock
    flock --exclusive ${LOCK_FD}

    if [[ $ret -eq 77 ]] || [[ $ret -eq 127 ]]; then
        echo "$name skipped"
        echo "$name" >>/skipped-tests
        {
            echo "--- $name begin ---"
            journalctl --unit="$name" --no-hostname -o short-monotonic
            echo "--- $name end ---"
        } >>/skipped
    elif [[ $ret -ne 0 ]]; then
        echo "$name failed with $ret"
        echo "$name" >>/failed-tests
        {
            echo "--- $name begin ---"
            journalctl --unit="$name" --no-hostname -o short-monotonic
            echo "--- $name end ---"
        } >>/failed
    else
        echo "$name OK"
        echo "$name" >>/testok
    fi

    exec {LOCK_FD}<&-
}

export -f run_test

find /usr/lib/systemd/tests/unit-tests/ -maxdepth 1 -type f -name "${TEST_MATCH_SUBTEST}" -print0 |
    xargs -0 -I {} --max-procs="$MAX_QUEUE_SIZE" bash -ec "run_test {}"

# Write all pending messages, so they don't get mixed with the summaries below
journalctl --sync

# No need for full test logs in this case
if [[ -s /skipped-tests ]]; then
    : "=== SKIPPED TESTS ==="
    cat /skipped-tests
fi

if [[ -s /failed ]]; then
    : "=== FAILED TESTS ==="
    cat /failed
fi

# Test logs are sometimes lost, as the system shuts down immediately after
journalctl --sync

test ! -s /failed
touch /testok