2018-03-28 00:30:51 +00:00
|
|
|
package influxdb_test
|
2015-11-13 17:39:36 +00:00
|
|
|
|
|
|
|
import (
|
2018-03-28 00:30:51 +00:00
|
|
|
"context"
|
2018-04-02 23:18:33 +00:00
|
|
|
"net/http"
|
2015-11-13 17:39:36 +00:00
|
|
|
"testing"
|
2018-03-28 00:30:51 +00:00
|
|
|
"time"
|
2015-11-13 17:39:36 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/internal"
|
2018-04-02 23:18:33 +00:00
|
|
|
"github.com/influxdata/telegraf/metric"
|
2018-03-28 00:30:51 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs/influxdb"
|
2015-11-13 17:39:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
type MockClient struct {
|
|
|
|
URLF func() string
|
|
|
|
DatabaseF func() string
|
|
|
|
WriteF func(context.Context, []telegraf.Metric) error
|
|
|
|
CreateDatabaseF func(ctx context.Context) error
|
2017-05-25 20:25:52 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (c *MockClient) URL() string {
|
|
|
|
return c.URLF()
|
2015-11-13 17:39:36 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (c *MockClient) Database() string {
|
|
|
|
return c.DatabaseF()
|
2016-12-04 20:18:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (c *MockClient) Write(ctx context.Context, metrics []telegraf.Metric) error {
|
|
|
|
return c.WriteF(ctx, metrics)
|
2016-12-04 20:18:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func (c *MockClient) CreateDatabase(ctx context.Context) error {
|
|
|
|
return c.CreateDatabaseF(ctx)
|
2016-12-04 20:18:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func TestDeprecatedURLSupport(t *testing.T) {
|
|
|
|
var actual *influxdb.UDPConfig
|
|
|
|
output := influxdb.InfluxDB{
|
2018-04-02 23:18:33 +00:00
|
|
|
URL: "udp://localhost:8089",
|
2016-12-04 20:18:13 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
CreateUDPClientF: func(config *influxdb.UDPConfig) (influxdb.Client, error) {
|
|
|
|
actual = config
|
|
|
|
return &MockClient{}, nil
|
|
|
|
},
|
2016-12-04 20:18:13 +00:00
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
err := output.Connect()
|
2015-11-13 17:39:36 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-02 23:18:33 +00:00
|
|
|
require.Equal(t, "udp://localhost:8089", actual.URL.String())
|
2016-12-04 20:18:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func TestDefaultURL(t *testing.T) {
|
|
|
|
var actual *influxdb.HTTPConfig
|
|
|
|
output := influxdb.InfluxDB{
|
|
|
|
CreateHTTPClientF: func(config *influxdb.HTTPConfig) (influxdb.Client, error) {
|
|
|
|
actual = config
|
|
|
|
return &MockClient{
|
|
|
|
CreateDatabaseF: func(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
2016-12-04 20:18:13 +00:00
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
err := output.Connect()
|
2015-11-13 17:39:36 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-28 00:30:51 +00:00
|
|
|
require.Equal(t, "http://localhost:8086", actual.URL.String())
|
2015-11-13 17:39:36 +00:00
|
|
|
}
|
2017-01-11 16:01:32 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func TestConnectUDPConfig(t *testing.T) {
|
|
|
|
var actual *influxdb.UDPConfig
|
2017-08-22 23:52:26 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
output := influxdb.InfluxDB{
|
2018-04-02 23:18:33 +00:00
|
|
|
URLs: []string{"udp://localhost:8089"},
|
2018-03-28 00:30:51 +00:00
|
|
|
UDPPayload: 42,
|
2017-08-22 23:52:26 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
CreateUDPClientF: func(config *influxdb.UDPConfig) (influxdb.Client, error) {
|
|
|
|
actual = config
|
|
|
|
return &MockClient{}, nil
|
|
|
|
},
|
2017-08-22 23:52:26 +00:00
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
err := output.Connect()
|
|
|
|
require.NoError(t, err)
|
2017-05-31 00:38:32 +00:00
|
|
|
|
2018-04-02 23:18:33 +00:00
|
|
|
require.Equal(t, "udp://localhost:8089", actual.URL.String())
|
2018-03-28 00:30:51 +00:00
|
|
|
require.Equal(t, 42, actual.MaxPayloadSize)
|
|
|
|
require.NotNil(t, actual.Serializer)
|
2017-05-31 00:38:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func TestConnectHTTPConfig(t *testing.T) {
|
|
|
|
var actual *influxdb.HTTPConfig
|
|
|
|
|
|
|
|
output := influxdb.InfluxDB{
|
2018-04-02 23:18:33 +00:00
|
|
|
URLs: []string{"http://localhost:8086"},
|
2018-03-28 00:30:51 +00:00
|
|
|
Database: "telegraf",
|
|
|
|
RetentionPolicy: "default",
|
|
|
|
WriteConsistency: "any",
|
|
|
|
Timeout: internal.Duration{Duration: 5 * time.Second},
|
|
|
|
Username: "guy",
|
|
|
|
Password: "smiley",
|
|
|
|
UserAgent: "telegraf",
|
2018-04-02 23:18:33 +00:00
|
|
|
HTTPProxy: "http://localhost:8086",
|
2018-03-28 00:30:51 +00:00
|
|
|
HTTPHeaders: map[string]string{
|
|
|
|
"x": "y",
|
|
|
|
},
|
|
|
|
ContentEncoding: "gzip",
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
|
|
|
|
CreateHTTPClientF: func(config *influxdb.HTTPConfig) (influxdb.Client, error) {
|
|
|
|
actual = config
|
|
|
|
return &MockClient{
|
|
|
|
CreateDatabaseF: func(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := output.Connect()
|
|
|
|
require.NoError(t, err)
|
2017-05-31 00:38:32 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
require.Equal(t, output.URLs[0], actual.URL.String())
|
|
|
|
require.Equal(t, output.UserAgent, actual.UserAgent)
|
|
|
|
require.Equal(t, output.Timeout.Duration, actual.Timeout)
|
|
|
|
require.Equal(t, output.Username, actual.Username)
|
|
|
|
require.Equal(t, output.Password, actual.Password)
|
|
|
|
require.Equal(t, output.HTTPProxy, actual.Proxy.String())
|
|
|
|
require.Equal(t, output.HTTPHeaders, actual.Headers)
|
|
|
|
require.Equal(t, output.ContentEncoding, actual.ContentEncoding)
|
|
|
|
require.Equal(t, output.Database, actual.Database)
|
|
|
|
require.Equal(t, output.RetentionPolicy, actual.RetentionPolicy)
|
|
|
|
require.Equal(t, output.WriteConsistency, actual.Consistency)
|
|
|
|
require.NotNil(t, actual.TLSConfig)
|
|
|
|
require.NotNil(t, actual.Serializer)
|
|
|
|
|
|
|
|
require.Equal(t, output.Database, actual.Database)
|
2017-05-31 00:38:32 +00:00
|
|
|
}
|
2018-04-02 23:18:33 +00:00
|
|
|
|
|
|
|
func TestWriteRecreateDatabaseIfDatabaseNotFound(t *testing.T) {
|
|
|
|
var createDatabaseCalled bool
|
|
|
|
|
|
|
|
output := influxdb.InfluxDB{
|
|
|
|
URLs: []string{"http://localhost:8086"},
|
|
|
|
|
|
|
|
CreateHTTPClientF: func(config *influxdb.HTTPConfig) (influxdb.Client, error) {
|
|
|
|
return &MockClient{
|
|
|
|
CreateDatabaseF: func(ctx context.Context) error {
|
|
|
|
createDatabaseCalled = true
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
WriteF: func(ctx context.Context, metrics []telegraf.Metric) error {
|
|
|
|
return &influxdb.APIError{
|
|
|
|
StatusCode: http.StatusNotFound,
|
|
|
|
Title: "404 Not Found",
|
|
|
|
Description: `database not found "telegraf"`,
|
|
|
|
Type: influxdb.DatabaseNotFound,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
URLF: func() string {
|
|
|
|
return "http://localhost:8086"
|
|
|
|
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := output.Connect()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err := metric.New(
|
|
|
|
"cpu",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
metrics := []telegraf.Metric{m}
|
|
|
|
|
|
|
|
createDatabaseCalled = false
|
|
|
|
err = output.Write(metrics)
|
|
|
|
// We only have one URL, so we expect an error
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|