]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: add tests for networkctl edit/cat 27157/head
authorMike Yuan <me@yhndnzj.com>
Mon, 10 Apr 2023 10:45:00 +0000 (18:45 +0800)
committerMike Yuan <me@yhndnzj.com>
Sun, 21 May 2023 07:55:42 +0000 (15:55 +0800)
test/TEST-74-AUX-UTILS/test.sh
test/units/testsuite-74.networkctl.sh [new file with mode: 0755]

index 1e360658ed5364a268dac9ecbe98582f5cfa914a..f033ec469f38dec0d74011f41635015b479edc66 100755 (executable)
@@ -3,6 +3,7 @@
 set -e
 
 TEST_DESCRIPTION="Tests for auxiliary utilities"
+NSPAWN_ARGUMENTS="--private-network"
 
 # shellcheck source=test/test-functions
 . "${TEST_BASE_DIR:?}/test-functions"
diff --git a/test/units/testsuite-74.networkctl.sh b/test/units/testsuite-74.networkctl.sh
new file mode 100755 (executable)
index 0000000..63c9ff1
--- /dev/null
@@ -0,0 +1,86 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: LGPL-2.1-or-later
+# shellcheck disable=SC2016
+set -eux
+set -o pipefail
+
+# shellcheck source=test/units/util.sh
+. "$(dirname "$0")"/util.sh
+
+at_exit() {
+    systemctl stop systemd-networkd
+
+    if [[ -v NETWORK_NAME && -v NETDEV_NAME && -v LINK_NAME ]]; then
+        rm -fvr {/usr/lib,/etc}/systemd/network/"$NETWORK_NAME" "/usr/lib/systemd/network/$NETDEV_NAME" \
+            {/usr/lib,/etc}/systemd/network/"$LINK_NAME" "/etc/systemd/network/${NETWORK_NAME}.d" \
+            "new" "+4"
+    fi
+}
+
+trap at_exit EXIT
+
+export NETWORK_NAME="networkctl-test-$RANDOM.network"
+export NETDEV_NAME="networkctl-test-$RANDOM.netdev"
+export LINK_NAME="90-networkctl-test-$RANDOM.link"
+cat >"/usr/lib/systemd/network/$NETWORK_NAME" <<EOF
+[Match]
+Name=test
+EOF
+
+# Test files
+networkctl cat "$NETWORK_NAME" | tail -n +2 | cmp - "/usr/lib/systemd/network/$NETWORK_NAME"
+
+cat >new <<EOF
+[Match]
+Name=test2
+EOF
+
+EDITOR='mv new' script -ec 'networkctl edit "$NETWORK_NAME"' /dev/null
+printf '%s\n' '[Match]' 'Name=test2' | cmp - "/etc/systemd/network/$NETWORK_NAME"
+
+cat >"+4" <<EOF
+[Network]
+DHCP=yes
+EOF
+
+EDITOR='cp' script -ec 'networkctl edit "$NETWORK_NAME" --drop-in test' /dev/null
+cmp "+4" "/etc/systemd/network/${NETWORK_NAME}.d/test.conf"
+
+networkctl cat "$NETWORK_NAME" | grep '^# ' |
+    cmp - <(printf '%s\n' "# /etc/systemd/network/$NETWORK_NAME" "# /etc/systemd/network/${NETWORK_NAME}.d/test.conf")
+
+cat >"/usr/lib/systemd/network/$NETDEV_NAME" <<EOF
+[NetDev]
+Name=test2
+Kind=dummy
+EOF
+
+networkctl cat "$NETDEV_NAME"
+
+cat >"/usr/lib/systemd/network/$LINK_NAME" <<EOF
+[Match]
+OriginalName=test2
+
+[Link]
+Alias=test_alias
+EOF
+
+SYSTEMD_LOG_LEVEL=debug EDITOR='true' script -ec 'networkctl edit "$LINK_NAME"' /dev/null
+cmp "/usr/lib/systemd/network/$LINK_NAME" "/etc/systemd/network/$LINK_NAME"
+
+# Test links
+systemctl unmask systemd-networkd
+systemctl stop systemd-networkd
+(! networkctl cat @test2)
+
+systemctl start systemd-networkd
+sleep 1
+networkctl cat @test2:network | cmp - <(networkctl cat "$NETWORK_NAME")
+
+EDITOR='cp' script -ec 'networkctl edit @test2 --drop-in test2.conf' /dev/null
+cmp "+4" "/etc/systemd/network/${NETWORK_NAME}.d/test2.conf"
+
+ip_link="$(ip link show test2)"
+if systemctl --quiet is-active systemd-udevd; then
+    assert_in 'alias test_alias' "$ip_link"
+fi