diff --git a/dbtest/db.go b/dbtest/db.go index 97e7290..a6b1dbb 100644 --- a/dbtest/db.go +++ b/dbtest/db.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "log" - "os" "time" "github.com/docker/go-connections/nat" @@ -38,7 +37,7 @@ type TestDatabase struct { Container testcontainers.Container } -func SetupTestDatabase(ctx context.Context, testDatabaseContainerRequest testcontainers.GenericContainerRequest) (*TestDatabase, error) { +func SetupTestDatabase(ctx context.Context, testDatabaseContainerRequest testcontainers.GenericContainerRequest, absoluteMigrationsPath string) (*TestDatabase, error) { // setup db container container, dbInstance, err := createContainer(ctx, testDatabaseContainerRequest) if err != nil { @@ -58,7 +57,7 @@ func SetupTestDatabase(ctx context.Context, testDatabaseContainerRequest testcon dbAddr := fmt.Sprintf("%s:%s", dbHost, dbPort.Port()) // migrate db schema - err = migrateDb(dbAddr) + err = migrateDb(dbAddr, absoluteMigrationsPath) if err != nil { return nil, fmt.Errorf("setup test bank:migrate db: %v", err) } @@ -152,11 +151,11 @@ func createContainer(ctx context.Context, req testcontainers.GenericContainerReq return container, db, nil } -func migrateDb(dbAddr string) error { +func migrateDb(dbAddr, absoluteMigrationsPath string) error { databaseURL := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", DBUser, DBPass, dbAddr, DBName) - // file:./ to be relative to working directory - migrationsURL := os.Getenv("MIGRATION_URL") + // URL file:./ needed fir golang-migrate + migrationsURL := fmt.Sprintf("file:%s", absoluteMigrationsPath) if len(migrationsURL) < 1 { return fmt.Errorf("migrate db:missing env migration_url: %s", migrationsURL) diff --git a/internal/app/bank/integration/main_test.go b/internal/app/bank/integration/main_test.go index df3680f..337c04c 100644 --- a/internal/app/bank/integration/main_test.go +++ b/internal/app/bank/integration/main_test.go @@ -4,27 +4,31 @@ package integration import ( "context" - "fmt" "log" "os" + "path/filepath" "testing" "github.com/hthunberg/course-golang-postgres-grpc-api/dbtest" "github.com/hthunberg/course-golang-postgres-grpc-api/internal/app/bank" + "go.uber.org/zap" ) var testee bank.Bank func TestMain(m *testing.M) { - os.Setenv("MIGRATION_URL", fmt.Sprintf("file:.%s", "/../../../../build/db/migrations")) - ctx := context.Background() + absoluteMigrationsPath, err := filepath.Abs("../../../../build/db/migrations") + if err != nil { + log.Fatal("failed to calculate absolute path to db migrations", zap.Error(err)) + } + // Set up a postgres DB testDBRequest := dbtest.TestDatabaseContainerRequest() - testDB, err := dbtest.SetupTestDatabase(ctx, testDBRequest) + testDB, err := dbtest.SetupTestDatabase(ctx, testDBRequest, absoluteMigrationsPath) if err != nil { - log.Fatal("failed to setup postgres db", err) + log.Fatal("failed to setup postgres db", zap.Error(err)) } defer testDB.TearDown() testee = bank.NewBank(testDB.DbInstance) diff --git a/test/integration/logconsumer.go b/test/integration/logconsumer.go index 9aaacd0..f0453ce 100644 --- a/test/integration/logconsumer.go +++ b/test/integration/logconsumer.go @@ -27,7 +27,6 @@ func newTestLogConsumer(msgs []string, done chan bool) TestLogConsumer { } func (g *TestLogConsumer) Accept(l tc.Log) { - // fmt.Println(string(l.Content)) s := string(l.Content) if s == fmt.Sprintf("echo %s\n", lastMessage) { g.Done <- true diff --git a/test/integration/main_test.go b/test/integration/main_test.go index 2b6ea6a..deae20b 100644 --- a/test/integration/main_test.go +++ b/test/integration/main_test.go @@ -7,11 +7,13 @@ import ( "fmt" "log" "os" + "path/filepath" "testing" "time" "github.com/hthunberg/course-golang-postgres-grpc-api/dbtest" "github.com/jackc/pgx/v5/pgxpool" + "go.uber.org/zap" ) var ( @@ -20,8 +22,6 @@ var ( ) func TestMain(m *testing.M) { - os.Setenv("MIGRATION_URL", fmt.Sprintf("file:.%s", "/../../build/db/migrations")) - ctx := context.Background() // Docker provides the ability for us to create custom networks and place containers on one or more networks. @@ -36,23 +36,28 @@ func TestMain(m *testing.M) { // Alias for postgres db when running inside a custom network, testDBAlias := "testdb" + absoluteMigrationsPath, err := filepath.Abs("../../build/db/migrations") + if err != nil { + log.Fatal("failed to calculate absolute path to db migrations", zap.Error(err)) + } + // Set up a postgres DB testDBRequest := dbtest.TestDatabaseContainerRequest() network.ApplyNetworkAlias(&testDBRequest, testDBAlias) - testDB, err := dbtest.SetupTestDatabase(ctx, testDBRequest) + testDB, err := dbtest.SetupTestDatabase(ctx, testDBRequest, absoluteMigrationsPath) if err != nil { - log.Fatal("failed to setup postgres db", err) + log.Fatal("failed to setup postgres db", zap.Error(err)) } defer testDB.TearDown() testDbInstance = testDB.DbInstance // Set up a test bank dbAddr := fmt.Sprintf("%s:%s", testDBAlias, dbtest.DBPort) - testBankRequest := TestBankContainerRequest(dbAddr) + testBankRequest := TestBankContainerRequest(dbAddr, absoluteMigrationsPath) network.ApplyNetworkAlias(&testBankRequest, "testbank") testBank, err := setupTestBank(ctx, testBankRequest) if err != nil { - log.Fatal("failed to setup test bank", err) + log.Fatal("failed to setup test bank", zap.Error(err)) } defer testBank.TearDown() diff --git a/test/integration/testbank.go b/test/integration/testbank.go index 4196567..a21c2e6 100644 --- a/test/integration/testbank.go +++ b/test/integration/testbank.go @@ -49,32 +49,27 @@ func (tdb *TestBank) TearDown() { _ = tdb.container.Terminate(context.Background()) } -func TestBankContainerRequest(dbAddr string) tc.GenericContainerRequest { +func TestBankContainerRequest(dbAddr, absoluteMigrationsPath string) tc.GenericContainerRequest { dbSource := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", dbtest.DBUser, dbtest.DBPass, dbAddr, dbtest.DBName) - // TODO: Hans fixa - // file:./ to be relative to working directory - // hostPathMigrationsURL := os.Getenv("MIGRATION_URL") - hostPathMigrationsURL := "/Users/hansthunberg/git-views/golang/course-golang-postgres-grpc-api/build/db/migrations" - env := map[string]string{ "ENVIRONMENT": "integrationtest", "DB_SOURCE": dbSource, - "MIGRATION_URL": "file://migrations", + "MIGRATION_URL": "file://migrations", // Relative to /app/bin/migrations "LOG_LEVEL": "DEBUG", } port := "8080/tcp" req := tc.GenericContainerRequest{ ContainerRequest: tc.ContainerRequest{ - Name: "testbank", Image: "bank:latest", + Labels: map[string]string{"app": "testbank"}, ExposedPorts: []string{port}, Env: env, Mounts: tc.ContainerMounts{ tc.ContainerMount{ Source: tc.GenericBindMountSource{ - HostPath: hostPathMigrationsURL, + HostPath: absoluteMigrationsPath, }, Target: tc.ContainerMountTarget("/app/bin/migrations"), },