Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use go-ucfg based variable expansion support #1984

Merged
merged 1 commit into from
Jul 8, 2016
Merged
Show file tree
Hide file tree
Changes from all 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: 1 addition & 17 deletions libbeat/cfgfile/cfgfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cfgfile
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -60,22 +59,7 @@ func Load(path string) (*common.Config, error) {
if path == "" {
path = *configfile
}

fileContent, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read %s: %v", path, err)
}
fileContent, err = expandEnv(filepath.Base(path), fileContent)
if err != nil {
return nil, err
}

config, err := common.NewConfigWithYAML(fileContent, path)
if err != nil {
return nil, fmt.Errorf("YAML config parsing failed on %s: %v", path, err)
}

return config, nil
return common.LoadFile(path)
}

// IsTestConfig returns whether or not this is configuration used for testing
Expand Down
74 changes: 7 additions & 67 deletions libbeat/cfgfile/cfgfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
)

type TestConfig struct {
Output ElasticsearchConfig
Output ElasticsearchConfig
Env string `config:"env.test_key"`
EnvDefault string `config:"env.default"`
}

type ElasticsearchConfig struct {
Expand All @@ -25,6 +27,7 @@ type Connection struct {

func TestRead(t *testing.T) {
absPath, err := filepath.Abs("../tests/files/")
os.Setenv("TEST_KEY", "test_value")

assert.NotNil(t, absPath)
assert.Nil(t, err)
Expand All @@ -34,72 +37,9 @@ func TestRead(t *testing.T) {
err = Read(config, absPath+"/config.yml")
assert.Nil(t, err)

// Access config
// validate
assert.Equal(t, "localhost", config.Output.Elasticsearch.Host)

// Chat that it is integer
assert.Equal(t, 9200, config.Output.Elasticsearch.Port)
}

func TestExpandEnv(t *testing.T) {
var tests = []struct {
in string
out string
err string
}{
// Environment variables can be specified as ${env} only.
{"${y}", "y", ""},
{"$y", "$y", ""},

// Environment variables are case-sensitive.
{"${Y}", "", ""},

// Defaults can be specified.
{"x${Z:D}", "xD", ""},
{"x${Z:A B C D}", "xA B C D", ""}, // Spaces are allowed in the default.
{"x${Z:}", "x", ""},

// Un-matched braces cause an error.
{"x${Y ${Z:Z}", "", "unexpected character in variable expression: " +
"U+0020 ' ', expected a default value or closing brace"},

// Special environment variables are not replaced.
{"$*", "$*", ""},
{"${*}", "", "shell variable cannot start with U+002A '*'"},
{"$@", "$@", ""},
{"${@}", "", "shell variable cannot start with U+0040 '@'"},
{"$1", "$1", ""},
{"${1}", "", "shell variable cannot start with U+0031 '1'"},

{"", "", ""},
{"$$", "$$", ""},

{"${a_b}", "", ""}, // Underscores are allowed in variable names.

// ${} cannot be split across newlines.
{"hello ${name: world\n}", "", "unterminated brace"},

// To use a literal '${' you write '$${'.
{`password: "abc$${!"`, `password: "abc${!"`, ""},

// The full error contains the line number.
{"test:\n name: ${var", "", "failure while expanding environment " +
"variables in config.yml at line=2, unterminated brace"},
}

for _, test := range tests {
os.Setenv("y", "y")
output, err := expandEnv("config.yml", []byte(test.in))

switch {
case test.err != "" && err == nil:
t.Errorf("Expected an error for test case %+v", test)
case test.err == "" && err != nil:
t.Errorf("Unexpected error for test case %+v, %v", test, err)
case err != nil:
assert.Contains(t, err.Error(), test.err)
default:
assert.Equal(t, test.out, string(output), "Input: %s", test.in)
}
}
assert.Equal(t, "test_value", config.Env)
assert.Equal(t, "default", config.EnvDefault)
}
Loading