github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/contrib/cirrus/lib.sh.t (about) 1 #!/bin/bash 2 # 3 # Unit tests for some functions in lib.sh 4 # 5 source $(dirname $0)/lib.sh 6 7 # Iterator and return code; updated in test functions 8 testnum=0 9 rc=0 10 11 function check_result { 12 testnum=$(expr $testnum + 1) 13 MSG=$(echo "$1" | tr -d '*>\012'|sed -e 's/^ \+//') 14 if [ "$MSG" = "$2" ]; then 15 echo "ok $testnum $(echo $3) = $(echo $MSG)" 16 else 17 echo "not ok $testnum $3" 18 echo "# expected: $2" 19 echo "# actual: $MSG" 20 rc=1 21 fi 22 } 23 24 ############################################################################### 25 # tests for die() 26 27 function test_die() { 28 local input_status=$1 29 local input_msg=$2 30 local expected_status=$3 31 local expected_msg=$4 32 33 local msg 34 msg=$(die $input_status "$input_msg") 35 local status=$? 36 37 check_result "$msg" "$expected_msg" "die $input_status $input_msg" 38 } 39 40 test_die 1 "a message" 1 "a message" 41 test_die 2 "" 2 "FATAL ERROR (but no message given!) in test_die()" 42 test_die '' '' 1 "FATAL ERROR (but no message given!) in test_die()" 43 44 ############################################################################### 45 # tests for req_env_var() 46 47 function test_rev() { 48 local input_args=$1 49 local expected_status=$2 50 local expected_msg=$3 51 52 # bash gotcha: doing 'local msg=...' on one line loses exit status 53 local msg 54 msg=$(req_env_var $input_args) 55 local status=$? 56 57 check_result "$msg" "$expected_msg" "req_env_var $input_args" 58 check_result "$status" "$expected_status" "req_env_var $input_args (rc)" 59 } 60 61 # error if called with no args 62 test_rev '' 1 'FATAL: req_env_var: invoked without arguments' 63 64 # error if desired envariable is unset 65 unset FOO BAR 66 test_rev FOO 9 'FATAL: test_rev() requires $FOO to be non-empty' 67 test_rev BAR 9 'FATAL: test_rev() requires $BAR to be non-empty' 68 # OK if desired envariable was unset 69 FOO=1 70 test_rev FOO 0 '' 71 72 # OK if multiple vars are non-empty 73 FOO="stuff" 74 BAR="things" 75 ENV_VARS="FOO BAR" 76 test_rev "$ENV_VARS" 0 '' 77 unset BAR 78 79 # ...but error if any single desired one is unset 80 test_rev "FOO BAR" 9 'FATAL: test_rev() requires $BAR to be non-empty' 81 82 # ...and OK if all args are set 83 BAR=1 84 test_rev "FOO BAR" 0 '' 85 86 ############################################################################### 87 # tests for test_okay() 88 89 function test_item_test { 90 local exp_msg=$1 91 local exp_ret=$2 92 local item=$3 93 shift 3 94 local test_args="$@" 95 local msg 96 msg=$(item_test "$item" "$@") 97 local status=$? 98 99 check_result "$msg" "$exp_msg" "test_item $item $test_args" 100 check_result "$status" "$exp_ret" "test_item $item $test_args (actual rc $status)" 101 } 102 103 # negative tests 104 test_item_test "FATAL: item_test() requires \$ITEM to be non-empty" 9 "" "" 105 test_item_test "FATAL: item_test() requires \$TEST_ARGS to be non-empty" 9 "foo" "" 106 test_item_test "not ok foo: -gt 5 ~= bar: too many arguments" 2 "foo" "-gt" "5" "~=" "bar" 107 test_item_test "not ok bar: a -ge 10: a: integer expression expected" 2 "bar" "a" "-ge" "10" 108 test_item_test "not ok basic logic: 0 -ne 0" 1 "basic logic" "0" "-ne" "0" 109 110 # positive tests 111 test_item_test "ok snafu" 0 "snafu" "foo" "!=" "bar" 112 test_item_test "ok foobar" 0 "foobar" "one two three" "=" "one two three" 113 test_item_test "ok oh boy" 0 "oh boy" "line 1 114 line2" "!=" "line 1 115 116 line2" 117 test_item_test "ok okay enough" 0 "okay enough" "line 1 118 line2" "=" "line 1 119 line2" 120 121 ############################################################################### 122 # tests for is_release() 123 124 # N/B: Assuming tests run in their own process, so wiping out the local 125 # CIRRUS_BASE_SHA CIRRUS_CHANGE_IN_REPO and CIRRUS_TAG will be okay. 126 function test_is_release() { 127 CIRRUS_BASE_SHA="$1" 128 CIRRUS_CHANGE_IN_REPO="$2" 129 CIRRUS_TAG="$3" 130 local exp_status=$4 131 local exp_msg=$5 132 local msg 133 msg=$(is_release) 134 local status=$? 135 136 check_result "$msg" "$exp_msg" "is_release(CIRRUS_BASE_SHA='$1' CIRRUS_CHANGE_IN_REPO='$2' CIRRUS_TAG='$3')" 137 check_result "$status" "$exp_status" "is_release(...) returned $status" 138 } 139 140 # FROM TO TAG RET MSG 141 test_is_release "" "" "" "9" "FATAL: is_release() requires \$CIRRUS_CHANGE_IN_REPO to be non-empty" 142 test_is_release "x" "" "" "9" "FATAL: is_release() requires \$CIRRUS_CHANGE_IN_REPO to be non-empty" 143 144 # post-merge / tag-push testing, FROM will be set 'unknown' by (lib.sh default) 145 test_is_release "unknown" "x" "" "1" "" 146 # post-merge / tag-push testing, oddball tag is set, FROM will be set 'unknown' 147 test_is_release "unknown" "unknown" "test-tag" "2" "Found \$RELVER test-tag" 148 # post-merge / tag-push testing, sane tag is set, FROM will be set 'unknown' 149 test_is_release "unknown" "unknown" "0.0.0" "0" "Found \$RELVER 0.0.0" 150 # hack/get_ci_vm or PR testing, FROM and TO are set, no tag is set 151 test_is_release "x" "x" "" "1" "" 152 153 # Negative-testing git with this function is very difficult, assume git works 154 # test_is_release ... "is_release() failed to fetch tags" 155 # test_is_release ... "is_release() failed to parse tags" 156 157 BF_V1=$(git rev-parse v1.0.0^) 158 AT_V1=$(git rev-parse v1.0.0) 159 test_is_release "$BF_V1" "$BF_V1" "v9.8.7-dev" "2" "Found \$RELVER v9.8.7-dev" 160 test_is_release "$BF_V1" "$AT_V1" "v9.8.7-dev" "2" "Found \$RELVER v9.8.7-dev" 161 test_is_release "$BF_V1" "$AT_V1" "" "0" "Found \$RELVER v1.0.0" 162 163 exit $rc