From 303dd7f93f401afc439640bce75b6cb87896e88b Mon Sep 17 00:00:00 2001 From: Vadim Gedz Date: Mon, 18 Mar 2024 21:58:42 +0200 Subject: [PATCH 1/6] chore: simplify dsn generation --- cmd/argo-watcher/config/config.go | 13 ++++++++----- cmd/argo-watcher/state/postgres_state.go | 7 +------ cmd/argo-watcher/state/postgres_state_test.go | 17 ----------------- 3 files changed, 9 insertions(+), 28 deletions(-) diff --git a/cmd/argo-watcher/config/config.go b/cmd/argo-watcher/config/config.go index 9cf699f7..e6896cd3 100644 --- a/cmd/argo-watcher/config/config.go +++ b/cmd/argo-watcher/config/config.go @@ -20,11 +20,14 @@ type KeycloakConfig struct { } type DatabaseConfig struct { - Host string `env:"DB_HOST" json:"db_host,omitempty"` - Port int `env:"DB_PORT" json:"db_port,omitempty"` - Name string `env:"DB_NAME" json:"db_name,omitempty"` - User string `env:"DB_USER" json:"db_user,omitempty"` - Password string `env:"DB_PASSWORD" json:"-"` + Host string `env:"DB_HOST"` + Port string `env:"DB_PORT"` + Name string `env:"DB_NAME"` + User string `env:"DB_USER"` + Password string `env:"DB_PASSWORD"` + SSLMode string `env:"DB_SSL_MODE" envDefault:"disable"` + TimeZone string `env:"DB_TIMEZONE" envDefault:"UTC"` + DSN string `env:"DB_DSN,expand" envDefault:"host=${DB_HOST} port=${DB_PORT} user=${DB_USER} password=${DB_PASSWORD} dbname=${DB_NAME} sslmode=${DB_SSL_MODE} TimeZone=${DB_TIMEZONE}"` } type ServerConfig struct { diff --git a/cmd/argo-watcher/state/postgres_state.go b/cmd/argo-watcher/state/postgres_state.go index 6c901d4c..50054e4d 100644 --- a/cmd/argo-watcher/state/postgres_state.go +++ b/cmd/argo-watcher/state/postgres_state.go @@ -27,7 +27,7 @@ type PostgresState struct { // Connect establishes a connection to the PostgreSQL database using the provided server configuration. func (state *PostgresState) Connect(serverConfig *config.ServerConfig) error { // create ORM driver - if orm, err := gorm.Open(postgres.Open(getDsn(serverConfig)), getOrmLogger(serverConfig)); err != nil { + if orm, err := gorm.Open(postgres.Open(serverConfig.Db.DSN), getOrmLogger(serverConfig)); err != nil { return err } else { state.orm = orm @@ -193,11 +193,6 @@ func (state *PostgresState) doProcessPostgresObsoleteTasks() error { return nil } -func getDsn(serverConfig *config.ServerConfig) string { - dsnTemplate := "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable TimeZone=UTC" - return fmt.Sprintf(dsnTemplate, serverConfig.Db.Host, serverConfig.Db.Port, serverConfig.Db.User, serverConfig.Db.Password, serverConfig.Db.Name) -} - func getOrmLogger(serverConfig *config.ServerConfig) *gorm.Config { ormConfig := &gorm.Config{} if serverConfig.LogFormat != config.LogFormatText { diff --git a/cmd/argo-watcher/state/postgres_state_test.go b/cmd/argo-watcher/state/postgres_state_test.go index 30801444..047ffbb6 100644 --- a/cmd/argo-watcher/state/postgres_state_test.go +++ b/cmd/argo-watcher/state/postgres_state_test.go @@ -188,20 +188,3 @@ func TestPostgresState_Check(t *testing.T) { // Check that we return true if connection is ok assert.True(t, postgresState.Check()) } - -func TestGetDsn(t *testing.T) { - databaseConfig := config.DatabaseConfig{ - Host: "localhost", - Port: 5432, - Name: "testdb", - User: "admin", - Password: "password123", - } - testConfig := &config.ServerConfig{ - Db: databaseConfig, - } - - expectedDsn := "host=localhost port=5432 user=admin password=password123 dbname=testdb sslmode=disable TimeZone=UTC" - - assert.Equal(t, expectedDsn, getDsn(testConfig)) -} From 38c47c663f831a2f37f47556d896aadc14931163 Mon Sep 17 00:00:00 2001 From: Vadim Gedz Date: Mon, 18 Mar 2024 22:13:20 +0200 Subject: [PATCH 2/6] fix test --- cmd/argo-watcher/state/postgres_state_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/argo-watcher/state/postgres_state_test.go b/cmd/argo-watcher/state/postgres_state_test.go index 047ffbb6..b9097fcd 100644 --- a/cmd/argo-watcher/state/postgres_state_test.go +++ b/cmd/argo-watcher/state/postgres_state_test.go @@ -61,7 +61,7 @@ var ( func TestPostgresState_Add(t *testing.T) { databaseConfig := config.DatabaseConfig{ Host: os.Getenv("DB_HOST"), - Port: 5432, + Port: "5432", Name: os.Getenv("DB_NAME"), User: os.Getenv("DB_USER"), Password: os.Getenv("DB_PASSWORD"), From cf69b229180a67b40b9f93df461544e9cede932f Mon Sep 17 00:00:00 2001 From: Vadim Gedz Date: Mon, 18 Mar 2024 22:26:17 +0200 Subject: [PATCH 3/6] change panic with assert --- cmd/argo-watcher/state/postgres_state_test.go | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/cmd/argo-watcher/state/postgres_state_test.go b/cmd/argo-watcher/state/postgres_state_test.go index b9097fcd..9d9fb77f 100644 --- a/cmd/argo-watcher/state/postgres_state_test.go +++ b/cmd/argo-watcher/state/postgres_state_test.go @@ -71,33 +71,27 @@ func TestPostgresState_Add(t *testing.T) { Db: databaseConfig, } err := postgresState.Connect(testConfig) - if err != nil { - panic(err) - } + assert.NoError(t, err) + db, err := postgresState.orm.DB() - if err != nil { - panic(err) - } + assert.NoError(t, err) + _, err = db.Exec("TRUNCATE TABLE tasks") - if err != nil { - panic(err) - } + assert.NoError(t, err) + deployedTaskResult, err := postgresState.Add(deployedTask) - if err != nil { - t.Errorf("got error %s, expected nil", err.Error()) - } + assert.NoError(t, err) + deployedTaskId = deployedTaskResult.Id appNotFoundTaskResult, err := postgresState.Add(appNotFoundTask) - if err != nil { - t.Errorf("got error %s, expected nil", err.Error()) - } + assert.NoError(t, err) + appNotFoundTaskId = appNotFoundTaskResult.Id abortedTaskResult, err := postgresState.Add(abortedTask) - if err != nil { - t.Errorf("got error %s, expected nil", err.Error()) - } + assert.NoError(t, err) + abortedTaskId = abortedTaskResult.Id } From 7a4fb679c92e06bd257d72a4f1836c04f0fb33d0 Mon Sep 17 00:00:00 2001 From: Vadim Gedz Date: Mon, 18 Mar 2024 22:32:31 +0200 Subject: [PATCH 4/6] adjust logic in test --- cmd/argo-watcher/state/postgres_state_test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cmd/argo-watcher/state/postgres_state_test.go b/cmd/argo-watcher/state/postgres_state_test.go index 9d9fb77f..20e3b85c 100644 --- a/cmd/argo-watcher/state/postgres_state_test.go +++ b/cmd/argo-watcher/state/postgres_state_test.go @@ -5,6 +5,8 @@ import ( "testing" "time" + envConfig "github.com/caarlos0/env/v10" + "github.com/stretchr/testify/assert" "github.com/shini4i/argo-watcher/cmd/argo-watcher/config" @@ -66,11 +68,15 @@ func TestPostgresState_Add(t *testing.T) { User: os.Getenv("DB_USER"), Password: os.Getenv("DB_PASSWORD"), } + + err := envConfig.Parse(&databaseConfig) + assert.NoError(t, err) + testConfig := &config.ServerConfig{ StateType: "postgres", Db: databaseConfig, } - err := postgresState.Connect(testConfig) + err = postgresState.Connect(testConfig) assert.NoError(t, err) db, err := postgresState.orm.DB() From c16ee7b1d3f41048a86fb7a169c8b61b08555d58 Mon Sep 17 00:00:00 2001 From: Vadim Gedz Date: Mon, 18 Mar 2024 22:33:30 +0200 Subject: [PATCH 5/6] adjust logic in test --- cmd/argo-watcher/state/postgres_state_test.go | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/cmd/argo-watcher/state/postgres_state_test.go b/cmd/argo-watcher/state/postgres_state_test.go index 20e3b85c..694ffa70 100644 --- a/cmd/argo-watcher/state/postgres_state_test.go +++ b/cmd/argo-watcher/state/postgres_state_test.go @@ -1,7 +1,6 @@ package state import ( - "os" "testing" "time" @@ -61,13 +60,7 @@ var ( ) func TestPostgresState_Add(t *testing.T) { - databaseConfig := config.DatabaseConfig{ - Host: os.Getenv("DB_HOST"), - Port: "5432", - Name: os.Getenv("DB_NAME"), - User: os.Getenv("DB_USER"), - Password: os.Getenv("DB_PASSWORD"), - } + databaseConfig := config.DatabaseConfig{} err := envConfig.Parse(&databaseConfig) assert.NoError(t, err) From 68057062eb9f5ec568d79bb10479afefc55a31db Mon Sep 17 00:00:00 2001 From: Vadim Gedz Date: Mon, 18 Mar 2024 22:36:47 +0200 Subject: [PATCH 6/6] fix: set default DB_PORT value --- cmd/argo-watcher/config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/argo-watcher/config/config.go b/cmd/argo-watcher/config/config.go index e6896cd3..d9fd0234 100644 --- a/cmd/argo-watcher/config/config.go +++ b/cmd/argo-watcher/config/config.go @@ -21,7 +21,7 @@ type KeycloakConfig struct { type DatabaseConfig struct { Host string `env:"DB_HOST"` - Port string `env:"DB_PORT"` + Port string `env:"DB_PORT" envDefault:"5432"` Name string `env:"DB_NAME"` User string `env:"DB_USER"` Password string `env:"DB_PASSWORD"`