2023-11-28 14:52:29 +01:00
|
|
|
package datadog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRequestUnmarshalFailure(t *testing.T) {
|
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
|
|
|
req := new(Request)
|
|
|
|
if err := req.Unmarshal([]byte(s)); err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error for Unmarshal(%q)", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("")
|
|
|
|
f("foobar")
|
|
|
|
f(`{"series":123`)
|
|
|
|
f(`1234`)
|
|
|
|
f(`[]`)
|
|
|
|
}
|
|
|
|
|
2023-12-05 01:19:29 +01:00
|
|
|
func unmarshalRequestValidator(t *testing.T, s []byte, reqExpected *Request) {
|
|
|
|
t.Helper()
|
|
|
|
req := new(Request)
|
|
|
|
if err := req.Unmarshal(s); err != nil {
|
|
|
|
t.Fatalf("unexpected error in Unmarshal(%q): %s", s, err)
|
2023-11-28 14:52:29 +01:00
|
|
|
}
|
2023-12-05 01:19:29 +01:00
|
|
|
if !reflect.DeepEqual(req, reqExpected) {
|
|
|
|
t.Fatalf("unexpected row;\ngot\n%+v\nwant\n%+v", req, reqExpected)
|
|
|
|
}
|
|
|
|
}
|
2023-11-28 14:52:29 +01:00
|
|
|
|
2023-12-05 01:19:29 +01:00
|
|
|
func TestRequestUnmarshalSuccess(t *testing.T) {
|
|
|
|
unmarshalRequestValidator(
|
|
|
|
t, []byte("{}"), new(Request),
|
|
|
|
)
|
|
|
|
unmarshalRequestValidator(t, []byte(`
|
2023-11-28 14:52:29 +01:00
|
|
|
{
|
|
|
|
"series": [
|
|
|
|
{
|
|
|
|
"interval": 20,
|
|
|
|
"metric": "system.load.1",
|
|
|
|
"resources": [
|
|
|
|
{
|
|
|
|
"name": "test.example.com",
|
|
|
|
"type": "host"
|
|
|
|
}, {
|
|
|
|
"name": "/dev/sda",
|
|
|
|
"type": "device"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"points": [{
|
|
|
|
"timestamp": 1575317847,
|
|
|
|
"value": 0.5
|
|
|
|
}],
|
|
|
|
"tags": [
|
|
|
|
"environment:test"
|
|
|
|
],
|
|
|
|
"type": "rate"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`), &Request{
|
|
|
|
Series: []series{{
|
|
|
|
Metric: "system.load.1",
|
|
|
|
Resources: []resource{{
|
|
|
|
Name: "test.example.com",
|
|
|
|
Type: "host",
|
|
|
|
}, {
|
|
|
|
Name: "/dev/sda",
|
|
|
|
Type: "device",
|
|
|
|
}},
|
|
|
|
Points: []point{{
|
|
|
|
Timestamp: 1575317847,
|
|
|
|
Value: 0.5,
|
|
|
|
}},
|
|
|
|
Tags: []string{
|
|
|
|
"environment:test",
|
|
|
|
},
|
|
|
|
}},
|
2023-12-05 01:19:29 +01:00
|
|
|
})
|
2023-11-28 14:52:29 +01:00
|
|
|
}
|