Skip to content

Commit

Permalink
Add LoadConfigFromYaml tests
Browse files Browse the repository at this point in the history
* db_port as uint16
* remove parameter from LoadConfigFromYaml method
  • Loading branch information
AndresCidoncha committed Sep 22, 2019
1 parent a9e6ec2 commit d98bd9e
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 35 deletions.
78 changes: 46 additions & 32 deletions config/config.go
Expand Up @@ -11,47 +11,61 @@ import (
"gopkg.in/yaml.v2"
)

// LogConfig stores the log configuration
type LogConfig struct {
Level string `short:"l" long:"log-level" env:"TERRABOARD_LOG_LEVEL" yaml:"level" description:"Set log level ('debug', 'info', 'warn', 'error', 'fatal', 'panic')." default:"info"`
Format string `long:"log-format" yaml:"format" env:"TERRABOARD_LOG_FORMAT" description:"Set log format ('plain', 'json')." default:"plain"`
}

// DBConfig stores the database configuration
type DBConfig struct {
Host string `long:"db-host" env:"DB_HOST" yaml:"host" description:"Database host." default:"db"`
Port uint16 `long:"db-port" env:"DB_PORT" yaml:"port" description:"Database port." default:"5432"`
User string `long:"db-user" env:"DB_USER" yaml:"user" description:"Database user." default:"gorm"`
Password string `long:"db-password" env:"DB_PASSWORD" yaml:"password" description:"Database password."`
Name string `long:"db-name" env:"DB_NAME" yaml:"name" description:"Database name." default:"gorm"`
NoSync bool `long:"no-sync" yaml:"no-sync" description:"Do not sync database."`
}

// S3BucketConfig stores the S3 bucket configuration
type S3BucketConfig struct {
Bucket string `long:"s3-bucket" env:"AWS_BUCKET" yaml:"bucket" description:"AWS S3 bucket."`
KeyPrefix string `long:"key-prefix" env:"AWS_KEY_PREFIX" yaml:"key-prefix" description:"AWS Key Prefix."`
FileExtension string `long:"file-extension" env:"AWS_FILE_EXTENSION" yaml:"file-extension" description:"File extension of state files." default:".tfstate"`
}

// AWSConfig stores the DynamoDB table and S3 Bucket configuration
type AWSConfig struct {
DynamoDBTable string `long:"dynamodb-table" env:"AWS_DYNAMODB_TABLE" yaml:"dynamodb-table" description:"AWS DynamoDB table for locks."`
S3 S3BucketConfig `group:"S3 Options" yaml:"s3"`
}

// WebConfig stores the UI interface parameters
type WebConfig struct {
Port uint16 `short:"p" long:"port" env:"TERRABOARD_PORT" yaml:"port" description:"Port to listen on." default:"8080"`
BaseURL string `long:"base-url" env:"TERRABOARD_BASE_URL" yaml:"base-url" description:"Base URL." default:"/"`
LogoutURL string `long:"logout-url" env:"TERRABOARD_LOGOUT_URL" yaml:"logout-url" description:"Logout URL."`
}

// Config stores the handler's configuration and UI interface parameters
type Config struct {
Version bool `short:"V" long:"version" description:"Display version."`

ConfigFilePath string `short:"c" long:"config-file" env:"CONFIG_FILE" description:"Config File path"`

Log struct {
Level string `short:"l" long:"log-level" env:"TERRABOARD_LOG_LEVEL" yaml:"level" description:"Set log level ('debug', 'info', 'warn', 'error', 'fatal', 'panic')." default:"info"`
Format string `long:"log-format" yaml:"format" env:"TERRABOARD_LOG_FORMAT" description:"Set log format ('plain', 'json')." default:"plain"`
} `group:"Logging Options" yaml:"log"`

DB struct {
Host string `long:"db-host" env:"DB_HOST" yaml:"host" description:"Database host." default:"db"`
Port string `long:"db-port" env:"DB_PORT" yaml:"port" description:"Database port." default:"5432"`
User string `long:"db-user" env:"DB_USER" yaml:"user" description:"Database user." default:"gorm"`
Password string `long:"db-password" env:"DB_PASSWORD" yaml:"password" description:"Database password."`
Name string `long:"db-name" env:"DB_NAME" yaml:"name" description:"Database name." default:"gorm"`
NoSync bool `long:"no-sync" yaml:"no-sync" description:"Do not sync database."`
} `group:"Database Options" yaml:"database"`

AWS struct {
DynamoDBTable string `long:"dynamodb-table" env:"AWS_DYNAMODB_TABLE" yaml:"dynamodb-table" description:"AWS DynamoDB table for locks."`

S3 struct {
Bucket string `long:"s3-bucket" env:"AWS_BUCKET" yaml:"bucket" description:"AWS S3 bucket."`
KeyPrefix string `long:"key-prefix" env:"AWS_KEY_PREFIX" yaml:"key-prefix" description:"AWS Key Prefix."`
FileExtension string `long:"file-extension" env:"AWS_FILE_EXTENSION" yaml:"file-extension" description:"File extension of state files." default:".tfstate"`
} `group:"S3 Options" yaml:"s3"`
} `group:"AWS Options" yaml:"aws"`

Web struct {
Port int `short:"p" long:"port" env:"TERRABOARD_PORT" yaml:"port" description:"Port to listen on." default:"8080"`
BaseURL string `long:"base-url" env:"TERRABOARD_BASE_URL" yaml:"base-url" description:"Base URL." default:"/"`
LogoutURL string `long:"logout-url" env:"TERRABOARD_LOGOUT_URL" yaml:"logout-url" description:"Logout URL."`
} `group:"Web" yaml:"web"`
Log LogConfig `group:"Logging Options" yaml:"log"`

DB DBConfig `group:"Database Options" yaml:"database"`

AWS AWSConfig `group:"AWS Options" yaml:"aws"`

Web WebConfig `group:"Web" yaml:"web"`
}

