2015-10-26 18:31:21 +00:00
|
|
|
package librato
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-11-22 12:51:57 +00:00
|
|
|
"github.com/influxdata/telegraf/metric"
|
2015-10-26 18:31:21 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-08-09 07:29:15 +00:00
|
|
|
fakeURL = "http://test.librato.com"
|
2015-10-26 18:31:21 +00:00
|
|
|
fakeUser = "telegraf@influxdb.com"
|
|
|
|
fakeToken = "123456"
|
|
|
|
)
|
|
|
|
|
|
|
|
func fakeLibrato() *Librato {
|
2016-08-09 07:29:15 +00:00
|
|
|
l := NewLibrato(fakeURL)
|
|
|
|
l.APIUser = fakeUser
|
|
|
|
l.APIToken = fakeToken
|
2015-10-26 18:31:21 +00:00
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUriOverride(t *testing.T) {
|
2016-08-09 07:29:15 +00:00
|
|
|
ts := httptest.NewServer(
|
|
|
|
http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}))
|
2015-10-26 18:31:21 +00:00
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
l := NewLibrato(ts.URL)
|
2016-08-09 07:29:15 +00:00
|
|
|
l.APIUser = "telegraf@influxdb.com"
|
|
|
|
l.APIToken = "123456"
|
2015-10-26 18:31:21 +00:00
|
|
|
err := l.Connect()
|
|
|
|
require.NoError(t, err)
|
2016-08-09 07:29:15 +00:00
|
|
|
err = l.Write([]telegraf.Metric{newHostMetric(int32(0), "name", "host")})
|
2015-10-26 18:31:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadStatusCode(t *testing.T) {
|
2016-08-09 07:29:15 +00:00
|
|
|
ts := httptest.NewServer(
|
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
|
|
}))
|
2015-10-26 18:31:21 +00:00
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
l := NewLibrato(ts.URL)
|
2016-08-09 07:29:15 +00:00
|
|
|
l.APIUser = "telegraf@influxdb.com"
|
|
|
|
l.APIToken = "123456"
|
2015-10-26 18:31:21 +00:00
|
|
|
err := l.Connect()
|
|
|
|
require.NoError(t, err)
|
2016-08-09 07:29:15 +00:00
|
|
|
err = l.Write([]telegraf.Metric{newHostMetric(int32(0), "name", "host")})
|
2015-10-26 18:31:21 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("error expected but none returned")
|
|
|
|
} else {
|
2016-08-09 07:29:15 +00:00
|
|
|
require.EqualError(
|
|
|
|
t,
|
|
|
|
fmt.Errorf("received bad status code, 503\n "), err.Error())
|
2015-10-26 18:31:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildGauge(t *testing.T) {
|
2016-08-09 07:29:15 +00:00
|
|
|
|
|
|
|
mtime := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()
|
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
|
|
|
|
}{
|
|
|
|
{
|
2016-08-09 07:29:15 +00:00
|
|
|
newHostMetric(0.0, "test1", "host1"),
|
2015-10-26 18:31:21 +00:00
|
|
|
&Gauge{
|
2016-08-09 07:29:15 +00:00
|
|
|
Name: "test1",
|
|
|
|
MeasureTime: mtime,
|
2015-10-26 18:31:21 +00:00
|
|
|
Value: 0.0,
|
2016-08-09 07:29:15 +00:00
|
|
|
Source: "host1",
|
2015-10-26 18:31:21 +00:00
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2016-08-09 07:29:15 +00:00
|
|
|
newHostMetric(1.0, "test2", "host2"),
|
2015-10-26 18:31:21 +00:00
|
|
|
&Gauge{
|
2016-08-09 07:29:15 +00:00
|
|
|
Name: "test2",
|
|
|
|
MeasureTime: mtime,
|
2015-10-26 18:31:21 +00:00
|
|
|
Value: 1.0,
|
2016-08-09 07:29:15 +00:00
|
|
|
Source: "host2",
|
2015-10-26 18:31:21 +00:00
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2016-08-09 07:29:15 +00:00
|
|
|
newHostMetric(10, "test3", "host3"),
|
2015-10-26 18:31:21 +00:00
|
|
|
&Gauge{
|
2016-08-09 07:29:15 +00:00
|
|
|
Name: "test3",
|
|
|
|
MeasureTime: mtime,
|
2015-10-26 18:31:21 +00:00
|
|
|
Value: 10.0,
|
2016-08-09 07:29:15 +00:00
|
|
|
Source: "host3",
|
2015-10-26 18:31:21 +00:00
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2016-08-09 07:29:15 +00:00
|
|
|
newHostMetric(int32(112345), "test4", "host4"),
|
2015-10-26 18:31:21 +00:00
|
|
|
&Gauge{
|
2016-08-09 07:29:15 +00:00
|
|
|
Name: "test4",
|
|
|
|
MeasureTime: mtime,
|
2015-10-26 18:31:21 +00:00
|
|
|
Value: 112345.0,
|
2016-08-09 07:29:15 +00:00
|
|
|
Source: "host4",
|
2015-10-26 18:31:21 +00:00
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2016-08-09 07:29:15 +00:00
|
|
|
newHostMetric(int64(112345), "test5", "host5"),
|
2015-10-26 18:31:21 +00:00
|
|
|
&Gauge{
|
2016-08-09 07:29:15 +00:00
|
|
|
Name: "test5",
|
|
|
|
MeasureTime: mtime,
|
2015-10-26 18:31:21 +00:00
|
|
|
Value: 112345.0,
|
2016-08-09 07:29:15 +00:00
|
|
|
Source: "host5",
|
2015-10-26 18:31:21 +00:00
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2016-08-09 07:29:15 +00:00
|
|
|
newHostMetric(float32(11234.5), "test6", "host6"),
|
2015-10-26 18:31:21 +00:00
|
|
|
&Gauge{
|
2016-08-09 07:29:15 +00:00
|
|
|
Name: "test6",
|
|
|
|
MeasureTime: mtime,
|
2015-10-26 18:31:21 +00:00
|
|
|
Value: 11234.5,
|
2016-08-09 07:29:15 +00:00
|
|
|
Source: "host6",
|
2015-10-26 18:31:21 +00:00
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2016-08-09 07:29:15 +00:00
|
|
|
newHostMetric("11234.5", "test7", "host7"),
|
2016-03-22 15:07:01 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
2015-10-26 18:31:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-08-09 07:29:15 +00:00
|
|
|
l := NewLibrato(fakeURL)
|
2015-10-26 18:31:21 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 18:19:35 +00:00
|
|
|
func newHostMetric(value interface{}, name, host string) telegraf.Metric {
|
|
|
|
m, _ := metric.New(
|
2016-08-09 07:29:15 +00:00
|
|
|
name,
|
|
|
|
map[string]string{"host": host},
|
|
|
|
map[string]interface{}{"value": value},
|
|
|
|
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
|
|
|
|
)
|
2016-11-28 18:19:35 +00:00
|
|
|
return m
|
2016-08-09 07:29:15 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 18:31:21 +00:00
|
|
|
func TestBuildGaugeWithSource(t *testing.T) {
|
2016-08-09 07:29:15 +00:00
|
|
|
mtime := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
|
2016-11-22 12:51:57 +00:00
|
|
|
pt1, _ := metric.New(
|
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},
|
2016-08-09 07:29:15 +00:00
|
|
|
mtime,
|
2015-11-11 00:05:28 +00:00
|
|
|
)
|
2016-11-22 12:51:57 +00:00
|
|
|
pt2, _ := metric.New(
|
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},
|
2016-08-09 07:29:15 +00:00
|
|
|
mtime,
|
|
|
|
)
|
2016-11-22 12:51:57 +00:00
|
|
|
pt3, _ := metric.New(
|
2016-08-09 07:29:15 +00:00
|
|
|
"test3",
|
|
|
|
map[string]string{
|
|
|
|
"hostname": "192.168.0.1",
|
|
|
|
"tag2": "value2",
|
|
|
|
"tag1": "value1"},
|
|
|
|
map[string]interface{}{"value": 1.0},
|
|
|
|
mtime,
|
|
|
|
)
|
2016-11-22 12:51:57 +00:00
|
|
|
pt4, _ := metric.New(
|
2016-08-09 07:29:15 +00:00
|
|
|
"test4",
|
|
|
|
map[string]string{
|
|
|
|
"hostname": "192.168.0.1",
|
|
|
|
"tag2": "value2",
|
|
|
|
"tag1": "value1"},
|
|
|
|
map[string]interface{}{"value": 1.0},
|
|
|
|
mtime,
|
2015-11-11 00:05:28 +00:00
|
|
|
)
|
2015-10-26 18:31:21 +00:00
|
|
|
var gaugeTests = []struct {
|
2016-01-27 23:15:14 +00:00
|
|
|
ptIn telegraf.Metric
|
2016-08-09 07:29:15 +00:00
|
|
|
template string
|
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,
|
2016-08-09 07:29:15 +00:00
|
|
|
"hostname",
|
2015-10-26 18:31:21 +00:00
|
|
|
&Gauge{
|
2016-08-09 07:29:15 +00:00
|
|
|
Name: "test1",
|
|
|
|
MeasureTime: mtime.Unix(),
|
2015-10-26 18:31:21 +00:00
|
|
|
Value: 0.0,
|
2016-08-09 07:29:15 +00:00
|
|
|
Source: "192_168_0_1",
|
2015-10-26 18:31:21 +00:00
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2015-11-11 00:05:28 +00:00
|
|
|
pt2,
|
2016-08-09 07:29:15 +00:00
|
|
|
"hostname",
|
2015-10-26 18:31:21 +00:00
|
|
|
&Gauge{
|
2016-08-09 07:29:15 +00:00
|
|
|
Name: "test2",
|
|
|
|
MeasureTime: mtime.Unix(),
|
2015-10-26 18:31:21 +00:00
|
|
|
Value: 1.0,
|
|
|
|
},
|
|
|
|
fmt.Errorf("undeterminable Source type from Field, hostname"),
|
|
|
|
},
|
2016-08-09 07:29:15 +00:00
|
|
|
{
|
|
|
|
pt3,
|
|
|
|
"tags",
|
|
|
|
&Gauge{
|
|
|
|
Name: "test3",
|
|
|
|
MeasureTime: mtime.Unix(),
|
|
|
|
Value: 1.0,
|
|
|
|
Source: "192_168_0_1.value1.value2",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pt4,
|
|
|
|
"hostname.tag2",
|
|
|
|
&Gauge{
|
|
|
|
Name: "test4",
|
|
|
|
MeasureTime: mtime.Unix(),
|
|
|
|
Value: 1.0,
|
|
|
|
Source: "192_168_0_1.value2",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
2015-10-26 18:31:21 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 07:29:15 +00:00
|
|
|
l := NewLibrato(fakeURL)
|
2015-10-26 18:31:21 +00:00
|
|
|
for _, gt := range gaugeTests {
|
2016-08-09 07:29:15 +00:00
|
|
|
l.Template = gt.template
|
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-08-09 07:29:15 +00:00
|
|
|
t.Errorf(
|
|
|
|
"%s: expected an error (%s) but none returned",
|
|
|
|
gt.ptIn.Name(),
|
|
|
|
gt.err.Error())
|
2015-10-26 18:31:21 +00:00
|
|
|
}
|
2016-01-07 17:09:04 +00:00
|
|
|
if len(gauges) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if gt.err == nil && !reflect.DeepEqual(gauges[0], gt.outGauge) {
|
2016-08-09 07:29:15 +00:00
|
|
|
t.Errorf(
|
|
|
|
"%s: \nexpected %+v\ngot %+v\n",
|
|
|
|
gt.ptIn.Name(),
|
|
|
|
gt.outGauge, gauges[0])
|
2015-10-26 18:31:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|