From 82350324807e704edbd59dd7b5cfedaee0d07ebd Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Sat, 29 Aug 2026 21:43:18 +0200 Subject: [PATCH 1/5] Same PG container in all tests --- broker/cmd/archive/main_test.go | 2 +- broker/cmd/broker/main_test.go | 2 +- broker/test/api/api-handler_test.go | 2 +- broker/test/catalog/holdings_test.go | 2 +- broker/test/client/client_test.go | 2 +- broker/test/events/eventbus_test.go | 2 +- broker/test/handler/iso18626-handler_test.go | 2 +- broker/test/patron_request/api/api-handler_test.go | 2 +- broker/test/patron_request/db/prrepo_test.go | 2 +- broker/test/pullslip/api/api_handler_test.go | 2 +- broker/test/scheduler/db/schedrepo_test.go | 2 +- broker/test/scheduler/service/scheduler_test.go | 2 +- broker/test/service/e2e_test.go | 2 +- broker/test/utils/utils.go | 2 +- directory/test/00_integration_test.go | 4 ++-- 15 files changed, 16 insertions(+), 16 deletions(-) diff --git a/broker/cmd/archive/main_test.go b/broker/cmd/archive/main_test.go index d42f9b85d..ca376892e 100644 --- a/broker/cmd/archive/main_test.go +++ b/broker/cmd/archive/main_test.go @@ -19,7 +19,7 @@ func TestMain(m *testing.M) { ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres", + pgContainer, err := postgres.Run(ctx, "postgres:16", postgres.WithDatabase("crosslink"), postgres.WithUsername("crosslink"), postgres.WithPassword("crosslink"), diff --git a/broker/cmd/broker/main_test.go b/broker/cmd/broker/main_test.go index 2ed67e84e..70f388a3b 100644 --- a/broker/cmd/broker/main_test.go +++ b/broker/cmd/broker/main_test.go @@ -25,7 +25,7 @@ func TestMain(m *testing.M) { ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres", + pgContainer, err := postgres.Run(ctx, "postgres:16", postgres.WithDatabase("crosslink"), postgres.WithUsername("crosslink"), postgres.WithPassword("crosslink"), diff --git a/broker/test/api/api-handler_test.go b/broker/test/api/api-handler_test.go index b5a8dcc35..845bc5018 100644 --- a/broker/test/api/api-handler_test.go +++ b/broker/test/api/api-handler_test.go @@ -52,7 +52,7 @@ func TestMain(m *testing.M) { ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres", + pgContainer, err := postgres.Run(ctx, "postgres:16", postgres.WithDatabase("crosslink"), postgres.WithUsername("crosslink"), postgres.WithPassword("crosslink"), diff --git a/broker/test/catalog/holdings_test.go b/broker/test/catalog/holdings_test.go index 1d1bac8f8..a11201994 100644 --- a/broker/test/catalog/holdings_test.go +++ b/broker/test/catalog/holdings_test.go @@ -41,7 +41,7 @@ func TestMain(m *testing.M) { ill_db.PeerRefreshInterval = 0 //force refresh for every test ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres", + pgContainer, err := postgres.Run(ctx, "postgres:16", postgres.WithDatabase("crosslink"), postgres.WithUsername("crosslink"), postgres.WithPassword("crosslink"), diff --git a/broker/test/client/client_test.go b/broker/test/client/client_test.go index 407d03e0c..363b7b1ac 100644 --- a/broker/test/client/client_test.go +++ b/broker/test/client/client_test.go @@ -42,7 +42,7 @@ func TestMain(m *testing.M) { ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres", + pgContainer, err := postgres.Run(ctx, "postgres:16", postgres.WithDatabase("crosslink"), postgres.WithUsername("crosslink"), postgres.WithPassword("crosslink"), diff --git a/broker/test/events/eventbus_test.go b/broker/test/events/eventbus_test.go index 76472658c..e23a9094b 100644 --- a/broker/test/events/eventbus_test.go +++ b/broker/test/events/eventbus_test.go @@ -36,7 +36,7 @@ func TestMain(m *testing.M) { ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres", + pgContainer, err := postgres.Run(ctx, "postgres:16", postgres.WithDatabase("crosslink"), postgres.WithUsername("crosslink"), postgres.WithPassword("crosslink"), diff --git a/broker/test/handler/iso18626-handler_test.go b/broker/test/handler/iso18626-handler_test.go index 987ce274a..bd1a49fe1 100644 --- a/broker/test/handler/iso18626-handler_test.go +++ b/broker/test/handler/iso18626-handler_test.go @@ -49,7 +49,7 @@ var illRepo ill_db.IllRepo func TestMain(m *testing.M) { ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres", + pgContainer, err := postgres.Run(ctx, "postgres:16", postgres.WithDatabase("crosslink"), postgres.WithUsername("crosslink"), postgres.WithPassword("crosslink"), diff --git a/broker/test/patron_request/api/api-handler_test.go b/broker/test/patron_request/api/api-handler_test.go index 22bf16f72..015809a0c 100644 --- a/broker/test/patron_request/api/api-handler_test.go +++ b/broker/test/patron_request/api/api-handler_test.go @@ -49,7 +49,7 @@ func TestMain(m *testing.M) { app.DB_PROVISION = true app.DB_EXPLAIN_ANALYZE = false - pgContainer, err := postgres.Run(ctx, "postgres", + pgContainer, err := postgres.Run(ctx, "postgres:16", postgres.WithDatabase("crosslink"), postgres.WithUsername("crosslink"), postgres.WithPassword("crosslink"), diff --git a/broker/test/patron_request/db/prrepo_test.go b/broker/test/patron_request/db/prrepo_test.go index b6b8ce3b2..e926dc509 100644 --- a/broker/test/patron_request/db/prrepo_test.go +++ b/broker/test/patron_request/db/prrepo_test.go @@ -36,7 +36,7 @@ func TestMain(m *testing.M) { ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres", + pgContainer, err := postgres.Run(ctx, "postgres:16", postgres.WithDatabase("crosslink"), postgres.WithUsername("crosslink"), postgres.WithPassword("crosslink"), diff --git a/broker/test/pullslip/api/api_handler_test.go b/broker/test/pullslip/api/api_handler_test.go index 86a8d0c6e..b985e0e5a 100644 --- a/broker/test/pullslip/api/api_handler_test.go +++ b/broker/test/pullslip/api/api_handler_test.go @@ -37,7 +37,7 @@ func TestMain(m *testing.M) { ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres", + pgContainer, err := postgres.Run(ctx, "postgres:16", postgres.WithDatabase("crosslink"), postgres.WithUsername("crosslink"), postgres.WithPassword("crosslink"), diff --git a/broker/test/scheduler/db/schedrepo_test.go b/broker/test/scheduler/db/schedrepo_test.go index 34bf0c766..f4ef6aa3e 100644 --- a/broker/test/scheduler/db/schedrepo_test.go +++ b/broker/test/scheduler/db/schedrepo_test.go @@ -29,7 +29,7 @@ var appCtx = common.CreateExtCtxWithArgs(context.Background(), nil) func TestMain(m *testing.M) { ctx := context.Background() - pgContainer, err := postgres.Run(ctx, "postgres", + pgContainer, err := postgres.Run(ctx, "postgres:16", postgres.WithDatabase("crosslink"), postgres.WithUsername("crosslink"), postgres.WithPassword("crosslink"), diff --git a/broker/test/scheduler/service/scheduler_test.go b/broker/test/scheduler/service/scheduler_test.go index 6e991d6ac..73d12cd56 100644 --- a/broker/test/scheduler/service/scheduler_test.go +++ b/broker/test/scheduler/service/scheduler_test.go @@ -30,7 +30,7 @@ var appCtx = common.CreateExtCtxWithArgs(context.Background(), nil) func TestMain(m *testing.M) { ctx := context.Background() - pgContainer, err := postgres.Run(ctx, "postgres", + pgContainer, err := postgres.Run(ctx, "postgres:16", postgres.WithDatabase("crosslink"), postgres.WithUsername("crosslink"), postgres.WithPassword("crosslink"), diff --git a/broker/test/service/e2e_test.go b/broker/test/service/e2e_test.go index 2b423817f..a55cbdc03 100644 --- a/broker/test/service/e2e_test.go +++ b/broker/test/service/e2e_test.go @@ -36,7 +36,7 @@ func TestMain(m *testing.M) { ill_db.PeerRefreshInterval = 0 //force refresh for every test ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres", + pgContainer, err := postgres.Run(ctx, "postgres:16", postgres.WithDatabase("crosslink"), postgres.WithUsername("crosslink"), postgres.WithPassword("crosslink"), diff --git a/broker/test/utils/utils.go b/broker/test/utils/utils.go index 40b1e226c..6a1aaa242 100644 --- a/broker/test/utils/utils.go +++ b/broker/test/utils/utils.go @@ -86,7 +86,7 @@ func WaitForServiceUp(port int) { func StartPGContainer() (context.Context, *postgres.PostgresContainer, string, error) { ctx := context.Background() - pgContainer, err := postgres.Run(ctx, "postgres", + pgContainer, err := postgres.Run(ctx, "postgres:16", postgres.WithDatabase("crosslink"), postgres.WithUsername("crosslink"), postgres.WithPassword("crosslink"), diff --git a/directory/test/00_integration_test.go b/directory/test/00_integration_test.go index 1d4026ecd..103c83be4 100644 --- a/directory/test/00_integration_test.go +++ b/directory/test/00_integration_test.go @@ -58,13 +58,13 @@ func jsonReq(t *testing.T, method string, endpoint string, bodyStr string, addlH func TestMain(m *testing.M) { ctx := context.Background() - pgContainer, err := postgres.Run(ctx, "postgres", + pgContainer, err := postgres.Run(ctx, "postgres:16", postgres.WithDatabase("directory_test"), postgres.WithUsername("directory"), postgres.WithPassword("directory"), testcontainers.WithWaitStrategy( wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(5*time.Second)), + WithOccurrence(2).WithStartupTimeout(30*time.Second)), ) if err != nil { panic(fmt.Sprintf("failed to start db container: %s", err)) From 0bf0499dee868880bc17813740163a27ff0ee227 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 1 Sep 2026 11:02:24 +0200 Subject: [PATCH 2/5] testutil postgres --- broker/cmd/archive/main_test.go | 14 ++-------- broker/cmd/broker/main_test.go | 13 ++------- broker/go.mod | 2 +- broker/ill_db/illrepo_test.go | 8 ++++-- broker/test/api/api-handler_test.go | 13 ++------- broker/test/catalog/holdings_test.go | 14 ++-------- broker/test/client/client_test.go | 14 ++-------- broker/test/events/eventbus_test.go | 17 +++--------- broker/test/handler/iso18626-handler_test.go | 13 ++------- .../patron_request/api/api-handler_test.go | 13 ++------- broker/test/patron_request/db/prrepo_test.go | 13 ++------- broker/test/pullslip/api/api_handler_test.go | 13 ++------- broker/test/scheduler/db/schedrepo_test.go | 13 ++------- .../test/scheduler/service/scheduler_test.go | 13 ++------- broker/test/service/e2e_test.go | 14 ++-------- broker/test/utils/utils.go | 23 ---------------- directory/go.mod | 7 +++-- directory/test/00_integration_test.go | 13 ++------- testutil/go.mod | 1 + testutil/go.sum | 18 +++++++++++++ testutil/postgres.go | 27 +++++++++++++++++++ 21 files changed, 87 insertions(+), 189 deletions(-) create mode 100644 testutil/postgres.go diff --git a/broker/cmd/archive/main_test.go b/broker/cmd/archive/main_test.go index ca376892e..3fafb0283 100644 --- a/broker/cmd/archive/main_test.go +++ b/broker/cmd/archive/main_test.go @@ -4,29 +4,19 @@ import ( "context" "os" "testing" - "time" "github.com/indexdata/crosslink/broker/app" test "github.com/indexdata/crosslink/broker/test/utils" + "github.com/indexdata/crosslink/testutil" _ "github.com/lib/pq" // PostgreSQL driver "github.com/stretchr/testify/assert" - "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/modules/postgres" - "github.com/testcontainers/testcontainers-go/wait" ) func TestMain(m *testing.M) { ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres:16", - postgres.WithDatabase("crosslink"), - postgres.WithUsername("crosslink"), - postgres.WithPassword("crosslink"), - testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(30*time.Second)), - ) + pgContainer, err := testutil.RunPostgres(ctx) test.Expect(err, "failed to start db container") connStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable") diff --git a/broker/cmd/broker/main_test.go b/broker/cmd/broker/main_test.go index 70f388a3b..480947b59 100644 --- a/broker/cmd/broker/main_test.go +++ b/broker/cmd/broker/main_test.go @@ -13,26 +13,17 @@ import ( "github.com/indexdata/crosslink/broker/app" test "github.com/indexdata/crosslink/broker/test/utils" + "github.com/indexdata/crosslink/testutil" "github.com/indexdata/go-utils/utils" _ "github.com/lib/pq" // PostgreSQL driver "github.com/stretchr/testify/assert" - "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/modules/postgres" - "github.com/testcontainers/testcontainers-go/wait" ) func TestMain(m *testing.M) { ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres:16", - postgres.WithDatabase("crosslink"), - postgres.WithUsername("crosslink"), - postgres.WithPassword("crosslink"), - testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(30*time.Second)), - ) + pgContainer, err := testutil.RunPostgres(ctx) test.Expect(err, "failed to start db container") connStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable") diff --git a/broker/go.mod b/broker/go.mod index 3fe08b2bb..4b77ec4cd 100644 --- a/broker/go.mod +++ b/broker/go.mod @@ -45,7 +45,6 @@ require ( github.com/stretchr/testify v1.12.1 github.com/teambition/rrule-go v1.8.2 github.com/testcontainers/testcontainers-go v0.44.0 - github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0 ) require ( @@ -126,6 +125,7 @@ require ( github.com/sqlc-dev/doubleclick v1.0.0 // indirect github.com/sqlc-dev/sqlc v1.31.1 // indirect github.com/stretchr/objx v0.5.3 // indirect + github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0 // indirect github.com/tetratelabs/wazero v1.12.0 // indirect github.com/tklauser/go-sysconf v0.4.0 // indirect github.com/tklauser/numcpus v0.12.0 // indirect diff --git a/broker/ill_db/illrepo_test.go b/broker/ill_db/illrepo_test.go index b35fc7f1b..23e3497ab 100644 --- a/broker/ill_db/illrepo_test.go +++ b/broker/ill_db/illrepo_test.go @@ -15,6 +15,7 @@ import ( "github.com/indexdata/crosslink/broker/dbutil" test "github.com/indexdata/crosslink/broker/test/utils" dirapi "github.com/indexdata/crosslink/directory/api" + "github.com/indexdata/crosslink/testutil" "github.com/jackc/pgx/v5/pgtype" "github.com/stretchr/testify/assert" ) @@ -28,9 +29,12 @@ func createDirectoryAdapter(urls ...string) adapter.DirectoryLookupAdapter { } func TestMain(m *testing.M) { - ctx, pgc, connStr, err := test.StartPGContainer() - connStr = connStr + dbutil.SearchPath("crosslink_broker") + ctx := context.Background() + pgc, err := testutil.RunPostgres(ctx) test.Expect(err, "failed to start db container") + connStr, err := pgc.ConnectionString(ctx, "sslmode=disable") + test.Expect(err, "failed to get conn string") + connStr = connStr + dbutil.SearchPath("crosslink_broker") pgIllRepo := new(PgIllRepo) pgIllRepo.Pool, err = dbutil.InitDbPool(connStr) test.Expect(err, "failed to create ill repo") diff --git a/broker/test/api/api-handler_test.go b/broker/test/api/api-handler_test.go index 845bc5018..04b729630 100644 --- a/broker/test/api/api-handler_test.go +++ b/broker/test/api/api-handler_test.go @@ -34,10 +34,8 @@ import ( mocks "github.com/indexdata/crosslink/broker/test/mocks" test "github.com/indexdata/crosslink/broker/test/utils" dirapi "github.com/indexdata/crosslink/directory/api" + "github.com/indexdata/crosslink/testutil" "github.com/indexdata/go-utils/utils" - "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/modules/postgres" - "github.com/testcontainers/testcontainers-go/wait" ) var illRepo ill_db.IllRepo @@ -52,14 +50,7 @@ func TestMain(m *testing.M) { ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres:16", - postgres.WithDatabase("crosslink"), - postgres.WithUsername("crosslink"), - postgres.WithPassword("crosslink"), - testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(30*time.Second)), - ) + pgContainer, err := testutil.RunPostgres(ctx) test.Expect(err, "failed to start db container") connStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable") diff --git a/broker/test/catalog/holdings_test.go b/broker/test/catalog/holdings_test.go index a11201994..4e19f4d23 100644 --- a/broker/test/catalog/holdings_test.go +++ b/broker/test/catalog/holdings_test.go @@ -10,7 +10,6 @@ import ( "strconv" "sync/atomic" "testing" - "time" "github.com/indexdata/crosslink/broker/app" "github.com/indexdata/crosslink/broker/catalog" @@ -20,12 +19,10 @@ import ( apptest "github.com/indexdata/crosslink/broker/test/apputils" test "github.com/indexdata/crosslink/broker/test/utils" dirapi "github.com/indexdata/crosslink/directory/api" + "github.com/indexdata/crosslink/testutil" "github.com/indexdata/go-utils/utils" "github.com/jackc/pgx/v5/pgtype" "github.com/stretchr/testify/assert" - "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/modules/postgres" - "github.com/testcontainers/testcontainers-go/wait" ) var eventBus events.EventBus @@ -41,14 +38,7 @@ func TestMain(m *testing.M) { ill_db.PeerRefreshInterval = 0 //force refresh for every test ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres:16", - postgres.WithDatabase("crosslink"), - postgres.WithUsername("crosslink"), - postgres.WithPassword("crosslink"), - testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(30*time.Second)), - ) + pgContainer, err := testutil.RunPostgres(ctx) test.Expect(err, "failed to start db container") connStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable") diff --git a/broker/test/client/client_test.go b/broker/test/client/client_test.go index 363b7b1ac..8c76c45c0 100644 --- a/broker/test/client/client_test.go +++ b/broker/test/client/client_test.go @@ -11,7 +11,6 @@ import ( "strconv" "strings" "testing" - "time" "github.com/indexdata/crosslink/broker/adapter" "github.com/stretchr/testify/assert" @@ -27,10 +26,8 @@ import ( apptest "github.com/indexdata/crosslink/broker/test/apputils" test "github.com/indexdata/crosslink/broker/test/utils" "github.com/indexdata/crosslink/iso18626" + "github.com/indexdata/crosslink/testutil" "github.com/jackc/pgx/v5/pgtype" - "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/modules/postgres" - "github.com/testcontainers/testcontainers-go/wait" ) var LocalAddress = "" @@ -42,14 +39,7 @@ func TestMain(m *testing.M) { ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres:16", - postgres.WithDatabase("crosslink"), - postgres.WithUsername("crosslink"), - postgres.WithPassword("crosslink"), - testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(30*time.Second)), - ) + pgContainer, err := testutil.RunPostgres(ctx) test.Expect(err, "failed to start db container") connStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable") diff --git a/broker/test/events/eventbus_test.go b/broker/test/events/eventbus_test.go index e23a9094b..d9b77f3ee 100644 --- a/broker/test/events/eventbus_test.go +++ b/broker/test/events/eventbus_test.go @@ -17,15 +17,11 @@ import ( "github.com/indexdata/crosslink/broker/dbutil" "github.com/indexdata/crosslink/broker/events" "github.com/indexdata/crosslink/broker/ill_db" + apptest "github.com/indexdata/crosslink/broker/test/apputils" + test "github.com/indexdata/crosslink/broker/test/utils" "github.com/indexdata/crosslink/testutil" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" - "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/modules/postgres" - "github.com/testcontainers/testcontainers-go/wait" - - apptest "github.com/indexdata/crosslink/broker/test/apputils" - test "github.com/indexdata/crosslink/broker/test/utils" ) var eventBus events.EventBus @@ -36,14 +32,7 @@ func TestMain(m *testing.M) { ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres:16", - postgres.WithDatabase("crosslink"), - postgres.WithUsername("crosslink"), - postgres.WithPassword("crosslink"), - testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(30*time.Second)), - ) + pgContainer, err := testutil.RunPostgres(ctx) test.Expect(err, "failed to start db container") connStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable") diff --git a/broker/test/handler/iso18626-handler_test.go b/broker/test/handler/iso18626-handler_test.go index bd1a49fe1..d050ced34 100644 --- a/broker/test/handler/iso18626-handler_test.go +++ b/broker/test/handler/iso18626-handler_test.go @@ -20,11 +20,9 @@ import ( "github.com/indexdata/crosslink/broker/common" "github.com/indexdata/crosslink/broker/vcs" "github.com/indexdata/crosslink/iso18626" + "github.com/indexdata/crosslink/testutil" "github.com/indexdata/go-utils/utils" "github.com/stretchr/testify/assert" - "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/modules/postgres" - "github.com/testcontainers/testcontainers-go/wait" "github.com/indexdata/crosslink/broker/events" "github.com/indexdata/crosslink/broker/handler" @@ -49,14 +47,7 @@ var illRepo ill_db.IllRepo func TestMain(m *testing.M) { ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres:16", - postgres.WithDatabase("crosslink"), - postgres.WithUsername("crosslink"), - postgres.WithPassword("crosslink"), - testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(30*time.Second)), - ) + pgContainer, err := testutil.RunPostgres(ctx) test.Expect(err, "failed to start db container") connStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable") diff --git a/broker/test/patron_request/api/api-handler_test.go b/broker/test/patron_request/api/api-handler_test.go index 015809a0c..bd3146bec 100644 --- a/broker/test/patron_request/api/api-handler_test.go +++ b/broker/test/patron_request/api/api-handler_test.go @@ -30,12 +30,10 @@ import ( prservice "github.com/indexdata/crosslink/broker/patron_request/service" apptest "github.com/indexdata/crosslink/broker/test/apputils" test "github.com/indexdata/crosslink/broker/test/utils" + "github.com/indexdata/crosslink/testutil" "github.com/indexdata/go-utils/utils" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/modules/postgres" - "github.com/testcontainers/testcontainers-go/wait" ) var basePath = "/patron_requests" @@ -49,14 +47,7 @@ func TestMain(m *testing.M) { app.DB_PROVISION = true app.DB_EXPLAIN_ANALYZE = false - pgContainer, err := postgres.Run(ctx, "postgres:16", - postgres.WithDatabase("crosslink"), - postgres.WithUsername("crosslink"), - postgres.WithPassword("crosslink"), - testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(30*time.Second)), - ) + pgContainer, err := testutil.RunPostgres(ctx) test.Expect(err, "failed to start db container") connStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable") diff --git a/broker/test/patron_request/db/prrepo_test.go b/broker/test/patron_request/db/prrepo_test.go index e926dc509..b51e6081e 100644 --- a/broker/test/patron_request/db/prrepo_test.go +++ b/broker/test/patron_request/db/prrepo_test.go @@ -18,13 +18,11 @@ import ( apptest "github.com/indexdata/crosslink/broker/test/apputils" test "github.com/indexdata/crosslink/broker/test/utils" "github.com/indexdata/crosslink/iso18626" + "github.com/indexdata/crosslink/testutil" "github.com/indexdata/go-utils/utils" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" "github.com/stretchr/testify/assert" - "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/modules/postgres" - "github.com/testcontainers/testcontainers-go/wait" ) var prRepo pr_db.PrRepo @@ -36,14 +34,7 @@ func TestMain(m *testing.M) { ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres:16", - postgres.WithDatabase("crosslink"), - postgres.WithUsername("crosslink"), - postgres.WithPassword("crosslink"), - testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(30*time.Second)), - ) + pgContainer, err := testutil.RunPostgres(ctx) test.Expect(err, "failed to start db container") connStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable") diff --git a/broker/test/pullslip/api/api_handler_test.go b/broker/test/pullslip/api/api_handler_test.go index 95bd8deae..297ce12fc 100644 --- a/broker/test/pullslip/api/api_handler_test.go +++ b/broker/test/pullslip/api/api_handler_test.go @@ -22,12 +22,10 @@ import ( apptest "github.com/indexdata/crosslink/broker/test/apputils" test "github.com/indexdata/crosslink/broker/test/utils" "github.com/indexdata/crosslink/iso18626" + "github.com/indexdata/crosslink/testutil" "github.com/indexdata/go-utils/utils" "github.com/jackc/pgx/v5/pgtype" "github.com/stretchr/testify/assert" - "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/modules/postgres" - "github.com/testcontainers/testcontainers-go/wait" ) var basePath = "/pullslips" @@ -38,14 +36,7 @@ func TestMain(m *testing.M) { ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres:16", - postgres.WithDatabase("crosslink"), - postgres.WithUsername("crosslink"), - postgres.WithPassword("crosslink"), - testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(30*time.Second)), - ) + pgContainer, err := testutil.RunPostgres(ctx) test.Expect(err, "failed to start db container") connStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable") diff --git a/broker/test/scheduler/db/schedrepo_test.go b/broker/test/scheduler/db/schedrepo_test.go index f4ef6aa3e..b83e6d3df 100644 --- a/broker/test/scheduler/db/schedrepo_test.go +++ b/broker/test/scheduler/db/schedrepo_test.go @@ -13,13 +13,11 @@ import ( "github.com/indexdata/crosslink/broker/events" sched_db "github.com/indexdata/crosslink/broker/scheduler/db" test "github.com/indexdata/crosslink/broker/test/utils" + "github.com/indexdata/crosslink/testutil" "github.com/indexdata/go-utils/utils" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" "github.com/stretchr/testify/assert" - "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/modules/postgres" - "github.com/testcontainers/testcontainers-go/wait" ) var schedRepo sched_db.SchedRepo @@ -29,14 +27,7 @@ var appCtx = common.CreateExtCtxWithArgs(context.Background(), nil) func TestMain(m *testing.M) { ctx := context.Background() - pgContainer, err := postgres.Run(ctx, "postgres:16", - postgres.WithDatabase("crosslink"), - postgres.WithUsername("crosslink"), - postgres.WithPassword("crosslink"), - testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(30*time.Second)), - ) + pgContainer, err := testutil.RunPostgres(ctx) test.Expect(err, "failed to start db container") connStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable") diff --git a/broker/test/scheduler/service/scheduler_test.go b/broker/test/scheduler/service/scheduler_test.go index 73d12cd56..459e175cb 100644 --- a/broker/test/scheduler/service/scheduler_test.go +++ b/broker/test/scheduler/service/scheduler_test.go @@ -15,12 +15,10 @@ import ( sched_db "github.com/indexdata/crosslink/broker/scheduler/db" sched_service "github.com/indexdata/crosslink/broker/scheduler/service" test "github.com/indexdata/crosslink/broker/test/utils" + "github.com/indexdata/crosslink/testutil" "github.com/indexdata/go-utils/utils" "github.com/jackc/pgx/v5/pgtype" "github.com/stretchr/testify/assert" - "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/modules/postgres" - "github.com/testcontainers/testcontainers-go/wait" ) var connString string @@ -30,14 +28,7 @@ var appCtx = common.CreateExtCtxWithArgs(context.Background(), nil) func TestMain(m *testing.M) { ctx := context.Background() - pgContainer, err := postgres.Run(ctx, "postgres:16", - postgres.WithDatabase("crosslink"), - postgres.WithUsername("crosslink"), - postgres.WithPassword("crosslink"), - testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(30*time.Second)), - ) + pgContainer, err := testutil.RunPostgres(ctx) test.Expect(err, "failed to start db container") connStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable") diff --git a/broker/test/service/e2e_test.go b/broker/test/service/e2e_test.go index a55cbdc03..a27b21122 100644 --- a/broker/test/service/e2e_test.go +++ b/broker/test/service/e2e_test.go @@ -11,7 +11,6 @@ import ( "strconv" "strings" "testing" - "time" "github.com/indexdata/crosslink/broker/catalog" "github.com/indexdata/crosslink/broker/events" @@ -24,26 +23,17 @@ import ( apptest "github.com/indexdata/crosslink/broker/test/apputils" test "github.com/indexdata/crosslink/broker/test/utils" "github.com/indexdata/crosslink/iso18626" + "github.com/indexdata/crosslink/testutil" "github.com/indexdata/go-utils/utils" "github.com/jackc/pgx/v5/pgtype" "github.com/stretchr/testify/assert" - "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/modules/postgres" - "github.com/testcontainers/testcontainers-go/wait" ) func TestMain(m *testing.M) { ill_db.PeerRefreshInterval = 0 //force refresh for every test ctx := context.Background() app.DB_PROVISION = true - pgContainer, err := postgres.Run(ctx, "postgres:16", - postgres.WithDatabase("crosslink"), - postgres.WithUsername("crosslink"), - postgres.WithPassword("crosslink"), - testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(30*time.Second)), - ) + pgContainer, err := testutil.RunPostgres(ctx) test.Expect(err, "failed to start db container") connStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable") diff --git a/broker/test/utils/utils.go b/broker/test/utils/utils.go index 6a1aaa242..7f904468e 100644 --- a/broker/test/utils/utils.go +++ b/broker/test/utils/utils.go @@ -12,8 +12,6 @@ import ( cerrdefs "github.com/containerd/errdefs" "github.com/jackc/pgx/v5/pgtype" "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/modules/postgres" - "github.com/testcontainers/testcontainers-go/wait" ) func GetNow() pgtype.Timestamp { @@ -84,27 +82,6 @@ func WaitForServiceUp(port int) { } } -func StartPGContainer() (context.Context, *postgres.PostgresContainer, string, error) { - ctx := context.Background() - pgContainer, err := postgres.Run(ctx, "postgres:16", - postgres.WithDatabase("crosslink"), - postgres.WithUsername("crosslink"), - postgres.WithPassword("crosslink"), - testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(30*time.Second)), - ) - if err != nil { - return ctx, pgContainer, "", fmt.Errorf("failed to start db container: %w", err) - } - - connStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable") - if err != nil { - return ctx, pgContainer, "", fmt.Errorf("failed to get conn string: %w", err) - } - return ctx, pgContainer, connStr, nil -} - // TerminatePGContainer stops a Postgres test container. It silently ignores the // conflict error (HTTP 409 "removal already in progress") that arises when // testcontainers' Ryuk reaper races with the explicit Terminate call as the diff --git a/directory/go.mod b/directory/go.mod index f45564076..3b01e6106 100644 --- a/directory/go.mod +++ b/directory/go.mod @@ -8,6 +8,7 @@ require ( github.com/golang-migrate/migrate/v4 v4.19.1 github.com/google/uuid v1.6.0 github.com/indexdata/cql-go v1.0.1 + github.com/indexdata/crosslink/testutil v0.0.0 github.com/jackc/pgx/v5 v5.10.0 github.com/kinbiko/jsonassert v1.2.0 github.com/oapi-codegen/nethttp-middleware v1.2.0 @@ -15,12 +16,12 @@ require ( github.com/oapi-codegen/oapi-codegen/v2 v2.8.0 github.com/oapi-codegen/runtime v1.7.0 github.com/sqlc-dev/sqlc v1.31.1 - github.com/testcontainers/testcontainers-go v0.44.0 - github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0 github.com/veqryn/slog-context v0.9.0 github.com/vgarvardt/pgx-google-uuid/v5 v5.6.0 ) +replace github.com/indexdata/crosslink/testutil => ../testutil + require ( cel.dev/expr v0.25.3 // indirect dario.cat/mergo v1.0.2 // indirect @@ -97,6 +98,8 @@ require ( github.com/spf13/pflag v1.0.10 // indirect github.com/sqlc-dev/doubleclick v1.0.0 // indirect github.com/stretchr/testify v1.12.1 // indirect + github.com/testcontainers/testcontainers-go v0.44.0 // indirect + github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0 // indirect github.com/tetratelabs/wazero v1.12.0 // indirect github.com/tklauser/go-sysconf v0.4.0 // indirect github.com/tklauser/numcpus v0.12.0 // indirect diff --git a/directory/test/00_integration_test.go b/directory/test/00_integration_test.go index 103c83be4..9826e43ae 100644 --- a/directory/test/00_integration_test.go +++ b/directory/test/00_integration_test.go @@ -14,12 +14,10 @@ import ( "time" "github.com/go-testfixtures/testfixtures/v3" + "github.com/indexdata/crosslink/testutil" "github.com/jackc/pgx/v5/pgxpool" _ "github.com/jackc/pgx/v5/stdlib" "github.com/kinbiko/jsonassert" - "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/modules/postgres" - "github.com/testcontainers/testcontainers-go/wait" "github.com/indexdata/crosslink/directory/app" ) @@ -58,14 +56,7 @@ func jsonReq(t *testing.T, method string, endpoint string, bodyStr string, addlH func TestMain(m *testing.M) { ctx := context.Background() - pgContainer, err := postgres.Run(ctx, "postgres:16", - postgres.WithDatabase("directory_test"), - postgres.WithUsername("directory"), - postgres.WithPassword("directory"), - testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(30*time.Second)), - ) + pgContainer, err := testutil.RunPostgres(ctx) if err != nil { panic(fmt.Sprintf("failed to start db container: %s", err)) } diff --git a/testutil/go.mod b/testutil/go.mod index e208144b7..441992e29 100644 --- a/testutil/go.mod +++ b/testutil/go.mod @@ -5,6 +5,7 @@ go 1.27.0 require ( github.com/stretchr/testify v1.12.1 github.com/testcontainers/testcontainers-go v0.44.0 + github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0 ) require ( diff --git a/testutil/go.sum b/testutil/go.sum index 426b7fa68..54a61cf20 100644 --- a/testutil/go.sum +++ b/testutil/go.sum @@ -44,12 +44,24 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.9.2 h1:3ZhOzMWnR4yJ+RW1XImIPsD1aNSz4T4fyP7zlQb56hw= +github.com/jackc/pgx/v5 v5.9.2/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/klauspost/compress v1.19.2 h1:hMRETovs/pu/dVWN7zIT1PGG8t509MwT6bO7XSi26R8= github.com/klauspost/compress v1.19.2/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lufia/plan9stats v0.0.0-20260802145828-341c2f0c90b5 h1:eveIIGn4BGM3qknO74omf6HYr30/exH+eVUTuAgwjZ0= github.com/lufia/plan9stats v0.0.0-20260802145828-341c2f0c90b5/go.mod h1:autxFIvghDt3jPTLoqZ9OZ7s9qTGNAWmYCjVFWPX/zg= github.com/magiconair/properties v1.18.11 h1:j5ozYZl0zCjG7ahMDH0GWIobOvvUzT0BdAguG0ViKy0= github.com/magiconair/properties v1.18.11/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/mdelapenya/tlscert v0.2.0 h1:7H81W6Z/4weDvZBNOfQte5GpIMo0lGYEeWbkGp5LJHI= +github.com/mdelapenya/tlscert v0.2.0/go.mod h1:O4njj3ELLnJjGdkN7M/vIVCpZ+Cf0L6muqOG4tLSl8o= github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo= github.com/moby/go-archive v0.3.3 h1:OxxR9paxsluYi+zDUEXTTaIxtkK3viymW+Ka7vRhhME= @@ -88,6 +100,8 @@ github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWD github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= github.com/testcontainers/testcontainers-go v0.44.0 h1:/Fwh6HY1mIikhnm9e7HwoxGycx0lzRAE0f5VQpjFxzI= github.com/testcontainers/testcontainers-go v0.44.0/go.mod h1:IcnwQrYTO86xHXu5bvMaBH7ATlbS3Qn1M1QWW3c66rE= +github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0 h1:8fdv/9y3JMxjQ+ULAcOG8RtgeNu5t9XF9LolSXDuTwM= +github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0/go.mod h1:CFr2LncGYokw+OKjXcr8ARCKG1SaC2UEnGxFBovE86g= github.com/tklauser/go-sysconf v0.4.0 h1:7H0uAN+7RkwWRaxhYXDLqa5V3LPrJeV8wmD9dRUgPQU= github.com/tklauser/go-sysconf v0.4.0/go.mod h1:8mTNWyog7H+MpKijp4VmKJAd2bbYQ2zuUwkYRbUArPI= github.com/tklauser/numcpus v0.12.0 h1:NR85qdvHA9pFse3x3weVZ0r0ST8R6l5RHbZrlRaqob4= @@ -112,6 +126,8 @@ go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= golang.org/x/crypto v0.55.0 h1:+KWHjbgOaAQ66dh/YlkZKHlz9ZUlq61AFirAR9ntP8M= golang.org/x/crypto v0.55.0/go.mod h1:uq0V9dE/fzQuJtbnL+2EhWOE63vo164FY8xqEnV9xis= +golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek= +golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -120,6 +136,8 @@ golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs= golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= golang.org/x/term v0.45.0 h1:NwWyBmoJCbfTHpxrWoZ9C6/VxOf7ic219I8xZZFdrf0= golang.org/x/term v0.45.0/go.mod h1:9aqxs0blBcrm/n0L9QW0aRVD+ktan8ssZromtqJC43w= +golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8= +golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M= gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q= gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA= pgregory.net/rapid v1.2.0 h1:keKAYRcjm+e1F0oAuU5F5+YPAWcyxNNRK2wud503Gnk= diff --git a/testutil/postgres.go b/testutil/postgres.go new file mode 100644 index 000000000..d64dc9da5 --- /dev/null +++ b/testutil/postgres.go @@ -0,0 +1,27 @@ +package testutil + +import ( + "context" + "time" + + "github.com/testcontainers/testcontainers-go" + "github.com/testcontainers/testcontainers-go/modules/postgres" + "github.com/testcontainers/testcontainers-go/wait" +) + +const postgresImage = "postgres:16" + +// RunPostgres starts a PostgreSQL container with the settings shared by the +// Crosslink integration tests. +func RunPostgres(ctx context.Context) (*postgres.PostgresContainer, error) { + return postgres.Run(ctx, postgresImage, + postgres.WithDatabase("crosslink"), + postgres.WithUsername("crosslink"), + postgres.WithPassword("crosslink"), + testcontainers.WithWaitStrategy( + wait.ForLog("database system is ready to accept connections"). + WithOccurrence(2). + WithStartupTimeout(30*time.Second), + ), + ) +} From 5a8e2422a52533c1e1788694bac949c6808de101 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 1 Sep 2026 11:07:55 +0200 Subject: [PATCH 3/5] go mod tidy (to fix lint check) --- illmock/go.mod | 16 +++++++--------- illmock/go.sum | 14 ++++++++++++++ zoom/go.mod | 1 + zoom/go.sum | 18 ++++++++++++++++++ 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/illmock/go.mod b/illmock/go.mod index 06ddfa6d8..810ef1a08 100644 --- a/illmock/go.mod +++ b/illmock/go.mod @@ -2,15 +2,6 @@ module github.com/indexdata/crosslink/illmock go 1.27.0 -require ( - github.com/indexdata/crosslink/httpclient v0.0.0 - github.com/indexdata/crosslink/iso18626 v0.0.0 - github.com/indexdata/crosslink/marcxml v0.0.0 - github.com/indexdata/crosslink/ncip v0.0.0 - github.com/indexdata/crosslink/sru v0.0.0 - github.com/indexdata/crosslink/testutil v0.0.0 -) - replace ( github.com/indexdata/crosslink/directory => ../directory github.com/indexdata/crosslink/httpclient => ../httpclient @@ -25,6 +16,12 @@ require ( github.com/getkin/kin-openapi v0.147.0 github.com/google/uuid v1.6.0 github.com/indexdata/cql-go v1.0.1 + github.com/indexdata/crosslink/httpclient v0.0.0 + github.com/indexdata/crosslink/iso18626 v0.0.0 + github.com/indexdata/crosslink/marcxml v0.0.0 + github.com/indexdata/crosslink/ncip v0.0.0 + github.com/indexdata/crosslink/sru v0.0.0 + github.com/indexdata/crosslink/testutil v0.0.0 github.com/indexdata/go-utils v1.0.0 github.com/oapi-codegen/nethttp-middleware v1.2.0 github.com/oapi-codegen/nullable v1.2.0 @@ -79,6 +76,7 @@ require ( github.com/speakeasy-api/jsonpath v0.6.3 // indirect github.com/speakeasy-api/openapi v1.25.0 // indirect github.com/testcontainers/testcontainers-go v0.44.0 // indirect + github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0 // indirect github.com/tklauser/go-sysconf v0.4.0 // indirect github.com/tklauser/numcpus v0.12.0 // indirect github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect diff --git a/illmock/go.sum b/illmock/go.sum index 62bd0f64c..a0e66cc10 100644 --- a/illmock/go.sum +++ b/illmock/go.sum @@ -72,6 +72,14 @@ github.com/indexdata/cql-go v1.0.1 h1:euscRPTVpskLvhJOdqUoiIpNgO8wFQ8eYgR068r+D5 github.com/indexdata/cql-go v1.0.1/go.mod h1:zmSHcE8JyK94EWZrV7VyjLr2QfRoj+EeEOttl9wm64U= github.com/indexdata/go-utils v1.0.0 h1:Vs1JGMETCLXBWxbDtQe6mpNPBK5kotU+4ZKUIA2U1QA= github.com/indexdata/go-utils v1.0.0/go.mod h1:0sW6Szxv8GNU3LBtK6mgBKDEUnlovPfghiG9xi+i0R8= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.9.2 h1:3ZhOzMWnR4yJ+RW1XImIPsD1aNSz4T4fyP7zlQb56hw= +github.com/jackc/pgx/v5 v5.9.2/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= github.com/klauspost/compress v1.19.2 h1:hMRETovs/pu/dVWN7zIT1PGG8t509MwT6bO7XSi26R8= github.com/klauspost/compress v1.19.2/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= @@ -82,10 +90,14 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lufia/plan9stats v0.0.0-20260802145828-341c2f0c90b5 h1:eveIIGn4BGM3qknO74omf6HYr30/exH+eVUTuAgwjZ0= github.com/lufia/plan9stats v0.0.0-20260802145828-341c2f0c90b5/go.mod h1:autxFIvghDt3jPTLoqZ9OZ7s9qTGNAWmYCjVFWPX/zg= github.com/magiconair/properties v1.18.11 h1:j5ozYZl0zCjG7ahMDH0GWIobOvvUzT0BdAguG0ViKy0= github.com/magiconair/properties v1.18.11/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/mdelapenya/tlscert v0.2.0 h1:7H81W6Z/4weDvZBNOfQte5GpIMo0lGYEeWbkGp5LJHI= +github.com/mdelapenya/tlscert v0.2.0/go.mod h1:O4njj3ELLnJjGdkN7M/vIVCpZ+Cf0L6muqOG4tLSl8o= github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo= github.com/moby/go-archive v0.3.3 h1:OxxR9paxsluYi+zDUEXTTaIxtkK3viymW+Ka7vRhhME= @@ -161,6 +173,8 @@ github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWD github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= github.com/testcontainers/testcontainers-go v0.44.0 h1:/Fwh6HY1mIikhnm9e7HwoxGycx0lzRAE0f5VQpjFxzI= github.com/testcontainers/testcontainers-go v0.44.0/go.mod h1:IcnwQrYTO86xHXu5bvMaBH7ATlbS3Qn1M1QWW3c66rE= +github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0 h1:8fdv/9y3JMxjQ+ULAcOG8RtgeNu5t9XF9LolSXDuTwM= +github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0/go.mod h1:CFr2LncGYokw+OKjXcr8ARCKG1SaC2UEnGxFBovE86g= github.com/tklauser/go-sysconf v0.4.0 h1:7H0uAN+7RkwWRaxhYXDLqa5V3LPrJeV8wmD9dRUgPQU= github.com/tklauser/go-sysconf v0.4.0/go.mod h1:8mTNWyog7H+MpKijp4VmKJAd2bbYQ2zuUwkYRbUArPI= github.com/tklauser/numcpus v0.12.0 h1:NR85qdvHA9pFse3x3weVZ0r0ST8R6l5RHbZrlRaqob4= diff --git a/zoom/go.mod b/zoom/go.mod index ae515b49a..f8fbc8c6b 100644 --- a/zoom/go.mod +++ b/zoom/go.mod @@ -47,6 +47,7 @@ require ( github.com/shirou/gopsutil/v4 v4.26.7 // indirect github.com/sirupsen/logrus v1.10.1 // indirect github.com/testcontainers/testcontainers-go v0.44.0 // indirect + github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0 // indirect github.com/tklauser/go-sysconf v0.4.0 // indirect github.com/tklauser/numcpus v0.12.0 // indirect github.com/yusufpapurcu/wmi v1.2.4 // indirect diff --git a/zoom/go.sum b/zoom/go.sum index 426b7fa68..54a61cf20 100644 --- a/zoom/go.sum +++ b/zoom/go.sum @@ -44,12 +44,24 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.9.2 h1:3ZhOzMWnR4yJ+RW1XImIPsD1aNSz4T4fyP7zlQb56hw= +github.com/jackc/pgx/v5 v5.9.2/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/klauspost/compress v1.19.2 h1:hMRETovs/pu/dVWN7zIT1PGG8t509MwT6bO7XSi26R8= github.com/klauspost/compress v1.19.2/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lufia/plan9stats v0.0.0-20260802145828-341c2f0c90b5 h1:eveIIGn4BGM3qknO74omf6HYr30/exH+eVUTuAgwjZ0= github.com/lufia/plan9stats v0.0.0-20260802145828-341c2f0c90b5/go.mod h1:autxFIvghDt3jPTLoqZ9OZ7s9qTGNAWmYCjVFWPX/zg= github.com/magiconair/properties v1.18.11 h1:j5ozYZl0zCjG7ahMDH0GWIobOvvUzT0BdAguG0ViKy0= github.com/magiconair/properties v1.18.11/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/mdelapenya/tlscert v0.2.0 h1:7H81W6Z/4weDvZBNOfQte5GpIMo0lGYEeWbkGp5LJHI= +github.com/mdelapenya/tlscert v0.2.0/go.mod h1:O4njj3ELLnJjGdkN7M/vIVCpZ+Cf0L6muqOG4tLSl8o= github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo= github.com/moby/go-archive v0.3.3 h1:OxxR9paxsluYi+zDUEXTTaIxtkK3viymW+Ka7vRhhME= @@ -88,6 +100,8 @@ github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWD github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= github.com/testcontainers/testcontainers-go v0.44.0 h1:/Fwh6HY1mIikhnm9e7HwoxGycx0lzRAE0f5VQpjFxzI= github.com/testcontainers/testcontainers-go v0.44.0/go.mod h1:IcnwQrYTO86xHXu5bvMaBH7ATlbS3Qn1M1QWW3c66rE= +github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0 h1:8fdv/9y3JMxjQ+ULAcOG8RtgeNu5t9XF9LolSXDuTwM= +github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0/go.mod h1:CFr2LncGYokw+OKjXcr8ARCKG1SaC2UEnGxFBovE86g= github.com/tklauser/go-sysconf v0.4.0 h1:7H0uAN+7RkwWRaxhYXDLqa5V3LPrJeV8wmD9dRUgPQU= github.com/tklauser/go-sysconf v0.4.0/go.mod h1:8mTNWyog7H+MpKijp4VmKJAd2bbYQ2zuUwkYRbUArPI= github.com/tklauser/numcpus v0.12.0 h1:NR85qdvHA9pFse3x3weVZ0r0ST8R6l5RHbZrlRaqob4= @@ -112,6 +126,8 @@ go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= golang.org/x/crypto v0.55.0 h1:+KWHjbgOaAQ66dh/YlkZKHlz9ZUlq61AFirAR9ntP8M= golang.org/x/crypto v0.55.0/go.mod h1:uq0V9dE/fzQuJtbnL+2EhWOE63vo164FY8xqEnV9xis= +golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek= +golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -120,6 +136,8 @@ golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs= golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= golang.org/x/term v0.45.0 h1:NwWyBmoJCbfTHpxrWoZ9C6/VxOf7ic219I8xZZFdrf0= golang.org/x/term v0.45.0/go.mod h1:9aqxs0blBcrm/n0L9QW0aRVD+ktan8ssZromtqJC43w= +golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8= +golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M= gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q= gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA= pgregory.net/rapid v1.2.0 h1:keKAYRcjm+e1F0oAuU5F5+YPAWcyxNNRK2wud503Gnk= From 24b24379cf256dce0bfa884aacb5fc4b174d63c0 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 1 Sep 2026 11:12:45 +0200 Subject: [PATCH 4/5] Test database includes 'test' --- testutil/postgres.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testutil/postgres.go b/testutil/postgres.go index d64dc9da5..ffe76fcb2 100644 --- a/testutil/postgres.go +++ b/testutil/postgres.go @@ -15,7 +15,7 @@ const postgresImage = "postgres:16" // Crosslink integration tests. func RunPostgres(ctx context.Context) (*postgres.PostgresContainer, error) { return postgres.Run(ctx, postgresImage, - postgres.WithDatabase("crosslink"), + postgres.WithDatabase("crosslink_test"), postgres.WithUsername("crosslink"), postgres.WithPassword("crosslink"), testcontainers.WithWaitStrategy( From f66eaee23375c60d8626c14b76608949f7163589 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 1 Sep 2026 14:50:48 +0200 Subject: [PATCH 5/5] Fix directory Dockerfile And ensure it's checked when performing 'git push'. --- directory/Dockerfile | 30 ++++++++++++++++-------------- pre-push | 2 ++ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/directory/Dockerfile b/directory/Dockerfile index b9a1b92a1..4dc9fed65 100644 --- a/directory/Dockerfile +++ b/directory/Dockerfile @@ -1,30 +1,32 @@ FROM golang:1.27 AS build -# Builds from the workspace root dir WORKDIR /app -# Copy only the Directory module manifests before sources to leverage Docker cache. -# Build with GOWORK=off so the root workspace does not require unrelated modules. +# Copy manifests first so source changes do not invalidate the dependency cache. COPY directory/go.mod directory/go.sum ./directory/ +COPY testutil/go.mod testutil/go.sum ./testutil/ -# Set destination for COPY -WORKDIR /app/directory +RUN go work init \ + ./directory \ + ./testutil -# see .dockerignore for what is getting copied -COPY directory/ ./ +WORKDIR /app/directory -# download go deps, caches GOMODPATH RUN --mount=type=cache,sharing=shared,target=/go/pkg/mod \ - GOWORK=off go mod download + go mod download + +# Copy sources after dependencies have been downloaded. +COPY testutil/ /app/testutil/ +COPY directory/ ./ # Generate API and database code from the tracked specifications. RUN --mount=type=cache,sharing=shared,target=/go/pkg/mod \ - GOWORK=off go generate . + go generate . -# Build, caches GOCACHE -RUN --mount=type=cache,sharing=shared,target=/root/.cache/go-build \ +# Build +RUN --mount=type=cache,sharing=shared,target=/go/pkg/mod \ + --mount=type=cache,sharing=shared,target=/root/.cache/go-build \ CGO_ENABLED=0 \ - GOWORK=off \ GOOS=linux \ go build -o /directory ./cmd/directory @@ -47,7 +49,7 @@ COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ COPY --from=build /etc/passwd /etc/passwd COPY --from=build /etc/group /etc/group -# copy binaries +# copy the binary COPY --from=build /directory . # copy migrations COPY --from=build /app/directory/migrations /migrations diff --git a/pre-push b/pre-push index 24cc696e2..df46458f0 100755 --- a/pre-push +++ b/pre-push @@ -7,4 +7,6 @@ cd broker make docker cd ../illmock make docker +cd ../directory +make docker