2015-07-07 01:21:46 +00:00
|
|
|
package testutil
|
|
|
|
|
2015-08-03 23:16:02 +00:00
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2015-11-11 00:05:28 +00:00
|
|
|
"time"
|
2015-08-26 17:02:10 +00:00
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2015-08-03 23:16:02 +00:00
|
|
|
)
|
2015-07-07 01:21:46 +00:00
|
|
|
|
|
|
|
var localhost = "localhost"
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// GetLocalHost returns the DOCKER_HOST environment variable, parsing
|
|
|
|
// out any scheme or ports so that only the IP address is returned.
|
2015-07-07 01:21:46 +00:00
|
|
|
func GetLocalHost() string {
|
|
|
|
if dockerHostVar := os.Getenv("DOCKER_HOST"); dockerHostVar != "" {
|
2015-08-03 23:16:02 +00:00
|
|
|
u, err := url.Parse(dockerHostVar)
|
|
|
|
if err != nil {
|
|
|
|
return dockerHostVar
|
|
|
|
}
|
|
|
|
|
|
|
|
// split out the ip addr from the port
|
|
|
|
host, _, err := net.SplitHostPort(u.Host)
|
|
|
|
if err != nil {
|
|
|
|
return dockerHostVar
|
|
|
|
}
|
|
|
|
|
|
|
|
return host
|
2015-07-07 01:21:46 +00:00
|
|
|
}
|
|
|
|
return localhost
|
|
|
|
}
|
2015-08-26 17:02:10 +00:00
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
// MockMetrics returns a mock []telegraf.Metric object for using in unit tests
|
2015-08-26 17:02:10 +00:00
|
|
|
// of telegraf output sinks.
|
2016-01-27 23:15:14 +00:00
|
|
|
func MockMetrics() []telegraf.Metric {
|
|
|
|
metrics := make([]telegraf.Metric, 0)
|
2015-10-16 22:13:32 +00:00
|
|
|
// Create a new point batch
|
2016-01-27 23:15:14 +00:00
|
|
|
metrics = append(metrics, TestMetric(1.0))
|
|
|
|
return metrics
|
2015-11-11 00:05:28 +00:00
|
|
|
}
|
2015-10-16 22:13:32 +00:00
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
// TestMetric Returns a simple test point:
|
2015-11-11 00:05:28 +00:00
|
|
|
// measurement -> "test1" or name
|
|
|
|
// tags -> "tag1":"value1"
|
|
|
|
// value -> value
|
|
|
|
// time -> time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
|
2016-01-27 23:15:14 +00:00
|
|
|
func TestMetric(value interface{}, name ...string) telegraf.Metric {
|
2015-11-11 00:05:28 +00:00
|
|
|
if value == nil {
|
|
|
|
panic("Cannot use a nil value")
|
|
|
|
}
|
|
|
|
measurement := "test1"
|
|
|
|
if len(name) > 0 {
|
|
|
|
measurement = name[0]
|
|
|
|
}
|
2015-10-16 22:13:32 +00:00
|
|
|
tags := map[string]string{"tag1": "value1"}
|
2016-01-27 23:15:14 +00:00
|
|
|
pt, _ := telegraf.NewMetric(
|
2015-11-11 00:05:28 +00:00
|
|
|
measurement,
|
|
|
|
tags,
|
|
|
|
map[string]interface{}{"value": value},
|
|
|
|
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
|
|
|
|
)
|
|
|
|
return pt
|
2015-08-26 17:02:10 +00:00
|
|
|
}
|