|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# RAM Manual Testing Commands |
| 4 | +# Collection of test commands for RAM functionality |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +# Configuration |
| 9 | +PG_BIN="/usr/local/pgsql.17/bin" |
| 10 | +BASE_PORT=5433 |
| 11 | +RAM_BASE_PORT=8080 |
| 12 | + |
| 13 | +# Colors for output |
| 14 | +RED='\033[0;31m' |
| 15 | +GREEN='\033[0;32m' |
| 16 | +YELLOW='\033[1;33m' |
| 17 | +BLUE='\033[0;34m' |
| 18 | +NC='\033[0m' # No Color |
| 19 | + |
| 20 | +echo -e "${BLUE}=== RAM Manual Testing Commands ===${NC}" |
| 21 | + |
| 22 | +# Function to run SQL command on all nodes |
| 23 | +run_sql_all() { |
| 24 | + local sql="$1" |
| 25 | + echo -e "${YELLOW}Running SQL on all nodes: $sql${NC}" |
| 26 | + |
| 27 | + for i in {1..3}; do |
| 28 | + PORT=$((BASE_PORT + i - 1)) |
| 29 | + echo -e "${BLUE}Node$i (port $PORT):${NC}" |
| 30 | + $PG_BIN/psql -p $PORT -h localhost -d postgres -c "$sql" |
| 31 | + echo "" |
| 32 | + done |
| 33 | +} |
| 34 | + |
| 35 | +# Function to check cluster status |
| 36 | +check_cluster_status() { |
| 37 | + echo -e "${YELLOW}=== Cluster Status Check ===${NC}" |
| 38 | + |
| 39 | + # Check PostgreSQL status |
| 40 | + echo -e "${BLUE}PostgreSQL Status:${NC}" |
| 41 | + for i in {1..3}; do |
| 42 | + PORT=$((BASE_PORT + i - 1)) |
| 43 | + if $PG_BIN/pg_isready -p $PORT -h localhost; then |
| 44 | + echo -e "${GREEN}✓ Node$i (port $PORT): Ready${NC}" |
| 45 | + else |
| 46 | + echo -e "${RED}✗ Node$i (port $PORT): Not ready${NC}" |
| 47 | + fi |
| 48 | + done |
| 49 | + |
| 50 | + # Check pgraft status |
| 51 | + echo -e "${BLUE}pgraft Extension Status:${NC}" |
| 52 | + for i in {1..3}; do |
| 53 | + PORT=$((BASE_PORT + i - 1)) |
| 54 | + echo -e "${BLUE}Node$i:${NC}" |
| 55 | + $PG_BIN/psql -p $PORT -h localhost -d postgres -c "SELECT * FROM pg_extension WHERE extname = 'pgraft';" |
| 56 | + done |
| 57 | + |
| 58 | + # Check RAM API status (if ramd is running) |
| 59 | + echo -e "${BLUE}RAM API Status:${NC}" |
| 60 | + for i in {1..3}; do |
| 61 | + HTTP_PORT=$((RAM_BASE_PORT + i - 1)) |
| 62 | + if curl -s http://localhost:$HTTP_PORT/health >/dev/null 2>&1; then |
| 63 | + echo -e "${GREEN}✓ RAM API Node$i (port $HTTP_PORT): Ready${NC}" |
| 64 | + else |
| 65 | + echo -e "${RED}✗ RAM API Node$i (port $HTTP_PORT): Not ready${NC}" |
| 66 | + fi |
| 67 | + done |
| 68 | +} |
| 69 | + |
| 70 | +# Function to test basic functionality |
| 71 | +test_basic_functionality() { |
| 72 | + echo -e "${YELLOW}=== Basic Functionality Test ===${NC}" |
| 73 | + |
| 74 | + # Create test table |
| 75 | + echo -e "${BLUE}Creating test table...${NC}" |
| 76 | + run_sql_all "CREATE TABLE IF NOT EXISTS test_table (id SERIAL PRIMARY KEY, data TEXT, created_at TIMESTAMP DEFAULT NOW());" |
| 77 | + |
| 78 | + # Insert test data |
| 79 | + echo -e "${BLUE}Inserting test data...${NC}" |
| 80 | + run_sql_all "INSERT INTO test_table (data) VALUES ('test_data_1'), ('test_data_2'), ('test_data_3');" |
| 81 | + |
| 82 | + # Query test data |
| 83 | + echo -e "${BLUE}Querying test data...${NC}" |
| 84 | + run_sql_all "SELECT * FROM test_table ORDER BY id;" |
| 85 | + |
| 86 | + # Check replication status |
| 87 | + echo -e "${BLUE}Checking replication status...${NC}" |
| 88 | + for i in {1..3}; do |
| 89 | + PORT=$((BASE_PORT + i - 1)) |
| 90 | + echo -e "${BLUE}Node$i replication status:${NC}" |
| 91 | + $PG_BIN/psql -p $PORT -h localhost -d postgres -c "SELECT * FROM pg_stat_replication;" |
| 92 | + echo "" |
| 93 | + done |
| 94 | +} |
| 95 | + |
| 96 | +# Function to test failover |
| 97 | +test_failover() { |
| 98 | + echo -e "${YELLOW}=== Failover Test ===${NC}" |
| 99 | + |
| 100 | + # Get current primary |
| 101 | + echo -e "${BLUE}Current cluster state:${NC}" |
| 102 | + check_cluster_status |
| 103 | + |
| 104 | + # Simulate primary failure |
| 105 | + echo -e "${BLUE}Simulating primary failure...${NC}" |
| 106 | + echo "Press Enter to continue with failover test..." |
| 107 | + read |
| 108 | + |
| 109 | + # Check which node is primary |
| 110 | + echo -e "${BLUE}Checking primary node...${NC}" |
| 111 | + for i in {1..3}; do |
| 112 | + PORT=$((BASE_PORT + i - 1)) |
| 113 | + echo -e "${BLUE}Node$i:${NC}" |
| 114 | + $PG_BIN/psql -p $PORT -h localhost -d postgres -c "SELECT pg_is_in_recovery();" |
| 115 | + done |
| 116 | + |
| 117 | + # Test write operations |
| 118 | + echo -e "${BLUE}Testing write operations...${NC}" |
| 119 | + for i in {1..3}; do |
| 120 | + PORT=$((BASE_PORT + i - 1)) |
| 121 | + echo -e "${BLUE}Testing writes on Node$i:${NC}" |
| 122 | + $PG_BIN/psql -p $PORT -h localhost -d postgres -c "INSERT INTO test_table (data) VALUES ('failover_test_$(date +%s)');" 2>/dev/null || echo "Write failed (expected for standby)" |
| 123 | + echo "" |
| 124 | + done |
| 125 | +} |
| 126 | + |
| 127 | +# Function to test RAM API |
| 128 | +test_ram_api() { |
| 129 | + echo -e "${YELLOW}=== RAM API Test ===${NC}" |
| 130 | + |
| 131 | + for i in {1..3}; do |
| 132 | + HTTP_PORT=$((RAM_BASE_PORT + i - 1)) |
| 133 | + echo -e "${BLUE}Testing RAM API on Node$i (port $HTTP_PORT):${NC}" |
| 134 | + |
| 135 | + # Health check |
| 136 | + echo "Health check:" |
| 137 | + curl -s http://localhost:$HTTP_PORT/health | jq . 2>/dev/null || echo "Health check failed" |
| 138 | + |
| 139 | + # Cluster status |
| 140 | + echo "Cluster status:" |
| 141 | + curl -s http://localhost:$HTTP_PORT/api/v1/cluster/status | jq . 2>/dev/null || echo "Cluster status failed" |
| 142 | + |
| 143 | + # Node info |
| 144 | + echo "Node info:" |
| 145 | + curl -s http://localhost:$HTTP_PORT/api/v1/node/info | jq . 2>/dev/null || echo "Node info failed" |
| 146 | + |
| 147 | + echo "" |
| 148 | + done |
| 149 | +} |
| 150 | + |
| 151 | +# Function to show monitoring data |
| 152 | +show_monitoring() { |
| 153 | + echo -e "${YELLOW}=== Monitoring Data ===${NC}" |
| 154 | + |
| 155 | + # PostgreSQL statistics |
| 156 | + echo -e "${BLUE}PostgreSQL Statistics:${NC}" |
| 157 | + for i in {1..3}; do |
| 158 | + PORT=$((BASE_PORT + i - 1)) |
| 159 | + echo -e "${BLUE}Node$i:${NC}" |
| 160 | + $PG_BIN/psql -p $PORT -h localhost -d postgres -c "SELECT datname, numbackends, xact_commit, xact_rollback, blks_read, blks_hit FROM pg_stat_database WHERE datname = 'postgres';" |
| 161 | + echo "" |
| 162 | + done |
| 163 | + |
| 164 | + # RAM metrics (if available) |
| 165 | + echo -e "${BLUE}RAM Metrics:${NC}" |
| 166 | + for i in {1..3}; do |
| 167 | + HTTP_PORT=$((RAM_BASE_PORT + i - 1)) |
| 168 | + echo -e "${BLUE}Node$i metrics:${NC}" |
| 169 | + curl -s http://localhost:$HTTP_PORT/metrics 2>/dev/null | grep -E "(ram_|pgraft_)" | head -10 || echo "Metrics not available" |
| 170 | + echo "" |
| 171 | + done |
| 172 | +} |
| 173 | + |
| 174 | +# Main menu |
| 175 | +show_menu() { |
| 176 | + echo -e "${BLUE}=== RAM Testing Menu ===${NC}" |
| 177 | + echo "1. Check cluster status" |
| 178 | + echo "2. Test basic functionality" |
| 179 | + echo "3. Test failover" |
| 180 | + echo "4. Test RAM API" |
| 181 | + echo "5. Show monitoring data" |
| 182 | + echo "6. Run all tests" |
| 183 | + echo "7. Exit" |
| 184 | + echo "" |
| 185 | + echo -n "Choose an option (1-7): " |
| 186 | +} |
| 187 | + |
| 188 | +# Main execution |
| 189 | +if [ "$1" = "all" ]; then |
| 190 | + check_cluster_status |
| 191 | + test_basic_functionality |
| 192 | + test_ram_api |
| 193 | + show_monitoring |
| 194 | +elif [ "$1" = "status" ]; then |
| 195 | + check_cluster_status |
| 196 | +elif [ "$1" = "basic" ]; then |
| 197 | + test_basic_functionality |
| 198 | +elif [ "$1" = "failover" ]; then |
| 199 | + test_failover |
| 200 | +elif [ "$1" = "api" ]; then |
| 201 | + test_ram_api |
| 202 | +elif [ "$1" = "monitor" ]; then |
| 203 | + show_monitoring |
| 204 | +else |
| 205 | + while true; do |
| 206 | + show_menu |
| 207 | + read choice |
| 208 | + case $choice in |
| 209 | + 1) check_cluster_status ;; |
| 210 | + 2) test_basic_functionality ;; |
| 211 | + 3) test_failover ;; |
| 212 | + 4) test_ram_api ;; |
| 213 | + 5) show_monitoring ;; |
| 214 | + 6) |
| 215 | + check_cluster_status |
| 216 | + test_basic_functionality |
| 217 | + test_ram_api |
| 218 | + show_monitoring |
| 219 | + ;; |
| 220 | + 7) echo "Exiting..."; exit 0 ;; |
| 221 | + *) echo "Invalid option. Please try again." ;; |
| 222 | + esac |
| 223 | + echo "" |
| 224 | + echo "Press Enter to continue..." |
| 225 | + read |
| 226 | + done |
| 227 | +fi |
0 commit comments