Skip to content

Commit

Permalink
Merge pull request #45 from sjyothi54/integration_testing
Browse files Browse the repository at this point in the history
Integration testing setup for postgres
  • Loading branch information
rahulreddy15 authored Jan 16, 2025
2 parents 8509bcd + 870a0e2 commit 4714ac5
Show file tree
Hide file tree
Showing 20 changed files with 1,548 additions and 71 deletions.
14 changes: 11 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@ test:

integration-test:
@echo "=== $(INTEGRATION) === [ test ]: running integration tests..."
@docker compose -f tests/docker-compose.yml pull
@go test -v -tags=integration -count 1 ./tests/. || (ret=$$?; docker compose -f tests/docker-compose.yml down && exit $$ret)
@docker compose -f tests/docker-compose.yml down
@docker compose -f tests/docker-compose.yml up -d
@sleep 10
@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)
@docker compose -f tests/docker-compose.yml down -v
@echo "=== $(INTEGRATION) === [ test ]: running integration tests for query performance monitoring..."
@echo "Starting containers for performance tests..."
@docker compose -f tests/docker-compose-performance.yml up -d
@sleep 30
@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)
@echo "Stopping performance test containers..."
@docker compose -f tests/docker-compose-performance.yml down -v

install: compile
@echo "=== $(INTEGRATION) === [ install ]: installing bin/$(BINARY_NAME)..."
Expand Down
70 changes: 70 additions & 0 deletions tests/docker-compose-performance.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
services:

postgres12:
build:
context: ./perf-testing/oldest_supported/
dockerfile: Dockerfile
container_name: "postgresql-perf-oldest"
restart: always
environment:
- POSTGRES_USER=dbuser
- POSTGRES_PASSWORD=dbpassword
- POSTGRES_DB=demo
volumes:
- postgres12:/var/lib/postgresql/data
ports:
- "6432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5

postgresql-latest:
build:
context: ./perf-testing/oldest_supported/
dockerfile: Dockerfile
restart: always
container_name: "postgresql-perf-latest"
environment:
- POSTGRES_USER=dbuser
- POSTGRES_PASSWORD=dbpassword
- POSTGRES_DB=demo
volumes:
- pgdata_latest:/var/lib/postgresql/data
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5

postgres-without-extensions:
image: postgres:17.0
restart: always
container_name: "postgresql-noext"
environment:
- POSTGRES_USER=dbuser
- POSTGRES_PASSWORD=dbpassword
- POSTGRES_DB=demo
volumes:
- pgdata_noext:/var/lib/postgresql/data
ports:
- "7432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5

nri-postgresql:
container_name: nri_postgresql
build:
context: ../
dockerfile: tests/perf-testing/integration/Dockerfile

volumes:
pgdata_latest:
postgres12:
pgdata_noext:
15 changes: 6 additions & 9 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ services:
postgres-9-6:
image: postgres:9.6
restart: always
container_name: postgres-9-6
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: example
Expand All @@ -10,18 +11,14 @@ services:
postgres-latest-supported:
image: postgres:17.0
restart: always
container_name: postgres-latest-supported
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: example
POSTGRES_DB: demo

nri-postgresql:
image: golang:1.23.4-bookworm
container_name: nri_postgresql
working_dir: /code
depends_on:
- postgres-9-6
- postgres-latest-supported
volumes:
- ../:/code
entrypoint: go run /code/src/main.go
container_name: nri-postgresql
build:
context: ../
dockerfile: tests/perf-testing/integration/Dockerfile
9 changes: 9 additions & 0 deletions tests/perf-testing/integration/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM golang:1.23.4-bookworm as builder
ARG CGO_ENABLED=0
WORKDIR /go/src/github.com/newrelic/nri-postgresql
COPY . .
RUN make clean compile

FROM alpine:latest
COPY --from=builder /go/src/github.com/newrelic/nri-postgresql/bin /
CMD ["sleep", "1h"]
5 changes: 5 additions & 0 deletions tests/perf-testing/latest_supported/01-init-extensions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;

CREATE EXTENSION IF NOT EXISTS pg_wait_sampling;

CREATE EXTENSION IF NOT EXISTS pg_stat_monitor;
1 change: 1 addition & 0 deletions tests/perf-testing/latest_supported/02-create-database.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE DATABASE titanic;
11 changes: 11 additions & 0 deletions tests/perf-testing/latest_supported/03-import-data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Connect to titanic database
\c titanic;

-- Import the titanic.sql file that was downloaded during Docker build
\i /docker-entrypoint-initdb.d/titanic.sql;

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO dbuser;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO dbuser;

-- Analyze tables for better query planning
ANALYZE VERBOSE;
40 changes: 40 additions & 0 deletions tests/perf-testing/latest_supported/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
FROM postgres:17.0

