test_anastasis_reducer_select_country.sh (3454B)
1 #!/bin/bash 2 # This file is in the public domain. 3 4 set -eu 5 6 # Exit, with status code "skip" (no 'real' failure) 7 function exit_skip() { 8 echo " SKIP: $1" 9 exit 77 10 } 11 12 # Exit, with error message (hard failure) 13 function exit_fail() { 14 echo " FAIL: $1" 15 exit 1 16 } 17 18 # Cleanup to run whenever we exit 19 function cleanup() 20 { 21 for n in `jobs -p` 22 do 23 kill $n 2> /dev/null || true 24 done 25 rm -f $TFILE 26 wait 27 } 28 29 30 31 TFILE=`mktemp test_reducer_stateXXXXXX` 32 33 # Install cleanup handler (except for kill -9) 34 trap cleanup EXIT 35 36 # Check we can actually run 37 echo -n "Testing for jq" 38 jq -h > /dev/null || exit_skip "jq required" 39 echo " FOUND" 40 41 echo -n "Testing for anastasis-reducer ..." 42 anastasis-reducer -h > /dev/null || exit_skip "anastasis-reducer required" 43 echo " FOUND" 44 45 46 47 # Test continent re-selection 48 echo -n "Test continent re-selection ..." 49 anastasis-reducer -a '{"continent": "Europe"}' select_continent resources/01-recovery.json $TFILE 50 51 echo -n "." 52 53 54 STATE=`jq -r -e .recovery_state < $TFILE` 55 if test "$STATE" != "COUNTRY_SELECTING" 56 then 57 exit_fail "Expected new state to be COUNTRY_SELECTING, got $STATE" 58 fi 59 60 echo -n "." 61 62 jq -e .countries[0] < $TFILE > /dev/null || exit_fail "Expected new state to include countries" 63 jq -e .countries[0].code < $TFILE > /dev/null || exit_fail "Expected new state to include countries with code" 64 jq -e .countries[0].continent < $TFILE > /dev/null || exit_fail "Expected new state to include countries with continent" 65 jq -e .countries[0].name < $TFILE > /dev/null || exit_fail "Expected new state to include countries with name" 66 67 SELECTED_CONTINENT=`jq -r -e .selected_continent < $TFILE` 68 if test "$SELECTED_CONTINENT" != "Europe" 69 then 70 exit_fail "Expected selected continent to be 'Europe', got $SELECTED_CONTINENT" 71 fi 72 73 echo " OK" 74 75 76 echo -n "Test invalid continent re-selection ..." 77 anastasis-reducer -a '{"continent": "Pangaia"}' select_continent resources/00-recovery.json $TFILE 2> /dev/null \ 78 && exit_fail "Expected selection to fail. Check '$TFILE'" 79 80 echo " OK" 81 82 83 echo -n "Test NX country selection ..." 84 85 anastasis-reducer -a \ 86 '{"country_code": "zz"}' \ 87 select_country \ 88 resources/01-backup.json $TFILE 2> /dev/null \ 89 && exit_fail "Expected selection to fail. Check '$TFILE'" 90 91 echo " OK" 92 93 echo -n "Test invalid country selection for continent ..." 94 95 anastasis-reducer -a \ 96 '{"country_code": "de"}' \ 97 select_country \ 98 resources/01-backup.json $TFILE 2> /dev/null \ 99 && exit_fail "Expected selection to fail. Check '$TFILE'" 100 101 echo " OK" 102 103 echo -n "Test country selection ..." 104 105 anastasis-reducer -a \ 106 '{"country_code": "xx"}' \ 107 select_country resources/01-backup.json $TFILE 108 109 STATE=`jq -r -e .backup_state < $TFILE` 110 if test "$STATE" != "USER_ATTRIBUTES_COLLECTING" 111 then 112 exit_fail "Expected new state to be 'USER_ATTRIBUTES_COLLECTING', got '$STATE'" 113 fi 114 echo -n "." 115 SELECTED_COUNTRY=`jq -r -e .selected_country < $TFILE` 116 if test "$SELECTED_COUNTRY" != "xx" 117 then 118 exit_fail "Expected selected country to be 'xx', got '$SELECTED_COUNTRY'" 119 fi 120 echo -n "." 121 REQ_ATTRIBUTES=`jq -r -e .required_attributes < $TFILE` 122 if test "$REQ_ATTRIBUTES" == NULL 123 then 124 exit_fail "Expected required attributes array not to be NULL" 125 fi 126 echo -n "." 127 AUTH_PROVIDERS=`jq -r -e .authentication_providers < $TFILE` 128 if test "$AUTH_PROVIDERS" == NULL 129 then 130 exit_fail "Expected authentication_providers array not to be NULL" 131 fi 132 133 echo " OK" 134 135 exit 0