Skip to content

Commit 4714ac5

Browse files
authored
Merge pull request #45 from sjyothi54/integration_testing
Integration testing setup for postgres
2 parents 8509bcd + 870a0e2 commit 4714ac5

20 files changed

+1548
-71
lines changed

Makefile

+11-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,17 @@ test:
3030

3131
integration-test:
3232
@echo "=== $(INTEGRATION) === [ test ]: running integration tests..."
33-
@docker compose -f tests/docker-compose.yml pull
34-
@go test -v -tags=integration -count 1 ./tests/. || (ret=$$?; docker compose -f tests/docker-compose.yml down && exit $$ret)
35-
@docker compose -f tests/docker-compose.yml down
33+
@docker compose -f tests/docker-compose.yml up -d
34+
@sleep 10
35+
@go test -v -tags=integration -count 1 ./tests/postgresql_test.go -timeout 300s || (ret=$$?; docker compose -f tests/docker-compose.yml down -v && exit $$ret)
36+
@docker compose -f tests/docker-compose.yml down -v
37+
@echo "=== $(INTEGRATION) === [ test ]: running integration tests for query performance monitoring..."
38+
@echo "Starting containers for performance tests..."
39+
@docker compose -f tests/docker-compose-performance.yml up -d
40+
@sleep 30
41+
@go test -v -tags=query_performance ./tests/postgresqlperf_test.go -timeout 600s || (ret=$$?; docker compose -f tests/docker-compose-performance.yml down -v && exit $$ret)
42+
@echo "Stopping performance test containers..."
43+
@docker compose -f tests/docker-compose-performance.yml down -v
3644

3745
install: compile
3846
@echo "=== $(INTEGRATION) === [ install ]: installing bin/$(BINARY_NAME)..."

tests/docker-compose-performance.yml

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
services:
2+
3+
postgres12:
4+
build:
5+
context: ./perf-testing/oldest_supported/
6+
dockerfile: Dockerfile
7+
container_name: "postgresql-perf-oldest"
8+
restart: always
9+
environment:
10+
- POSTGRES_USER=dbuser
11+
- POSTGRES_PASSWORD=dbpassword
12+
- POSTGRES_DB=demo
13+
volumes:
14+
- postgres12:/var/lib/postgresql/data
15+
ports:
16+
- "6432:5432"
17+
healthcheck:
18+
test: ["CMD-SHELL", "pg_isready -U postgres"]
19+
interval: 10s
20+
timeout: 5s
21+
retries: 5
22+
23+
postgresql-latest:
24+
build:
25+
context: ./perf-testing/oldest_supported/
26+
dockerfile: Dockerfile
27+
restart: always
28+
container_name: "postgresql-perf-latest"
29+
environment:
30+
- POSTGRES_USER=dbuser
31+
- POSTGRES_PASSWORD=dbpassword
32+
- POSTGRES_DB=demo
33+
volumes:
34+
- pgdata_latest:/var/lib/postgresql/data
35+
ports:
36+
- "5432:5432"
37+
healthcheck:
38+
test: ["CMD-SHELL", "pg_isready -U postgres"]
39+
interval: 10s
40+
timeout: 5s
41+
retries: 5
42+
43+
postgres-without-extensions:
44+
image: postgres:17.0
45+
restart: always
46+
container_name: "postgresql-noext"
47+
environment:
48+
- POSTGRES_USER=dbuser
49+
- POSTGRES_PASSWORD=dbpassword
50+
- POSTGRES_DB=demo
51+
volumes:
52+
- pgdata_noext:/var/lib/postgresql/data
53+
ports:
54+
- "7432:5432"
55+
healthcheck:
56+
test: ["CMD-SHELL", "pg_isready -U postgres"]
57+
interval: 10s
58+
timeout: 5s
59+
retries: 5
60+
61+
nri-postgresql:
62+
container_name: nri_postgresql
63+
build:
64+
context: ../
65+
dockerfile: tests/perf-testing/integration/Dockerfile
66+
67+
volumes:
68+
pgdata_latest:
69+
postgres12:
70+
pgdata_noext:

tests/docker-compose.yml

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ services:
22
postgres-9-6:
33
image: postgres:9.6
44
restart: always
5+
container_name: postgres-9-6
56
environment:
67
POSTGRES_USER: postgres
78
POSTGRES_PASSWORD: example
@@ -10,18 +11,14 @@ services:
1011
postgres-latest-supported:
1112
image: postgres:17.0
1213
restart: always
14+
container_name: postgres-latest-supported
1315
environment:
1416
POSTGRES_USER: postgres
1517
POSTGRES_PASSWORD: example
1618
POSTGRES_DB: demo
1719

