Skip to content

Commit

Permalink
Fix bug preventing database from being recreated (#3962)
Browse files Browse the repository at this point in the history
(cherry picked from commit 265d0e6)
  • Loading branch information
danielnelson committed Apr 2, 2018
1 parent 7b23287 commit 1e95f97
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion plugins/outputs/influxdb/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
62 changes: 56 additions & 6 deletions plugins/outputs/influxdb/influxdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
}
Expand All @@ -90,15 +92,15 @@ 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",
Timeout: internal.Duration{Duration: 5 * time.Second},
Username: "guy",
Password: "smiley",
UserAgent: "telegraf",
HTTPProxy: "http://localhost:8089",
HTTPProxy: "http://localhost:8086",
HTTPHeaders: map[string]string{
"x": "y",
},
Expand Down Expand Up @@ -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)
}

0 comments on commit 1e95f97

Please sign in to comment.