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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#!/bin/bash
set -eu -o pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
export ETCDCTL_API=3
# testing parameters
vip=10.0.2.123
function get_dev {
# select a suitable device for testing purposes
# * a device that is an "ether"
# * state is UP not DOWN
# * and isn't a nil hardware address
# strip suffix from name (veth3@if8 -> veth3)
ip -oneline link show | grep link/ether | grep state.UP | grep -v 00:00:00:00:00:00 | cut -d ":" -f2 | cut -d "@" -f 1 | head -n1
}
dev="`get_dev`"
# prerequisite test: do we have a suitable device?
test -n "$dev"
#cleanup
function cleanup {
if test -f .ncatPid
then
kill `cat .ncatPid` 2> /dev/null || true
rm .ncatPid
fi
if test -f .vipPid
then
kill `cat .vipPid` 2> /dev/null || true
rm .vipPid
fi
if test -f .etcdPid
then
kill `cat .etcdPid` 2> /dev/null || true
rm .etcdPid
fi
if test -f .failed
then
echo -e "${RED}### Some tests failed! ###${NC}"
rm .failed
fi
}
trap cleanup EXIT
# prerequisite test: vip should not yet be registered
! ip address show dev $dev | grep $vip
# run etcd with podman/docker maybe?
# podman rm etcd || true
# podman run -d --name etcd -p 2379:2379 -e "ALLOW_NONE_AUTHENTICATION=yes" bitnami/etcd
# run etcd locally maybe?
etcd &
echo $! > .etcdPid
sleep 2
# simulate server, e.g. postgres
ncat -vlk 0.0.0.0 12345 -e "/bin/echo $HOSTNAME" &
echo $! > .ncatPid
etcdctl del service/pgcluster/leader || true
touch .failed
vip-manager --interval 3000 --interface $dev --ip $vip --netmask 32 --trigger-key service/pgcluster/leader --trigger-value $HOSTNAME & #2>&1 &
echo $! > .vipPid
sleep 2
# test 1: vip should still not be registered
! ip address show dev $dev | grep $vip
# simulate patroni member promoting to leader
etcdctl put service/pgcluster/leader $HOSTNAME
sleep 2
# test 2: vip should now be registered
ip address show dev $dev | grep $vip
ncat -vzw 1 $vip 12345
# simulate leader change
etcdctl put service/pgcluster/leader 0xGARBAGE
sleep 2
# test 3: vip should be deregistered again
! ip address show dev $dev | grep $vip
! ncat -vzw 1 $vip 12345
rm .failed
echo -e "${GREEN}### You've reached the end of the script, all \"tests\" have successfully been passed! ###${NC}"
|