Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: simplify dsn generation #262

Merged
merged 6 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions cmd/argo-watcher/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 1 addition & 6 deletions cmd/argo-watcher/state/postgres_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
64 changes: 20 additions & 44 deletions cmd/argo-watcher/state/postgres_state_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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))
}
Loading