Fix bug preventing database from being recreated (#3962)
This commit is contained in:
parent
413cf6dd23
commit
265d0e6d84
|
@ -210,7 +210,7 @@ func (i *InfluxDB) Write(metrics []telegraf.Metric) error {
|
|||
}
|
||||
|
||||
switch apiError := err.(type) {
|
||||
case APIError:
|
||||
case *APIError:
|
||||
if !i.SkipDatabaseCreation {
|
||||
if apiError.Type == DatabaseNotFound {
|
||||
err := client.CreateDatabase(ctx)
|
||||
|
|
|
@ -2,11 +2,13 @@ package influxdb_test
|
|||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/internal"
|
||||
"github.com/influxdata/telegraf/metric"
|
||||
"github.com/influxdata/telegraf/plugins/outputs/influxdb"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
@ -37,7 +39,7 @@ func (c *MockClient) CreateDatabase(ctx context.Context) error {
|
|||
func TestDeprecatedURLSupport(t *testing.T) {
|
||||
var actual *influxdb.UDPConfig
|
||||
output := influxdb.InfluxDB{
|
||||
URL: "udp://localhost:8086",
|
||||
URL: "udp://localhost:8089",
|
||||
|
||||
CreateUDPClientF: func(config *influxdb.UDPConfig) (influxdb.Client, error) {
|
||||
actual = config
|
||||
|
@ -46,7 +48,7 @@ func TestDeprecatedURLSupport(t *testing.T) {
|
|||
}
|
||||
err := output.Connect()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "udp://localhost:8086", actual.URL.String())
|
||||
require.Equal(t, "udp://localhost:8089", actual.URL.String())
|
||||
}
|
||||
|
||||
func TestDefaultURL(t *testing.T) {
|
||||
|
@ -70,7 +72,7 @@ func TestConnectUDPConfig(t *testing.T) {
|
|||
var actual *influxdb.UDPConfig
|
||||
|
||||
output := influxdb.InfluxDB{
|
||||
URLs: []string{"udp://localhost:8086"},
|
||||
URLs: []string{"udp://localhost:8089"},
|
||||
UDPPayload: 42,
|
||||
|
||||
CreateUDPClientF: func(config *influxdb.UDPConfig) (influxdb.Client, error) {
|
||||
|
@ -81,7 +83,7 @@ func TestConnectUDPConfig(t *testing.T) {
|
|||
err := output.Connect()
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, "udp://localhost:8086", actual.URL.String())
|
||||
require.Equal(t, "udp://localhost:8089", actual.URL.String())
|
||||
require.Equal(t, 42, actual.MaxPayloadSize)
|
||||
require.NotNil(t, actual.Serializer)
|
||||
}
|
||||
|
@ -90,7 +92,7 @@ func TestConnectHTTPConfig(t *testing.T) {
|
|||
var actual *influxdb.HTTPConfig
|
||||
|
||||
output := influxdb.InfluxDB{
|
||||
URLs: []string{"http://localhost:8089"},
|
||||
URLs: []string{"http://localhost:8086"},
|
||||
Database: "telegraf",
|
||||
RetentionPolicy: "default",
|
||||
WriteConsistency: "any",
|
||||
|
@ -98,7 +100,7 @@ func TestConnectHTTPConfig(t *testing.T) {
|
|||
Username: "guy",
|
||||
Password: "smiley",
|
||||
UserAgent: "telegraf",
|
||||
HTTPProxy: "http://localhost:8089",
|
||||
HTTPProxy: "http://localhost:8086",
|
||||
HTTPHeaders: map[string]string{
|
||||
"x": "y",
|
||||
},
|
||||
|
@ -133,3 +135,51 @@ func TestConnectHTTPConfig(t *testing.T) {
|
|||
|
||||
require.Equal(t, output.Database, actual.Database)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue