run.sh (8287B)
1 #!/bin/sh 2 3 # Copyright (c) 2024, Natacha Porté 4 # 5 # Permission to use, copy, modify, and distribute this software for any 6 # purpose with or without fee is hereby granted, provided that the above 7 # copyright notice and this permission notice appear in all copies. 8 # 9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 17 set -Cue 18 19 if test "$#" -lt 1; then 20 echo "Usage: $0 [start-cmd]" >&2 21 exit 1 22 fi 23 24 : "${TEST_DIR:=$(dirname "$0")}" 25 : "${TMP_DIR:=/tmp}" 26 27 TO_CLEAN="test-default.json test-one.json test-two.json" 28 29 trap 'rm -f ${TO_CLEAN}' EXIT 30 31 if test -z "${TEST_DB-}"; then 32 TEST_DB="$(mktemp "${TMP_DIR}/pref-matrix-test.XXXXXXXX")" 33 TO_CLEAN="${TO_CLEAN} ${TEST_DB} ${TEST_DB}-shm ${TEST_DB}-wal" 34 fi 35 36 if test -z "${TEST_TRACE-}"; then 37 TEST_TRACE="$(mktemp "${TMP_DIR}/pref-matrix-test.XXXXXXXX")" 38 TO_CLEAN="${TO_CLEAN} ${TEST_TRACE}" 39 fi 40 41 do_post(){ 42 URI_PATH="$1" 43 EXPECTED_CODE="$2" 44 shift 2 45 echo "; POST ${URI_PATH} <- $*" >>"${TEST_TRACE}" 46 47 RESULT=$(curl -s "$@" "http://localhost:9090${URI_PATH}" \ 48 | tee -a "${TEST_TRACE}" \ 49 | sed -n -e '/^; POST/{;s/^.*//;x;}' \ 50 -e 's/.*title>\([0-9][0-9][0-9]\) - .*/\1/p') 51 52 if ! test "${EXPECTED_CODE}" = "${RESULT}"; then 53 echo "POST ${URI_PATH} <- $*" 54 echo " returned ${RESULT}, expected ${EXPECTED_CODE}" 55 false 56 fi 57 } 58 59 check_text(){ 60 diff -u "${TEST_DIR}/$1" "$2" 61 } 62 63 64 ################### 65 ## Test 1: replay 66 67 echo -n "" >|"${TEST_TRACE}" 68 "$@" :memory: "${TEST_TRACE}" "${TEST_DIR}/test-1.scm" 69 sed '/; 2[0-9][0-9][0-9]-/d;$s/$/\n(generate-json)\n(exit)/' "${TEST_TRACE}" \ 70 | check_text test-1.scm - 71 check_text test-1.json test-default.json 72 73 #################################### 74 ## Test 2: HTTP with default topic 75 76 rm -f "${TEST_DB}" "${TEST_DB}-shm" "${TEST_DB}-wal" 77 "$@" "${TEST_DB}" "${TEST_TRACE}" "${TEST_DIR}/test-2.scm" & 78 SRV_PID=$! 79 80 trap 'rm -f ${TO_CLEAN}; kill ${SRV_PID}' EXIT 81 82 sleep 1 83 84 do_post '/new-subject' 200 -d 'name=foo' 85 check_text test-2-01.json test-default.json 86 do_post '/new-object' 200 -d 'name=01' 87 check_text test-2-02.json test-default.json 88 do_post '/new-object' 200 -d 'name=03' 89 check_text test-2-03.json test-default.json 90 do_post '/new-subject' 200 -d 'name=bar' 91 check_text test-2-04.json test-default.json 92 do_post '/bin/set-pref' 200 -d 'sub=bar' -d '01=3' -d '04=4' 93 check_text test-2-05.json test-default.json 94 do_post '/do/new-object' 200 -d 'name=02' 95 check_text test-2-06.json test-default.json 96 do_post '/new-subject' 409 -d 'name=bar' 97 check_text test-2-06.json test-default.json 98 do_post '/new-object' 200 -d 'name=04' 99 check_text test-2-07.json test-default.json 100 do_post '/set-pref' 200 -d 'sub=meow' -d '04=2' -d '01=4' 101 check_text test-2-07.json test-default.json 102 do_post '/new-subject' 200 -d 'name=meow' 103 check_text test-2-08.json test-default.json 104 do_post '/set-pref' 200 -d 'sub=foo' -d '01=1' -d '04=2' -d '01=4' 105 check_text test-2-09.json test-default.json 106 do_post '/set-pref' 200 -d 'sub=bar' -d '01=0' 107 check_text test-2-10.json test-default.json 108 do_post '/new-object' 409 -d 'name=04' 109 check_text test-2-10.json test-default.json 110 111 kill "${SRV_PID}" 112 trap 'rm -f ${TO_CLEAN}' EXIT 113 114 sqlite3 "${TEST_DB}" .dump | check_text test-2-dump.sql - 115 116 ############################################## 117 ## Test 3: database migration from schema v1 118 119 cp -f "${TEST_DIR}/test-2-v1.sqlite" "${TEST_DB}" 120 rm -f "${TEST_DB}-shm" "${TEST_DB}-wal" 121 "$@" "${TEST_DB}" "${TEST_TRACE}" "${TEST_DIR}/test-3.scm" 122 sqlite3 "${TEST_DB}" .dump | check_text test-2-dump.sql - 123 124 ##################################### 125 ## Test 4: HTTP with several topics 126 127 rm -f test-default.json test-one.json test-two.json 128 "$@" :memory: "${TEST_TRACE}" "${TEST_DIR}/test-4.scm" & 129 SRV_PID=$! 130 131 trap 'rm -f ${TO_CLEAN}; kill ${SRV_PID}' EXIT 132 133 sleep 1 134 135 do_post '/new-topic' 200 -d 'name=one' 136 check_text test-4-1a.json test-one.json 137 ! test -e test-two.json 138 do_post '/new-topic' 200 -d 'name=two' 139 check_text test-4-1a.json test-one.json 140 check_text test-4-1b.json test-two.json 141 do_post '/new-topic' 409 -d 'name=two' 142 check_text test-4-1a.json test-one.json 143 check_text test-4-1b.json test-two.json 144 do_post '/new-subject' 409 -d 'name=foo' 145 check_text test-4-1a.json test-one.json 146 check_text test-4-1b.json test-two.json 147 ! test -e test-default.json 148 do_post '/new-subject' 200 -d 'topic=one' -d 'name=foo' 149 check_text test-4-2a.json test-one.json 150 check_text test-4-1b.json test-two.json 151 do_post '/new-subject' 200 -d 'topic=two' -d 'name=foo' 152 check_text test-4-2a.json test-one.json 153 check_text test-4-2b.json test-two.json 154 do_post '/new-subject' 409 -d 'topic=one' -d 'name=foo' 155 check_text test-4-2a.json test-one.json 156 check_text test-4-2b.json test-two.json 157 do_post '/new-subject' 409 -d 'topic=other' -d 'name=bar' 158 check_text test-4-2a.json test-one.json 159 check_text test-4-2b.json test-two.json 160 ! test -e test-other.json 161 do_post '/new-object' 200 -d 'topic=one' -d 'name=some' 162 check_text test-4-3a.json test-one.json 163 check_text test-4-2b.json test-two.json 164 do_post '/new-object' 200 -d 'topic=one' -d 'name=common' 165 check_text test-4-4a.json test-one.json 166 check_text test-4-2b.json test-two.json 167 do_post '/new-object' 200 -d 'topic=two' -d 'name=common' 168 check_text test-4-4a.json test-one.json 169 check_text test-4-3b.json test-two.json 170 do_post '/new-object' 200 -d 'topic=two' -d 'name=thing' 171 check_text test-4-4a.json test-one.json 172 check_text test-4-4b.json test-two.json 173 do_post '/new-subject' 200 -d 'topic=one' -d 'name=bar' 174 check_text test-4-5a.json test-one.json 175 check_text test-4-4b.json test-two.json 176 do_post '/new-subject' 200 -d 'topic=two' -d 'name=baz' 177 check_text test-4-5a.json test-one.json 178 check_text test-4-5b.json test-two.json 179 do_post '/set-pref' 200 -d 'topic=one' -d 'sub=foo' -d 'some=4' -d 'common=3' 180 check_text test-4-6a.json test-one.json 181 check_text test-4-5b.json test-two.json 182 do_post '/set-pref' 200 -d 'topic=two' -d 'sub=foo' -d 'thing=2' -d 'common=1' 183 check_text test-4-6a.json test-one.json 184 check_text test-4-6b.json test-two.json 185 do_post '/set-pref' 200 -d 'topic=one' -d 'sub=baz' -d 'some=4' -d 'common=3' 186 check_text test-4-6a.json test-one.json 187 check_text test-4-6b.json test-two.json 188 do_post '/set-pref' 200 -d 'topic=two' -d 'sub=bar' -d 'thing=2' -d 'common=1' 189 check_text test-4-6a.json test-one.json 190 check_text test-4-6b.json test-two.json 191 do_post '/set-pref' 200 -d 'topic=two' -d 'sub=baz' -d 'some=4' -d 'common=5' 192 check_text test-4-6a.json test-one.json 193 check_text test-4-7b.json test-two.json 194 do_post '/set-pref' 200 -d 'topic=one' -d 'sub=bar' -d 'thing=2' -d 'common=5' 195 check_text test-4-7a.json test-one.json 196 check_text test-4-7b.json test-two.json 197 do_post '/hide-subject' 200 -d 'topic=one' -d 'name=foo' 198 check_text test-4-8a.json test-one.json 199 check_text test-4-7b.json test-two.json 200 do_post '/hide-object' 200 -d 'topic=two' -d 'name=common' 201 check_text test-4-8a.json test-one.json 202 check_text test-4-8b.json test-two.json 203 do_post '/hide-object' 200 -d 'topic=two' -d 'name=non-existent' 204 check_text test-4-8a.json test-one.json 205 check_text test-4-8b.json test-two.json 206 do_post '/new-object' 200 -d 'topic=two' -d 'name=common' 207 check_text test-4-8a.json test-one.json 208 check_text test-4-7b.json test-two.json 209 do_post '/new-subject' 200 -d 'topic=one' -d 'name=foo' 210 check_text test-4-7a.json test-one.json 211 check_text test-4-7b.json test-two.json 212 do_post '/close-topic' 200 -d 'name=one' 213 check_text test-4-7a.json test-one.json 214 check_text test-4-7b.json test-two.json 215 do_post '/set-pref' 200 -d 'topic=two' -d 'sub=foo' -d 'common=5' 216 check_text test-4-7a.json test-one.json 217 check_text test-4-9b.json test-two.json 218 do_post '/set-pref' 200 -d 'topic=one' -d 'sub=foo' -d 'common=5' 219 check_text test-4-7a.json test-one.json 220 check_text test-4-9b.json test-two.json 221 do_post '/new-subject' 409 -d 'topic=one' -d 'name=extra' 222 check_text test-4-7a.json test-one.json 223 check_text test-4-9b.json test-two.json 224 225 kill "${SRV_PID}" 226 trap 'rm -f ${TO_CLEAN}' EXIT