test_anastasis_reducer_select_continent.sh (3942B)
1 #!/bin/bash 2 3 set -eu 4 5 # Exit, with status code "skip" (no 'real' failure) 6 function exit_skip() { 7 echo " SKIP: $1" 8 exit 77 9 } 10 11 # Exit, with error message (hard failure) 12 function exit_fail() { 13 echo " FAIL: $1" 14 exit 1 15 } 16 17 # Cleanup to run whenever we exit 18 function cleanup() 19 { 20 for n in `jobs -p` 21 do 22 kill $n 2> /dev/null || true 23 done 24 rm -f $SFILE $TFILE 25 wait 26 } 27 28 # Install cleanup handler (except for kill -9) 29 SFILE=`mktemp test_reducer_stateXXXXXX` 30 TFILE=`mktemp test_reducer_stateXXXXXX` 31 32 trap cleanup EXIT 33 34 # Check we can actually run 35 echo -n "Testing for jq ..." 36 jq -h > /dev/null || exit_skip "jq required" 37 echo " FOUND" 38 echo -n "Testing for anastasis-reducer ..." 39 anastasis-reducer -h > /dev/null || exit_skip "anastasis-reducer required" 40 echo " FOUND" 41 42 # Test continent selection in a backup state 43 echo -n "Test continent selection in a backup state ..." 44 anastasis-reducer -a '{"continent": "Demoworld"}' select_continent resources/00-backup.json $TFILE 45 46 STATE=`jq -r -e .backup_state < $TFILE` 47 if test "$STATE" != "COUNTRY_SELECTING" 48 then 49 exit_fail "Expected new state to be COUNTRY_SELECTING, got $STATE" 50 fi 51 SELECTED_CONTINENT=`jq -r -e .selected_continent < $TFILE` 52 if test "$SELECTED_CONTINENT" != "Demoworld" 53 then 54 exit_fail "Expected selected continent to be Demoworld, got $SELECTED_CONTINENT" 55 fi 56 COUNTRIES=`jq -r -e .countries < $TFILE` 57 if test "$COUNTRIES" == NULL 58 then 59 exit_fail "Expected country array (countries) not to be NULL" 60 fi 61 echo " OK" 62 63 64 echo -n "Test invalid continent selection ..." 65 anastasis-reducer -a '{"continent": "Pangaia"}' select_continent resources/00-recovery.json $TFILE 2> /dev/null \ 66 && exit_fail "Expected selection to fail. Check '$TFILE'" 67 68 echo " OK" 69 70 echo -n "Test continent selection in a recovery state ..." 71 anastasis-reducer -a '{"continent": "Demoworld"}' select_continent resources/00-recovery.json $TFILE 72 73 STATE=`jq -r -e .recovery_state < $TFILE` 74 if test "$STATE" != "COUNTRY_SELECTING" 75 then 76 exit_fail "Expected new state to be COUNTRY_SELECTING, got $STATE" 77 fi 78 jq -e .countries[0] < $TFILE > /dev/null || exit_fail "Expected new state to include countries" 79 jq -e .countries[0].code < $TFILE > /dev/null || exit_fail "Expected new state to include countries with code" 80 jq -e .countries[0].continent < $TFILE > /dev/null || exit_fail "Expected new state to include countries with continent" 81 jq -e .countries[0].name < $TFILE > /dev/null || exit_fail "Expected new state to include countries with name" 82 83 SELECTED_CONTINENT=`jq -r -e .selected_continent < $TFILE` 84 if test "$SELECTED_CONTINENT" != "Demoworld" 85 then 86 exit_fail "Expected selected continent to be 'Demoworld', got $SELECTED_CONTINENT" 87 fi 88 89 COUNTRIES=`jq -r -e .countries < $TFILE` 90 if test "$COUNTRIES" == NULL 91 then 92 exit_fail "Expected country array (countries) not to be NULL" 93 fi 94 jq -e .countries[0] < $TFILE > /dev/null || exit_fail "Expected new state to include countries" 95 jq -e .countries[0].code < $TFILE > /dev/null || exit_fail "Expected new state to include countries with code" 96 jq -e .countries[0].continent < $TFILE > /dev/null || exit_fail "Expected new state to include countries with continent" 97 jq -e .countries[0].name < $TFILE > /dev/null || exit_fail "Expected new state to include countries with name" 98 99 echo " OK" 100 101 102 # Test missing arguments in a recovery state 103 echo -n "Test bogus country selection in a recovery state ..." 104 anastasis-reducer -a '{"country": "Germany"}' select_continent resources/00-recovery.json $TFILE 2> /dev/null && exit_fail "Expected state transition to fail, but it worked, check $TFILE" 105 106 echo " OK" 107 108 # Test continent selection in a recovery state 109 echo -n "Test bogus continent selection in a recovery state ..." 110 anastasis-reducer -a '{"continent": "Germany"}' select_continent resources/00-recovery.json $TFILE 2> /dev/null && exit_fail "Expected state transition to fail, but it worked, check $TFILE" 111 112 echo " OK" 113 114 exit 0