Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

Differentiate between failures to load config file not existing vs. parse errors #242

Merged
merged 5 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 11 additions & 7 deletions core/componentConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,20 @@ func (cc *ComponentConfig) MergeConfigFile(path string, environment string) (err
func (cc *ComponentConfig) Load(environment string) (err error) {
err = cc.UnmarshalYAMLConfig(environment)

// fall back to looking for JSON if loading YAML fails.
if err != nil {
err = cc.UnmarshalJSONConfig(environment)
// If success or loading or parsing the file failed for reasons other than it didn't exist, return.
if err == nil || !os.IsNotExist(err) {
timfpark marked this conversation as resolved.
Show resolved Hide resolved
return err
}

if err != nil {
// couldn't find any config files, so default back to yaml serialization
cc.Serialization = "yaml"
}
err = cc.UnmarshalJSONConfig(environment)

// If success or loading or parsing the file failed for reasons other than it didn't exist, return.
if err == nil || !os.IsNotExist(err) {
timfpark marked this conversation as resolved.
Show resolved Hide resolved
return err
}

// Otherwise, no config files were found, so default to yaml serialization and return.
cc.Serialization = "yaml"
return nil
}

Expand Down
18 changes: 18 additions & 0 deletions core/componentConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ func TestLoad(t *testing.T) {
assert.Equal(t, "myapp", config.Namespace)
}

func TestFailedYAMLLoad(t *testing.T) {
config := ComponentConfig{
Path: "../testdata/badyamlconfig",
}

err := config.Load("common")
assert.NotNil(t, err)
}

func TestFailedJSONLoad(t *testing.T) {
config := ComponentConfig{
Path: "../testdata/badjsonconfig",
}

err := config.Load("common")
assert.NotNil(t, err)
}

func TestMerge(t *testing.T) {
currentConfig := NewComponentConfig("../testdata/merge")
err := currentConfig.Load("current")
Expand Down
1 change: 1 addition & 0 deletions testdata/badjsonconfig/config/common.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
THIS IS NOT VALID JSON
17 changes: 17 additions & 0 deletions testdata/badyamlconfig/config/common.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
subcomponents:
anotherservice:
config:
image:
tag: "744"
replicaCount: 1
screenprofiler:
config:
envVar:
important: true
fabshouldfail: true
is_this_a_bug: definitely
culprit:"this is where it all goes wrong due to the lack of space between key and value"
image:
repository: somewheregood.azurecr.io/screenprofiler
tag: "73"
replicaCount: 1