summaryrefslogtreecommitdiffstats
path: root/src/lib/asiolink/tests/process_spawn_app.sh.in
blob: eab73107116959926d2be113115eabe8c786562a (plain)
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
#!/bin/sh

# Copyright (C) 2015-2021 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.


# This script is used for testing the ProcessSpawn utility class. This
# class is used to fork and execute a new process. It also allows for
# checking the exit code returned when the process terminates.
# The unit tests execute this script via ProcessSpawn class with
# different command line parameters to test the class functionality.
#
# In particular, they check if the class correctly records the exit code
# returned. The exit code returned is controlled by the caller. It is
# possible to explicitly specify the exit code to be returned using
# the command line options. It is also possible to specify that the
# exit code is "unique" for the process, so as the test can check
# that two distinct processes spawned by the same ProcessSpawn
# object may return different status code. The command line of this
# script also allows for forcing the process to sleep so as the
# test has much enough time to verify that the convenience methods
# checking the state of the process, i.e. process running or not.

# Exit with error if commands exit with non-zero and if undefined variables are
# used.
set -eu

exit_code=

while test "${#}" -gt 0
do
    option=${1}
    case ${option} in
        -p)
            exit_code=${$}
            ;;
        -e)
            shift
            exit_code=${1-}
            ;;
        -s)
            shift
            sleep "${1}"
            ;;
        -v)
            shift
            VAR_NAME=${1}
            shift
            VAR_VALUE=${1}
            EXPECTED=$(env | grep -E "^${VAR_NAME}=")
            if ! test "${VAR_NAME}=${VAR_VALUE}" = "${EXPECTED}"; then
                exit 123
            fi
            ;;
        *)
            exit 123
            ;;
    esac
    # We've shifted in the loop so we may have run out of parameters in the
    # meantime. Check again.
    if test "${#}" -gt 0; then
      shift
    fi
done

# The exit code of 32 is returned when no args specified or
# when only the -s arg has been specified.
if [ -z "${exit_code}" ]; then
    exit 32
fi

exit "${exit_code}"