2016-02-10 22:50:07 +00:00
|
|
|
package influx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-11-28 18:19:35 +00:00
|
|
|
"strings"
|
2016-02-10 22:50:07 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2016-11-22 12:51:57 +00:00
|
|
|
"github.com/influxdata/telegraf/metric"
|
2016-02-10 22:50:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSerializeMetricFloat(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
tags := map[string]string{
|
|
|
|
"cpu": "cpu0",
|
|
|
|
}
|
|
|
|
fields := map[string]interface{}{
|
|
|
|
"usage_idle": float64(91.5),
|
|
|
|
}
|
2016-11-22 12:51:57 +00:00
|
|
|
m, err := metric.New("cpu", tags, fields, now)
|
2016-02-10 22:50:07 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
s := InfluxSerializer{}
|
2016-11-28 18:19:35 +00:00
|
|
|
buf, _ := s.Serialize(m)
|
|
|
|
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
2016-02-10 22:50:07 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
expS := []string{fmt.Sprintf("cpu,cpu=cpu0 usage_idle=91.5 %d", now.UnixNano())}
|
|
|
|
assert.Equal(t, expS, mS)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSerializeMetricInt(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
tags := map[string]string{
|
|
|
|
"cpu": "cpu0",
|
|
|
|
}
|
|
|
|
fields := map[string]interface{}{
|
|
|
|
"usage_idle": int64(90),
|
|
|
|
}
|
2016-11-22 12:51:57 +00:00
|
|
|
m, err := metric.New("cpu", tags, fields, now)
|
2016-02-10 22:50:07 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
s := InfluxSerializer{}
|
2016-11-28 18:19:35 +00:00
|
|
|
buf, _ := s.Serialize(m)
|
|
|
|
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
2016-02-10 22:50:07 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
expS := []string{fmt.Sprintf("cpu,cpu=cpu0 usage_idle=90i %d", now.UnixNano())}
|
|
|
|
assert.Equal(t, expS, mS)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSerializeMetricString(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
tags := map[string]string{
|
|
|
|
"cpu": "cpu0",
|
|
|
|
}
|
|
|
|
fields := map[string]interface{}{
|
|
|
|
"usage_idle": "foobar",
|
|
|
|
}
|
2016-11-22 12:51:57 +00:00
|
|
|
m, err := metric.New("cpu", tags, fields, now)
|
2016-02-10 22:50:07 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
s := InfluxSerializer{}
|
2016-11-28 18:19:35 +00:00
|
|
|
buf, _ := s.Serialize(m)
|
|
|
|
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
2016-02-10 22:50:07 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
expS := []string{fmt.Sprintf("cpu,cpu=cpu0 usage_idle=\"foobar\" %d", now.UnixNano())}
|
|
|
|
assert.Equal(t, expS, mS)
|
|
|
|
}
|