From 64b98a9b61ba85f975823aab0ae6e7c59c3b323e Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Thu, 7 Jan 2016 10:52:46 -0700 Subject: [PATCH] 0.3.0 unit tests: agent and prometheus --- agent_test.go | 41 +++++++++---- internal/config/testdata/telegraf-agent.toml | 61 ++++++------------- .../prometheus_client_test.go | 10 +-- 3 files changed, 53 insertions(+), 59 deletions(-) diff --git a/agent_test.go b/agent_test.go index 7dd65ef26..c08fbe292 100644 --- a/agent_test.go +++ b/agent_test.go @@ -16,31 +16,36 @@ import ( func TestAgent_LoadPlugin(t *testing.T) { c := config.NewConfig() c.PluginFilters = []string{"mysql"} - c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + err := c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + assert.NoError(t, err) a, _ := NewAgent(c) assert.Equal(t, 1, len(a.Config.Plugins)) c = config.NewConfig() c.PluginFilters = []string{"foo"} - c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + assert.NoError(t, err) a, _ = NewAgent(c) assert.Equal(t, 0, len(a.Config.Plugins)) c = config.NewConfig() c.PluginFilters = []string{"mysql", "foo"} - c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + assert.NoError(t, err) a, _ = NewAgent(c) assert.Equal(t, 1, len(a.Config.Plugins)) c = config.NewConfig() c.PluginFilters = []string{"mysql", "redis"} - c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + assert.NoError(t, err) a, _ = NewAgent(c) assert.Equal(t, 2, len(a.Config.Plugins)) c = config.NewConfig() c.PluginFilters = []string{"mysql", "foo", "redis", "bar"} - c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + assert.NoError(t, err) a, _ = NewAgent(c) assert.Equal(t, 2, len(a.Config.Plugins)) } @@ -48,37 +53,51 @@ func TestAgent_LoadPlugin(t *testing.T) { func TestAgent_LoadOutput(t *testing.T) { c := config.NewConfig() c.OutputFilters = []string{"influxdb"} - c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + err := c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + assert.NoError(t, err) a, _ := NewAgent(c) assert.Equal(t, 2, len(a.Config.Outputs)) + c = config.NewConfig() + c.OutputFilters = []string{"kafka"} + err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + assert.NoError(t, err) + a, _ = NewAgent(c) + assert.Equal(t, 1, len(a.Config.Outputs)) + c = config.NewConfig() c.OutputFilters = []string{} - c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + assert.NoError(t, err) a, _ = NewAgent(c) assert.Equal(t, 3, len(a.Config.Outputs)) c = config.NewConfig() c.OutputFilters = []string{"foo"} - c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + assert.NoError(t, err) a, _ = NewAgent(c) assert.Equal(t, 0, len(a.Config.Outputs)) c = config.NewConfig() c.OutputFilters = []string{"influxdb", "foo"} - c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + assert.NoError(t, err) a, _ = NewAgent(c) assert.Equal(t, 2, len(a.Config.Outputs)) c = config.NewConfig() c.OutputFilters = []string{"influxdb", "kafka"} - c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + assert.NoError(t, err) + assert.Equal(t, 3, len(c.Outputs)) a, _ = NewAgent(c) assert.Equal(t, 3, len(a.Config.Outputs)) c = config.NewConfig() c.OutputFilters = []string{"influxdb", "foo", "kafka", "bar"} - c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml") + assert.NoError(t, err) a, _ = NewAgent(c) assert.Equal(t, 3, len(a.Config.Outputs)) } diff --git a/internal/config/testdata/telegraf-agent.toml b/internal/config/testdata/telegraf-agent.toml index 42ce89cd8..7b8cdb2bb 100644 --- a/internal/config/testdata/telegraf-agent.toml +++ b/internal/config/testdata/telegraf-agent.toml @@ -21,20 +21,13 @@ # Tags can also be specified via a normal map, but only one form at a time: [tags] - # dc = "us-east-1" + dc = "us-east-1" # Configuration for telegraf agent [agent] # Default data collection interval for all plugins interval = "10s" - # If utc = false, uses local time (utc is highly recommended) - utc = true - - # Precision of writes, valid values are n, u, ms, s, m, and h - # note: using second precision greatly helps InfluxDB compression - precision = "s" - # run telegraf in debug mode debug = false @@ -58,17 +51,6 @@ # The target database for metrics. This database must already exist database = "telegraf" # required. - # Connection timeout (for the connection with InfluxDB), formatted as a string. - # Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". - # If not provided, will default to 0 (no timeout) - # timeout = "5s" - - # username = "telegraf" - # password = "metricsmetricsmetricsmetrics" - - # Set the user agent for the POSTs (can be useful for log differentiation) - # user_agent = "telegraf" - [[outputs.influxdb]] urls = ["udp://localhost:8089"] database = "udp-telegraf" @@ -92,8 +74,8 @@ # Read Apache status information (mod_status) [[plugins.apache]] -# An array of Apache status URI to gather stats. -urls = ["http://localhost/server-status?auto"] + # An array of Apache status URI to gather stats. + urls = ["http://localhost/server-status?auto"] # Read metrics about cpu usage [[plugins.cpu]] @@ -128,8 +110,6 @@ urls = ["http://localhost/server-status?auto"] # Read flattened metrics from one or more commands that output JSON to stdout [[plugins.exec]] - # specify commands via an array of tables - [[exec.commands]] # the command to run command = "/usr/bin/mycollector --foo=bar" @@ -148,28 +128,25 @@ urls = ["http://localhost/server-status?auto"] # Read flattened metrics from one or more JSON HTTP endpoints [[plugins.httpjson]] - # Specify services via an array of tables - [[httpjson.services]] + # a name for the service being polled + name = "webserver_stats" - # a name for the service being polled - name = "webserver_stats" + # URL of each server in the service's cluster + servers = [ + "http://localhost:9999/stats/", + "http://localhost:9998/stats/", + ] - # URL of each server in the service's cluster - servers = [ - "http://localhost:9999/stats/", - "http://localhost:9998/stats/", - ] + # HTTP method to use (case-sensitive) + method = "GET" - # HTTP method to use (case-sensitive) - method = "GET" - - # HTTP parameters (all values must be strings) - [httpjson.services.parameters] - event_type = "cpu_spike" - threshold = "0.75" + # HTTP parameters (all values must be strings) + [httpjson.parameters] + event_type = "cpu_spike" + threshold = "0.75" # Read metrics about disk IO by device -[[plugins.io]] +[[plugins.diskio]] # no configuration # read metrics from a Kafka topic @@ -261,9 +238,6 @@ urls = ["http://localhost/server-status?auto"] # Read metrics from one or many postgresql servers [[plugins.postgresql]] - # specify servers via an array of tables - [[postgresql.servers]] - # specify address via a url matching: # postgres://[pqgotest[:password]]@localhost[/dbname]?sslmode=[disable|verify-ca|verify-full] # or a simple string: @@ -297,7 +271,6 @@ urls = ["http://localhost/server-status?auto"] # Read metrics from one or many RabbitMQ servers via the management API [[plugins.rabbitmq]] # Specify servers via an array of tables - [[rabbitmq.servers]] # name = "rmq-server-1" # optional tag # url = "http://localhost:15672" # username = "guest" diff --git a/outputs/prometheus_client/prometheus_client_test.go b/outputs/prometheus_client/prometheus_client_test.go index 6bb1ec614..53adcac17 100644 --- a/outputs/prometheus_client/prometheus_client_test.go +++ b/outputs/prometheus_client/prometheus_client_test.go @@ -3,11 +3,11 @@ package prometheus_client import ( "testing" + "github.com/stretchr/testify/require" + "github.com/influxdb/influxdb/client/v2" "github.com/influxdb/telegraf/plugins/prometheus" "github.com/influxdb/telegraf/testutil" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) var pTesting *PrometheusClient @@ -48,7 +48,8 @@ func TestPrometheusWritePointEmptyTag(t *testing.T) { require.NoError(t, p.Gather(&acc)) for _, e := range expected { - assert.NoError(t, acc.ValidateValue(e.name, e.value)) + acc.AssertContainsFields(t, "prometheus_"+e.name, + map[string]interface{}{"value": e.value}) } } @@ -88,7 +89,8 @@ func TestPrometheusWritePointTag(t *testing.T) { require.NoError(t, p.Gather(&acc)) for _, e := range expected { - assert.True(t, acc.CheckTaggedValue(e.name, e.value, tags)) + acc.AssertContainsFields(t, "prometheus_"+e.name, + map[string]interface{}{"value": e.value}) } }