// LoadConfigFromYaml loads the config from config file
func (c *Config) LoadConfigFromYaml(configFilePath string) *Config {
func (c *Config) LoadConfigFromYaml() *Config {
fmt.Printf("Loading config from %s\n", c.ConfigFilePath)
yamlFile, err := ioutil.ReadFile(configFilePath)
yamlFile, err := ioutil.ReadFile(c.ConfigFilePath)
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
Expand All @@ -74,7 +88,7 @@ func LoadConfig(version string) *Config {

if c.ConfigFilePath != "" {
if _, err := os.Stat(c.ConfigFilePath); err == nil {
c.LoadConfigFromYaml(c.ConfigFilePath)
c.LoadConfigFromYaml()
} else {
fmt.Printf("File %s doesn't exists!\n", c.ConfigFilePath)
os.Exit(1)
Expand Down
38 changes: 37 additions & 1 deletion config/config_test.go
@@ -1,10 +1,46 @@
package config

import (
"testing"

log "github.com/sirupsen/logrus"
)

import "testing"
func TestLoadConfigFromYaml(t *testing.T) {
compareConfig := Config{
Log: LogConfig{
Level: "error",
Format: "json",
},
ConfigFilePath: "config_test.yml",
DB: DBConfig{
Host: "postgres",
Port: 15432,
User: "terraboard-user",
Password: "terraboard-pass",
Name: "terraboard-db",
NoSync: true,
},
AWS: AWSConfig{
DynamoDBTable: "terraboard-dynamodb",
S3: S3BucketConfig{
Bucket: "terraboard-bucket",
KeyPrefix: "test/",
FileExtension: ".tfstate",
},
},
Web: WebConfig{
Port: 39090,
BaseURL: "/test/",
LogoutURL: "/test-logout",
},
}
c := Config{ConfigFilePath: "config_test.yml"}
c.LoadConfigFromYaml()
if c != compareConfig {
t.Fatalf("Expected: %v\nGot: %v", compareConfig, c)
}
}

func TestSetLogging_debug(t *testing.T) {
c := Config{}
Expand Down
23 changes: 23 additions & 0 deletions config/config_test.yml
@@ -0,0 +1,23 @@
log:
level: error
format: json

database:
host: postgres
port: 15432
user: terraboard-user
password: terraboard-pass
name: terraboard-db
no-sync: true

aws:
dynamodb-table: terraboard-dynamodb
s3:
bucket: terraboard-bucket
key-prefix: test/
file-extension: .tfstate

web:
port: 39090
base-url: /test/
logout-url: /test-logout
4 changes: 2 additions & 2 deletions db/db.go
Expand Up @@ -25,9 +25,9 @@ type Database struct {
var pageSize = 20

// Init setups up the Database and a pointer to it
func Init(host, port, user, dbname, password, logLevel string) *Database {
func Init(host string, port uint16, user, dbname, password, logLevel string) *Database {
var err error
connString := fmt.Sprintf("host=%s port=%s user=%s dbname=%s sslmode=disable password=%s", host, port, user, dbname, password)
connString := fmt.Sprintf("host=%s port=%d user=%s dbname=%s sslmode=disable password=%s", host, port, user, dbname, password)
db, err := gorm.Open("postgres", connString)
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit d98bd9e

Please sign in to comment.