MCPcopy
hub / github.com/crowdsecurity/crowdsec / testOneParser

Function testOneParser

pkg/parser/parsing_test.go:99–153  ·  view source on GitHub ↗
(t require.TestingT, pctx *UnixParserCtx, ectx EnricherCtx, dir string, b *testing.B)

Source from the content-addressed store, hash-verified

97}
98
99func testOneParser(t require.TestingT, pctx *UnixParserCtx, ectx EnricherCtx, dir string, b *testing.B) error {
100 var (
101 pnodes []Node
102 parserConfigs []Stagefile
103 )
104
105 log.Warningf("testing %s", dir)
106
107 parserCfgFile := filepath.Join(dir, "parsers.yaml")
108
109 cfg, err := os.ReadFile(parserCfgFile)
110 if err != nil {
111 return fmt.Errorf("failed opening %s: %w", parserCfgFile, err)
112 }
113
114 tmpl, err := template.New("test").Parse(string(cfg))
115 if err != nil {
116 return fmt.Errorf("failed to parse template %s: %w", cfg, err)
117 }
118
119 var out bytes.Buffer
120
121 err = tmpl.Execute(&out, map[string]string{"TestDirectory": dir})
122 if err != nil {
123 panic(err)
124 }
125
126 if err = yaml.UnmarshalStrict(out.Bytes(), &parserConfigs); err != nil {
127 return fmt.Errorf("failed to parse %s: %w", parserCfgFile, err)
128 }
129
130 pnodes, err = LoadStages(parserConfigs, pctx, ectx)
131 if err != nil {
132 return fmt.Errorf("unable to load parser config: %w", err)
133 }
134
135 // TBD: Load post overflows
136 // func testFile(t *testing.T, file string, pctx UnixParserCtx, nodes []Node) bool {
137 parserTestFile := filepath.Join(dir, "test.yaml")
138 tests := loadTestFile(t, parserTestFile)
139 count := 1
140
141 if b != nil {
142 count = b.N
143 b.ResetTimer()
144 }
145
146 for range count {
147 if !testFile(t, tests, *pctx, pnodes) {
148 return errors.New("test failed")
149 }
150 }
151
152 return nil
153}
154
155// prepTests is going to do the initialisation of parser : it's going to load enrichment plugins and load the patterns. This is done here so that we don't redo it for each test
156func prepTests(t require.TestingT) (*UnixParserCtx, EnricherCtx) {

Callers 2

TestParserFunction · 0.85
BenchmarkParserFunction · 0.85

Calls 5

LoadStagesFunction · 0.85
loadTestFileFunction · 0.85
testFileFunction · 0.70
ParseMethod · 0.45
ExecuteMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…