From e99462642949297b07d08bf0bbd43662d2f2910f Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Fri, 14 Sep 2012 15:04:05 +0930 Subject: [PATCH] Refactor test definitions Move create_rhizome_identities() from testdefs_rhizome.sh to create_identities() in testdefs.sh Rename create_identity() in testdefs.sh to create_single_identity(), uses create_identities() to do its work. --- testdefs.sh | 138 +++++++++++++++++++++++++++++++----------- testdefs_rhizome.sh | 46 -------------- tests/dnahelper | 3 + tests/rhizomeops | 4 +- tests/rhizomeprotocol | 4 +- 5 files changed, 108 insertions(+), 87 deletions(-) diff --git a/testdefs.sh b/testdefs.sh index 71bfdf0d..ac0b9763 100644 --- a/testdefs.sh +++ b/testdefs.sh @@ -416,41 +416,89 @@ assert_all_servald_servers_no_errors() { } # Utility function -# - create an identity +# - create an identity in the current instance {I} # - assign a phone number (DID) and name to the new identity, use defaults # if not specified by arg1 and arg2 -# - set the SID variable to the SID of the new identity -# - set the DID variable to the phone number of the new identity -# - set the NAME variable to the name of the new identity -create_identity() { - executeOk_servald keyring add - assert [ -e "$SERVALINSTANCE_PATH/serval.keyring" ] +# - assert the new identity is the only one in this instance +# - set the SID{I} variable, eg SIDA, to the SID of the new identity +# - set the DID{I} variable, eg DIDA, to the phone number of the new identity +# - set the NAME{I} variable, eg NAMEA, to the name of the new identity +create_single_identity() { + DID${instance_name}1="${1-$((5550000 + $instance_number))}" + NAME${instance_name}1="${2-Agent $instance_name Smith}" + create_identities 1 + local sidvar=SID${instance_name}1 + local didvar=DID${instance_name}1 + local namevar=NAME${instance_name}1 + eval SID$instance_name="${!sidvar}" + eval DID$instance_name="${!didvar}" + eval NAME$instance_name="${!namevar}" +} + +# Utility function: +# - create N identities in the current instance {I} +# - if variables DID{I}{1..N} and/or NAME{I}{1..N} are already set, then use +# them to set the DIDs and names of each identity +# - assert that all SIDs are unique +# - assert that all SIDs appear in keyring list +# - set variables SID{I}{1..N} to SIDs of identities, eg, SIDA1, SIDA2... +# - set variables DID{I}{1..N} to DIDs of identities, eg, DIDA1, DIDA2... +# - set variables NAME{I}{1..N} to names of identities, eg, NAMEA1, NAMEA2... +create_identities() { + local N="$1" + case "$N" in + +([0-9]));; + *) error "invalid arg1: $N";; + esac + local i j + for ((i = 1; i <= N; ++i)); do + executeOk_servald keyring add + assert [ -e "$SERVALINSTANCE_PATH/serval.keyring" ] + local sidvar=SID$instance_name$i + local didvar=DID$instance_name$i + local namevar=NAME$instance_name$i + extract_stdout_keyvalue $sidvar sid "$rexp_sid" + tfw_log "$sidvar=${!sidvar}" + # If the DID and/or NAME is already specified in the variables, then use + # them, otherwise extract the DID and NAME automatically generated by + # servald. + if [ -n "${!didvar}" -o -n "${!namevar}" ]; then + executeOk_servald set did $SID "${!didvar}" "${!namevar}" + eval "$didvar=\${!didvar}" + eval "$namevar=\${!namevar}" + else + extract_stdout_keyvalue_optional DID$instance_name$i did "$rexp_did" && tfw_log "$didvar=${!didvar}" + extract_stdout_keyvalue_optional NAME$instance_name$i name ".*" && tfw_log "$namevar=${!namevar}" + fi + done + for ((i = 1; i <= N; ++i)); do + for ((j = 1; j <= N; ++j)); do + [ $i -ne $j ] && eval assert [ "\$SID$instance_name$i" != "\$SID$instance_name$j" ] + done + done executeOk_servald keyring list - SID=$(replayStdout | sed -ne "1s/^\($rexp_sid\):.*\$/\1/p") - assert --message='main identity known' [ -n "$SID" ] - DID="${1-$((5550000 + $instance_number))}" - NAME="${2-Agent $instance_name Smith}" - executeOk_servald set did $SID "$DID" "$NAME" - tfw_log "Identity $instance_name: $SID $DID $NAME" + assertStdoutLineCount '==' $N + for ((i = 1; i <= N; ++i)); do + local sidvar=SID$instance_name$i + local didvar=DID$instance_name$i + local namevar=NAME$instance_name$i + local re_name=$(escape_grep_basic "${!namevar}") + assertStdoutGrep --matches=1 "^${!sidvar}:${!didvar}:${re_name}\$" + done } # Utility function, to be overridden as needed: -# - set up the configuration immediately prior to starting a servald server -# process +# - set up the configuration immediately prior to starting a servald server process # - called by start_servald_instances configure_servald_server() { : } # Utility function: -# - start a set of servald server processes running on a shared dummy interface -# and with its own private monitor and MDP abstract socket names +# - start a set of servald server processes running on a shared dummy interface and with its own +# private monitor and MDP abstract socket names # - set variable DUMMYNET to the full path name of shared dummy interface -# - set variables SIDx to the SID of instance x: SIDA, SIDB, etc. -# - set variables DIDx to the DID of instance x: DIDA, DIDB, etc. -# - set variables NAMEx to the names of instance x: NAMEA, NAMEB, etc. -# - set variables LOGx to the full path of server log file for instance x: LOGA, -# LOGB, etc, +# - set variables LOGx to the full path of server log file for instance x: LOGA, LOGB, etc, # - wait for all instances to detect each other # - assert that all instances are in each others' peer lists start_servald_instances() { @@ -458,47 +506,63 @@ start_servald_instances() { tfw_log "# start servald instances $*" DUMMYNET=$SERVALD_VAR/dummy >$DUMMYNET - local I J + local I for I; do set_instance $I - create_identity + # These config settings can be overridden in a caller-supplied configure_servald_server(). + # They are extremely useful for the majority of fixtures. executeOk_servald config set interfaces "+>$DUMMYNET" executeOk_servald config set monitor.socket "org.servalproject.servald.monitor.socket.$TFWUNIQUE.$instance_name" executeOk_servald config set mdp.socket "org.servalproject.servald.mdp.socket.$TFWUNIQUE.$instance_name" configure_servald_server start_servald_server - eval SID$instance_name="$SID" - eval DID$instance_name="$(shellarg "$DID")" - eval NAME$instance_name="$(shellarg "$NAME")" eval LOG$instance_name="$(shellarg "$instance_servald_log")" done # Now wait until they see each other. wait_until --sleep=0.25 instances_see_each_other "$@" tfw_log "# dummynet file:" $(ls -l $DUMMYNET) - # Assert that all instances report complete all-peer lists. + pop_instance +} + +# Assertion function: +# - asserts that all running instances report peer lists that contain all the SIDs of all the other +# running instances +# - uses the SID{I}{1..N} variables set by create_instances() +assert_all_instance_peers_complete() { + push_instance + local I J N for I; do set_instance $I executeOk_servald id allpeers - assertStdoutLineCount '==' $(($# - 1)) for J; do [ $I = $J ] && continue - local sidvar=SID${J#+} - assertStdoutGrep "${!sidvar}" + for ((N=1; 1; ++N)); do + local sidvar=SID${J#+}$N + [ -n "${!sidvar}" ] || break + assertStdoutGrep "${!sidvar}" + done done done pop_instance } +# Predicate function: +# - useful in combination with assert() and wait_until() +# - return true if all instances have logged that they have seen all other instances via the +# selfannounce mechanism instances_see_each_other() { - local I J + local I J N for I; do + local logvar=LOG${I#+} for J; do [ $I = $J ] && continue - local logvar=LOG${I#+} - local sidvar=SID${J#+} - if ! grep "ADD OVERLAY NODE sid=${!sidvar}" "${!logvar}"; then - return 1 - fi + for ((N=1; 1; ++N)); do + local sidvar=SID${J#+}$N + [ -n "${!sidvar}" ] || break + if ! grep "ADD OVERLAY NODE sid=${!sidvar}" "${!logvar}"; then + return 1 + fi + done done done return 0 diff --git a/testdefs_rhizome.sh b/testdefs_rhizome.sh index a8b36fc0..721483bb 100644 --- a/testdefs_rhizome.sh +++ b/testdefs_rhizome.sh @@ -26,52 +26,6 @@ rexp_filesize='[0-9]\{1,\}' rexp_version='[0-9]\{1,\}' rexp_date='[0-9]\{1,\}' -# Utility function: -# - create N identities in the current instance (I) -# - set variable SID to SID of first identity -# - set variable SID{I} to SID of first identity, eg, SIDA -# - set variables SID{I}{1..N} to SIDs of identities, eg, SIDA1, SIDA2... -# - set variables DID{I}{1..N} to DIDs of identities, eg, DIDA1, DIDA2... -# - set variables NAME{I}{1..N} to names of identities, eg, NAMEA1, NAMEA2... -# - assert that all SIDs are unique -# - assert that all SIDs appear in keyring list -create_rhizome_identities() { - local N="$1" - case "$N" in - +([0-9]));; - *) error "invalid arg1: $N";; - esac - local i j - for ((i = 1; i <= N; ++i)); do - executeOk_servald keyring add - local sidvar=SID$instance_name$i - local didvar=DID$instance_name$i - local namevar=NAME$instance_name$i - extract_stdout_keyvalue $sidvar sid "$rexp_sid" - tfw_log "$sidvar=${!sidvar}" - if [ $i -eq 1 ]; then - SID="${!sidvar}" - eval SID$instance_name="$SID" - fi - extract_stdout_keyvalue_optional DID$instance_name$i did "$rexp_did" && tfw_log "$didvar=${!didvar}" - extract_stdout_keyvalue_optional NAME$instance_name$i name ".*" && tfw_log "$namevar=${!namevar}" - done - for ((i = 1; i <= N; ++i)); do - for ((j = 1; j <= N; ++j)); do - [ $i -ne $j ] && eval assert [ "\$SID$instance_name$i" != "\$SID$instance_name$j" ] - done - done - executeOk_servald keyring list - assertStdoutLineCount '==' $N - for ((i = 1; i <= N; ++i)); do - local sidvar=SID$instance_name$i - local didvar=DID$instance_name$i - local namevar=NAME$instance_name$i - local re_name=$(escape_grep_basic "${!namevar}") - assertStdoutGrep --matches=1 "^${!sidvar}:${!didvar}:${re_name}\$" - done -} - assert_manifest_complete() { local manifest="$1" tfw_cat -v "$manifest" diff --git a/tests/dnahelper b/tests/dnahelper index 262f1603..433d6d1a 100755 --- a/tests/dnahelper +++ b/tests/dnahelper @@ -26,6 +26,7 @@ setup() { assert_no_servald_processes setup_dnahelper start_servald_instances +A + assert_all_instance_peers_complete +A } teardown() { @@ -221,6 +222,7 @@ setup_ExecError() { dnahelper=/non/existent assert [ ! -e "$dnahelper" ] start_servald_instances +A + assert_all_instance_peers_complete +A } test_ExecError() { executeOk_servald dna lookup 12345 @@ -246,6 +248,7 @@ done EOF chmod 0755 "$dnahelper" start_servald_instances +A + assert_all_instance_peers_complete +A } test_ExecArg1() { executeOk_servald dna lookup 12345 diff --git a/tests/rhizomeops b/tests/rhizomeops index 827d3aa6..836189be 100755 --- a/tests/rhizomeops +++ b/tests/rhizomeops @@ -27,10 +27,10 @@ shopt -s extglob setup_rhizome() { set_instance +A executeOk_servald config set debug.rhizome on - create_rhizome_identities 1 + create_identities 1 set_instance +B executeOk_servald config set debug.rhizome on - create_rhizome_identities 4 + create_identities 4 assert [ $SIDB1 != $SIDA1 ] assert [ $SIDB2 != $SIDA1 ] assert [ $SIDB3 != $SIDA1 ] diff --git a/tests/rhizomeprotocol b/tests/rhizomeprotocol index 6a37c334..5d620f76 100755 --- a/tests/rhizomeprotocol +++ b/tests/rhizomeprotocol @@ -32,9 +32,9 @@ teardown() { setup_rhizome() { set_instance +A - create_rhizome_identities 1 + create_identities 1 set_instance +B - create_rhizome_identities 1 + create_identities 1 } # Called by start_servald_instances for each instance.