2023-07-20 08:10:55 +00:00
|
|
|
package loki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-06-17 20:28:15 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vlinsert/insertutils"
|
2023-07-20 08:10:55 +00:00
|
|
|
)
|
|
|
|
|
2024-06-17 20:28:15 +00:00
|
|
|
func TestParseJSONRequest_Failure(t *testing.T) {
|
2023-07-20 23:21:47 +00:00
|
|
|
f := func(s string) {
|
2023-07-20 08:10:55 +00:00
|
|
|
t.Helper()
|
2024-06-17 20:28:15 +00:00
|
|
|
|
|
|
|
tlp := &insertutils.TestLogMessageProcessor{}
|
|
|
|
n, err := parseJSONRequest([]byte(s), tlp)
|
2023-07-20 23:21:47 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
2023-07-20 23:21:47 +00:00
|
|
|
if n != 0 {
|
|
|
|
t.Fatalf("unexpected number of parsed lines: %d; want 0", n)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-20 23:21:47 +00:00
|
|
|
f(``)
|
2023-07-20 08:10:55 +00:00
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
// Invalid json
|
|
|
|
f(`{}`)
|
|
|
|
f(`[]`)
|
|
|
|
f(`"foo"`)
|
|
|
|
f(`123`)
|
2023-07-20 08:10:55 +00:00
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
// invalid type for `streams` item
|
|
|
|
f(`{"streams":123}`)
|
2023-07-20 08:10:55 +00:00
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
// Missing `values` item
|
|
|
|
f(`{"streams":[{}]}`)
|
2023-07-20 08:10:55 +00:00
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
// Invalid type for `values` item
|
|
|
|
f(`{"streams":[{"values":"foobar"}]}`)
|
|
|
|
|
|
|
|
// Invalid type for `stream` item
|
|
|
|
f(`{"streams":[{"stream":[],"values":[]}]}`)
|
|
|
|
|
|
|
|
// Invalid type for `values` individual item
|
|
|
|
f(`{"streams":[{"values":[123]}]}`)
|
|
|
|
|
|
|
|
// Invalid length of `values` individual item
|
|
|
|
f(`{"streams":[{"values":[[]]}]}`)
|
|
|
|
f(`{"streams":[{"values":[["123"]]}]}`)
|
2024-11-06 18:23:35 +00:00
|
|
|
f(`{"streams":[{"values":[["123","456","789","8123"]]}]}`)
|
2023-07-20 23:21:47 +00:00
|
|
|
|
|
|
|
// Invalid type for timestamp inside `values` individual item
|
|
|
|
f(`{"streams":[{"values":[[123,"456"]}]}`)
|
2023-07-20 08:10:55 +00:00
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
// Invalid type for log message
|
|
|
|
f(`{"streams":[{"values":[["123",1234]]}]}`)
|
2024-11-06 18:23:35 +00:00
|
|
|
|
2024-11-06 16:25:05 +00:00
|
|
|
// invalid structured metadata type
|
|
|
|
f(`{"streams":[{"values":[["1577836800000000001", "foo bar", ["metadata_1", "md_value"]]]}]}`)
|
2024-11-06 18:23:35 +00:00
|
|
|
|
2024-11-06 16:25:05 +00:00
|
|
|
// structured metadata with unexpected value type
|
|
|
|
f(`{"streams":[{"values":[["1577836800000000001", "foo bar", {"metadata_1": 1}]] }]}`)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
|
2024-06-17 20:28:15 +00:00
|
|
|
func TestParseJSONRequest_Success(t *testing.T) {
|
|
|
|
f := func(s string, timestampsExpected []int64, resultExpected string) {
|
2023-07-20 08:10:55 +00:00
|
|
|
t.Helper()
|
2024-06-17 20:28:15 +00:00
|
|
|
|
|
|
|
tlp := &insertutils.TestLogMessageProcessor{}
|
|
|
|
|
|
|
|
n, err := parseJSONRequest([]byte(s), tlp)
|
2023-07-20 08:10:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2024-06-17 20:28:15 +00:00
|
|
|
if err := tlp.Verify(n, timestampsExpected, resultExpected); err != nil {
|
|
|
|
t.Fatal(err)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
// Empty streams
|
2024-06-17 20:28:15 +00:00
|
|
|
f(`{"streams":[]}`, nil, ``)
|
|
|
|
f(`{"streams":[{"values":[]}]}`, nil, ``)
|
|
|
|
f(`{"streams":[{"stream":{},"values":[]}]}`, nil, ``)
|
|
|
|
f(`{"streams":[{"stream":{"foo":"bar"},"values":[]}]}`, nil, ``)
|
2023-07-20 23:21:47 +00:00
|
|
|
|
|
|
|
// Empty stream labels
|
2024-06-17 20:28:15 +00:00
|
|
|
f(`{"streams":[{"values":[["1577836800000000001", "foo bar"]]}]}`, []int64{1577836800000000001}, `{"_msg":"foo bar"}`)
|
|
|
|
f(`{"streams":[{"stream":{},"values":[["1577836800000000001", "foo bar"]]}]}`, []int64{1577836800000000001}, `{"_msg":"foo bar"}`)
|
2023-07-20 23:21:47 +00:00
|
|
|
|
|
|
|
// Non-empty stream labels
|
|
|
|
f(`{"streams":[{"stream":{
|
|
|
|
"label1": "value1",
|
|
|
|
"label2": "value2"
|
|
|
|
},"values":[
|
|
|
|
["1577836800000000001", "foo bar"],
|
|
|
|
["1477836900005000002", "abc"],
|
|
|
|
["147.78369e9", "foobar"]
|
2024-06-17 20:28:15 +00:00
|
|
|
]}]}`, []int64{1577836800000000001, 1477836900005000002, 147783690000}, `{"label1":"value1","label2":"value2","_msg":"foo bar"}
|
|
|
|
{"label1":"value1","label2":"value2","_msg":"abc"}
|
|
|
|
{"label1":"value1","label2":"value2","_msg":"foobar"}`)
|
2023-07-20 23:21:47 +00:00
|
|
|
|
|
|
|
// Multiple streams
|
|
|
|
f(`{
|
|
|
|
"streams": [
|
|
|
|
{
|
|
|
|
"stream": {
|
|
|
|
"foo": "bar",
|
|
|
|
"a": "b"
|
|
|
|
},
|
|
|
|
"values": [
|
|
|
|
["1577836800000000001", "foo bar"],
|
|
|
|
["1577836900005000002", "abc"]
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"stream": {
|
|
|
|
"x": "y"
|
|
|
|
},
|
|
|
|
"values": [
|
|
|
|
["1877836900005000002", "yx"]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
2024-06-17 20:28:15 +00:00
|
|
|
}`, []int64{1577836800000000001, 1577836900005000002, 1877836900005000002}, `{"foo":"bar","a":"b","_msg":"foo bar"}
|
|
|
|
{"foo":"bar","a":"b","_msg":"abc"}
|
|
|
|
{"x":"y","_msg":"yx"}`)
|
2024-11-06 16:25:05 +00:00
|
|
|
|
|
|
|
// values with metadata
|
|
|
|
f(`{"streams":[{"values":[["1577836800000000001", "foo bar", {"metadata_1": "md_value"}]]}]}`, []int64{1577836800000000001}, `{"_msg":"foo bar","metadata_1":"md_value"}`)
|
|
|
|
f(`{"streams":[{"values":[["1577836800000000001", "foo bar", {}]]}]}`, []int64{1577836800000000001}, `{"_msg":"foo bar"}`)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|