tests/eventscripts: add extra infrastructure for policy routing tests
authorMartin Schwenke <martin@meltin.net>
Wed, 10 Oct 2012 03:48:59 +0000 (14:48 +1100)
committerMartin Schwenke <martin@meltin.net>
Thu, 11 Oct 2012 01:10:45 +0000 (12:10 +1100)
Less copying and pasting is a good thing...

Signed-off-by: Martin Schwenke <martin@meltin.net>
(This used to be ctdb commit 7d4b8cce96f33fff647a0c9d259c121dfc8403e9)

16 files changed:
ctdb/tests/eventscripts/13.per_ip_routing.001.sh
ctdb/tests/eventscripts/13.per_ip_routing.002.sh
ctdb/tests/eventscripts/13.per_ip_routing.003.sh
ctdb/tests/eventscripts/13.per_ip_routing.004.sh
ctdb/tests/eventscripts/13.per_ip_routing.005.sh
ctdb/tests/eventscripts/13.per_ip_routing.006.sh
ctdb/tests/eventscripts/13.per_ip_routing.007.sh
ctdb/tests/eventscripts/13.per_ip_routing.008.sh
ctdb/tests/eventscripts/13.per_ip_routing.009.sh
ctdb/tests/eventscripts/13.per_ip_routing.010.sh
ctdb/tests/eventscripts/13.per_ip_routing.011.sh
ctdb/tests/eventscripts/13.per_ip_routing.012.sh
ctdb/tests/eventscripts/13.per_ip_routing.013.sh
ctdb/tests/eventscripts/13.per_ip_routing.014.sh
ctdb/tests/eventscripts/13.per_ip_routing.015.sh
ctdb/tests/eventscripts/scripts/local.sh

index 3d22864edfbc06076b0e91e6d84cab8dde74dd4a..8523c1010f127392e322947dbeffdd4381e54588 100755 (executable)
@@ -6,11 +6,10 @@ define_test "not configured"
 
 setup_ctdb
 
-ok <<EOF
-# ip rule show
-0:     from all lookup local 
-32766: from all lookup main 
-32767: from all lookup default 
-EOF
+ok_null
+simple_test_event "takeip"
 
-simple_test_command dump_routes
+ok_null
+simple_test_event "ipreallocate"
+
+check_routes 0
index 4d5c68cce900ff80ad614d656dfff93852b43746..d6320c65d894962f53db714953493aec7f992eb0 100755 (executable)
@@ -7,6 +7,8 @@ define_test "missing config file"
 setup_ctdb
 setup_ctdb_policy_routing
 
+# Error because policy routing is configured but the configuration
+# file is missing.
 required_result 1 <<EOF
 error: CTDB_PER_IP_ROUTING_CONF=${CTDB_BASE}/policy_routing file not found
 EOF
index c1e0d01294d8164bbaa2834c3b02b0d7fface551..bb2c4b70fb2548f5ca125663668d412ad51df004 100755 (executable)
@@ -7,18 +7,11 @@ define_test "empty config, ipreallocated"
 setup_ctdb
 setup_ctdb_policy_routing
 
-touch "$CTDB_PER_IP_ROUTING_CONF"
+create_policy_routing_config 0
 
+# ipreallocated should silently add any missing routes
 ok_null
-
-# ipreallocated should add any missing routes
 simple_test_event "ipreallocated"
 
-ok <<EOF
-# ip rule show
-0:     from all lookup local 
-32766: from all lookup main 
-32767: from all lookup default 
-EOF
-
-simple_test_command dump_routes
+# empty configuration file should mean there are no routes
+check_routes 0
index 88ffac369cbb3c0362468d432b8520158b9272d7..4595313b715e5a58ae16de42a259d4feabaa25cd 100755 (executable)
@@ -7,19 +7,12 @@ define_test "empty config, takeip"
 setup_ctdb
 setup_ctdb_policy_routing
 
-touch "$CTDB_PER_IP_ROUTING_CONF"
+create_policy_routing_config 0
 
 public_address=$(ctdb_get_1_public_address)
 
 ok_null
