func TestUnmarshal_Time(t *testing.T) { // Its important to remember that collectd stores high resolution time // as "near" nanoseconds (2^30) so we have to take that into account // when feeding time into the test. // Since we only store microseconds, we round it off (mostly to make testing easier) 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) } var tests = []struct { name string packet gollectd.Packet points []influxdb.Point }{ { name: "Should parse timeHR properly", packet: gollectd.Packet{ TimeHR: timeHR(testTime), Values: []gollectd.Value{ gollectd.Value{ Value: 1, }, }, }, points: []influxdb.Point{ influxdb.Point{Timestamp: testTime}, }, }, { name: "Should parse time properly", packet: gollectd.Packet{ Time: uint64(testTime.Round(time.Second).Unix()), Values: []gollectd.Value{ gollectd.Value{ Value: 1, }, }, }, points: []influxdb.Point{ influxdb.Point{Timestamp: testTime.Round(time.Second)}, }, }, } for _, test := range tests { t.Logf("testing %q", test.name) points := collectd.Unmarshal(&test.packet) if len(points) != len(test.points) { t.Errorf("point len mismatch. expected %d, got %d", len(test.points), len(points)) } for _, p := range points { if test.packet.TimeHR > 0 { if p.Timestamp.Format(time.RFC3339Nano) != testTime.Format(time.RFC3339Nano) { t.Errorf("timestamp mis-match, got %v, expected %v", p.Timestamp.Format(time.RFC3339Nano), testTime.Format(time.RFC3339Nano)) } else if p.Timestamp.Format(time.RFC3339) != testTime.Format(time.RFC3339) { t.Errorf("timestamp mis-match, got %v, expected %v", p.Timestamp.Format(time.RFC3339), testTime.Format(time.RFC3339)) } } } } }
func TestUnmarshal_Points(t *testing.T) { /* 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": "" }, … ] */ var tests = []struct { name string packet gollectd.Packet points []influxdb.Point }{ { name: "single value", points: []influxdb.Point{ influxdb.Point{Name: "disk_read", Values: map[string]interface{}{"disk_read": float64(1)}}, }, packet: gollectd.Packet{ Plugin: "disk", Values: []gollectd.Value{ gollectd.Value{Name: "read", Value: 1}, }, }, }, { name: "multi value", points: []influxdb.Point{ influxdb.Point{Name: "disk_read", Values: map[string]interface{}{"disk_read": float64(1)}}, influxdb.Point{Name: "disk_write", Values: map[string]interface{}{"disk_write": float64(5)}}, }, packet: gollectd.Packet{ Plugin: "disk", Values: []gollectd.Value{ gollectd.Value{Name: "read", Value: 1}, gollectd.Value{Name: "write", Value: 5}, }, }, }, { name: "tags", points: []influxdb.Point{ influxdb.Point{ Name: "disk_read", Tags: map[string]string{"host": "server01", "instance": "sdk", "type": "disk_octets", "type_instance": "single"}, Values: map[string]interface{}{"disk_read": float64(1)}, }, }, packet: gollectd.Packet{ Plugin: "disk", Hostname: "server01", PluginInstance: "sdk", Type: "disk_octets", TypeInstance: "single", Values: []gollectd.Value{ gollectd.Value{Name: "read", Value: 1}, }, }, }, } for _, test := range tests { t.Logf("testing %q", test.name) points := collectd.Unmarshal(&test.packet) if len(points) != len(test.points) { t.Errorf("points len mismatch. expected %d, got %d", len(test.points), len(points)) } for i, m := range test.points { // test name name := fmt.Sprintf("%s_%s", test.packet.Plugin, test.packet.Values[i].Name) if m.Name != name { t.Errorf("point name mismatch. expected %q, got %q", name, m.Name) } // test value mv := m.Values[m.Name].(float64) pv := test.packet.Values[i].Value if mv != pv { t.Errorf("point value mismatch. expected %v, got %v", pv, mv) } // test tags if test.packet.Hostname != m.Tags["host"] { t.Errorf(`point tags["host"] mismatch. expected %q, got %q`, test.packet.Hostname, m.Tags["host"]) } if test.packet.PluginInstance != m.Tags["instance"] { t.Errorf(`point tags["instance"] mismatch. expected %q, got %q`, test.packet.PluginInstance, m.Tags["instance"]) } if test.packet.Type != m.Tags["type"] { t.Errorf(`point tags["type"] mismatch. expected %q, got %q`, test.packet.Type, m.Tags["type"]) } if test.packet.TypeInstance != m.Tags["type_instance"] { t.Errorf(`point tags["type_instance"] mismatch. expected %q, got %q`, test.packet.TypeInstance, m.Tags["type_instance"]) } } } }