Skip to content

Commit 3478717

Browse files
committed
test
1 parent ac4370a commit 3478717

2 files changed

Lines changed: 75 additions & 27 deletions

File tree

Dockerfile

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,25 @@ ENV CERTS="{}" \
2020
HBA_EXTRA_RULES=""
2121
RUN apk add --no-cache python3 py3-netifaces \
2222
&& if [ "${PG_MAJOR:-0}" -ge 12 ]; then \
23-
apk add --no-cache --virtual .pgvector-build build-base linux-headers ca-certificates \
24-
&& for v in 23 22 21 20 19; do \
25-
apk add --no-cache --virtual .pgvector-build clang${v} llvm${v} && break; \
26-
done \
23+
PG_CLANG="$(pg_config --configure | tr "'" "\n" | sed -n 's/^CLANG=clang-//p')" \
24+
&& test -n "${PG_CLANG}" \
25+
&& PG_LLVM="$(pg_config --configure | tr "'" "\n" | sed -n 's#^LLVM_CONFIG=/usr/lib/llvm\([0-9][0-9]*\)/bin/llvm-config#\1#p')" \
26+
&& test -n "${PG_LLVM}" \
27+
&& test "${PG_CLANG}" = "${PG_LLVM}" \
28+
&& apk add --no-cache --virtual .pgvector-build \
29+
build-base \
30+
linux-headers \
31+
ca-certificates \
32+
"clang${PG_CLANG}" \
33+
"llvm${PG_LLVM}" \
2734
&& wget -qO- "https://github.com/pgvector/pgvector/archive/refs/tags/v${PGVECTOR_VERSION}.tar.gz" \
28-
| tar -xz -C /tmp; \
29-
cd "/tmp/pgvector-${PGVECTOR_VERSION}" \
30-
&& make PG_CONFIG=/usr/local/bin/pg_config \
31-
&& make install PG_CONFIG=/usr/local/bin/pg_config; \
32-
cd / && rm -rf "/tmp/pgvector-${PGVECTOR_VERSION}"; \
33-
apk del .pgvector-build; \
35+
| tar -xz -C /tmp \
36+
&& cd "/tmp/pgvector-${PGVECTOR_VERSION}" \
37+
&& make PG_CONFIG=/usr/local/bin/pg_config \
38+
&& make install PG_CONFIG=/usr/local/bin/pg_config \
39+
&& cd / \
40+
&& rm -rf "/tmp/pgvector-${PGVECTOR_VERSION}" \
41+
&& apk del .pgvector-build; \
3442
fi \
3543
&& mkdir -p /etc/postgres \
3644
&& chmod a=rwx /etc/postgres

tests/test.py

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,30 +58,31 @@ def _generate_certs(self):
5858
"""Generate certificates for testing the image."""
5959
certgen("example.localdomain", "test_user")
6060

61+
def _exec_local_psql(self, command, dbname="test_db", username="test_user"):
62+
"""Execute a SQL command inside the Postgres container."""
63+
return docker(
64+
"container",
65+
"exec",
66+
self.postgres_container,
67+
"psql",
68+
"--command",
69+
command,
70+
"--dbname",
71+
dbname,
72+
"--no-align",
73+
"--tuples-only",
74+
"--username",
75+
username,
76+
)
77+
6178
def _check_local_connection(self):
6279
"""Check that local connection works fine."""
6380
# The 1st test could fail while postgres boots
6481
for attempt in range(10):
6582
try:
6683
time.sleep(15)
6784
# Test local connections via unix socket work
68-
self.assertEqual(
69-
"1\n",
70-
docker(
71-
"container",
72-
"exec",
73-
self.postgres_container,
74-
"psql",
75-
"--command",
76-
"SELECT 1",
77-
"--dbname",
78-
"test_db",
79-
"--no-align",
80-
"--tuples-only",
81-
"--username",
82-
"test_user",
83-
),
84-
)
85+
self.assertEqual("1\n", self._exec_local_psql("SELECT 1"))
8586
except (AssertionError, ProcessExecutionError):
8687
if attempt < 9:
8788
print("Failure number {}. Retrying...".format(attempt))
@@ -313,6 +314,45 @@ def test_certs_falsy_lan(self):
313314
with self.assertRaises(ProcessExecutionError):
314315
self._check_password_auth("example.localdomain")
315316

317+
def test_pgvector_extension_available(self):
318+
"""Supported PostgreSQL images expose a working vector extension."""
319+
self.postgres_container = docker(
320+
"container",
321+
"run",
322+
*self._platform_args(),
323+
"-d",
324+
"--network",
325+
"lan",
326+
"-e",
327+
"POSTGRES_DB=test_db",
328+
"-e",
329+
"POSTGRES_PASSWORD=test_password",
330+
"-e",
331+
"POSTGRES_USER=test_user",
332+
CONF_EXTRA,
333+
self.image,
334+
).strip()
335+
self._check_local_connection()
336+
337+
server_version_num = int(
338+
self._exec_local_psql("SHOW server_version_num").strip()
339+
)
340+
if server_version_num < 120000:
341+
self.skipTest("pgvector is only built for PostgreSQL 12+")
342+
343+
vector_version = self._exec_local_psql(
344+
"SELECT default_version FROM pg_available_extensions WHERE name = 'vector'"
345+
).strip()
346+
self.assertTrue(vector_version)
347+
348+
self._exec_local_psql("CREATE EXTENSION vector")
349+
self.assertEqual(
350+
"1\n",
351+
self._exec_local_psql(
352+
"SELECT ('[1,2,3]'::vector <-> '[1,2,4]'::vector)::int"
353+
),
354+
)
355+
316356
def test_hba_extra_rules_added(self):
317357
"""Test that HBA_EXTRA_RULES lines are added to pg_hba.conf."""
318358
if "9.6" in self.image:

0 commit comments

Comments
 (0)