-
 simple_test_event "takeip" $public_address
 
-ok <<EOF
-# ip rule show
-0:     from all lookup local 
-32766: from all lookup main 
-32767: from all lookup default 
-EOF
-
-simple_test_command dump_routes
+# empty configuration file should mean there are no routes
+check_routes 0
index 822f39f044d903ae498e9d2d6b147b62c73a4f2c..9495cc54ce7690d04100a3c8750928e3a9ceb281 100755 (executable)
@@ -7,32 +7,15 @@ define_test "1 IP configured, takeip"
 setup_ctdb
 setup_ctdb_policy_routing
 
-ctdb_get_1_public_address |
-{
-    read dev ip bits
-
-    net=$(ipv4_host_addr_to_net "$ip" "$bits")
-    gw="${net%.*}.1" # a dumb, calculated default
-
-    cat >"$CTDB_PER_IP_ROUTING_CONF" <<EOF
-$ip $net
-$ip 0.0.0.0/0 $gw
-EOF
+# Configuration for 1 IP
+create_policy_routing_config 1 default
 
+# takeip should add routes for the given address
+ctdb_get_1_public_address |
+while read dev ip bits ; do
     ok_null
-
     simple_test_event "takeip" $dev $ip $bits
+done
 
-    ok <<EOF
-# ip rule show
-0:     from all lookup local 
-${CTDB_PER_IP_ROUTING_RULE_PREF}:      from $ip lookup ctdb.$ip 
-32766: from all lookup main 
-32767: from all lookup default 
-# ip route show table ctdb.$ip
-$net dev $dev  scope link 
-default via $gw dev $dev 
-EOF
-
-    simple_test_command dump_routes
-}
+# Should have routes for 1 IP
+check_routes 1 default
index ed9df846fe75bfae27fd11874c003a742e9654cd..b93b6cdea1e422c9bd3ee0c4fa5029c900eea60c 100755 (executable)
@@ -7,32 +7,19 @@ define_test "1 IP configured, takeip, releaseip"
 setup_ctdb
 setup_ctdb_policy_routing
 
-ctdb_get_1_public_address |
-{
-    read dev ip bits
-
-    net=$(ipv4_host_addr_to_net "$ip" "$bits")
-    gw="${net%.*}.1" # a dumb, calculated default
-
-    cat >"$CTDB_PER_IP_ROUTING_CONF" <<EOF
-$ip $net
-$ip 0.0.0.0/0 $gw
-EOF
+# create config for 1 IP
+create_policy_routing_config 1 default
 
+ctdb_get_1_public_address |
+while read dev ip bits ; do
+    # takeip adds routes
     ok_null
-
     simple_test_event "takeip" $dev $ip $bits
 
+    # releaseip removes routes
     ok_null
-
     simple_test_event "releaseip" $dev $ip $bits
+done
 
-    ok <<EOF
-# ip rule show
-0:     from all lookup local 
-32766: from all lookup main 
-32767: from all lookup default 
-EOF
-
-    simple_test_command dump_routes
-}
+# should have no routes
+check_routes 0
index f7332ac50b990ea258f5c600a4d92caa6fdf0d7b..096bc96d99697e7db1eddbefc7c5d10b64a39576 100755 (executable)
@@ -7,34 +7,12 @@ define_test "1 IP configured, ipreallocated"
 setup_ctdb
 setup_ctdb_policy_routing
 
-ctdb_get_1_public_address |
-{
-    read dev ip bits
+# create config for 1 IP
+create_policy_routing_config 1 default
 
-    net=$(ipv4_host_addr_to_net "$ip" "$bits")
-    gw="${net%.*}.1" # a dumb, calculated default
-
-    cat >"$CTDB_PER_IP_ROUTING_CONF" <<EOF
-$ip $net
-$ip 0.0.0.0/0 $gw
-EOF
-
-    ok_null
-
-    # ipreallocated should add any missing routes
-    simple_test_event "ipreallocated"
-
-    ok <<EOF
-# ip rule show
-0:     from all lookup local 
-${CTDB_PER_IP_ROUTING_RULE_PREF}:      from $ip lookup ctdb.$ip 
-32766: from all lookup main 
-32767: from all lookup default 
-# ip route show table ctdb.$ip
-$net dev $dev  scope link 
-default via $gw dev $dev 
-EOF
-
-    simple_test_command dump_routes
-}
+# no takeip, but ipreallocated should add any missing routes
+ok_null
+simple_test_event "ipreallocated"
 
