ctdb-tests: Reformat with "shfmt -w -p -i 0 -fn"
[cs/samba-autobuild/.git] / ctdb / tests / UNIT / eventscripts / scripts / 11.natgw.sh
1 setup()
2 {
3         debug "Setting up NAT gateway"
4
5         natgw_nodes="${CTDB_BASE}/natgw_nodes"
6
7         ctdb_set_pnn
8 }
9
10 # A separate function for this makes sense because it can be done
11 # multiple times per test
12 setup_ctdb_natgw()
13 {
14         # Read from stdin
15         while read _ip _opts; do
16                 case "$_opts" in
17                 leader)
18                         export FAKE_CTDB_NATGW_LEADER="$_ip"
19                         echo "$_ip"
20                         ;;
21                 follower-only)
22                         printf "%s\tfollower-only\n" "$_ip"
23                         ;;
24                 *)
25                         echo "$_ip"
26                         ;;
27                 esac
28         done >"$natgw_nodes"
29
30         # Assume all of the nodes are on a /24 network and have IPv4
31         # addresses:
32         read _ip <"$natgw_nodes"
33
34         setup_script_options <<EOF
35 CTDB_NATGW_NODES="$natgw_nodes"
36 CTDB_NATGW_PRIVATE_NETWORK="${_ip%.*}.0/24"
37 # These are fixed.  Probably don't use the same network for the
38 # private node IPs.  To unset the default gateway just set it to
39 # "".  :-)
40 CTDB_NATGW_PUBLIC_IP="10.1.1.121/24"
41 CTDB_NATGW_PUBLIC_IFACE="eth1"
42 CTDB_NATGW_DEFAULT_GATEWAY="10.1.1.254"
43 EOF
44 }
45
46 ok_natgw_leader_ip_addr_show()
47 {
48         _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" |
49                 cksum |
50                 sed -r -e 's@(..)(..)(..).*@fe:fe:fe:\1:\2:\3@')
51
52         # This is based on CTDB_NATGW_PUBLIC_IP
53         _brd="10.1.1.255"
54
55         ok <<EOF
56 1: ${CTDB_NATGW_PUBLIC_IFACE}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
57     link/ether ${_mac} brd ff:ff:ff:ff:ff:ff
58     inet ${CTDB_NATGW_PUBLIC_IP} brd ${_brd} scope global ${CTDB_NATGW_PUBLIC_IFACE}
59        valid_lft forever preferred_lft forever
60 EOF
61 }
62
63 ok_natgw_follower_ip_addr_show()
64 {
65         _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" |
66                 cksum |
67                 sed -r -e 's@(..)(..)(..).*@fe:fe:fe:\1:\2:\3@')
68
69         ok <<EOF
70 1: ${CTDB_NATGW_PUBLIC_IFACE}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
71     link/ether ${_mac} brd ff:ff:ff:ff:ff:ff
72 EOF
73 }
74
75 ok_natgw_leader_static_routes()
76 {
77         _nl="
78 "
79         _t=""
80         for _i in $CTDB_NATGW_STATIC_ROUTES; do
81                 # This is intentionally different to the code in 11.natgw ;-)
82                 case "$_i" in
83                 *@*)
84                         _net=$(echo "$_i" | sed -e 's|@.*||')
85                         _gw=$(echo "$_i" | sed -e 's|.*@||')
86                         ;;
87                 *)
88                         _net="$_i"
89                         _gw="$CTDB_NATGW_DEFAULT_GATEWAY"
90                         ;;
91                 esac
92
93                 [ -n "$_gw" ] || continue
94                 _t="${_t}${_t:+${_nl}}"
95                 _t="${_t}${_net} via ${_gw} dev ethXXX  metric 10 "
96         done
97         _t=$(echo "$_t" | sort)
98         ok "$_t"
99 }
100
101 ok_natgw_follower_static_routes()
102 {
103         _nl="
104 "
105         _t=""
106         for _i in $CTDB_NATGW_STATIC_ROUTES; do
107                 # This is intentionally different to the code in 11.natgw ;-)
108                 _net=$(echo "$_i" | sed -e 's|@.*||')
109
110                 # The interface for the private network isn't
111                 # specified as part of the NATGW configuration and
112                 # isn't part of the command to add the route.  It is
113                 # implicitly added by "ip route" but our stub doesn't
114                 # do this and adds "ethXXX".
115                 _t="${_t}${_t:+${_nl}}"
116                 _t="${_t}${_net} via ${FAKE_CTDB_NATGW_LEADER} dev ethXXX  metric 10 "
117         done
118         _t=$(echo "$_t" | sort)
119         ok "$_t"
120 }