|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# Source query IDs from setup script |
| 6 | +if [ -z "$QUERY_ID" ]; then |
| 7 | + echo "Error: Query IDs not set. Run setup_test_data.sh first." |
| 8 | + echo "Usage: source setup_test_data.sh && ./check_logs_table.sh" |
| 9 | + exit 1 |
| 10 | +fi |
| 11 | + |
| 12 | +execute_query() { |
| 13 | + local sql="$1" |
| 14 | + curl -s -u root: -XPOST "http://localhost:8000/v1/query" -H 'Content-Type: application/json' -d "{\"sql\": \"$sql\", \"pagination\": {\"wait_time_secs\": 10}}" |
| 15 | +} |
| 16 | + |
| 17 | +check_query_log() { |
| 18 | + local test_name=$1 |
| 19 | + local query_id=$2 |
| 20 | + local check_query=$3 |
| 21 | + local expected_result=$4 |
| 22 | + |
| 23 | + local full_sql_query="$check_query" |
| 24 | + |
| 25 | + if [ -n "$query_id" ] && [ "$query_id" != "null" ]; then |
| 26 | + full_sql_query+=" query_id = '$query_id'" |
| 27 | + fi |
| 28 | + |
| 29 | + echo "$full_sql_query" |
| 30 | + response=$(execute_query "$full_sql_query") |
| 31 | + |
| 32 | + result=$(echo "$response" | jq -r '.data[0][0]' | tr -d '"') |
| 33 | + if [ "$result" != "$expected_result" ]; then |
| 34 | + echo "Log table test #$test_name failed, Result: $result, Expected: $expected_result" |
| 35 | + exit 1 |
| 36 | + else |
| 37 | + echo "Log table test #$test_name passed, Result: $result, Expected: $expected_result" |
| 38 | + fi |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +# Basic log table tests |
| 43 | +check_query_log "basic-1" "$QUERY_ID" "SELECT count(*) FROM system_history.log_history WHERE target = 'databend::log::profile' and" "1" |
| 44 | + |
| 45 | +check_query_log "basic-2" "$QUERY_ID" "SELECT count(*) FROM system_history.profile_history WHERE" "1" |
| 46 | + |
| 47 | +check_query_log "basic-3" "$QUERY_ID" "SELECT count(*) FROM system_history.log_history WHERE target = 'databend::log::query' and" "2" |
| 48 | + |
| 49 | +check_query_log "basic-4" "$QUERY_ID" "SELECT count(*) FROM system_history.query_history WHERE" "1" |
| 50 | + |
| 51 | +check_query_log "basic-6" "$SELECT_QUERY_ID" "SELECT count(*) FROM system_history.access_history WHERE" "1" |
| 52 | + |
| 53 | +# Access history tests |
| 54 | +check_query_log "basic-7" "$CREATE_QUERY_ID" "SELECT object_modified_by_ddl[0]['object_name'] FROM system_history.access_history WHERE" "default.default.t" |
| 55 | + |
| 56 | +check_query_log "basic-8" "$INSERT_QUERY_ID" "SELECT objects_modified[0]['object_name'] FROM system_history.access_history WHERE" "default.default.t" |
| 57 | + |
| 58 | +check_query_log "basic-9" "$SELECT_QUERY_ID" "SELECT base_objects_accessed[0]['object_name'] FROM system_history.access_history WHERE" "default.default.t" |
| 59 | + |
| 60 | +check_query_log "basic-10" "$CREATE_VIEW_QUERY_ID" "select query_text from system_history.query_history where" "CREATE VIEW v AS SELECT a FROM t" |
| 61 | + |
| 62 | +check_query_log "basic-11" null "SELECT count(*) FROM system_history.login_history WHERE session_id = '$SELECT_SESSION_ID' " "1" |
| 63 | + |
| 64 | +# Timezone tests - regression test for https://github.com/databendlabs/databend/pull/18059 |
| 65 | +check_query_log "timezone-1" "$SELECT_QUERY_ID" "settings (timezone='Asia/Shanghai') SELECT DATE_DIFF(hour, timestamp, now()) FROM system_history.log_history WHERE target = 'databend::log::profile' and" "0" |
| 66 | + |
| 67 | +check_query_log "timezone-2" "$SELECT_QUERY_ID" "settings (timezone='Asia/Shanghai') SELECT DATE_DIFF(hour, event_time, now()) FROM system_history.query_history WHERE" "0" |
| 68 | + |
| 69 | +check_query_log "timezone-3" "$SELECT_QUERY_ID" "settings (timezone='Asia/Shanghai') SELECT DATE_DIFF(hour, timestamp, now()) FROM system_history.profile_history WHERE" "0" |
| 70 | + |
| 71 | +check_query_log "timezone-4" null "settings (timezone='Asia/Shanghai') SELECT sum(DATE_DIFF(hour, event_time, now())) FROM system_history.login_history" "0" |
| 72 | + |
| 73 | +check_query_log "timezone-5" "$SELECT_QUERY_ID" "settings (timezone='Asia/Shanghai') SELECT DATE_DIFF(hour, query_start, now()) FROM system_history.access_history WHERE" "0" |
| 74 | + |
| 75 | +# Login history tests |
| 76 | +check_query_log "login-1" null "SELECT CASE WHEN count(*) >= 1 THEN 1 ELSE 0 END FROM system_history.login_history WHERE user_name = 'root'" "1" |
| 77 | + |
| 78 | +check_query_log "login-2" null "SELECT CASE WHEN count(*) >= 1 THEN 1 ELSE 0 END FROM system_history.login_history WHERE error_message LIKE 'AuthenticateFailure.%' AND user_name = 'wrong_pass_user'" "1" |
| 79 | + |
| 80 | + |
| 81 | +echo "All log table tests passed successfully!" |
0 commit comments