+# should have routes for 1 IP
+check_routes 1 default
index db37cbb2411825295030c757238d0c39fb0b7dc3..9bb0c195fddd5cccd06e09507ec8d6bac31513a5 100755 (executable)
@@ -7,36 +7,18 @@ define_test "1 IP configured, takeip twice"
 setup_ctdb
 setup_ctdb_policy_routing
 
-ctdb_get_1_public_address |
-{
-    read dev ip bits
-
-    net=$(ipv4_host_addr_to_net "$ip" "$bits")
-    gw="${net%.*}.1" # a dumb, calculated default
-
-    cat >"$CTDB_PER_IP_ROUTING_CONF" <<EOF
-$ip $net
-$ip 0.0.0.0/0 $gw
-EOF
+# create config for 1 IP
+create_policy_routing_config 1 default
 
+ctdb_get_1_public_address |
+while read dev ip bits ; do
     ok_null
-
     simple_test_event "takeip" $dev $ip $bits
 
+    # 2nd takeip event for the same IP should be a no-op
     ok_null
-
     simple_test_event "takeip" $dev $ip $bits
+done
 
-    ok <<EOF
-# ip rule show
-0:     from all lookup local 
-${CTDB_PER_IP_ROUTING_RULE_PREF}:      from $ip lookup ctdb.$ip 
-32766: from all lookup main 
-32767: from all lookup default 
-# ip route show table ctdb.$ip
-$net dev $dev  scope link 
-default via $gw dev $dev 
-EOF
-
-    simple_test_command dump_routes
-}
+# should be routes for 1 IP
+check_routes 1 default
index 3e33e903c2b0c66770e6d61d3eb0cc360ab8a8d4..cbea1ade61cf96f0cc2c9741db9c4f19e7720110 100755 (executable)
@@ -2,43 +2,20 @@
 
 . "${TEST_SCRIPTS_DIR}/unit.sh"
 
-define_test "All IPs configured, takeip"
+define_test "All IPs configured, takeip 1 address"
 
 setup_ctdb
 setup_ctdb_policy_routing
 
-ctdb_get_all_public_addresses |
-while read dev ip bits ; do
-    net=$(ipv4_host_addr_to_net "$ip" "$bits")
-    gw="${net%.*}.1" # a dumb, calculated default
-
-    cat <<EOF
-$ip $net
-$ip 0.0.0.0/0 $gw
-EOF
-done >"$CTDB_PER_IP_ROUTING_CONF"
+# configure all addresses
+create_policy_routing_config all default
 
+# add routes for all 1 IP
 ctdb_get_1_public_address |
-{
-    read dev ip bits
-
-    net=$(ipv4_host_addr_to_net "$ip" "$bits")
-    gw="${net%.*}.1" # a dumb, calculated default
-
+while read dev ip bits ; do
     ok_null
-
     simple_test_event "takeip" $dev $ip $bits
+done
 
-    ok <<EOF
-# ip rule show
-0:     from all lookup local 
-${CTDB_PER_IP_ROUTING_RULE_PREF}:      from $ip lookup ctdb.$ip 
-32766: from all lookup main 
-32767: from all lookup default 
-# ip route show table ctdb.$ip
-$net dev $dev  scope link 
-default via $gw dev $dev 
-EOF
-
-    simple_test_command dump_routes
-}
+# for 1 IP
+check_routes 1 default
index 4704b9f04c7b890bdee18aebf27f4a7e173bde44..d11585ec51068851af12d1c8b6fad6ea3642de6d 100755 (executable)
@@ -2,51 +2,19 @@
 
 . "${TEST_SCRIPTS_DIR}/unit.sh"
 
