Skip to content

Commit

Permalink
[Amir/Jenson] Creating a method to load proctor configs in non-single…
Browse files Browse the repository at this point in the history
…ton manner
  • Loading branch information
jenson committed Nov 6, 2018
1 parent 692eee5 commit eb64f47
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
43 changes: 43 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,49 @@ import (
"github.com/spf13/viper"
)

const (
Environment = "ENVIRONMENT"
Host = "PROCTOR_HOST"
Email = "EMAIL_ID"
Token = "ACCESS_TOKEN"
)

type ProctorConfig struct {
Host string
Email string
AccessToken string
}

func LoadConfig() (ProctorConfig, error) {
viper.AutomaticEnv()

viper.AddConfigPath(ConfigFileDir())
viper.SetConfigName("proctor")
viper.SetConfigType("yaml")

err := viper.ReadInConfig()

if err != nil {
return ProctorConfig{}, err
}

proctorHost := viper.GetString(Host)
emailId := viper.GetString(Email)
accessToken := viper.GetString(Token)
return ProctorConfig{Host: proctorHost, Email: emailId, AccessToken: accessToken}, nil
}

// Returns Config file directory
// Returns /tmp if environment variable `ENVIRONMENT` is set to test, otherwise returns $HOME/.proctor
// This allows to test on dev environment without conflicting with installed proctor config file
func ConfigFileDir() string {
if os.Getenv("ENVIRONMENT") == "test" {
return "/tmp"
} else {
return fmt.Sprintf("%s/.proctor", os.Getenv("HOME"))
}
}

func InitConfig() {
viper.SetConfigType("yaml")
viper.AutomaticEnv()
Expand Down
59 changes: 59 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config_test

import (
"fmt"
"io/ioutil"
"os"
"testing"
Expand All @@ -9,6 +10,64 @@ import (
"github.com/stretchr/testify/assert"
)

func setUp() {
os.Setenv(config.Environment, "test")
}

func TestReturnsConfigDirAsTmpIfEnvironmentIsTest(t *testing.T) {
os.Setenv(config.Environment, "test")
dir := config.ConfigFileDir()
assert.Equal(t, "/tmp", dir)
}

func TestReturnsConfigDirAsHomeDotProctorIfEnvironmentIsNotSet(t *testing.T) {
os.Unsetenv(config.Environment)

dir := config.ConfigFileDir()
expectedDir := fmt.Sprintf("%s/.proctor", os.Getenv("HOME"))
assert.Equal(t, expectedDir, dir)
}

func TestLoadConfigsFromEnvironmentVariables(t *testing.T) {
setUp()
proctorHost := "test.example.com"
email := "user@example.com"
accessToken := "test-token"
os.Setenv(config.Host, proctorHost)
os.Setenv(config.Email, email)
os.Setenv(config.Token, accessToken)
configFilePath := createProctorConfigFile(t, "")
defer os.Remove(configFilePath)

proctorConfig, err := config.LoadConfig()

assert.NoError(t, err)
assert.Equal(t, config.ProctorConfig{Host: proctorHost, Email: email, AccessToken: accessToken}, proctorConfig)
}

func TestLoadConfigFromFile(t *testing.T) {
setUp()
os.Unsetenv(config.Host)
os.Unsetenv(config.Email)
os.Unsetenv(config.Token)

configFilePath := createProctorConfigFile(t, "PROCTOR_HOST: file.example.com\nEMAIL_ID: file@example.com\nACCESS_TOKEN: file-token")
defer os.Remove(configFilePath)

proctorConfig, err := config.LoadConfig()

assert.NoError(t, err)
assert.Equal(t, config.ProctorConfig{Host: "file.example.com", Email: "file@example.com", AccessToken: "file-token"}, proctorConfig)
}

func createProctorConfigFile(t *testing.T, content string) string {
proctorHost := []byte(fmt.Sprintf(content))
configFilePath := fmt.Sprintf("%s/proctor.yaml", config.ConfigFileDir())
err := ioutil.WriteFile(configFilePath, proctorHost, 0644)
assert.NoError(t, err)
return configFilePath
}

func TestProctorHost(t *testing.T) {
proctorConfigFilePath := "/tmp/proctor.yaml"
proctorHost := []byte("PROCTOR_HOST: any-random-host.com")
Expand Down

0 comments on commit eb64f47

Please sign in to comment.