-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.sh
More file actions
140 lines (116 loc) · 3.84 KB
/
Copy pathtest.sh
File metadata and controls
140 lines (116 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/bash
set -euo pipefail
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
MODE="${1:-all}"
# Prevent Git Bash from mangling paths
export MSYS_NO_PATHCONV=1
# Fail-fast guard: every mode in this script depends on the 'postgres'
# compose service being defined. If someone edits docker-compose.yml and
# drops the service (as happened in commit 4d0f666), `docker compose up -d
# postgres` would fail with an opaque "no such service" error. Surface the
# misconfiguration clearly instead.
if ! docker compose config --services 2>/dev/null | grep -q '^postgres$'; then
echo -e "${RED}✗ 'postgres' service is not defined in docker-compose.yml${NC}"
echo -e " test.sh requires a postgres compose service; add one back to docker-compose.yml."
exit 1
fi
GO_TEST_CMD="go test -p 1 -count=1 ./... -v -timeout 120s"
# Helper function to run commands in Docker
run_in_docker() {
local cmd="$1"
docker run --rm \
--network midas_default \
-v "$(pwd):/app" \
-w /app \
-e DATABASE_URL="postgresql://midas:midas@postgres:5432/midas?sslmode=disable" \
-e MIDAS_TEST_DATABASE_URL="postgresql://midas:midas@postgres:5432/midas?sslmode=disable" \
golang:1.26-alpine \
sh -c "$cmd"
}
# Helper function to wait for postgres to be ready
wait_for_postgres() {
echo -e "${YELLOW}Waiting for Postgres to be ready...${NC}"
for i in {1..30}; do
if docker run --rm --network midas_default postgres:17-alpine \
sh -c "PGPASSWORD=midas psql -h postgres -U midas -d postgres -c 'SELECT 1;'" > /dev/null 2>&1; then
echo -e "${GREEN}✓ Postgres is ready${NC}"
return 0
fi
echo -n "."
sleep 1
done
echo ""
echo -e "${RED}✗ Postgres failed to become ready${NC}"
return 1
}
# Helper function to initialize database schema
init_database() {
echo -e "${YELLOW}Initializing database schema...${NC}"
# Drop and recreate database
docker run --rm \
--network midas_default \
postgres:17-alpine \
sh -c "
export PGPASSWORD=midas
psql -h postgres -U midas -d postgres -c 'DROP DATABASE IF EXISTS midas;'
psql -h postgres -U midas -d postgres -c 'CREATE DATABASE midas;'
" > /dev/null 2>&1
# Apply schema from internal/store/postgres/schema.sql
docker run --rm \
--network midas_default \
-v "$(pwd):/app" \
-w /app \
postgres:17-alpine \
sh -c "export PGPASSWORD=midas && psql -h postgres -U midas -d midas -f /app/internal/store/postgres/schema.sql" > /dev/null 2>&1
echo -e "${GREEN}✓ Database schema initialized${NC}"
}
case "$MODE" in
all)
echo -e "${GREEN}Running all MIDAS tests...${NC}"
echo ""
# Ensure Docker Postgres is running
if ! docker compose ps | grep -q "postgres.*Up"; then
echo -e "${YELLOW}Starting Docker Postgres...${NC}"
docker compose down > /dev/null 2>&1 || true
docker compose up -d postgres
wait_for_postgres
else
echo -e "${GREEN}✓ Postgres is already running${NC}"
wait_for_postgres
fi
# Initialize database with fresh schema
init_database
echo ""
echo -e "${YELLOW}Running:${NC} ${GO_TEST_CMD}"
run_in_docker "${GO_TEST_CMD}"
;;
build)
echo -e "${GREEN}Running build...${NC}"
run_in_docker "go build ./..."
;;
vet)
echo -e "${GREEN}Running go vet...${NC}"
run_in_docker "go vet ./..."
;;
decision)
echo -e "${GREEN}Running decision tests...${NC}"
init_database
run_in_docker "go test -p 1 -count=1 ./internal/decision/... -v -timeout 120s"
;;
postgres)
echo -e "${GREEN}Running postgres tests...${NC}"
init_database
run_in_docker "go test -p 1 -count=1 ./internal/store/postgres/... -v -timeout 120s"
;;
*)
echo -e "${RED}Unknown mode: $MODE${NC}"
echo "Usage: $0 [all|build|vet|decision|postgres]"
exit 1
;;
esac
echo ""
echo -e "${GREEN}✓ Tests complete${NC}"