|
| 1 | +#!/usr/bin/env bash |
| 2 | +# run-fixtures.sh — Run OLR in batch mode against redo log fixtures |
| 3 | +# and compare output against golden files. |
| 4 | +# |
| 5 | +# Usage: ./run-fixtures.sh [fixture-name ...] |
| 6 | +# No arguments: auto-discover all fixtures from fixtures/ and sql/generated/ |
| 7 | +# With arguments: run only the named fixtures |
| 8 | +# |
| 9 | +# Environment: |
| 10 | +# OLR_BINARY — path to OLR binary (default: auto-detect from cmake build dir) |
| 11 | +# TESTS_DIR — path to tests/ directory (default: directory containing this script) |
| 12 | + |
| 13 | +set -euo pipefail |
| 14 | + |
| 15 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 16 | +TESTS_DIR="${TESTS_DIR:-$SCRIPT_DIR}" |
| 17 | + |
| 18 | +# Auto-detect OLR binary |
| 19 | +if [[ -z "${OLR_BINARY:-}" ]]; then |
| 20 | + for candidate in \ |
| 21 | + /opt/OpenLogReplicator/OpenLogReplicator \ |
| 22 | + /opt/OpenLogReplicator-local/cmake-build-Debug-x86_64/OpenLogReplicator \ |
| 23 | + "$TESTS_DIR/../cmake-build-Debug-x86_64/OpenLogReplicator" \ |
| 24 | + "$TESTS_DIR/../build/OpenLogReplicator"; do |
| 25 | + if [[ -x "$candidate" ]]; then |
| 26 | + OLR_BINARY="$candidate" |
| 27 | + break |
| 28 | + fi |
| 29 | + done |
| 30 | +fi |
| 31 | +if [[ -z "${OLR_BINARY:-}" ]]; then |
| 32 | + echo "ERROR: OLR binary not found. Set OLR_BINARY env var." >&2 |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | + |
| 36 | +# --- Discover fixtures --- |
| 37 | +discover_fixtures() { |
| 38 | + local base_dir="$1" # e.g., fixtures or sql/generated |
| 39 | + local dir="$TESTS_DIR/$base_dir/expected" |
| 40 | + [[ -d "$dir" ]] || return 0 |
| 41 | + for scenario_dir in "$dir"/*/; do |
| 42 | + [[ -f "${scenario_dir}output.json" ]] || continue |
| 43 | + echo "$base_dir/$(basename "$scenario_dir")" |
| 44 | + done |
| 45 | +} |
| 46 | + |
| 47 | +if [[ $# -gt 0 ]]; then |
| 48 | + FIXTURES=("$@") |
| 49 | +else |
| 50 | + FIXTURES=() |
| 51 | + while IFS= read -r f; do |
| 52 | + FIXTURES+=("$f") |
| 53 | + done < <({ discover_fixtures "fixtures"; discover_fixtures "sql/generated"; } | sort) |
| 54 | +fi |
| 55 | + |
| 56 | +if [[ ${#FIXTURES[@]} -eq 0 ]]; then |
| 57 | + echo "No fixtures found." |
| 58 | + exit 0 |
| 59 | +fi |
| 60 | + |
| 61 | +# --- Detect archive format from redo filenames --- |
| 62 | +detect_archive_format() { |
| 63 | + local redo_dir="$1" |
| 64 | + local sample |
| 65 | + sample=$(ls "$redo_dir"/* 2>/dev/null | head -1) |
| 66 | + [[ -n "$sample" ]] || { echo "%t_%s_%r.dbf"; return; } |
| 67 | + local fname |
| 68 | + fname=$(basename "$sample") |
| 69 | + local stem="${fname%.*}" |
| 70 | + local ext="${fname##*.}" |
| 71 | + # Expected: [prefix]<thread>_<seq>_<resetlogs>.<ext> |
| 72 | + local resetlogs="${stem##*_}" |
| 73 | + local rest="${stem%_*}" |
| 74 | + local seq="${rest##*_}" |
| 75 | + local prefix_thread="${rest%_*}" |
| 76 | + # prefix_thread is e.g. "arch1" — split into prefix + thread |
| 77 | + local thread="${prefix_thread##*[!0-9]}" |
| 78 | + local prefix="${prefix_thread%$thread}" |
| 79 | + echo "${prefix}%t_%s_%r.${ext}" |
| 80 | +} |
| 81 | + |
| 82 | +# --- Find schema checkpoint and extract start SCN --- |
| 83 | +find_schema() { |
| 84 | + local schema_dir="$1" |
| 85 | + [[ -d "$schema_dir" ]] || return 1 |
| 86 | + local best_file="" best_scn=999999999999 |
| 87 | + for f in "$schema_dir"/TEST-chkpt-*.json; do |
| 88 | + [[ -f "$f" ]] || continue |
| 89 | + local fname |
| 90 | + fname=$(basename "$f") |
| 91 | + # Extract SCN from TEST-chkpt-<scn>.json |
| 92 | + local scn="${fname#TEST-chkpt-}" |
| 93 | + scn="${scn%.json}" |
| 94 | + if [[ "$scn" -lt "$best_scn" ]] 2>/dev/null; then |
| 95 | + best_scn="$scn" |
| 96 | + best_file="$f" |
| 97 | + fi |
| 98 | + done |
| 99 | + [[ -n "$best_file" ]] || return 1 |
| 100 | + echo "$best_scn $best_file" |
| 101 | +} |
| 102 | + |
| 103 | +# --- Run one fixture --- |
| 104 | +run_fixture() { |
| 105 | + local fixture="$1" |
| 106 | + local scenario="${fixture##*/}" |
| 107 | + local dir_prefix="${fixture%/*}" |
| 108 | + |
| 109 | + case "$dir_prefix" in |
| 110 | + fixtures|sql/generated) ;; |
| 111 | + *) echo "FAIL $fixture (unknown prefix: $dir_prefix)"; return 1 ;; |
| 112 | + esac |
| 113 | + |
| 114 | + local redo_dir="$TESTS_DIR/$dir_prefix/redo/$scenario" |
| 115 | + local schema_dir="$TESTS_DIR/$dir_prefix/schema/$scenario" |
| 116 | + local expected="$TESTS_DIR/$dir_prefix/expected/$scenario/output.json" |
| 117 | + |
| 118 | + if [[ ! -d "$redo_dir" ]]; then |
| 119 | + echo "FAIL $fixture (redo logs missing: $redo_dir)" |
| 120 | + return 1 |
| 121 | + fi |
| 122 | + |
| 123 | + # Temp dir for this test |
| 124 | + local tmp_dir |
| 125 | + tmp_dir=$(mktemp -d) |
| 126 | + trap "rm -rf '$tmp_dir'" RETURN |
| 127 | + |
| 128 | + # Collect redo log files |
| 129 | + local redo_json="[" |
| 130 | + local first=1 |
| 131 | + for f in "$redo_dir"/*; do |
| 132 | + [[ -f "$f" ]] || continue |
| 133 | + [[ $first -eq 1 ]] && first=0 || redo_json+=", " |
| 134 | + redo_json+="\"$f\"" |
| 135 | + done |
| 136 | + redo_json+="]" |
| 137 | + |
| 138 | + local archive_format |
| 139 | + archive_format=$(detect_archive_format "$redo_dir") |
| 140 | + |
| 141 | + # Schema detection |
| 142 | + local reader_extra="" flags_line="" filter_section="" |
| 143 | + local schema_info |
| 144 | + if schema_info=$(find_schema "$schema_dir"); then |
| 145 | + local start_scn="${schema_info%% *}" |
| 146 | + local schema_file="${schema_info#* }" |
| 147 | + cp "$schema_file" "$tmp_dir/" |
| 148 | + reader_extra=$(printf ',\n "log-archive-format": "%s",\n "start-scn": %s' "$archive_format" "$start_scn") |
| 149 | + filter_section=', |
| 150 | + "filter": { |
| 151 | + "table": [ |
| 152 | + {"owner": "OLR_TEST", "table": ".*"} |
| 153 | + ] |
| 154 | + }' |
| 155 | + else |
| 156 | + reader_extra=$(printf ',\n "log-archive-format": ""') |
| 157 | + flags_line=', |
| 158 | + "flags": 2' |
| 159 | + fi |
| 160 | + |
| 161 | + local output="$tmp_dir/output.json" |
| 162 | + |
| 163 | + cat > "$tmp_dir/config.json" <<EOF |
| 164 | +{ |
| 165 | + "version": "1.9.0", |
| 166 | + "log-level": 3, |
| 167 | + "memory": { |
| 168 | + "min-mb": 32, |
| 169 | + "max-mb": 256 |
| 170 | + }, |
| 171 | + "state": { |
| 172 | + "type": "disk", |
| 173 | + "path": "$tmp_dir" |
| 174 | + }, |
| 175 | + "source": [ |
| 176 | + { |
| 177 | + "alias": "S1", |
| 178 | + "name": "TEST", |
| 179 | + "reader": { |
| 180 | + "type": "batch", |
| 181 | + "redo-log": $redo_json$reader_extra |
| 182 | + }, |
| 183 | + "format": { |
| 184 | + "type": "json", |
| 185 | + "scn": 1, |
| 186 | + "timestamp": 7, |
| 187 | + "timestamp-metadata": 7, |
| 188 | + "xid": 1 |
| 189 | + }$flags_line$filter_section |
| 190 | + } |
| 191 | + ], |
| 192 | + "target": [ |
| 193 | + { |
| 194 | + "alias": "T1", |
| 195 | + "source": "S1", |
| 196 | + "writer": { |
| 197 | + "type": "file", |
| 198 | + "output": "$output", |
| 199 | + "new-line": 1, |
| 200 | + "append": 1 |
| 201 | + } |
| 202 | + } |
| 203 | + ] |
| 204 | +} |
| 205 | +EOF |
| 206 | + |
| 207 | + # Run OLR |
| 208 | + local olr_log="$tmp_dir/olr.log" |
| 209 | + if ! "$OLR_BINARY" -r -f "$tmp_dir/config.json" > "$olr_log" 2>&1; then |
| 210 | + echo "FAIL $fixture (OLR exited with error)" |
| 211 | + cat "$olr_log" >&2 |
| 212 | + return 1 |
| 213 | + fi |
| 214 | + |
| 215 | + if [[ ! -f "$output" ]]; then |
| 216 | + echo "FAIL $fixture (no output file)" |
| 217 | + cat "$olr_log" >&2 |
| 218 | + return 1 |
| 219 | + fi |
| 220 | + |
| 221 | + # Compare against golden file |
| 222 | + local diff_out |
| 223 | + if diff_out=$(diff --unified "$expected" "$output"); then |
| 224 | + echo "PASS $fixture" |
| 225 | + return 0 |
| 226 | + else |
| 227 | + echo "FAIL $fixture (output differs)" |
| 228 | + echo "$diff_out" >&2 |
| 229 | + return 1 |
| 230 | + fi |
| 231 | +} |
| 232 | + |
| 233 | +# --- Main --- |
| 234 | +passed=0 |
| 235 | +failed=0 |
| 236 | +failures=() |
| 237 | + |
| 238 | +echo "Running ${#FIXTURES[@]} fixture(s)..." |
| 239 | +echo "" |
| 240 | + |
| 241 | +for fixture in "${FIXTURES[@]}"; do |
| 242 | + if run_fixture "$fixture"; then |
| 243 | + ((passed++)) || true |
| 244 | + else |
| 245 | + ((failed++)) || true |
| 246 | + failures+=("$fixture") |
| 247 | + fi |
| 248 | +done |
| 249 | + |
| 250 | +echo "" |
| 251 | +echo "Results: $passed passed, $failed failed, $((passed + failed)) total" |
| 252 | + |
| 253 | +if [[ $failed -gt 0 ]]; then |
| 254 | + echo "" |
| 255 | + echo "Failed:" |
| 256 | + for f in "${failures[@]}"; do |
| 257 | + echo " $f" |
| 258 | + done |
| 259 | + exit 1 |
| 260 | +fi |
0 commit comments