From 8e25667f87a4d422fc3f688453f69f96ced28f96 Mon Sep 17 00:00:00 2001 From: Jonathan Gold Date: Mon, 2 Apr 2018 00:15:39 -0400 Subject: [PATCH] engine: resources: net: test: Add shell test for net resource This patch adds a shell test for net, which creates a dummy interface and runs mgmt to bring it up and assign it with an address. It then checks if the state was applied correctly. Finally, it runs mgmt again to bring the interface down, and tests that it comes down and stays down. --- test/shell/net.sh | 109 ++++++++++++++++++++++++++++++++++++++++++++ test/shell/net0.mcl | 4 ++ test/shell/net1.mcl | 3 ++ 3 files changed, 116 insertions(+) create mode 100755 test/shell/net.sh create mode 100644 test/shell/net0.mcl create mode 100644 test/shell/net1.mcl diff --git a/test/shell/net.sh b/test/shell/net.sh new file mode 100755 index 00000000..23a1def4 --- /dev/null +++ b/test/shell/net.sh @@ -0,0 +1,109 @@ +#!/bin/bash -e + +set -x +set -o pipefail + +# can't test net without sudo +if ! timeout 1s sudo -A true; then + echo "sudo disabled: not checking net" + exit +fi + +# values from net0.yaml +IFACE="mgmtnet0" +ADDR="192.168.42.13/24" + +# bad value used to test events +BADADDR="10.0.0.254/24" + +# is_up returns 0 if the interface ($1) is up, and 1 if it is down. +function is_up { + if [ -z "$(ip link show $1 up)" ]; then + return 1 + fi +} + +# has_addr returns 0 if the iface ($1) has the addr ($2), and 1 if it doesn't. +function has_addr { + if [ -z "$(ip addr show $1 | grep $2)" ]; then + return 1 + fi +} + +# clean up when we're done +function cleanup { + sudo rm /etc/systemd/network/mgmt-${IFACE}.network || true + sudo ip link del $IFACE || true +} +trap cleanup EXIT + +# make sure there's somewhere to save the unit file +sudo mkdir -p /etc/systemd/network + +# add a dummy link for tests +sudo ip link add $IFACE type dummy || true + +# run mgmt net res with $IFACE and $ADDR set as above +sudo -A $timeout --kill-after=55s 50s "$MGMT" run --lang ./net0.mcl --converged-timeout=5 --tmp-prefix & +pid1=$! + +# give the engine time to start up +sleep 5 + +# make sure the interface is up +if ! is_up $IFACE; then + echo "failed to bring up $IFACE" + exit 1 +fi +# check the address +if ! has_addr $IFACE $ADDR; then + echo "failed to set addr: $ADDR on $IFACE" + exit 1 +fi + +# make sure the interface comes up if we set it down +sudo ip link set down $IFACE +if ! is_up $IFACE; then + echo "failed to bring $IFACE back up" + exit 1 +fi +# make sure the address is replaced if we delete it +sudo ip addr del $ADDR dev $IFACE +if ! has_addr $IFACE $ADDR; then + echo "failed to replace addr: $ADDR on $IFACE" + exit 1 +fi + +# add a bad address and make sure it is removed +sudo ip addr add $BADADDR dev $IFACE +if has_addr $IFACE $BADADDR; then + echo "failed to remove addr: $BADADDR from $IFACE" + exit 1 +fi + +wait $pid1 +e1=$? + +# run mgmt net res with $IFACE state => "down" +sudo -A $timeout --kill-after=20 15s "$MGMT" run --lang ./net1.mcl --converged-timeout=5 --tmp-prefix & + +# give the engine time to start up +sleep 5 + +# make sure the interface is down +if is_up $IFACE; then + echo "failed to bring down $IFACE" + exit 1 +fi + +# bring up the interface and make sure it's brought back down +sudo ip link set up $IFACE +if is_up $IFACE; then + echo "failed to bring $IFACE back down" + exit 1 +fi + +wait $pid2 +e2=$? + +exit $(($e1+$e2)) diff --git a/test/shell/net0.mcl b/test/shell/net0.mcl new file mode 100644 index 00000000..7ebcd853 --- /dev/null +++ b/test/shell/net0.mcl @@ -0,0 +1,4 @@ +net "mgmtnet0" { + state => "up", + addrs => ["192.168.42.13/24",], +} diff --git a/test/shell/net1.mcl b/test/shell/net1.mcl new file mode 100644 index 00000000..b51ab92e --- /dev/null +++ b/test/shell/net1.mcl @@ -0,0 +1,3 @@ +net "mgmtnet0" { + state => "down", +}