fix(db): prevent POSTGRES_HOST env var leakage between tests
Backend Tests / test (push) Successful in 59s
Backend Tests / test (push) Successful in 59s
resetEnv() conditionally set POSTGRES_HOST only when empty. When TestConnect_InvalidCredentials explicitly set it to 'localhost' and then called resetEnv(), the value was preserved because it wasn't empty. This leaked into TestConcurrentQueries, which then tried to connect to localhost:5432 instead of the workflow-configured postgres hostname. Fix: capture the POSTGRES_HOST value at init() time in a package-level variable (savedPOSTGRESHost) and always restore it in resetEnv(), so the correct value is always used regardless of which tests ran before.
This commit is contained in:
@@ -60,9 +60,7 @@ jobs:
|
||||
|
||||
- name: Test (all packages)
|
||||
working-directory: backend
|
||||
run: |
|
||||
echo "POSTGRES_HOST=$POSTGRES_HOST TEST_DB_HOST=$TEST_DB_HOST"
|
||||
go test -tags "test,dev" -count=1 -v -timeout 120s ./handlers/... ./auth/... ./mw/... ./internal/... ./db/...
|
||||
run: go test -tags "test,dev" -count=1 -v -timeout 120s ./handlers/... ./auth/... ./mw/... ./internal/... ./db/...
|
||||
env:
|
||||
POSTGRES_HOST: postgres
|
||||
TEST_DB_HOST: postgres
|
||||
|
||||
@@ -14,9 +14,7 @@ import (
|
||||
func resetEnv() {
|
||||
os.Setenv("POSTGRES_USER", "myuser")
|
||||
os.Setenv("POSTGRES_PASSWORD", "mypassword")
|
||||
if os.Getenv("POSTGRES_HOST") == "" {
|
||||
os.Setenv("POSTGRES_HOST", "localhost")
|
||||
}
|
||||
os.Setenv("POSTGRES_HOST", savedPOSTGRESHost)
|
||||
os.Setenv("POSTGRES_DB", "crussell_test_db")
|
||||
}
|
||||
|
||||
|
||||
@@ -9,11 +9,16 @@ import (
|
||||
|
||||
// DB is a compatibility alias for Conn, for tests not yet migrated from db.Conn to db.Conn.
|
||||
|
||||
// savedPOSTGRESHost captures the env value at init time so resetEnv() can
|
||||
// always restore it, even after a test temporarily overrides it.
|
||||
var savedPOSTGRESHost string
|
||||
|
||||
func init() {
|
||||
// Set defaults for test environment if not already set
|
||||
if os.Getenv("POSTGRES_HOST") == "" {
|
||||
os.Setenv("POSTGRES_HOST", "localhost")
|
||||
}
|
||||
savedPOSTGRESHost = os.Getenv("POSTGRES_HOST")
|
||||
if os.Getenv("POSTGRES_USER") == "" {
|
||||
os.Setenv("POSTGRES_USER", "myuser")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user