config/config_test: add test (#882)

* Adds a test for parsing the dev.yaml config included in the repository
* Changes readConfig() to return an error in addition to the envrionment
  config

Signed-off-by: John Sahhar <john@gravitl.com>
This commit is contained in:
john s 2022-03-18 14:06:48 -05:00 committed by GitHub
parent ef0d34c119
commit eb974dbd63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 13 deletions

View file

@ -26,6 +26,7 @@ func getEnv() string {
// Config : application config stored as global variable
var Config *EnvironmentConfig
var SetupErr error
// EnvironmentConfig - environment conf struct
type EnvironmentConfig struct {
@ -90,25 +91,26 @@ type SQLConfig struct {
}
// reading in the env file
func readConfig() *EnvironmentConfig {
file := fmt.Sprintf("config/environments/%s.yaml", getEnv())
func readConfig() (*EnvironmentConfig, error) {
file := fmt.Sprintf("environments/%s.yaml", getEnv())
f, err := os.Open(file)
var cfg EnvironmentConfig
if err != nil {
return &cfg
return &cfg, err
}
defer f.Close()
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&cfg)
if err != nil {
log.Fatal(err)
os.Exit(2)
if decoder.Decode(&cfg) != nil {
return &cfg, err
}
return &cfg
return &cfg, err
}
func init() {
Config = readConfig()
if Config, SetupErr = readConfig(); SetupErr != nil {
log.Fatal(SetupErr)
os.Exit(2)
}
}

View file

@ -1,8 +1,36 @@
//Environment file for getting variables
//Currently the only thing it does is set the master password
//Should probably have it take over functions from OS such as port and mongodb connection details
//Reads from the config/environments/dev.yaml file by default
package config
import "testing"
import (
"reflect"
"testing"
)
func TestReadConfig(t *testing.T) {
config := readConfig()
t.Log(config)
func Test_readConfig(t *testing.T) {
tests := []struct {
name string
want *EnvironmentConfig
wantErr bool
}{
{
"ensure development config parses",
&EnvironmentConfig{},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := readConfig()
if (err != nil) != tt.wantErr {
t.Errorf("readConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("readConfig() = %v, want %v", got, tt.want)
}
})
}
}