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

Update toml library used for improved configuration ingestion #5513

Merged
merged 11 commits into from
Apr 26, 2019
14 changes: 11 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@

[[constraint]]
name = "github.com/influxdata/toml"
branch = "master"
branch = "telegraf"

[[constraint]]
name = "github.com/influxdata/wlog"
Expand Down
75 changes: 75 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import (
"testing"
"time"

"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/models"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/inputs/exec"
"github.com/influxdata/telegraf/plugins/inputs/http_listener_v2"
"github.com/influxdata/telegraf/plugins/inputs/memcached"
"github.com/influxdata/telegraf/plugins/inputs/procstat"
httpOut "github.com/influxdata/telegraf/plugins/outputs/http"
"github.com/influxdata/telegraf/plugins/parsers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConfig_LoadSingleInputWithEnvVars(t *testing.T) {
Expand Down Expand Up @@ -176,3 +180,74 @@ func TestConfig_LoadDirectory(t *testing.T) {
assert.Equal(t, pConfig, c.Inputs[3].Config,
"Merged Testdata did not produce correct procstat metadata.")
}

func TestConfig_LoadSpecialTypes(t *testing.T) {
glinton marked this conversation as resolved.
Show resolved Hide resolved
c := NewConfig()
err := c.LoadConfig("./testdata/special_types.toml")
assert.NoError(t, err)
require.Equal(t, 1, len(c.Inputs))

inputHTTPListener, ok := c.Inputs[0].Input.(*http_listener_v2.HTTPListenerV2)
assert.Equal(t, true, ok)
// Tests telegraf duration parsing.
assert.Equal(t, internal.Duration{Duration: time.Second}, inputHTTPListener.WriteTimeout)
// Tests telegraf size parsing.
assert.Equal(t, internal.Size{Size: 1024 * 1024}, inputHTTPListener.MaxBodySize)
// Tests toml multiline basic strings.
assert.Equal(t, "/path/to/my/cert\n", inputHTTPListener.TLSCert)
}

func TestConfig_FieldNotDefined(t *testing.T) {
c := NewConfig()
err := c.LoadConfig("./testdata/invalid_field.toml")
require.Error(t, err, "invalid field name")
assert.Equal(t, "Error parsing ./testdata/invalid_field.toml, line 2: field corresponding to `not_a_field' is not defined in http_listener_v2.HTTPListenerV2", err.Error())

}

func TestConfig_WrongFieldType(t *testing.T) {
c := NewConfig()
err := c.LoadConfig("./testdata/wrong_field_type.toml")
require.Error(t, err, "invalid field type")
assert.Equal(t, "Error parsing ./testdata/wrong_field_type.toml, line 2: (http_listener_v2.HTTPListenerV2.Port) cannot unmarshal TOML string into int", err.Error())

c = NewConfig()
err = c.LoadConfig("./testdata/wrong_field_type2.toml")
glinton marked this conversation as resolved.
Show resolved Hide resolved
require.Error(t, err, "invalid field type2")
assert.Equal(t, "Error parsing ./testdata/wrong_field_type2.toml, line 2: (http_listener_v2.HTTPListenerV2.Methods) cannot unmarshal TOML string into []string", err.Error())
}

func TestConfig_InlineTables(t *testing.T) {
// #4098
c := NewConfig()
err := c.LoadConfig("./testdata/inline_table.toml")
assert.NoError(t, err)
require.Equal(t, 2, len(c.Outputs))

outputHTTP, ok := c.Outputs[1].Output.(*httpOut.HTTP)
assert.Equal(t, true, ok)
assert.Equal(t, map[string]string{"Authorization": "Token $TOKEN", "Content-Type": "application/json"}, outputHTTP.Headers)
assert.Equal(t, []string{"org_id"}, c.Outputs[0].Config.Filter.TagInclude)
}

func TestConfig_SliceComment(t *testing.T) {
t.Skipf("Skipping until #3642 is resolved")

c := NewConfig()
err := c.LoadConfig("./testdata/slice_comment.toml")
assert.NoError(t, err)
require.Equal(t, 1, len(c.Outputs))

outputHTTP, ok := c.Outputs[0].Output.(*httpOut.HTTP)
assert.Equal(t, []string{"test"}, outputHTTP.Scopes)
assert.Equal(t, true, ok)
}

func TestConfig_BadOrdering(t *testing.T) {
// #3444 defines an issue that is not currently an issue as it is technically invalid yaml.
glinton marked this conversation as resolved.
Show resolved Hide resolved

c := NewConfig()
err := c.LoadConfig("./testdata/non_slice_slice.toml")
require.Error(t, err, "bad ordering")
assert.Equal(t, "Error parsing ./testdata/non_slice_slice.toml, line 4: cannot unmarshal TOML array into string (need slice)", err.Error())
}
7 changes: 7 additions & 0 deletions internal/config/testdata/inline_table.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[[outputs.http]]
glinton marked this conversation as resolved.
Show resolved Hide resolved
headers = { Authorization = "Token $TOKEN",Content-Type = "application/json" }
taginclude = ["org_id"]

[[outputs.http]]
headers = { Authorization = "Token $TOKEN",Content-Type = "application/json" }
taginclude = ["org_id"]
2 changes: 2 additions & 0 deletions internal/config/testdata/invalid_field.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[[inputs.http_listener_v2]]
not_a_field = true
4 changes: 4 additions & 0 deletions internal/config/testdata/non_slice_slice.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[[outputs.http]]
[outputs.http.headers]
Content-Type = "application/json"
taginclude = ["org_id"]
5 changes: 5 additions & 0 deletions internal/config/testdata/slice_comment.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[[outputs.http]]
scopes = [
# comment
"test" # comment
]
9 changes: 9 additions & 0 deletions internal/config/testdata/special_types.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[[inputs.http_listener_v2]]
write_timeout = "1s"
max_body_size = "1MiB"
tls_cert = """
/path/to/my/cert
"""
tls_key = '''
/path/to/my/key
'''
2 changes: 2 additions & 0 deletions internal/config/testdata/wrong_field_type.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[[inputs.http_listener_v2]]
port = "80"
2 changes: 2 additions & 0 deletions internal/config/testdata/wrong_field_type2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[[inputs.http_listener_v2]]
methods = "POST"