diff --git a/cmd/argo-watcher/config/config.go b/cmd/argo-watcher/config/config.go index 9cf699f7..d9fd0234 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" envDefault:"5432"` + 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..694ffa70 100644 --- a/cmd/argo-watcher/state/postgres_state_test.go +++ b/cmd/argo-watcher/state/postgres_state_test.go @@ -1,10 +1,11 @@ package state import ( - "os" "testing" "time" + envConfig "github.com/caarlos0/env/v10" + "github.com/stretchr/testify/assert" "github.com/shini4i/argo-watcher/cmd/argo-watcher/config" @@ -59,45 +60,37 @@ 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) + testConfig := &config.ServerConfig{ StateType: "postgres", Db: databaseConfig, } - err := postgresState.Connect(testConfig) - if err != nil { - panic(err) - } + err = postgresState.Connect(testConfig) + 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 } @@ -188,20 +181,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)) -}