2016-03-17 17:50:39 +00:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2016-11-22 12:51:57 +00:00
|
|
|
"github.com/influxdata/telegraf/metric"
|
2016-03-17 17:50:39 +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-03-17 17:50:39 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
s := JsonSerializer{}
|
2016-11-28 18:19:35 +00:00
|
|
|
var buf []byte
|
|
|
|
buf, err = s.Serialize(m)
|
2016-03-17 17:50:39 +00:00
|
|
|
assert.NoError(t, err)
|
2016-12-01 15:18:46 +00:00
|
|
|
expS := []byte(fmt.Sprintf(`{"fields":{"usage_idle":91.5},"name":"cpu","tags":{"cpu":"cpu0"},"timestamp":%d}`, now.Unix()) + "\n")
|
|
|
|
assert.Equal(t, string(expS), string(buf))
|
2016-03-17 17:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-03-17 17:50:39 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
s := JsonSerializer{}
|
2016-11-28 18:19:35 +00:00
|
|
|
var buf []byte
|
|
|
|
buf, err = s.Serialize(m)
|
2016-03-17 17:50:39 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2016-12-01 15:18:46 +00:00
|
|
|
expS := []byte(fmt.Sprintf(`{"fields":{"usage_idle":90},"name":"cpu","tags":{"cpu":"cpu0"},"timestamp":%d}`, now.Unix()) + "\n")
|
|
|
|
assert.Equal(t, string(expS), string(buf))
|
2016-03-17 17:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-03-17 17:50:39 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
s := JsonSerializer{}
|
2016-11-28 18:19:35 +00:00
|
|
|
var buf []byte
|
|
|
|
buf, err = s.Serialize(m)
|
2016-03-17 17:50:39 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2016-12-01 15:18:46 +00:00
|
|
|
expS := []byte(fmt.Sprintf(`{"fields":{"usage_idle":"foobar"},"name":"cpu","tags":{"cpu":"cpu0"},"timestamp":%d}`, now.Unix()) + "\n")
|
|
|
|
assert.Equal(t, string(expS), string(buf))
|
2016-03-17 17:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSerializeMultiFields(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
tags := map[string]string{
|
|
|
|
"cpu": "cpu0",
|
|
|
|
}
|
|
|
|
fields := map[string]interface{}{
|
|
|
|
"usage_idle": int64(90),
|
|
|
|
"usage_total": 8559615,
|
|
|
|
}
|
2016-11-22 12:51:57 +00:00
|
|
|
m, err := metric.New("cpu", tags, fields, now)
|
2016-03-17 17:50:39 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
s := JsonSerializer{}
|
2016-11-28 18:19:35 +00:00
|
|
|
var buf []byte
|
|
|
|
buf, err = s.Serialize(m)
|
2016-03-17 17:50:39 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2016-12-01 15:18:46 +00:00
|
|
|
expS := []byte(fmt.Sprintf(`{"fields":{"usage_idle":90,"usage_total":8559615},"name":"cpu","tags":{"cpu":"cpu0"},"timestamp":%d}`, now.Unix()) + "\n")
|
|
|
|
assert.Equal(t, string(expS), string(buf))
|
2016-03-17 17:50:39 +00:00
|
|
|
}
|
2016-12-01 18:16:52 +00:00
|
|
|
|
|
|
|
func TestSerializeMetricWithEscapes(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
tags := map[string]string{
|
|
|
|
"cpu tag": "cpu0",
|
|
|
|
}
|
|
|
|
fields := map[string]interface{}{
|
|
|
|
"U,age=Idle": int64(90),
|
|
|
|
}
|
2016-12-01 15:18:46 +00:00
|
|
|
m, err := metric.New("My CPU", tags, fields, now)
|
2016-12-01 18:16:52 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
s := JsonSerializer{}
|
2016-12-01 15:18:46 +00:00
|
|
|
buf, err := s.Serialize(m)
|
2016-12-01 18:16:52 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2016-12-01 15:18:46 +00:00
|
|
|
expS := []byte(fmt.Sprintf(`{"fields":{"U,age=Idle":90},"name":"My CPU","tags":{"cpu tag":"cpu0"},"timestamp":%d}`, now.Unix()) + "\n")
|
|
|
|
assert.Equal(t, string(expS), string(buf))
|
2016-12-01 18:16:52 +00:00
|
|
|
}
|