package logstorage import ( "reflect" "testing" ) func TestExtractFormatApply(t *testing.T) { f := func(format, s string, resultsExpected []string) { t.Helper() ef, err := parseExtractFormat(format) if err != nil { t.Fatalf("unexpected error: %s", err) } ef.apply(s) if !reflect.DeepEqual(ef.results, resultsExpected) { t.Fatalf("unexpected results; got %q; want %q", ef.results, resultsExpected) } } f("", "", []string{""}) f("", "abc", []string{"abc"}) f("bar", "", []string{"", ""}) f("bar", "bar", []string{"", ""}) f("bar", "bazbar", []string{"baz", ""}) f("bar", "a bazbar xdsf", []string{"a baz", " xdsf"}) f("foo", "", []string{""}) f("foo", "foo", []string{""}) f("foo", "a foo xdf sdf", []string{" xdf sdf"}) f("foo", "a foo foobar", []string{" foobar"}) f("foobaz", "a foo foobar", []string{"", ""}) f("foobaz", "a foo foobar baz", []string{" foobar ", ""}) f("foobaz", "a foo foobar bazabc", []string{" foobar ", "abc"}) f("ip= <> path= ", "x=a, ip=1.2.3.4 method=GET host='abc' path=/foo/bar some tail here", []string{"1.2.3.4", "method=GET host='abc'", "/foo/bar", "some tail here"}) f("ip=<>", "foo ip=<1.2.3.4> bar", []string{"1.2.3.4", " bar"}) // quoted fields f(`"msg":,`, `{"foo":"bar","msg":"foo,b\"ar\n\t","baz":"x"}`, []string{`foo,b"ar`+"\n\t", `"baz":"x"}`}) f(`foo=`, "foo=`bar baz,abc` def", []string{"bar baz,abc"}) f(`foo= `, "foo=`bar baz,abc` def", []string{"bar baz,abc", "def"}) } func TestParseExtractFormatStepsSuccess(t *testing.T) { f := func(s string, stepsExpected []*extractFormatStep) { t.Helper() steps, err := parseExtractFormatSteps(s) if err != nil { t.Fatalf("unexpected error when parsing %q: %s", s, err) } if !reflect.DeepEqual(steps, stepsExpected) { t.Fatalf("unexpected steps for [%s]; got %v; want %v", s, steps, stepsExpected) } } f("", []*extractFormatStep{ { field: "foo", }, }) f("<_>", []*extractFormatStep{ {}, }) f("<*>", []*extractFormatStep{ {}, }) f("<>", []*extractFormatStep{ {}, }) f("bar", []*extractFormatStep{ { field: "foo", }, { prefix: "bar", }, }) f("<>bar", []*extractFormatStep{ {}, { prefix: "bar", field: "foo", }, }) f("bar", []*extractFormatStep{ { prefix: "bar", field: "foo", }, }) f("barabc", []*extractFormatStep{ { prefix: "bar", field: "foo", }, { prefix: "abc", }, }) f("barabc<_>", []*extractFormatStep{ { prefix: "bar", field: "foo", }, { prefix: "abc", }, }) f("bar", []*extractFormatStep{ { field: "foo", }, { prefix: "bar", field: "baz", }, }) f("barbaz", []*extractFormatStep{ { prefix: "bar", field: "foo", }, { prefix: "baz", }, }) f("<&gt;", []*extractFormatStep{ { prefix: "<", field: "foo", }, { prefix: ">", }, }) } func TestParseExtractFormatStepFailure(t *testing.T) { f := func(s string) { t.Helper() _, err := parseExtractFormatSteps(s) if err == nil { t.Fatalf("expecting non-nil error when parsing %q", s) } } // empty string f("") // zero fields f("foobar") // missing delimiter between fields f("") f("aa") f("aabb") // missing > f("