-define_test "All IPs configured, takeip all on node"
+define_test "All IPs configured, takeip on all nodes"
 
 setup_ctdb
 setup_ctdb_policy_routing
 
-# First setup the policy routing config for all possible IPs
-ctdb_get_all_public_addresses |
-while read dev ip bits ; do
-    net=$(ipv4_host_addr_to_net "$ip" "$bits")
-    gw="${net%.*}.1" # a dumb, calculated default
-
-    cat <<EOF
-$ip $net
-$ip 0.0.0.0/0 $gw
-EOF
-done >"$CTDB_PER_IP_ROUTING_CONF"
+# create config for all IPs
+create_policy_routing_config all default
 
-# Now do a takeip for each IP on the "current" node
 ctdb_get_my_public_addresses |
-{
-    policy_rules=""
-    policy_routes=""
-    while read dev ip bits ; do
-
-       net=$(ipv4_host_addr_to_net "$ip" "$bits")
-       gw="${net%.*}.1" # a dumb, calculated default
-
-       ok_null
-
-       simple_test_event "takeip" $dev $ip $bits
-
-       policy_rules="${policy_rules}
-${CTDB_PER_IP_ROUTING_RULE_PREF}:      from $ip lookup ctdb.$ip "
-       policy_routes="${policy_routes}
-# ip route show table ctdb.$ip
-$net dev $dev  scope link 
-default via $gw dev $dev "
-    done
-
-       ok <<EOF
-# ip rule show
-0:     from all lookup local ${policy_rules}
-32766: from all lookup main 
-32767: from all lookup default ${policy_routes}
-EOF
+while read dev ip bits ; do
+    ok_null
+    simple_test_event "takeip" $dev $ip $bits
+done
 
-    simple_test_command dump_routes
-}
+# should have routes for all IPs
+check_routes all default
index 65adbb5d5a59c4e553ef28e8de9d22abdb39c118..d8ec9ac2106c952470f2e4d0b501c19a1d80880e 100755 (executable)
@@ -6,36 +6,15 @@ define_test "__auto_link_local__, takeip all on node"
 
 setup_ctdb
 setup_ctdb_policy_routing
-# Override to do link local fu
-CTDB_PER_IP_ROUTING_CONF="__auto_link_local__"
 
-# Do a takeip for each IP on the "current" node
-ctdb_get_my_public_addresses |
-{
-    policy_rules=""
-    policy_routes=""
-    while read dev ip bits ; do
-
-       net=$(ipv4_host_addr_to_net "$ip" "$bits")
-       gw="${net%.*}.1" # a dumb, calculated default
-
-       ok_null
+# do link local fu instead of creating configuration
+export CTDB_PER_IP_ROUTING_CONF="__auto_link_local__"
 
-       simple_test_event "takeip" $dev $ip $bits
-
-       policy_rules="${policy_rules}
-${CTDB_PER_IP_ROUTING_RULE_PREF}:      from $ip lookup ctdb.$ip "
-       policy_routes="${policy_routes}
-# ip route show table ctdb.$ip
-$net dev $dev  scope link "
-    done
-
-       ok <<EOF
-# ip rule show
-0:     from all lookup local ${policy_rules}
-32766: from all lookup main 
-32767: from all lookup default ${policy_routes}
-EOF
+# add routes for all addresses
+ctdb_get_my_public_addresses |
+while read dev ip bits ; do
+    ok_null
+    simple_test_event "takeip" $dev $ip $bits
+done
 
-    simple_test_command dump_routes
-}
+check_routes all
index 198551f877f13506c626103ed51e8912675c325f..6c8a6ab4b33b289b74c1085cd10012d416e4d787 100755 (executable)
@@ -11,38 +11,20 @@ define_test "1 IP configured, takeip, releaseip, ipreallocated"
 setup_ctdb
 setup_ctdb_policy_routing
 
