Fix new error return of client.NewPoint

This commit is contained in:
Cameron Sparr 2015-11-10 17:05:28 -07:00
parent 92fb51026a
commit 352ccde52b
6 changed files with 90 additions and 180 deletions

View File

@ -50,19 +50,13 @@ func TestAuthenticatedUrl(t *testing.T) {
} }
func TestBuildPoint(t *testing.T) { func TestBuildPoint(t *testing.T) {
tags := make(map[string]string)
var tagtests = []struct { var tagtests = []struct {
ptIn *client.Point ptIn *client.Point
outPt Point outPt Point
err error err error
}{ }{
{ {
client.NewPoint( testutil.TestPoint(float64(0.0)),
"test1",
tags,
map[string]interface{}{"value": 0.0},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
),
Point{ Point{
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
0.0, 0.0,
@ -70,25 +64,15 @@ func TestBuildPoint(t *testing.T) {
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint(float64(1.0)),
"test2",
tags,
map[string]interface{}{"value": 1.0},
time.Date(2010, time.December, 10, 23, 0, 0, 0, time.UTC),
),
Point{ Point{
float64(time.Date(2010, time.December, 10, 23, 0, 0, 0, time.UTC).Unix()), float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
1.0, 1.0,
}, },
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint(int(10)),
"test3",
tags,
map[string]interface{}{"value": 10},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
),
Point{ Point{
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
10.0, 10.0,
@ -96,12 +80,7 @@ func TestBuildPoint(t *testing.T) {
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint(int32(112345)),
"test4",
tags,
map[string]interface{}{"value": int32(112345)},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
),
Point{ Point{
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
112345.0, 112345.0,
@ -109,12 +88,7 @@ func TestBuildPoint(t *testing.T) {
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint(int64(112345)),
"test5",
tags,
map[string]interface{}{"value": int64(112345)},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
),
Point{ Point{
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
112345.0, 112345.0,
@ -122,12 +96,7 @@ func TestBuildPoint(t *testing.T) {
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint(float32(11234.5)),
"test6",
tags,
map[string]interface{}{"value": float32(11234.5)},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
),
Point{ Point{
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
11234.5, 11234.5,
@ -135,12 +104,7 @@ func TestBuildPoint(t *testing.T) {
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint("11234.5"),
"test7",
tags,
map[string]interface{}{"value": "11234.5"},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
),
Point{ Point{
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
11234.5, 11234.5,

View File

@ -99,19 +99,13 @@ func TestBuildTags(t *testing.T) {
} }
func TestBuildPoint(t *testing.T) { func TestBuildPoint(t *testing.T) {
tags := make(map[string]string)
var tagtests = []struct { var tagtests = []struct {
ptIn *client.Point ptIn *client.Point
outPt Point outPt Point
err error err error
}{ }{
{ {
client.NewPoint( testutil.TestPoint(0.0, "test1"),
"test1",
tags,
map[string]interface{}{"value": 0.0},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
),
Point{ Point{
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
0.0, 0.0,
@ -119,25 +113,15 @@ func TestBuildPoint(t *testing.T) {
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint(1.0, "test2"),
"test2",
tags,
map[string]interface{}{"value": 1.0},
time.Date(2010, time.December, 10, 23, 0, 0, 0, time.UTC),
),
Point{ Point{
float64(time.Date(2010, time.December, 10, 23, 0, 0, 0, time.UTC).Unix()), float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
1.0, 1.0,
}, },
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint(10, "test3"),
"test3",
tags,
map[string]interface{}{"value": 10},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
),
Point{ Point{
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
10.0, 10.0,
@ -145,12 +129,7 @@ func TestBuildPoint(t *testing.T) {
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint(int32(112345), "test4"),
"test4",
tags,
map[string]interface{}{"value": int32(112345)},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
),
Point{ Point{
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
112345.0, 112345.0,
@ -158,12 +137,7 @@ func TestBuildPoint(t *testing.T) {
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint(int64(112345), "test5"),
"test5",
tags,
map[string]interface{}{"value": int64(112345)},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
),
Point{ Point{
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
112345.0, 112345.0,
@ -171,12 +145,7 @@ func TestBuildPoint(t *testing.T) {
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint(float32(11234.5), "test6"),
"test6",
tags,
map[string]interface{}{"value": float32(11234.5)},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
),
Point{ Point{
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
11234.5, 11234.5,
@ -184,12 +153,7 @@ func TestBuildPoint(t *testing.T) {
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint("11234.5", "test7"),
"test7",
tags,
map[string]interface{}{"value": "11234.5"},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
),
Point{ Point{
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
11234.5, 11234.5,

View File

@ -70,47 +70,31 @@ func TestBadStatusCode(t *testing.T) {
} }
func TestBuildGauge(t *testing.T) { func TestBuildGauge(t *testing.T) {
tags := make(map[string]string)
var gaugeTests = []struct { var gaugeTests = []struct {
ptIn *client.Point ptIn *client.Point
outGauge *Gauge outGauge *Gauge
err error err error
}{ }{
{ {
client.NewPoint( testutil.TestPoint(0.0, "test1"),
"test1",
tags,
map[string]interface{}{"value": 0.0},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
),
&Gauge{ &Gauge{
Name: "test1", Name: "test1",
MeasureTime: time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(), MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
Value: 0.0, Value: 0.0,
}, },
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint(1.0, "test2"),
"test2",
tags,
map[string]interface{}{"value": 1.0},
time.Date(2010, time.December, 10, 23, 0, 0, 0, time.UTC),
),
&Gauge{ &Gauge{
Name: "test2", Name: "test2",
MeasureTime: time.Date(2010, time.December, 10, 23, 0, 0, 0, time.UTC).Unix(), MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
Value: 1.0, Value: 1.0,
}, },
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint(10, "test3"),
"test3",
tags,
map[string]interface{}{"value": 10},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
),
&Gauge{ &Gauge{
Name: "test3", Name: "test3",
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(), MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
@ -119,12 +103,7 @@ func TestBuildGauge(t *testing.T) {
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint(int32(112345), "test4"),
"test4",
tags,
map[string]interface{}{"value": int32(112345)},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
),
&Gauge{ &Gauge{
Name: "test4", Name: "test4",
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(), MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
@ -133,12 +112,7 @@ func TestBuildGauge(t *testing.T) {
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint(int64(112345), "test5"),
"test5",
tags,
map[string]interface{}{"value": int64(112345)},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
),
&Gauge{ &Gauge{
Name: "test5", Name: "test5",
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(), MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
@ -147,12 +121,7 @@ func TestBuildGauge(t *testing.T) {
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint(float32(11234.5), "test6"),
"test6",
tags,
map[string]interface{}{"value": float32(11234.5)},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
),
&Gauge{ &Gauge{
Name: "test6", Name: "test6",
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(), MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
@ -161,12 +130,7 @@ func TestBuildGauge(t *testing.T) {
nil, nil,
}, },
{ {
client.NewPoint( testutil.TestPoint("11234.5", "test7"),
"test7",
tags,
map[string]interface{}{"value": "11234.5"},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
),
&Gauge{ &Gauge{
Name: "test7", Name: "test7",
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(), MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
@ -192,18 +156,26 @@ func TestBuildGauge(t *testing.T) {
} }
func TestBuildGaugeWithSource(t *testing.T) { func TestBuildGaugeWithSource(t *testing.T) {
pt1, _ := client.NewPoint(
"test1",
map[string]string{"hostname": "192.168.0.1"},
map[string]interface{}{"value": 0.0},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
pt2, _ := client.NewPoint(
"test2",
map[string]string{"hostnam": "192.168.0.1"},
map[string]interface{}{"value": 1.0},
time.Date(2010, time.December, 10, 23, 0, 0, 0, time.UTC),
)
var gaugeTests = []struct { var gaugeTests = []struct {
ptIn *client.Point ptIn *client.Point
outGauge *Gauge outGauge *Gauge
err error err error
}{ }{
{ {
client.NewPoint( pt1,
"test1",
map[string]string{"hostname": "192.168.0.1"},
map[string]interface{}{"value": 0.0},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
),
&Gauge{ &Gauge{
Name: "test1", Name: "test1",
MeasureTime: time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(), MeasureTime: time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
@ -213,12 +185,7 @@ func TestBuildGaugeWithSource(t *testing.T) {
nil, nil,
}, },
{ {
client.NewPoint( pt2,
"test2",
map[string]string{"hostnam": "192.168.0.1"},
map[string]interface{}{"value": 1.0},
time.Date(2010, time.December, 10, 23, 0, 0, 0, time.UTC),
),
&Gauge{ &Gauge{
Name: "test2", Name: "test2",
MeasureTime: time.Date(2010, time.December, 10, 23, 0, 0, 0, time.UTC).Unix(), MeasureTime: time.Date(2010, time.December, 10, 23, 0, 0, 0, time.UTC).Unix(),

View File

@ -4,7 +4,6 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/influxdb/influxdb/client/v2"
"github.com/influxdb/telegraf/testutil" "github.com/influxdb/telegraf/testutil"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -60,17 +59,11 @@ func TestWrite(t *testing.T) {
// Verify postive and negative test cases of writing data // Verify postive and negative test cases of writing data
bp := testutil.MockBatchPoints() bp := testutil.MockBatchPoints()
tags := make(map[string]string) bp.AddPoint(testutil.TestPoint(float64(1.0), "justametric.float"))
bp.AddPoint(client.NewPoint("justametric.float", tags, bp.AddPoint(testutil.TestPoint(int64(123456789), "justametric.int"))
map[string]interface{}{"value": float64(1.0)})) bp.AddPoint(testutil.TestPoint(uint64(123456789012345), "justametric.uint"))
bp.AddPoint(client.NewPoint("justametric.int", tags, bp.AddPoint(testutil.TestPoint("Lorem Ipsum", "justametric.string"))
map[string]interface{}{"value": int64(123456789)})) bp.AddPoint(testutil.TestPoint(float64(42.0), "justametric.anotherfloat"))
bp.AddPoint(client.NewPoint("justametric.uint", tags,
map[string]interface{}{"value": uint64(123456789012345)}))
bp.AddPoint(client.NewPoint("justametric.string", tags,
map[string]interface{}{"value": "Lorem Ipsum"}))
bp.AddPoint(client.NewPoint("justametric.anotherfloat", tags,
map[string]interface{}{"value": float64(42.0)}))
err = o.Write(bp.Points()) err = o.Write(bp.Points())
require.NoError(t, err) require.NoError(t, err)

View File

@ -18,15 +18,17 @@ func TestPrometheusWritePointEmptyTag(t *testing.T) {
Urls: []string{"http://localhost:9126/metrics"}, Urls: []string{"http://localhost:9126/metrics"},
} }
tags := make(map[string]string) tags := make(map[string]string)
pt1, _ := client.NewPoint(
"test_point_1",
tags,
map[string]interface{}{"value": 0.0})
pt2, _ := client.NewPoint(
"test_point_2",
tags,
map[string]interface{}{"value": 1.0})
var points = []*client.Point{ var points = []*client.Point{
client.NewPoint( pt1,
"test_point_1", pt2,
tags,
map[string]interface{}{"value": 0.0}),
client.NewPoint(
"test_point_2",
tags,
map[string]interface{}{"value": 1.0}),
} }
require.NoError(t, pTesting.Write(points)) require.NoError(t, pTesting.Write(points))
@ -54,15 +56,17 @@ func TestPrometheusWritePointTag(t *testing.T) {
} }
tags := make(map[string]string) tags := make(map[string]string)
tags["testtag"] = "testvalue" tags["testtag"] = "testvalue"
pt1, _ := client.NewPoint(
"test_point_3",
tags,
map[string]interface{}{"value": 0.0})
pt2, _ := client.NewPoint(
"test_point_4",
tags,
map[string]interface{}{"value": 1.0})
var points = []*client.Point{ var points = []*client.Point{
client.NewPoint( pt1,
"test_point_3", pt2,
tags,
map[string]interface{}{"value": 0.0}),
client.NewPoint(
"test_point_4",
tags,
map[string]interface{}{"value": 1.0}),
} }
require.NoError(t, pTesting.Write(points)) require.NoError(t, pTesting.Write(points))

View File

@ -4,6 +4,7 @@ import (
"net" "net"
"net/url" "net/url"
"os" "os"
"time"
"github.com/influxdb/influxdb/client/v2" "github.com/influxdb/influxdb/client/v2"
) )
@ -35,12 +36,29 @@ func GetLocalHost() string {
func MockBatchPoints() client.BatchPoints { func MockBatchPoints() client.BatchPoints {
// Create a new point batch // Create a new point batch
bp, _ := client.NewBatchPoints(client.BatchPointsConfig{}) bp, _ := client.NewBatchPoints(client.BatchPointsConfig{})
bp.AddPoint(TestPoint(1.0))
// 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)
return bp return bp
} }
// TestPoint Returns a simple test point:
// measurement -> "test1" or name
// tags -> "tag1":"value1"
// value -> value
// time -> time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
func TestPoint(value interface{}, name ...string) *client.Point {
if value == nil {
panic("Cannot use a nil value")
}
measurement := "test1"
if len(name) > 0 {
measurement = name[0]
}
tags := map[string]string{"tag1": "value1"}
pt, _ := client.NewPoint(
measurement,
tags,
map[string]interface{}{"value": value},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
)
return pt
}