diff --git a/config/config_test.go b/config/config_test.go index 1145a59..5957b4c 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/davecgh/go-spew/spew" + "github.com/jessevdk/go-flags" log "github.com/sirupsen/logrus" ) @@ -24,17 +25,22 @@ func TestSetLogging_debug(t *testing.T) { } func TestLoadConfig(t *testing.T) { - t.Skip("Skipping this test since go-flags can't parse properlly flags with go test command") - - c := LoadConfig("1.0.0") - compareConfig := Config{ + var tmpConfig configFlags + parser := flags.NewParser(&tmpConfig, flags.Default) + if _, err := parser.ParseArgs([]string{"--db-host=test", "--port=1234"}); err != nil { + if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp { + os.Exit(0) + } + log.Fatalf("Failed to parse flags: %s", err) + } + compareConfig := configFlags{ Log: LogConfig{ Level: "info", Format: "plain", }, ConfigFilePath: "", DB: DBConfig{ - Host: "db", + Host: "test", Port: 5432, User: "gorm", Password: "", @@ -43,50 +49,42 @@ func TestLoadConfig(t *testing.T) { NoSync: false, SyncInterval: 1, }, - AWS: []AWSConfig{ - { - AccessKey: "", - SecretAccessKey: "", - DynamoDBTable: "", - S3: []S3BucketConfig{{ - Bucket: "", - KeyPrefix: "", - FileExtension: []string{".tfstate"}, - ForcePathStyle: false, - }}, - }, + AWS: AWSConfig{ + AccessKey: "", + SecretAccessKey: "", + DynamoDBTable: "", }, - TFE: []TFEConfig{ - { - Address: "", - Token: "", - Organization: "", - }, + S3: S3BucketConfig{ + Bucket: "", + KeyPrefix: "", + FileExtension: []string{".tfstate"}, + ForcePathStyle: false, }, - GCP: []GCPConfig{ - { - GCSBuckets: nil, - GCPSAKey: "", - }, + TFE: TFEConfig{ + Address: "", + Token: "", + Organization: "", }, - Gitlab: []GitlabConfig{ - { - Address: "https://gitlab.com", - Token: "", - }, + GCP: GCPConfig{ + GCSBuckets: nil, + GCPSAKey: "", + }, + Gitlab: GitlabConfig{ + Address: "https://gitlab.com", + Token: "", }, Web: WebConfig{ - Port: 8080, + Port: 1234, SwaggerPort: 8081, BaseURL: "/", LogoutURL: "", }, } - if !reflect.DeepEqual(*c, compareConfig) { + if !reflect.DeepEqual(tmpConfig, compareConfig) { t.Errorf( "TestLoadConfig() -> \n\ngot:\n%v,\n\nwant:\n%v", - spew.Sdump(*c), + spew.Sdump(tmpConfig), spew.Sdump(compareConfig), ) } @@ -109,9 +107,9 @@ func TestLoadConfigFromYaml(t *testing.T) { User: "terraboard-user", Password: "terraboard-pass", Name: "terraboard-db", - SSLMode: "", + SSLMode: "require", NoSync: true, - SyncInterval: 0, + SyncInterval: 1, }, AWS: []AWSConfig{ { diff --git a/config/config_test.yml b/config/config_test.yml index 2acf015..234ba19 100644 --- a/config/config_test.yml +++ b/config/config_test.yml @@ -38,6 +38,5 @@ gitlab: web: port: 39090 - swagger-port: 8081 base-url: /test/ logout-url: /test-logout diff --git a/config/yaml.go b/config/yaml.go index 8e6f453..0f6f35f 100644 --- a/config/yaml.go +++ b/config/yaml.go @@ -6,6 +6,35 @@ package config * (and so makes them optional) *********************************************/ +func (s *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { + type rawConfig Config + raw := rawConfig{ + DB: DBConfig{ + Host: "db", + Port: 5432, + User: "gorm", + Name: "gorm", + SSLMode: "require", + SyncInterval: 1, + }, + Log: LogConfig{ + Level: "info", + Format: "plain", + }, + Web: WebConfig{ + Port: 8080, + SwaggerPort: 8081, + BaseURL: "/", + }, + } + if err := unmarshal(&raw); err != nil { + return err + } + + *s = Config(raw) + return nil +} + func (s *S3BucketConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { type rawS3BucketConfig S3BucketConfig raw := rawS3BucketConfig{