Skip to content

Commit

Permalink
test: added tests for configuration.
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Neudegg <andrew.neudegg@finbourne.com>
  • Loading branch information
AndrewNeudegg committed Jan 1, 2021
1 parent 271f3f1 commit 6029bca
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 4 deletions.
71 changes: 71 additions & 0 deletions pkg/configuration/config_test.go
@@ -0,0 +1,71 @@
package configuration

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestConfigurationSimple(t *testing.T) {
config := []byte(`
applicationSettings: {}
sourceConfigurations:
- name: http/simple
config:
ListenAddr: :5050
MaxBodySize: 512
relayConfigs:
- name: memory
config: {}
distributorConfigurations:
- name: http/direct
config:
Addr: http://localhost:5051
`)
c := RawConfig{
ConfigData: config,
}
lC, err := c.Load()
assert.Nil(t, err)

assert.Equal(t, lC.SourceConfigs[0].Name, "http/simple")
assert.Equal(t, lC.RelayConfigs[0].Name, "memory")
assert.Equal(t, lC.DistributorConfigs[0].Name, "http/direct")
}

func TestConfigurationSubConfigs(t *testing.T) {
config := []byte(`
applicationSettings: {}
sourceConfigurations:
- name: mock/config
config: {}
subConfigs:
- name: example/example1
config:
ListenAddr: :5050
MaxBodySize: 512
- name: example/example2
config:
ListenAddr: :5050
MaxBodySize: 512
relayConfigs:
- name: memory
config: {}
distributorConfigurations:
- name: http/direct
config:
Addr: http://localhost:5051
`)
c := RawConfig{
ConfigData: config,
}
lC, err := c.Load()
assert.Nil(t, err)

assert.Equal(t, lC.SourceConfigs[0].Name, "mock/config")
assert.Equal(t, lC.RelayConfigs[0].Name, "memory")
assert.Equal(t, lC.DistributorConfigs[0].Name, "http/direct")

assert.Equal(t, lC.SourceConfigs[0].SubConfigs[0].Name, "example/example1")
assert.Equal(t, lC.SourceConfigs[0].SubConfigs[1].Name, "example/example2")
}
35 changes: 34 additions & 1 deletion pkg/configuration/file_test.go
Expand Up @@ -38,7 +38,7 @@ distributorConfigurations:
B: example
`)

func TestSmoke(t *testing.T) {
func TestFileSmoke(t *testing.T) {
// Setup
dir, err := ioutil.TempDir("", "config")
if err != nil {
Expand All @@ -65,3 +65,36 @@ func TestSmoke(t *testing.T) {
assert.Equal(t, "first", sourceConfigs[0].Name)
assert.Equal(t, "second", sourceConfigs[1].Name)
}

func TestBadFile(t *testing.T) {
// Setup
fConfig := FileConfig{
Source: "doesnt-exist",
}

_, err := fConfig.Load()
assert.Error(t, err)
}

func TestBadFileContent(t *testing.T) {
testBytes := append([]byte("testing"), exampleConfig...)

// Setup
dir, err := ioutil.TempDir("", "config")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
tmpfn := filepath.Join(dir, "config.yaml")
if err := ioutil.WriteFile(tmpfn, testBytes, 0666); err != nil {
t.Fatal(err)
}

// -- Test --
fConfig := FileConfig{
Source: tmpfn,
}

_, err = fConfig.Load()
assert.Error(t, err)
}
55 changes: 55 additions & 0 deletions pkg/configuration/raw_test.go
@@ -0,0 +1,55 @@
package configuration

import (
"testing"

"github.com/stretchr/testify/assert"
)

var rawExampleConfig = []byte(`
applicationSettings: {}
# sourceConfigurations
sourceConfigurations:
- name: first
sourceConfig:
A: example
B: example
- name: second
sourceConfig:
A: example
B: example
# relayConfigs
relayConfigs:
- name: thing
sourceConfig:
A: example
B: example
# distributorConfigurations
distributorConfigurations:
- name: thing
sourceConfig:
A: example
B: example
`)

func TestRawSmoke(t *testing.T) {
f := RawConfig{
ConfigData: rawExampleConfig,
}

_, err := f.Load()
assert.Nil(t, err)
}

func TestRawBadData(t *testing.T) {
testBytes := append([]byte("testing"), rawExampleConfig...)
f := RawConfig{
ConfigData: testBytes,
}

_, err := f.Load()
assert.Error(t, err)
}
6 changes: 3 additions & 3 deletions pkg/configuration/structs.go
Expand Up @@ -21,7 +21,7 @@ type AppSettings struct{}

// NodeConfig defines the configuration for any given pluggable structure.
type NodeConfig struct {
Name string `yaml:"name"`
Config map[string]interface{} `yaml:"config"`
NodeConfigs []*NodeConfig `yaml:"nodeConfigs"`
Name string `yaml:"name"`
Config map[string]interface{} `yaml:"config"`
SubConfigs []*NodeConfig `yaml:"subConfigs"`
}

0 comments on commit 6029bca

Please sign in to comment.