# Dependencies
RUN apt-get update && apt-get install -y \
build-essential \
git \
wget \
postgresql-server-dev-17 \
&& rm -rf /var/lib/apt/lists/*

# Postgres Docker Images copy contents of postgresql.conf.sample to postgresql.conf during initialization
# COPY custom.conf /usr/share/postgresql/postgresql.conf.sample -- DO NOT USE
RUN echo "shared_preload_libraries = 'pg_stat_statements,pg_wait_sampling,pg_stat_monitor'" >> /usr/share/postgresql/postgresql.conf.sample
RUN echo "pg_stat_statements.track = all" >> /usr/share/postgresql/postgresql.conf.sample
RUN echo "pg_stat_statements.save = on" >> /usr/share/postgresql/postgresql.conf.sample
RUN echo "pg_stat_monitor.pgsm_enable_query_plan = on" >> /usr/share/postgresql/postgresql.conf.sample

# Install pg_wait_sampling
RUN git clone https://github.com/postgrespro/pg_wait_sampling.git \
&& cd pg_wait_sampling \
&& make USE_PGXS=1 \
&& make USE_PGXS=1 install \
&& cd .. \
&& rm -rf pg_wait_sampling

# Install pg_stat_monitor
RUN git clone https://github.com/percona/pg_stat_monitor.git \
&& cd pg_stat_monitor \
&& make USE_PGXS=1 \
&& make USE_PGXS=1 install \
&& cd .. \
&& rm -rf pg_stat_monitor

# Download the titanic database
RUN wget https://raw.githubusercontent.com/neondatabase/postgres-sample-dbs/main/titanic.sql -P /docker-entrypoint-initdb.d/

# Enable the extensions and setup the titanic database
COPY 01-init-extensions.sql /docker-entrypoint-initdb.d/01-init-extensions.sql
COPY 02-create-database.sql /docker-entrypoint-initdb.d/02-create-database.sql
COPY 03-import-data.sql /docker-entrypoint-initdb.d/03-import-data.sql
5 changes: 5 additions & 0 deletions tests/perf-testing/oldest_supported/01-init-extensions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;

CREATE EXTENSION IF NOT EXISTS pg_wait_sampling;

CREATE EXTENSION IF NOT EXISTS pg_stat_monitor;
1 change: 1 addition & 0 deletions tests/perf-testing/oldest_supported/02-create-database.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE DATABASE titanic;
11 changes: 11 additions & 0 deletions tests/perf-testing/oldest_supported/03-import-data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Connect to titanic database
\c titanic;

-- Import the titanic.sql file that was downloaded during Docker build
\i /docker-entrypoint-initdb.d/titanic.sql;

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO dbuser;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO dbuser;

-- Analyze tables for better query planning
ANALYZE VERBOSE;
42 changes: 42 additions & 0 deletions tests/perf-testing/oldest_supported/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FROM postgres:12

# Dependencies
RUN apt-get update && apt-get install -y \
build-essential \
git \
wget \
postgresql-server-dev-12 \
&& rm -rf /var/lib/apt/lists/*

# Postgres Docker Images copy contents of postgresql.conf.sample to postgresql.conf during initialization
# COPY custom.conf /usr/share/postgresql/postgresql.conf.sample -- DO NOT USE
RUN echo "shared_preload_libraries = 'pg_stat_statements,pg_wait_sampling,pg_stat_monitor'" >> /usr/share/postgresql/postgresql.conf.sample
RUN echo "pg_stat_statements.track = all" >> /usr/share/postgresql/postgresql.conf.sample
RUN echo "pg_stat_statements.save = on" >> /usr/share/postgresql/postgresql.conf.sample
RUN echo "pg_stat_monitor.pgsm_enable_query_plan = on" >> /usr/share/postgresql/postgresql.conf.sample

# Install pg_wait_sampling
RUN git clone https://github.com/postgrespro/pg_wait_sampling.git \
&& cd pg_wait_sampling \
&& make USE_PGXS=1 \
&& make USE_PGXS=1 install \
&& cd .. \
&& rm -rf pg_wait_sampling

# Install pg_stat_monitor
RUN git clone https://github.com/percona/pg_stat_monitor.git \
&& cd pg_stat_monitor \
&& make USE_PGXS=1 \
&& make USE_PGXS=1 install \
&& cd .. \
&& rm -rf pg_stat_monitor

# Download the titanic database
RUN wget https://raw.githubusercontent.com/neondatabase/postgres-sample-dbs/main/titanic.sql -P /docker-entrypoint-initdb.d/

RUN echo ${PWD} && ls -lR

# Enable the extensions and setup the titanic database
COPY 01-init-extensions.sql /docker-entrypoint-initdb.d/01-init-extensions.sql
COPY 02-create-database.sql /docker-entrypoint-initdb.d/02-create-database.sql
COPY 03-import-data.sql /docker-entrypoint-initdb.d/03-import-data.sql
Loading

0 comments on commit 4714ac5

Please sign in to comment.