2015-01-07 02:28:34 +00:00
|
|
|
package collectd_test
|
2015-01-07 23:10:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2015-01-08 21:36:07 +00:00
|
|
|
"time"
|
2015-01-07 23:10:27 +00:00
|
|
|
|
|
|
|
"github.com/influxdb/influxdb/collectd"
|
|
|
|
"github.com/kimor79/gollectd"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
This is a sample of what data can be represented like in json
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"values": [197141504, 175136768],
|
|
|
|
"dstypes": ["counter", "counter"],
|
|
|
|
"dsnames": ["read", "write"],
|
|
|
|
"time": 1251533299,
|
|
|
|
"interval": 10,
|
|
|
|
"host": "leeloo.lan.home.verplant.org",
|
|
|
|
"plugin": "disk",
|
|
|
|
"plugin_instance": "sda",
|
|
|
|
"type": "disk_octets",
|
|
|
|
"type_instance": ""
|
|
|
|
},
|
|
|
|
…
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
func Test_UnmarshalPacket(t *testing.T) {
|
2015-01-08 21:36:07 +00:00
|
|
|
testTime := time.Now().UTC().Round(time.Microsecond)
|
|
|
|
var timeHR = func(tm time.Time) uint64 {
|
|
|
|
sec, nsec := tm.Unix(), tm.UnixNano()%1000000000
|
|
|
|
hr := (sec << 30) + (nsec * 1000000000 / 1073741824)
|
|
|
|
return uint64(hr)
|
|
|
|
}
|
2015-01-07 23:10:27 +00:00
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
packet gollectd.Packet
|
2015-01-08 21:36:07 +00:00
|
|
|
metrics []collectd.Metric
|
2015-01-07 23:10:27 +00:00
|
|
|
}{
|
|
|
|
{
|
2015-01-08 21:36:07 +00:00
|
|
|
name: "Should parse timeHR properly",
|
|
|
|
packet: gollectd.Packet{
|
|
|
|
TimeHR: timeHR(testTime),
|
|
|
|
Values: []gollectd.Value{
|
|
|
|
gollectd.Value{
|
|
|
|
TypeName: "cpu",
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
metrics: []collectd.Metric{
|
|
|
|
collectd.Metric{Timestamp: testTime},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Should parse time properly",
|
2015-01-07 23:10:27 +00:00
|
|
|
packet: gollectd.Packet{
|
2015-01-08 21:36:07 +00:00
|
|
|
Time: uint64(testTime.Round(time.Second).Unix()),
|
|
|
|
Values: []gollectd.Value{
|
|
|
|
gollectd.Value{
|
|
|
|
TypeName: "cpu",
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
metrics: []collectd.Metric{
|
|
|
|
collectd.Metric{
|
|
|
|
Timestamp: testTime.Round(time.Second),
|
|
|
|
},
|
2015-01-07 23:10:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2015-01-08 21:36:07 +00:00
|
|
|
t.Logf("testing %q", test.name)
|
|
|
|
metrics := collectd.Unmarshal(&test.packet)
|
|
|
|
if len(metrics) != len(test.metrics) {
|
|
|
|
t.Errorf("metric len mismatch. expected %d, got %d", len(test.metrics), len(metrics))
|
|
|
|
}
|
|
|
|
for _, m := range metrics {
|
|
|
|
if test.packet.TimeHR > 0 {
|
|
|
|
if m.Timestamp.Format(time.RFC3339Nano) != testTime.Format(time.RFC3339Nano) {
|
|
|
|
t.Errorf("timestamp mis-match, got %v, expected %v", m.Timestamp.Format(time.RFC3339Nano), testTime.Format(time.RFC3339Nano))
|
|
|
|
} else if m.Timestamp.Format(time.RFC3339) != testTime.Format(time.RFC3339) {
|
|
|
|
t.Errorf("timestamp mis-match, got %v, expected %v", m.Timestamp.Format(time.RFC3339), testTime.Format(time.RFC3339))
|
|
|
|
}
|
|
|
|
}
|
2015-01-07 23:10:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|