-ctdb_get_1_public_address |
-{
-    read dev ip bits
-
-    net=$(ipv4_host_addr_to_net "$ip" "$bits")
-    gw="${net%.*}.1" # a dumb, calculated default
-
-    cat >"$CTDB_PER_IP_ROUTING_CONF" <<EOF
-$ip $net
-$ip 0.0.0.0/0 $gw
-EOF
+create_policy_routing_config 1 default
 
+ctdb_get_1_public_address |
+while read dev ip bits ; do
     ok_null
-
     simple_test_event "takeip" $dev $ip $bits
 
     ok_null
-
     ctdb moveip $ip 1
     simple_test_event "releaseip" $dev $ip $bits
 
     ok_null
-
-    # This will cause any
     simple_test_event "ipreallocated"
+done
 
-    ok <<EOF
-# ip rule show
-0:     from all lookup local 
-32766: from all lookup main 
-32767: from all lookup default 
-EOF
-
-    simple_test_command dump_routes
-}
+# all routes should have been removed and not added back
+check_routes 0
index 5c07632023876d98c2513826140fff386677a533..567622edd93aae911c4fcc91293f6abf677b5792 100755 (executable)
@@ -7,18 +7,10 @@ define_test "1 IP configured, releaseip of unassigned"
 setup_ctdb
 setup_ctdb_policy_routing
 
-ctdb_get_1_public_address |
-{
-    read dev ip bits
-
-    net=$(ipv4_host_addr_to_net "$ip" "$bits")
-    gw="${net%.*}.1" # a dumb, calculated default
-
-    cat >"$CTDB_PER_IP_ROUTING_CONF" <<EOF
-$ip $net
-$ip 0.0.0.0/0 $gw
-EOF
+create_policy_routing_config 1 default
 
+ctdb_get_1_public_address |
+while read dev ip bits ; do
     ok <<EOF
 WARNING: Failed to delete policy routing rule
   Command "ip rule del from $ip pref $CTDB_PER_IP_ROUTING_RULE_PREF table ctdb.$ip" failed:
@@ -26,13 +18,7 @@ WARNING: Failed to delete policy routing rule
 EOF
 
     simple_test_event "releaseip" $dev $ip $bits
+done
 
-    ok <<EOF
-# ip rule show
-0:     from all lookup local 
-32766: from all lookup main 
-32767: from all lookup default 
-EOF
-
-    simple_test_command dump_routes
-}
+# there should be no routes
+check_routes 0
index 9327264ecd3e517d7a4f1ccbb79fe32d5c1a6a28..ee08c36fa322761cb235279031d55a7f43fae609 100755 (executable)
@@ -10,38 +10,21 @@ define_test "1 IP configured, takeip, moveip, ipreallocated"
 setup_ctdb
 setup_ctdb_policy_routing
 
-ctdb_get_1_public_address |
-{
-    read dev ip bits
-
-    net=$(ipv4_host_addr_to_net "$ip" "$bits")
-    gw="${net%.*}.1" # a dumb, calculated default
-
-    cat >"$CTDB_PER_IP_ROUTING_CONF" <<EOF
-$ip $net
-$ip 0.0.0.0/0 $gw
-EOF
+create_policy_routing_config 1 default
 
+ctdb_get_1_public_address |
+while read dev ip bits ; do
     ok_null
-
     # Set up the routes for an IP that we have
     simple_test_event "takeip" $dev $ip $bits
 
     # Now move that IPs but don't run the associated "releaseip"
     ctdb moveip $ip 1
 
-    ok <<EOF
-Removing ip rule/routes for unhosted public address 10.0.0.3
-EOF
-
+    # This should handle removal of the routes
+    ok "Removing ip rule/routes for unhosted public address $ip"
     simple_test_event "ipreallocated"
+done
 
-    ok <<EOF
-# ip rule show
-0:     from all lookup local 
-32766: from all lookup main 
-32767: from all lookup default 
-EOF
-
-    simple_test_command dump_routes
-}
+# no routes left
+check_routes 0
index 5258438b002fc335d770834079edab53f573b930..2b9ecba05345af5a6d0498463fbb802fb5e5bb0e 100755 (executable)
@@ -9,18 +9,12 @@ setup_ctdb_policy_routing
 
 export IP_ROUTE_BAD_TABLE_ID=true
 
