aboutsummaryrefslogtreecommitdiff
path: root/tests/import-wireguard-configs/fake-nmcli
blob: 45b88cd2d16b601140375a2e47ae0e5bf351ea99 (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
#!/bin/bash
# Fake nmcli for the import-wireguard-configs tests.
#
# Behavior is driven by env vars set by the test harness:
#   FAKE_NMCLI_LOG    file every invocation's args are appended to (one line
#                     per call; for imports the staged file's basename and
#                     content hash context are visible in the args)
#   FAKE_NMCLI_NAMES  newline-separated connection names returned by
#                     `nmcli -t -f NAME connection show`
#   FAKE_NMCLI_IMPORT_OUT  override for the import command's stdout
#                     (default: the real NM success line with a per-call
#                     deterministic UUID)
#   FAKE_NMCLI_MODIFY_RC   exit code for `nmcli connection modify` (default 0)
#
# Import calls also copy the staged file into $FAKE_NMCLI_LOG.d/ so tests can
# assert the temp copy was named wgpvpn.conf and carried the right content.
set -euo pipefail

echo "$*" >>"$FAKE_NMCLI_LOG"

case "$1 $2" in
"-t -f")
    # nmcli -t -f NAME connection show
    printf '%s\n' "${FAKE_NMCLI_NAMES:-}"
    ;;
"connection import")
    # nmcli connection import type wireguard file <path>
    file="${6:?}"
    mkdir -p "$FAKE_NMCLI_LOG.d"
    n=$(find "$FAKE_NMCLI_LOG.d" -type f | wc -l)
    cp "$file" "$FAKE_NMCLI_LOG.d/import-$n-$(basename "$file")"
    if [ -n "${FAKE_NMCLI_IMPORT_OUT:-}" ]; then
        echo "$FAKE_NMCLI_IMPORT_OUT"
    else
        printf "Connection 'wgpvpn' (%08d-aaaa-bbbb-cccc-dddddddddddd) successfully added.\n" "$n"
    fi
    ;;
"connection modify")
    exit "${FAKE_NMCLI_MODIFY_RC:-0}"
    ;;
*)
    echo "fake-nmcli: unexpected args: $*" >&2
    exit 99
    ;;
esac