Skip to content

Commit 26072d6

Browse files
committed
fix the unix socket specs to use docker
1 parent 6b99ad2 commit 26072d6

3 files changed

Lines changed: 163 additions & 73 deletions

File tree

spec/pgmoon_unix_spec.moon

Lines changed: 125 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,127 @@
11
import Postgres from require "pgmoon"
22

3-
SOCKET_PATH = "/var/run/postgresql/.s.PGSQL.5432"
4-
5-
-- Basic test to verify Unix socket support loads correctly
6-
describe "Unix socket support", ->
7-
it "should choose nginx or luaposix when socket_path is provided", ->
8-
pg = Postgres {
9-
socket_path: "/tmp/test.sock"
10-
user: "postgres"
11-
database: "test"
12-
}
13-
14-
if ngx and ngx.get_phase! != "init"
15-
assert.equal "nginx", pg.sock_type
16-
else
17-
assert.equal "luaposix", pg.sock_type
18-
19-
it "should accept socket_path in configuration", ->
20-
pg = Postgres {
21-
socket_path: SOCKET_PATH
22-
user: "postgres"
23-
database: "test"
24-
}
25-
26-
assert.equal SOCKET_PATH, pg.config.socket_path
27-
28-
it "should default to luasocket when no socket_path is provided", ->
29-
pg = Postgres {
30-
user: "postgres"
31-
database: "test"
32-
}
33-
34-
-- Should not be luaposix since no socket_path provided
35-
assert.is_not.equal "luaposix", pg.sock_type
36-
37-
it "should have socket_path as nil in default config", ->
38-
pg = Postgres {}
39-
assert.is_nil pg.config.socket_path
40-
41-
it "should override socket_type when socket_path is provided", ->
42-
-- Even if we explicitly set socket_type to something else,
43-
-- providing socket_path should force luaposix
44-
pg = Postgres {
45-
socket_type: "luasocket"
46-
socket_path: "/tmp/test.sock"
47-
user: "postgres"
48-
database: "test"
49-
}
50-
51-
if ngx and ngx.get_phase! != "init"
52-
assert.equal "nginx", pg.sock_type
53-
else
54-
assert.equal "luaposix", pg.sock_type
55-
56-
-- This test would require an actual Unix socket PostgreSQL server
57-
-- Keeping it commented out for now
58-
it "should connect via Unix socket", ->
59-
pg = Postgres {
60-
socket_path: SOCKET_PATH
61-
user: "postgres"
62-
database: "postgres"
63-
}
64-
65-
success, err = pg\connect!
66-
assert success, "Failed to connect: #{err}"
67-
68-
result, err = pg\query "SELECT 1 as test"
69-
assert result, "Failed to query: #{err}"
70-
assert.equal 1, result[1].test
71-
72-
pg\disconnect!
3+
import psql_unix, SOCKET_PATH, USER, PASSWORD, DB from require "spec.util"
4+
5+
describe "pgmoon Unix socket with server", ->
6+
setup ->
7+
os.execute "spec/postgres.sh start unix"
8+
9+
r = { psql_unix "drop database if exists #{DB}" }
10+
assert 0 == r[#r], "failed to execute psql_unix: drop database"
11+
12+
r = { psql_unix "create database #{DB}" }
13+
assert 0 == r[#r], "failed to execute psql_unix: create database"
14+
15+
teardown ->
16+
os.execute "spec/postgres.sh stop"
17+
18+
-- Test that socket_path configuration selects the right socket type
19+
describe "socket type selection", ->
20+
it "should choose luaposix when socket_path is provided outside nginx", ->
21+
unless ngx
22+
pg = Postgres {
23+
socket_path: "/tmp/test.sock"
24+
user: "postgres"
25+
database: "test"
26+
}
27+
assert.equal "luaposix", pg.sock_type
28+
29+
it "should accept socket_path in configuration", ->
30+
pg = Postgres {
31+
socket_path: SOCKET_PATH
32+
user: "postgres"
33+
database: "test"
34+
}
35+
assert.equal SOCKET_PATH, pg.config.socket_path
36+
37+
it "should default to luasocket when no socket_path is provided", ->
38+
pg = Postgres {
39+
user: "postgres"
40+
database: "test"
41+
}
42+
-- Should not be luaposix since no socket_path provided
43+
assert.is_not.equal "luaposix", pg.sock_type
44+
45+
it "should have socket_path as nil in default config", ->
46+
pg = Postgres {}
47+
assert.is_nil pg.config.socket_path
48+
49+
it "should override socket_type when socket_path is provided", ->
50+
pg = Postgres {
51+
socket_type: "luasocket"
52+
socket_path: "/tmp/test.sock"
53+
user: "postgres"
54+
database: "test"
55+
}
56+
if ngx and ngx.get_phase! != "init"
57+
assert.equal "nginx", pg.sock_type
58+
else
59+
assert.equal "luaposix", pg.sock_type
60+
61+
-- Actual connection tests via Unix socket
62+
describe "luaposix socket", ->
63+
local pg
64+
65+
before_each ->
66+
pg = Postgres {
67+
socket_path: SOCKET_PATH
68+
user: USER
69+
password: PASSWORD
70+
database: DB
71+
}
72+
73+
after_each ->
74+
pg\disconnect! if pg
75+
76+
it "connects via Unix socket", ->
77+
success, err = pg\connect!
78+
assert success, "Failed to connect: #{err}"
79+
80+
result, err = pg\query "SELECT 1 as test"
81+
assert result, "Failed to query: #{err}"
82+
assert.same { { test: 1 } }, result
83+
84+
it "creates and queries table", ->
85+
assert pg\connect!
86+
87+
res = assert pg\query [[
88+
create table unix_test (
89+
id serial not null,
90+
name text,
91+
primary key (id)
92+
)
93+
]]
94+
assert.same true, res
95+
96+
res = assert pg\query [[
97+
insert into unix_test (name) values ('hello') returning id, name
98+
]]
99+
assert.same {
100+
affected_rows: 1
101+
{ id: 1, name: "hello" }
102+
}, res
103+
104+
res = assert pg\query [[
105+
select * from unix_test
106+
]]
107+
assert.same {
108+
{ id: 1, name: "hello" }
109+
}, res
110+
111+
assert pg\query [[drop table unix_test]]
112+
113+
it "handles errors correctly", ->
114+
assert pg\connect!
115+
116+
status, err = pg\query "select * from nonexistent_table"
117+
assert.falsy status
118+
assert.truthy err\match "does not exist"
119+
120+
it "can reconnect after disconnect", ->
121+
do return pending "luaposix socket does not support reconnect on same instance"
122+
assert pg\connect!
123+
assert pg\query "select 1"
124+
pg\disconnect!
125+
126+
assert pg\connect!
127+
assert pg\query "select 1"

spec/postgres.sh

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set -eo pipefail
33

44
pgroot=$(pwd)/pgdata
55
port=9999
6+
socket_dir="/tmp/pgmoon-test-socket"
67

78
postgres_version=${DOCKER_POSTGRES_VERSION:-latest}
89

@@ -20,25 +21,52 @@ function makecerts {
2021
}
2122

2223
function start {
24+
# Stop any existing container first
25+
docker stop pgmoon-test > /dev/null 2>&1 || true
26+
2327
INIT_SCRIPT=""
28+
VOLUME_MOUNT=""
29+
PORT_MAPPING="-p 127.0.0.1:$port:5432/tcp"
2430

2531
if [ "$1" = "ssl" ]; then
2632
INIT_SCRIPT="-v $(pwd)/spec/docker_enable_ssl.sh:/docker-entrypoint-initdb.d/docker_enable_ssl.sh"
2733
fi
2834

35+
if [ "$1" = "unix" ]; then
36+
# Create socket directory if it doesn't exist
37+
if [ ! -d "$socket_dir" ]; then
38+
mkdir -p "$socket_dir"
39+
chmod 777 "$socket_dir"
40+
fi
41+
VOLUME_MOUNT="-v $socket_dir:/var/run/postgresql"
42+
# No TCP port mapping needed for unix socket mode
43+
PORT_MAPPING=""
44+
fi
45+
2946
echo "$(tput setaf 4)Starting postgresql $postgres_version (docker run) $1 $(tput sgr0)"
3047
docker run --rm --name pgmoon-test \
31-
-p 127.0.0.1:$port:5432/tcp \
48+
$PORT_MAPPING \
3249
-e POSTGRES_PASSWORD=pgmoon \
3350
$INIT_SCRIPT \
51+
$VOLUME_MOUNT \
3452
-d \
3553
postgres:$postgres_version > /dev/null
3654

3755

3856
# -v "$pgroot:/var/lib/postgresql/data" \ # this can be used to inspect logs since we'll have the server data dir available after the sever stops
3957

4058
echo "$(tput setaf 4)Waiting for server to be ready$(tput sgr0)"
41-
until (PGHOST=127.0.0.1 PGPORT=$port PGUSER=postgres PGPASSWORD=pgmoon psql -c 'SELECT pg_reload_conf()' 2> /dev/null); do :; done
59+
if [ "$1" = "unix" ]; then
60+
# For unix socket mode, wait until socket file appears and postgres is ready
61+
until [ -S "$socket_dir/.s.PGSQL.5432" ]; do
62+
sleep 0.1
63+
done
64+
until docker exec pgmoon-test pg_isready -U postgres > /dev/null 2>&1; do
65+
sleep 0.1
66+
done
67+
else
68+
until (PGHOST=127.0.0.1 PGPORT=$port PGUSER=postgres PGPASSWORD=pgmoon psql -c 'SELECT pg_reload_conf()' 2> /dev/null); do :; done
69+
fi
4270
echo "$(tput setaf 4)Sever is ready$(tput sgr0)"
4371
}
4472

spec/util.moon

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@ USER = "postgres"
55
PASSWORD = "pgmoon"
66
DB = "pgmoon_test"
77

8+
SOCKET_DIR = "/tmp/pgmoon-test-socket"
9+
SOCKET_PATH = "#{SOCKET_DIR}/.s.PGSQL.5432"
10+
811
shell_escape = (str) -> str\gsub "'", "'\\''"
912

1013
psql = (query) ->
1114
os.execute "PGHOST='#{shell_escape HOST}' PGPORT='#{shell_escape PORT}' PGUSER='#{shell_escape USER}' PGPASSWORD='#{shell_escape PASSWORD}' psql -c '#{query}'"
1215

16+
-- psql via unix socket using local psql with socket path
17+
psql_unix = (query) ->
18+
os.execute "PGHOST='#{shell_escape SOCKET_DIR}' PGUSER='#{shell_escape USER}' PGPASSWORD='#{shell_escape PASSWORD}' psql -c '#{shell_escape query}'"
19+
1320

14-
{:psql, :HOST, :PORT, :USER, :PASSWORD, :DB }
21+
{:psql, :psql_unix, :HOST, :PORT, :USER, :PASSWORD, :DB, :SOCKET_PATH, :SOCKET_DIR}

0 commit comments

Comments
 (0)