+create_policy_routing_config 1 default
+
 ctdb_get_1_public_address |
 {
     read dev ip bits
 
-    net=$(ipv4_host_addr_to_net "$ip" "$bits")
-    gw="${net%.*}.1" # a dumb, calculated default
-
-    cat >"$CTDB_PER_IP_ROUTING_CONF" <<EOF
-$ip $net
-$ip 0.0.0.0/0 $gw
-EOF
-
     ok <<EOF
 WARNING: Failed to delete policy routing rule
   Command "ip rule del from $ip pref $CTDB_PER_IP_ROUTING_RULE_PREF table ctdb.$ip" failed:
@@ -29,13 +23,8 @@ WARNING: Failed to delete policy routing rule
 EOF
 
     simple_test_event "releaseip" $dev $ip $bits
+}
 
-    ok <<EOF
-# ip rule show
-0:     from all lookup local 
-32766: from all lookup main 
-32767: from all lookup default 
-EOF
 
-    simple_test_command dump_routes
-}
+# there should be no routes
+check_routes 0
index 56d09fa980e98ae4a5a50fce23df3a8952dfcb2f..9e746b52d1b325c62f9b6440fa6919b3e42494f9 100644 (file)
@@ -364,6 +364,8 @@ ctdb_fake_scriptstatus ()
     echo "$_code $_status $_err_out" >"$FAKE_CTDB_SCRIPTSTATUS/$script"
 }
 
+######################################################################
+
 setup_ctdb_policy_routing ()
 {
     export CTDB_PER_IP_ROUTING_CONF="$CTDB_BASE/policy_routing"
@@ -375,6 +377,76 @@ setup_ctdb_policy_routing ()
     rm -f "$CTDB_PER_IP_ROUTING_CONF"
 }
 
+# Create policy routing configuration in $CTDB_PER_IP_ROUTING_CONF.
+# $1 is the number of assigned IPs to use (<num>, all), defaulting to
+# 1.  If $2 is "default" then a default route is also added.
+create_policy_routing_config ()
+{
+    _num_ips="${1:-1}"
+    _should_add_default="$2"
+
+    ctdb_get_my_public_addresses |
+    if [ "$_num_ips" = "all" ] ; then
+       cat
+    else
+       head -n "$_num_ips"
+    fi |
+    while read _dev _ip _bits ; do
+       _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
+       _gw="${_net%.*}.1" # a dumb, calculated default
+
+       echo "$_ip $_net"
+
+       if [ "$_should_add_default" = "default" ] ; then
+           echo "$_ip 0.0.0.0/0 $_gw"
+       fi
+    done >"$CTDB_PER_IP_ROUTING_CONF"
+}
+
+# Check the routes against those that are expected.  $1 is the number
+# of assigned IPs to use (<num>, all), defaulting to 1.  If $2 is
+# "default" then expect default routes to have been added.
+check_routes ()
+{
+    _num_ips="${1:-1}"
+    _should_add_default="$2"
+
+    _policy_rules=""
+    _policy_routes=""
+
+    ctdb_get_my_public_addresses |
+    if [ "$_num_ips" = "all" ] ; then
+       cat
+    else
+       head -n "$_num_ips"
+    fi | {
+       while read _dev _ip _bits ; do
+           _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
+           _gw="${_net%.*}.1" # a dumb, calculated default
+
+           _policy_rules="${_policy_rules}
+${CTDB_PER_IP_ROUTING_RULE_PREF}:      from $_ip lookup ctdb.$_ip "
+           _policy_routes="${_policy_routes}
+# ip route show table ctdb.$_ip
+$_net dev $_dev  scope link "
+
+           if [ "$_should_add_default" = "default" ] ; then
+               _policy_routes="${_policy_routes}
+default via $_gw dev $_dev "
+           fi
+       done
+
+       ok <<EOF
+# ip rule show
+0:     from all lookup local ${_policy_rules}
+32766: from all lookup main 
+32767: from all lookup default ${_policy_routes}
+EOF
+
+       simple_test_command dump_routes
+    }
+}
+
 ######################################################################
 
 # Samba/winbind fakery