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.
This commit is contained in:
Jonathan Gold
2018-04-02 00:15:39 -04:00
parent 9b5c4c50e7
commit 8e25667f87
3 changed files with 116 additions and 0 deletions

109
test/shell/net.sh Executable file
View File

@@ -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))

4
test/shell/net0.mcl Normal file
View File

@@ -0,0 +1,4 @@
net "mgmtnet0" {
state => "up",
addrs => ["192.168.42.13/24",],
}

3
test/shell/net1.mcl Normal file
View File

@@ -0,0 +1,3 @@
net "mgmtnet0" {
state => "down",
}