-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathsample_test.go
89 lines (81 loc) · 2.43 KB
/
sample_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package jsonlog
import (
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/bradleyjkemp/cupaloy"
"github.com/jf-tech/go-corelib/jsons"
"github.com/stretchr/testify/assert"
"github.com/jf-tech/omniparser"
"github.com/jf-tech/omniparser/customfuncs"
"github.com/jf-tech/omniparser/extensions/omniv21"
v21 "github.com/jf-tech/omniparser/extensions/omniv21/customfuncs"
"github.com/jf-tech/omniparser/extensions/omniv21/fileformat"
"github.com/jf-tech/omniparser/extensions/omniv21/samples/customfileformats/jsonlog/jsonlogformat"
"github.com/jf-tech/omniparser/transformctx"
)
func normalizeSeverity(_ *transformctx.Ctx, sev string) (string, error) {
switch strings.ToUpper(sev) {
case "DEBUG":
return "D", nil
case "INFO":
return "I", nil
case "WARNING":
return "W", nil
case "ERROR":
return "E", nil
case "CRITICAL":
return "F", nil
default:
return "?", nil
}
}
func TestSample(t *testing.T) {
schemaFile := "./sample_schema.json"
schemaFileBaseName := filepath.Base(schemaFile)
schemaFileReader, err := os.Open(schemaFile)
assert.NoError(t, err)
defer schemaFileReader.Close()
inputFile := "./sample.log"
inputFileBaseName := filepath.Base(inputFile)
inputFileReader, err := os.Open(inputFile)
assert.NoError(t, err)
defer inputFileReader.Close()
schema, err := omniparser.NewSchema(
schemaFileBaseName,
schemaFileReader,
// Use this Extension to effectively replace the
// builtin schema handler (and its builtin fileformats)
// with, well, the same schema handler but with our own custom
// fileformat. Also let's demo how to add a new custom func.
omniparser.Extension{
CreateSchemaHandler: omniv21.CreateSchemaHandler,
CreateSchemaHandlerParams: &omniv21.CreateParams{
// But use our own FileFormat.
CustomFileFormats: []fileformat.FileFormat{
jsonlogformat.NewJSONLogFileFormat(schemaFileBaseName),
},
},
CustomFuncs: customfuncs.Merge(
customfuncs.CommonCustomFuncs,
v21.OmniV21CustomFuncs,
customfuncs.CustomFuncs{
"normalize_severity": normalizeSeverity,
}),
})
assert.NoError(t, err)
transform, err := schema.NewTransform(inputFileBaseName, inputFileReader, &transformctx.Ctx{})
assert.NoError(t, err)
var records []string
for {
recordBytes, err := transform.Read()
if err == io.EOF {
break
}
assert.NoError(t, err)
records = append(records, string(recordBytes))
}
cupaloy.SnapshotT(t, jsons.BPJ("["+strings.Join(records, ",")+"]"))
}