William Petit
82c93d3f1e
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
83 lines
2.0 KiB
Go
83 lines
2.0 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/pkg/errors"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func TestInterpolatedMap(t *testing.T) {
|
|
type testCase struct {
|
|
Path string
|
|
Env map[string]string
|
|
Assert func(t *testing.T, parsed InterpolatedMap)
|
|
}
|
|
|
|
testCases := []testCase{
|
|
{
|
|
Path: "testdata/environment/interpolated-map-1.yml",
|
|
Env: map[string]string{
|
|
"TEST_PROP1": "foo",
|
|
"TEST_SUB_PROP1": "bar",
|
|
"TEST_SUB2_PROP1": "baz",
|
|
},
|
|
Assert: func(t *testing.T, parsed InterpolatedMap) {
|
|
if e, g := "foo", parsed.Data["prop1"]; e != g {
|
|
t.Errorf("parsed.Data[\"prop1\"]: expected '%v', got '%v'", e, g)
|
|
}
|
|
|
|
if e, g := "bar", parsed.Data["sub"].(map[string]any)["subProp1"]; e != g {
|
|
t.Errorf("parsed.Data[\"sub\"][\"subProp1\"]: expected '%v', got '%v'", e, g)
|
|
}
|
|
|
|
if e, g := "baz", parsed.Data["sub2"].(map[string]any)["sub2Prop1"].([]any)[0]; e != g {
|
|
t.Errorf("parsed.Data[\"sub2\"][\"sub2Prop1\"][0]: expected '%v', got '%v'", e, g)
|
|
}
|
|
|
|
if e, g := "test", parsed.Data["sub2"].(map[string]any)["sub2Prop1"].([]any)[1]; e != g {
|
|
t.Errorf("parsed.Data[\"sub2\"][\"sub2Prop1\"][1]: expected '%v', got '%v'", e, g)
|
|
}
|
|
},
|
|
},
|
|
{
|
|
Path: "testdata/environment/interpolated-map-2.yml",
|
|
Env: map[string]string{
|
|
"BAR": "bar",
|
|
},
|
|
Assert: func(t *testing.T, parsed InterpolatedMap) {
|
|
if e, g := "http://bar", parsed.Data["foo"]; e != g {
|
|
t.Errorf("parsed.Data[\"foo\"]: expected '%v', got '%v'", e, g)
|
|
}
|
|
},
|
|
},
|
|
}
|
|
|
|
for idx, tc := range testCases {
|
|
t.Run(fmt.Sprintf("Case #%d", idx), func(t *testing.T) {
|
|
data, err := os.ReadFile(tc.Path)
|
|
if err != nil {
|
|
t.Fatalf("%+v", errors.WithStack(err))
|
|
}
|
|
|
|
var interpolatedMap InterpolatedMap
|
|
|
|
if tc.Env != nil {
|
|
interpolatedMap.getEnv = func(key string) string {
|
|
return tc.Env[key]
|
|
}
|
|
}
|
|
|
|
if err := yaml.Unmarshal(data, &interpolatedMap); err != nil {
|
|
t.Fatalf("%+v", errors.WithStack(err))
|
|
}
|
|
|
|
if tc.Assert != nil {
|
|
tc.Assert(t, interpolatedMap)
|
|
}
|
|
})
|
|
}
|
|
}
|