60 lines
1.4 KiB
Go

package convert
import (
"testing"
"git.opencomputing.cn/yumoqing/host-metrics-collector/internal/model"
)
func TestSampleRoundTrip(t *testing.T) {
in := model.Sample{
Name: "cpu.usage",
Value: 42.5,
Timestamp: 1700000000,
Labels: map[string]string{"host_id": "h-001"},
}
out := SampleFromPB(SampleToPB(in))
if out.Name != in.Name || out.Value != in.Value || out.Timestamp != in.Timestamp {
t.Fatalf("round trip = %+v, want %+v", out, in)
}
if out.Labels["host_id"] != "h-001" {
t.Fatalf("labels = %v", out.Labels)
}
}
func TestSampleFromPBNil(t *testing.T) {
out := SampleFromPB(nil)
if out.Name != "" || out.Labels != nil {
t.Fatalf("nil conversion = %+v", out)
}
}
func TestLogRoundTrip(t *testing.T) {
in := model.LogRecord{
Timestamp: 1700000000,
Level: "ERROR",
Source: "/var/log/app.log",
Message: "boom",
Fields: map[string]string{"trace_id": "tr-1"},
File: "/var/log/app.log",
Offset: 1024,
}
out := LogFromPB(LogToPB(in))
if out.Timestamp != in.Timestamp || out.Level != in.Level || out.Source != in.Source {
t.Fatalf("round trip = %+v, want %+v", out, in)
}
if out.Message != "boom" || out.Offset != 1024 {
t.Fatalf("round trip = %+v", out)
}
if out.Fields["trace_id"] != "tr-1" {
t.Fatalf("fields = %v", out.Fields)
}
}
func TestLogFromPBNil(t *testing.T) {
out := LogFromPB(nil)
if out.Message != "" || out.Fields != nil {
t.Fatalf("nil conversion = %+v", out)
}
}