2015-10-26 18:31:21 +00:00
|
|
|
package librato
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-02-26 20:06:56 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/serializers/graphite"
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2015-10-26 18:31:21 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
fakeUrl = "http://test.librato.com"
|
|
|
|
fakeUser = "telegraf@influxdb.com"
|
|
|
|
fakeToken = "123456"
|
|
|
|
)
|
|
|
|
|
|
|
|
func fakeLibrato() *Librato {
|
|
|
|
l := NewLibrato(fakeUrl)
|
|
|
|
l.ApiUser = fakeUser
|
|
|
|
l.ApiToken = fakeToken
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2016-02-26 20:06:56 +00:00
|
|
|
func BuildTags(t *testing.T) {
|
|
|
|
testMetric := testutil.TestMetric(0.0, "test1")
|
|
|
|
graphiteSerializer := graphite.GraphiteSerializer{}
|
|
|
|
tags, err := graphiteSerializer.Serialize(testMetric)
|
|
|
|
fmt.Printf("Tags: %v", tags)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2015-10-26 18:31:21 +00:00
|
|
|
func TestUriOverride(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
l := NewLibrato(ts.URL)
|
|
|
|
l.ApiUser = "telegraf@influxdb.com"
|
|
|
|
l.ApiToken = "123456"
|
|
|
|
err := l.Connect()
|
|
|
|
require.NoError(t, err)
|
2016-01-27 23:15:14 +00:00
|
|
|
err = l.Write(testutil.MockMetrics())
|
2015-10-26 18:31:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadStatusCode(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
|
|
json.NewEncoder(w).Encode(`{
|
|
|
|
"errors": {
|
|
|
|
"system": [
|
|
|
|
"The API is currently down for maintenance. It'll be back shortly."
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
l := NewLibrato(ts.URL)
|
|
|
|
l.ApiUser = "telegraf@influxdb.com"
|
|
|
|
l.ApiToken = "123456"
|
|
|
|
err := l.Connect()
|
|
|
|
require.NoError(t, err)
|
2016-01-27 23:15:14 +00:00
|
|
|
err = l.Write(testutil.MockMetrics())
|
2015-10-26 18:31:21 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("error expected but none returned")
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, fmt.Errorf("received bad status code, 503\n"), err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildGauge(t *testing.T) {
|
|
|
|
var gaugeTests = []struct {
|
2016-01-27 23:15:14 +00:00
|
|
|
ptIn telegraf.Metric
|
2015-10-26 18:31:21 +00:00
|
|
|
outGauge *Gauge
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
2016-01-27 23:15:14 +00:00
|
|
|
testutil.TestMetric(0.0, "test1"),
|
2015-10-26 18:31:21 +00:00
|
|
|
&Gauge{
|
2016-04-08 22:04:45 +00:00
|
|
|
Name: "value1.test1",
|
2015-11-11 00:05:28 +00:00
|
|
|
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
|
2015-10-26 18:31:21 +00:00
|
|
|
Value: 0.0,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2016-01-27 23:15:14 +00:00
|
|
|
testutil.TestMetric(1.0, "test2"),
|
2015-10-26 18:31:21 +00:00
|
|
|
&Gauge{
|
2016-04-08 22:04:45 +00:00
|
|
|
Name: "value1.test2",
|
2015-11-11 00:05:28 +00:00
|
|
|
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
|
2015-10-26 18:31:21 +00:00
|
|
|
Value: 1.0,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2016-01-27 23:15:14 +00:00
|
|
|
testutil.TestMetric(10, "test3"),
|
2015-10-26 18:31:21 +00:00
|
|
|
&Gauge{
|
2016-04-08 22:04:45 +00:00
|
|
|
Name: "value1.test3",
|
2015-10-26 18:31:21 +00:00
|
|
|
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
|
|
|
|
Value: 10.0,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2016-01-27 23:15:14 +00:00
|
|
|
testutil.TestMetric(int32(112345), "test4"),
|
2015-10-26 18:31:21 +00:00
|
|
|
&Gauge{
|
2016-04-08 22:04:45 +00:00
|
|
|
Name: "value1.test4",
|
2015-10-26 18:31:21 +00:00
|
|
|
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
|
|
|
|
Value: 112345.0,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2016-01-27 23:15:14 +00:00
|
|
|
testutil.TestMetric(int64(112345), "test5"),
|
2015-10-26 18:31:21 +00:00
|
|
|
&Gauge{
|
2016-04-08 22:04:45 +00:00
|
|
|
Name: "value1.test5",
|
2015-10-26 18:31:21 +00:00
|
|
|
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
|
|
|
|
Value: 112345.0,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2016-01-27 23:15:14 +00:00
|
|
|
testutil.TestMetric(float32(11234.5), "test6"),
|
2015-10-26 18:31:21 +00:00
|
|
|
&Gauge{
|
2016-04-08 22:04:45 +00:00
|
|
|
Name: "value1.test6",
|
2015-10-26 18:31:21 +00:00
|
|
|
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
|
|
|
|
Value: 11234.5,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2016-01-27 23:15:14 +00:00
|
|
|
testutil.TestMetric("11234.5", "test7"),
|
2016-03-22 15:07:01 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
2015-10-26 18:31:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
l := NewLibrato(fakeUrl)
|
|
|
|
for _, gt := range gaugeTests {
|
2016-01-07 17:09:04 +00:00
|
|
|
gauges, err := l.buildGauges(gt.ptIn)
|
2015-10-26 18:31:21 +00:00
|
|
|
if err != nil && gt.err == nil {
|
|
|
|
t.Errorf("%s: unexpected error, %+v\n", gt.ptIn.Name(), err)
|
|
|
|
}
|
|
|
|
if gt.err != nil && err == nil {
|
2016-01-07 17:09:04 +00:00
|
|
|
t.Errorf("%s: expected an error (%s) but none returned",
|
|
|
|
gt.ptIn.Name(), gt.err.Error())
|
|
|
|
}
|
2016-03-22 15:07:01 +00:00
|
|
|
if len(gauges) != 0 && gt.outGauge == nil {
|
|
|
|
t.Errorf("%s: unexpected gauge, %+v\n", gt.ptIn.Name(), gt.outGauge)
|
|
|
|
}
|
2016-01-07 17:09:04 +00:00
|
|
|
if len(gauges) == 0 {
|
|
|
|
continue
|
2015-10-26 18:31:21 +00:00
|
|
|
}
|
2016-01-07 17:09:04 +00:00
|
|
|
if gt.err == nil && !reflect.DeepEqual(gauges[0], gt.outGauge) {
|
|
|
|
t.Errorf("%s: \nexpected %+v\ngot %+v\n",
|
|
|
|
gt.ptIn.Name(), gt.outGauge, gauges[0])
|
2015-10-26 18:31:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildGaugeWithSource(t *testing.T) {
|
2016-01-27 23:15:14 +00:00
|
|
|
pt1, _ := telegraf.NewMetric(
|
2015-11-11 00:05:28 +00:00
|
|
|
"test1",
|
2016-02-26 20:06:56 +00:00
|
|
|
map[string]string{"hostname": "192.168.0.1", "tag1": "value1"},
|
2015-11-11 00:05:28 +00:00
|
|
|
map[string]interface{}{"value": 0.0},
|
|
|
|
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
|
|
|
|
)
|
2016-01-27 23:15:14 +00:00
|
|
|
pt2, _ := telegraf.NewMetric(
|
2015-11-11 00:05:28 +00:00
|
|
|
"test2",
|
2016-02-26 20:06:56 +00:00
|
|
|
map[string]string{"hostnam": "192.168.0.1", "tag1": "value1"},
|
2015-11-11 00:05:28 +00:00
|
|
|
map[string]interface{}{"value": 1.0},
|
|
|
|
time.Date(2010, time.December, 10, 23, 0, 0, 0, time.UTC),
|
|
|
|
)
|
2015-10-26 18:31:21 +00:00
|
|
|
var gaugeTests = []struct {
|
2016-01-27 23:15:14 +00:00
|
|
|
ptIn telegraf.Metric
|
2015-10-26 18:31:21 +00:00
|
|
|
outGauge *Gauge
|
|
|
|
err error
|
|
|
|
}{
|
2015-11-11 00:05:28 +00:00
|
|
|
|
2015-10-26 18:31:21 +00:00
|
|
|
{
|
2015-11-11 00:05:28 +00:00
|
|
|
pt1,
|
2015-10-26 18:31:21 +00:00
|
|
|
&Gauge{
|
2016-04-08 22:04:45 +00:00
|
|
|
Name: "192_168_0_1.value1.test1",
|
2015-10-26 18:31:21 +00:00
|
|
|
MeasureTime: time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
|
|
|
|
Value: 0.0,
|
|
|
|
Source: "192.168.0.1",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2015-11-11 00:05:28 +00:00
|
|
|
pt2,
|
2015-10-26 18:31:21 +00:00
|
|
|
&Gauge{
|
2016-04-08 22:04:45 +00:00
|
|
|
Name: "192_168_0_1.value1.test1",
|
2015-10-26 18:31:21 +00:00
|
|
|
MeasureTime: time.Date(2010, time.December, 10, 23, 0, 0, 0, time.UTC).Unix(),
|
|
|
|
Value: 1.0,
|
|
|
|
},
|
|
|
|
fmt.Errorf("undeterminable Source type from Field, hostname"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
l := NewLibrato(fakeUrl)
|
|
|
|
l.SourceTag = "hostname"
|
|
|
|
for _, gt := range gaugeTests {
|
2016-01-07 17:09:04 +00:00
|
|
|
gauges, err := l.buildGauges(gt.ptIn)
|
2015-10-26 18:31:21 +00:00
|
|
|
if err != nil && gt.err == nil {
|
|
|
|
t.Errorf("%s: unexpected error, %+v\n", gt.ptIn.Name(), err)
|
|
|
|
}
|
|
|
|
if gt.err != nil && err == nil {
|
|
|
|
t.Errorf("%s: expected an error (%s) but none returned", gt.ptIn.Name(), gt.err.Error())
|
|
|
|
}
|
2016-01-07 17:09:04 +00:00
|
|
|
if len(gauges) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if gt.err == nil && !reflect.DeepEqual(gauges[0], gt.outGauge) {
|
|
|
|
t.Errorf("%s: \nexpected %+v\ngot %+v\n", gt.ptIn.Name(), gt.outGauge, gauges[0])
|
2015-10-26 18:31:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|