From 18cbade269362a3fed5763e2b1b39be33ba56aaf Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 9 Mar 2024 12:07:30 -0500 Subject: Fix tests to work without Dockerfile.test --- .env.mysql | 18 ++++------ .env.postgres | 18 ++++------ docker-compose.postgres.yml | 10 +++--- internal/bolttest/db.go | 10 +++--- internal/bolttest/env.go | 19 ++++++++++ internal/configloader/configloader_test.go | 42 +++++++++++++++++++++- internal/configloader/configloader_unix_test.go | 31 ---------------- internal/configloader/configloader_windows_test.go | 34 ------------------ internal/services/migration_test.go | 6 ++-- internal/storage/db_test.go | 3 +- 10 files changed, 87 insertions(+), 104 deletions(-) create mode 100644 internal/bolttest/env.go delete mode 100644 internal/configloader/configloader_unix_test.go delete mode 100644 internal/configloader/configloader_windows_test.go diff --git a/.env.mysql b/.env.mysql index adc5881..7c0fa9e 100644 --- a/.env.mysql +++ b/.env.mysql @@ -1,12 +1,6 @@ -DB_NAME=test_db -DB_HOST=bolt_mysql_db -DB_PORT=1234 -DB_USERNAME=test_user -DB_PASSWORD=testpassword - -BOLT_CONNECTION_DBNAME=test_db -BOLT_CONNECTION_HOST=bolt_mysql_db -BOLT_CONNECTION_PORT=1234 -BOLT_CONNECTION_USER=test_user -BOLT_CONNECTION_PASSWORD=testpassword -BOLT_CONNECTION_DRIVER=mysql +BOLT_DB_CONN_DBNAME=test_db +BOLT_DB_CONN_HOST=localhost +BOLT_DB_CONN_PORT=1234 +BOLT_DB_CONN_USER=test_user +BOLT_DB_CONN_PASSWORD=testpassword +BOLT_DB_CONN_DRIVER=mysql diff --git a/.env.postgres b/.env.postgres index 6bff99b..5eb1a18 100644 --- a/.env.postgres +++ b/.env.postgres @@ -1,12 +1,6 @@ -DB_NAME=test_db -DB_HOST=bolt_postgres_db -DB_PORT=5432 -DB_USERNAME=test_user -DB_PASSWORD=testpassword - -BOLT_CONNECTION_DBNAME=test_db -BOLT_CONNECTION_HOST=bolt_postgres_db -BOLT_CONNECTION_PORT=5432 -BOLT_CONNECTION_USER=test_user -BOLT_CONNECTION_PASSWORD=testpassword -BOLT_CONNECTION_DRIVER=postgres +BOLT_DB_CONN_DBNAME=test_db +BOLT_DB_CONN_HOST=localhost +BOLT_DB_CONN_PORT=4001 +BOLT_DB_CONN_USER=test_user +BOLT_DB_CONN_PASSWORD=testpassword +BOLT_DB_CONN_DRIVER=postgres diff --git a/docker-compose.postgres.yml b/docker-compose.postgres.yml index 85f34fe..f3052fd 100644 --- a/docker-compose.postgres.yml +++ b/docker-compose.postgres.yml @@ -4,12 +4,12 @@ services: postgres_db: image: postgres:16.1-alpine3.19 container_name: bolt_postgres_db - env_file: - - .env.postgres + ports: + - "${BOLT_DB_CONN_PORT:?err}:5432" environment: - - POSTGRES_DB=${DB_NAME:?err} - - POSTGRES_USER=${DB_USERNAME:?err} - - POSTGRES_PASSWORD=${DB_PASSWORD:?err} + - POSTGRES_DB=${BOLT_DB_CONN_DBNAME:?err} + - POSTGRES_USER=${BOLT_DB_CONN_USER:?err} + - POSTGRES_PASSWORD=${BOLT_DB_CONN_PASSWORD:?err} volumes: - postgres_db_volume:/var/lib/postgresql/data diff --git a/internal/bolttest/db.go b/internal/bolttest/db.go index bde519b..6ebbabf 100644 --- a/internal/bolttest/db.go +++ b/internal/bolttest/db.go @@ -12,16 +12,16 @@ import ( ) func NewTestConnectionConfig(t *testing.T, driver string) *configloader.ConnectionConfig { - port, err := strconv.Atoi(os.Getenv("DB_PORT")) + port, err := strconv.Atoi(os.Getenv("BOLT_DB_CONN_PORT")) assert.Nil(t, err) return &configloader.ConnectionConfig{ Driver: driver, - DBName: os.Getenv("DB_NAME"), - Host: os.Getenv("DB_HOST"), + DBName: os.Getenv("BOLT_DB_CONN_DBNAME"), + Host: os.Getenv("BOLT_DB_CONN_HOST"), Port: port, - User: os.Getenv("DB_USERNAME"), - Password: os.Getenv("DB_PASSWORD"), + User: os.Getenv("BOLT_DB_CONN_USER"), + Password: os.Getenv("BOLT_DB_CONN_PASSWORD"), } } diff --git a/internal/bolttest/env.go b/internal/bolttest/env.go new file mode 100644 index 0000000..8f0ad63 --- /dev/null +++ b/internal/bolttest/env.go @@ -0,0 +1,19 @@ +package bolttest + +import ( + "os" + "testing" + + "github.com/eugenetriguba/checkmate/assert" +) + +func UnsetEnv(t *testing.T, key string) { + originalVal, isSet := os.LookupEnv(key) + if isSet { + t.Cleanup(func() { + err := os.Setenv(key, originalVal) + assert.Nil(t, err) + }) + os.Unsetenv(key) + } +} diff --git a/internal/configloader/configloader_test.go b/internal/configloader/configloader_test.go index 0ad6930..6aa3b45 100644 --- a/internal/configloader/configloader_test.go +++ b/internal/configloader/configloader_test.go @@ -3,6 +3,7 @@ package configloader_test import ( "fmt" "os" + "path/filepath" "testing" "github.com/eugenetriguba/bolt/internal/bolttest" @@ -35,6 +36,14 @@ func TestNewConfigWithInvalidVersionStyle(t *testing.T) { } func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) { + bolttest.UnsetEnv(t, "BOLT_DB_CONN_HOST") + bolttest.UnsetEnv(t, "BOLT_DB_CONN_PORT") + bolttest.UnsetEnv(t, "BOLT_DB_CONN_USER") + bolttest.UnsetEnv(t, "BOLT_DB_CONN_PASSWORD") + bolttest.UnsetEnv(t, "BOLT_DB_CONN_DBNAME") + bolttest.UnsetEnv(t, "BOLT_DB_CONN_DRIVER") + bolttest.UnsetEnv(t, "BOLT_MIGRATIONS_DIR_PATH") + bolttest.UnsetEnv(t, "BOLT_MIGRATIONS_VERSION_STYLE") expectedCfg := configloader.Config{ Migrations: configloader.MigrationsConfig{ DirectoryPath: "myfancymigrations", @@ -49,9 +58,12 @@ func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) { Driver: "postgres", }, } - bolttest.CreateConfigFile(t, &expectedCfg, "bolt.toml") + tmpdir := t.TempDir() + bolttest.ChangeCwd(t, tmpdir) + bolttest.CreateConfigFile(t, &expectedCfg, filepath.Join(tmpdir, "bolt.toml")) cfg, err := configloader.NewConfig() + assert.Nil(t, err) assert.DeepEqual(t, *cfg, expectedCfg) } @@ -100,3 +112,31 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) { assert.Nil(t, err) assert.DeepEqual(t, *cfg, envCfg) } + +func TestNewConfigSearchesParentDirectories(t *testing.T) { + bolttest.UnsetEnv(t, "BOLT_DB_CONN_HOST") + bolttest.UnsetEnv(t, "BOLT_DB_CONN_PORT") + bolttest.UnsetEnv(t, "BOLT_DB_CONN_USER") + bolttest.UnsetEnv(t, "BOLT_DB_CONN_PASSWORD") + bolttest.UnsetEnv(t, "BOLT_DB_CONN_DBNAME") + bolttest.UnsetEnv(t, "BOLT_DB_CONN_DRIVER") + bolttest.UnsetEnv(t, "BOLT_MIGRATIONS_DIR_PATH") + bolttest.UnsetEnv(t, "BOLT_MIGRATIONS_VERSION_STYLE") + expectedCfg := configloader.Config{ + Migrations: configloader.MigrationsConfig{ + DirectoryPath: "differentmigrationsdir", + VersionStyle: configloader.VersionStyleSequential, + }, + } + tmpdir := t.TempDir() + bolttest.CreateConfigFile(t, &expectedCfg, filepath.Join(tmpdir, "bolt.toml")) + nestedTmpDir := filepath.Join(tmpdir, "nested-dir", "nested-x2-dir") + err := os.MkdirAll(nestedTmpDir, 0755) + assert.Nil(t, err) + bolttest.ChangeCwd(t, nestedTmpDir) + + cfg, err := configloader.NewConfig() + assert.Nil(t, err) + + assert.DeepEqual(t, *cfg, expectedCfg) +} diff --git a/internal/configloader/configloader_unix_test.go b/internal/configloader/configloader_unix_test.go deleted file mode 100644 index 8043bdb..0000000 --- a/internal/configloader/configloader_unix_test.go +++ /dev/null @@ -1,31 +0,0 @@ -//go:build unix - -package configloader_test - -import ( - "os" - "testing" - - "github.com/eugenetriguba/bolt/internal/bolttest" - "github.com/eugenetriguba/bolt/internal/configloader" - "github.com/eugenetriguba/checkmate/assert" -) - -func TestNewConfigUnixSearchesToRootFilePath(t *testing.T) { - expectedCfg := configloader.Config{ - Migrations: configloader.MigrationsConfig{ - DirectoryPath: "differentmigrationsdir", - VersionStyle: configloader.VersionStyleSequential, - }, - } - bolttest.CreateConfigFile(t, &expectedCfg, "/bolt.toml") - - homedir, err := os.UserHomeDir() - assert.Nil(t, err) - bolttest.ChangeCwd(t, homedir) - - cfg, err := configloader.NewConfig() - assert.Nil(t, err) - - assert.DeepEqual(t, *cfg, expectedCfg) -} diff --git a/internal/configloader/configloader_windows_test.go b/internal/configloader/configloader_windows_test.go deleted file mode 100644 index 1ba6c51..0000000 --- a/internal/configloader/configloader_windows_test.go +++ /dev/null @@ -1,34 +0,0 @@ -//go:build windows - -package configloader_test - -import ( - "os" - "path/filepath" - "testing" - - "github.com/eugenetriguba/bolt/internal/bolttest" - "github.com/eugenetriguba/bolt/internal/configloader" - "github.com/eugenetriguba/checkmate/assert" -) - -func TestNewConfigWindowsSearchesToRootFilePath(t *testing.T) { - expectedCfg := configloader.Config{ - Migrations: configloader.MigrationsConfig{ - DirectoryPath: "differentmigrationsdir", - VersionStyle: configloader.VersionStyleSequential, - }, - } - configPath := filepath.Join(os.Getenv("SystemDrive"), "bolt.toml") - bolttest.CreateConfigFile(t, &expectedCfg, configPath) - assert.Nil(t, err) - - homedir, err := os.UserHomeDir() - assert.Nil(t, err) - bolttest.ChangeCwd(t, homedir) - - cfg, err := configloader.NewConfig() - assert.Nil(t, err) - - assert.DeepEqual(t, *cfg, expectedCfg) -} diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go index 0c30c76..b2b480b 100644 --- a/internal/services/migration_test.go +++ b/internal/services/migration_test.go @@ -373,12 +373,12 @@ func TestCreateMigration_VersionStyleTimestamp(t *testing.T) { bolttest.NullOutputter{}, ) - beforeCreateTs := time.Now().Add(-time.Second) + beforeCreateTs := time.Now().Add(30 * -time.Second) migration, err := svc.CreateMigration("new migration") - afterCreateTs := time.Now().Add(time.Second) + afterCreateTs := time.Now().Add(30 * time.Second) assert.Nil(t, err) - version, err := time.Parse("20060102150405", migration.Version) + version, err := time.ParseInLocation("20060102150405", migration.Version, time.Local) assert.Nil(t, err) check.True(t, beforeCreateTs.Before(version)) check.True(t, afterCreateTs.After(version)) diff --git a/internal/storage/db_test.go b/internal/storage/db_test.go index eaf9dfe..9f87d4c 100644 --- a/internal/storage/db_test.go +++ b/internal/storage/db_test.go @@ -12,8 +12,9 @@ import ( func TestDBConnect_Success(t *testing.T) { cfg := bolttest.NewTestConnectionConfig(t, "postgres") + connParams := storage.DBConnectionString(cfg) - conn, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) + conn, err := storage.DBConnect(cfg.Driver, connParams) assert.Nil(t, err) defer conn.Close() -- cgit v1.2.3