2015-07-07 01:21:46 +00:00
|
|
|
package testutil
|
|
|
|
|
2015-08-03 23:16:02 +00:00
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2015-08-26 17:02:10 +00:00
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
"github.com/influxdb/influxdb/client/v2"
|
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
|
|
|
|
|
|
|
// MockBatchPoints returns a mock BatchPoints object for using in unit tests
|
|
|
|
// of telegraf output sinks.
|
|
|
|
func MockBatchPoints() client.BatchPoints {
|
2015-10-16 22:13:32 +00:00
|
|
|
// Create a new point batch
|
|
|
|
bp, _ := client.NewBatchPoints(client.BatchPointsConfig{})
|
|
|
|
|
|
|
|
// Create a point and add to batch
|
|
|
|
tags := map[string]string{"tag1": "value1"}
|
|
|
|
fields := map[string]interface{}{"value": 1.0}
|
|
|
|
pt := client.NewPoint("test_point", tags, fields)
|
|
|
|
bp.AddPoint(pt)
|
|
|
|
|
2015-08-26 17:02:10 +00:00
|
|
|
return bp
|
|
|
|
}
|