1820
nri-postgresql:
19-
image: golang:1.23.4-bookworm
20-
container_name: nri_postgresql
21-
working_dir: /code
22-
depends_on:
23-
- postgres-9-6
24-
- postgres-latest-supported
25-
volumes:
26-
- ../:/code
27-
entrypoint: go run /code/src/main.go
21+
container_name: nri-postgresql
22+
build:
23+
context: ../
24+
dockerfile: tests/perf-testing/integration/Dockerfile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM golang:1.23.4-bookworm as builder
2+
ARG CGO_ENABLED=0
3+
WORKDIR /go/src/github.com/newrelic/nri-postgresql
4+
COPY . .
5+
RUN make clean compile
6+
7+
FROM alpine:latest
8+
COPY --from=builder /go/src/github.com/newrelic/nri-postgresql/bin /
9+
CMD ["sleep", "1h"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
2+
3+
CREATE EXTENSION IF NOT EXISTS pg_wait_sampling;
4+
5+
CREATE EXTENSION IF NOT EXISTS pg_stat_monitor;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE DATABASE titanic;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Connect to titanic database
2+
\c titanic;
3+
4+
-- Import the titanic.sql file that was downloaded during Docker build
5+
\i /docker-entrypoint-initdb.d/titanic.sql;
6+
7+
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO dbuser;
8+
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO dbuser;
9+
10+
-- Analyze tables for better query planning
11+
ANALYZE VERBOSE;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
FROM postgres:17.0
2+
3+
# Dependencies
4+
RUN apt-get update && apt-get install -y \
5+
build-essential \
6+
git \
7+
wget \
8+
postgresql-server-dev-17 \
9+
&& rm -rf /var/lib/apt/lists/*
10+
11+
# Postgres Docker Images copy contents of postgresql.conf.sample to postgresql.conf during initialization
12+
# COPY custom.conf /usr/share/postgresql/postgresql.conf.sample -- DO NOT USE
13+
RUN echo "shared_preload_libraries = 'pg_stat_statements,pg_wait_sampling,pg_stat_monitor'" >> /usr/share/postgresql/postgresql.conf.sample
14+
RUN echo "pg_stat_statements.track = all" >> /usr/share/postgresql/postgresql.conf.sample
15+
RUN echo "pg_stat_statements.save = on" >> /usr/share/postgresql/postgresql.conf.sample
16+
RUN echo "pg_stat_monitor.pgsm_enable_query_plan = on" >> /usr/share/postgresql/postgresql.conf.sample
17+
18+
# Install pg_wait_sampling
19+
RUN git clone https://github.com/postgrespro/pg_wait_sampling.git \
20+
&& cd pg_wait_sampling \
21+
&& make USE_PGXS=1 \
22+
&& make USE_PGXS=1 install \
23+
&& cd .. \
24+
&& rm -rf pg_wait_sampling
25+
26+
# Install pg_stat_monitor
27+
RUN git clone https://github.com/percona/pg_stat_monitor.git \
28+
&& cd pg_stat_monitor \
29+
&& make USE_PGXS=1 \
30+
&& make USE_PGXS=1 install \
31+
&& cd .. \
32+
&& rm -rf pg_stat_monitor
33+
34+
# Download the titanic database
35+
RUN wget https://raw.githubusercontent.com/neondatabase/postgres-sample-dbs/main/titanic.sql -P /docker-entrypoint-initdb.d/
36+
37+
# Enable the extensions and setup the titanic database
38+
COPY 01-init-extensions.sql /docker-entrypoint-initdb.d/01-init-extensions.sql
39+
COPY 02-create-database.sql /docker-entrypoint-initdb.d/02-create-database.sql
40+
COPY 03-import-data.sql /docker-entrypoint-initdb.d/03-import-data.sql
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
2+
3+
CREATE EXTENSION IF NOT EXISTS pg_wait_sampling;
4+
5+
CREATE EXTENSION IF NOT EXISTS pg_stat_monitor;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE DATABASE titanic;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Connect to titanic database
2+
\c titanic;
3+
4+
-- Import the titanic.sql file that was downloaded during Docker build
5+
\i /docker-entrypoint-initdb.d/titanic.sql;
6+
7+
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO dbuser;
8+
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO dbuser;
9+
10+
-- Analyze tables for better query planning
11+
ANALYZE VERBOSE;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
FROM postgres:12
2+
3+
# Dependencies
4+
RUN apt-get update && apt-get install -y \
5+
build-essential \
6+
git \
7+
wget \
8+
postgresql-server-dev-12 \
9+
&& rm -rf /var/lib/apt/lists/*
10+
11+
# Postgres Docker Images copy contents of postgresql.conf.sample to postgresql.conf during initialization
12+
# COPY custom.conf /usr/share/postgresql/postgresql.conf.sample -- DO NOT USE
13+
RUN echo "shared_preload_libraries = 'pg_stat_statements,pg_wait_sampling,pg_stat_monitor'" >> /usr/share/postgresql/postgresql.conf.sample
14+
RUN echo "pg_stat_statements.track = all" >> /usr/share/postgresql/postgresql.conf.sample
15+
RUN echo "pg_stat_statements.save = on" >> /usr/share/postgresql/postgresql.conf.sample
16+
RUN echo "pg_stat_monitor.pgsm_enable_query_plan = on" >> /usr/share/postgresql/postgresql.conf.sample
17+
18+
# Install pg_wait_sampling
19+
RUN git clone https://github.com/postgrespro/pg_wait_sampling.git \
20+
&& cd pg_wait_sampling \
21+
&& make USE_PGXS=1 \
22+
&& make USE_PGXS=1 install \
23+
&& cd .. \
24+
&& rm -rf pg_wait_sampling
25+
26+
# Install pg_stat_monitor
27+
RUN git clone https://github.com/percona/pg_stat_monitor.git \
28+
&& cd pg_stat_monitor \
29+
&& make USE_PGXS=1 \
30+
&& make USE_PGXS=1 install \
31+
&& cd .. \
32+
&& rm -rf pg_stat_monitor
33+
34+
# Download the titanic database
35+
RUN wget https://raw.githubusercontent.com/neondatabase/postgres-sample-dbs/main/titanic.sql -P /docker-entrypoint-initdb.d/
36+
37+
RUN echo ${PWD} && ls -lR
38+
39+
# Enable the extensions and setup the titanic database
40+
COPY 01-init-extensions.sql /docker-entrypoint-initdb.d/01-init-extensions.sql
41+
COPY 02-create-database.sql /docker-entrypoint-initdb.d/02-create-database.sql
42+
COPY 03-import-data.sql /docker-entrypoint-initdb.d/03-import-data.sql

0 commit comments

Comments
 (0)