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-05-04 23:33:23 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/tls"
|
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
|
|
|
|
WriteF func(context.Context, []telegraf.Metric) error
|
2019-02-27 18:54:02 +00:00
|
|
|
CreateDatabaseF func(ctx context.Context, database string) error
|
|
|
|
DatabaseF func() string
|
2019-05-31 23:55:31 +00:00
|
|
|
CloseF func()
|
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) Write(ctx context.Context, metrics []telegraf.Metric) error {
|
|
|
|
return c.WriteF(ctx, metrics)
|
2016-12-04 20:18:13 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 18:54:02 +00:00
|
|
|
func (c *MockClient) CreateDatabase(ctx context.Context, database string) error {
|
|
|
|
return c.CreateDatabaseF(ctx, database)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MockClient) Database() string {
|
|
|
|
return c.DatabaseF()
|
2016-12-04 20:18:13 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 23:55:31 +00:00
|
|
|
func (c *MockClient) Close() {
|
|
|
|
c.CloseF()
|
|
|
|
}
|
|
|
|
|
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{
|
2019-02-27 18:54:02 +00:00
|
|
|
DatabaseF: func() string {
|
|
|
|
return "telegraf"
|
|
|
|
},
|
|
|
|
CreateDatabaseF: func(ctx context.Context, database string) error {
|
2018-03-28 00:30:51 +00:00
|
|
|
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-10-19 18:17:18 +00:00
|
|
|
UDPPayload: internal.Size{Size: 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",
|
|
|
|
},
|
2018-05-04 23:33:23 +00:00
|
|
|
ContentEncoding: "gzip",
|
|
|
|
ClientConfig: tls.ClientConfig{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
2018-03-28 00:30:51 +00:00
|
|
|
|
|
|
|
CreateHTTPClientF: func(config *influxdb.HTTPConfig) (influxdb.Client, error) {
|
|
|
|
actual = config
|
|
|
|
return &MockClient{
|
2019-02-27 18:54:02 +00:00
|
|
|
DatabaseF: func() string {
|
|
|
|
return "telegraf"
|
|
|
|
},
|
|
|
|
CreateDatabaseF: func(ctx context.Context, database string) error {
|
2018-03-28 00:30:51 +00:00
|
|
|
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) {
|
|
|
|
output := influxdb.InfluxDB{
|
|
|
|
URLs: []string{"http://localhost:8086"},
|
|
|
|
|
|
|
|
CreateHTTPClientF: func(config *influxdb.HTTPConfig) (influxdb.Client, error) {
|
|
|
|
return &MockClient{
|
2019-02-27 18:54:02 +00:00
|
|
|
DatabaseF: func() string {
|
|
|
|
return "telegraf"
|
|
|
|
},
|
|
|
|
CreateDatabaseF: func(ctx context.Context, database string) error {
|
2018-04-02 23:18:33 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
WriteF: func(ctx context.Context, metrics []telegraf.Metric) error {
|
2019-02-27 18:54:02 +00:00
|
|
|
return &influxdb.DatabaseNotFoundError{
|
|
|
|
APIError: influxdb.APIError{
|
|
|
|
StatusCode: http.StatusNotFound,
|
|
|
|
Title: "404 Not Found",
|
|
|
|
Description: `database not found "telegraf"`,
|
|
|
|
},
|
2018-04-02 23:18:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
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}
|
|
|
|
|
|
|
|
err = output.Write(metrics)
|
|
|
|
// We only have one URL, so we expect an error
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|