mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-12 12:55:53 +02:00
enable linter: revive (superfluous-else) (#3082)
* enable linter: revive (superfluous-else) * lint (whitespace)
This commit is contained in:
parent
2f6d4cccd7
commit
e6ebf7af22
2 changed files with 63 additions and 26 deletions
|
@ -149,8 +149,6 @@ linters-settings:
|
||||||
disabled: true
|
disabled: true
|
||||||
- name: struct-tag
|
- name: struct-tag
|
||||||
disabled: true
|
disabled: true
|
||||||
- name: superfluous-else
|
|
||||||
disabled: true
|
|
||||||
- name: time-equal
|
- name: time-equal
|
||||||
disabled: true
|
disabled: true
|
||||||
- name: var-naming
|
- name: var-naming
|
||||||
|
|
|
@ -30,6 +30,7 @@ func TestParser(t *testing.T) {
|
||||||
debug = true
|
debug = true
|
||||||
|
|
||||||
log.SetLevel(log.InfoLevel)
|
log.SetLevel(log.InfoLevel)
|
||||||
|
|
||||||
envSetting := os.Getenv("TEST_ONLY")
|
envSetting := os.Getenv("TEST_ONLY")
|
||||||
|
|
||||||
pctx, ectx, err := prepTests()
|
pctx, ectx, err := prepTests()
|
||||||
|
@ -208,6 +209,7 @@ func loadTestFile(file string) []TestFile {
|
||||||
|
|
||||||
dec := yaml.NewDecoder(yamlFile)
|
dec := yaml.NewDecoder(yamlFile)
|
||||||
dec.SetStrict(true)
|
dec.SetStrict(true)
|
||||||
|
|
||||||
var testSet []TestFile
|
var testSet []TestFile
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
@ -232,23 +234,26 @@ func loadTestFile(file string) []TestFile {
|
||||||
|
|
||||||
func matchEvent(expected types.Event, out types.Event, debug bool) ([]string, bool) {
|
func matchEvent(expected types.Event, out types.Event, debug bool) ([]string, bool) {
|
||||||
var retInfo []string
|
var retInfo []string
|
||||||
var valid = false
|
|
||||||
|
valid := false
|
||||||
expectMaps := []map[string]string{expected.Parsed, expected.Meta, expected.Enriched}
|
expectMaps := []map[string]string{expected.Parsed, expected.Meta, expected.Enriched}
|
||||||
outMaps := []map[string]string{out.Parsed, out.Meta, out.Enriched}
|
outMaps := []map[string]string{out.Parsed, out.Meta, out.Enriched}
|
||||||
outLabels := []string{"Parsed", "Meta", "Enriched"}
|
outLabels := []string{"Parsed", "Meta", "Enriched"}
|
||||||
|
|
||||||
//allow to check as well for stage and processed flags
|
// allow to check as well for stage and processed flags
|
||||||
if expected.Stage != "" {
|
if expected.Stage != "" {
|
||||||
if expected.Stage != out.Stage {
|
if expected.Stage != out.Stage {
|
||||||
if debug {
|
if debug {
|
||||||
retInfo = append(retInfo, fmt.Sprintf("mismatch stage %s != %s", expected.Stage, out.Stage))
|
retInfo = append(retInfo, fmt.Sprintf("mismatch stage %s != %s", expected.Stage, out.Stage))
|
||||||
}
|
}
|
||||||
|
|
||||||
goto checkFinished
|
goto checkFinished
|
||||||
} else {
|
}
|
||||||
valid = true
|
|
||||||
if debug {
|
valid = true
|
||||||
retInfo = append(retInfo, fmt.Sprintf("ok stage %s == %s", expected.Stage, out.Stage))
|
|
||||||
}
|
if debug {
|
||||||
|
retInfo = append(retInfo, fmt.Sprintf("ok stage %s == %s", expected.Stage, out.Stage))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -256,26 +261,30 @@ func matchEvent(expected types.Event, out types.Event, debug bool) ([]string, bo
|
||||||
if debug {
|
if debug {
|
||||||
retInfo = append(retInfo, fmt.Sprintf("mismatch process %t != %t", expected.Process, out.Process))
|
retInfo = append(retInfo, fmt.Sprintf("mismatch process %t != %t", expected.Process, out.Process))
|
||||||
}
|
}
|
||||||
|
|
||||||
goto checkFinished
|
goto checkFinished
|
||||||
} else {
|
}
|
||||||
valid = true
|
|
||||||
if debug {
|
valid = true
|
||||||
retInfo = append(retInfo, fmt.Sprintf("ok process %t == %t", expected.Process, out.Process))
|
|
||||||
}
|
if debug {
|
||||||
|
retInfo = append(retInfo, fmt.Sprintf("ok process %t == %t", expected.Process, out.Process))
|
||||||
}
|
}
|
||||||
|
|
||||||
if expected.Whitelisted != out.Whitelisted {
|
if expected.Whitelisted != out.Whitelisted {
|
||||||
if debug {
|
if debug {
|
||||||
retInfo = append(retInfo, fmt.Sprintf("mismatch whitelist %t != %t", expected.Whitelisted, out.Whitelisted))
|
retInfo = append(retInfo, fmt.Sprintf("mismatch whitelist %t != %t", expected.Whitelisted, out.Whitelisted))
|
||||||
}
|
}
|
||||||
|
|
||||||
goto checkFinished
|
goto checkFinished
|
||||||
} else {
|
|
||||||
if debug {
|
|
||||||
retInfo = append(retInfo, fmt.Sprintf("ok whitelist %t == %t", expected.Whitelisted, out.Whitelisted))
|
|
||||||
}
|
|
||||||
valid = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if debug {
|
||||||
|
retInfo = append(retInfo, fmt.Sprintf("ok whitelist %t == %t", expected.Whitelisted, out.Whitelisted))
|
||||||
|
}
|
||||||
|
|
||||||
|
valid = true
|
||||||
|
|
||||||
for mapIdx := 0; mapIdx < len(expectMaps); mapIdx++ {
|
for mapIdx := 0; mapIdx < len(expectMaps); mapIdx++ {
|
||||||
for expKey, expVal := range expectMaps[mapIdx] {
|
for expKey, expVal := range expectMaps[mapIdx] {
|
||||||
outVal, ok := outMaps[mapIdx][expKey]
|
outVal, ok := outMaps[mapIdx][expKey]
|
||||||
|
@ -283,19 +292,26 @@ func matchEvent(expected types.Event, out types.Event, debug bool) ([]string, bo
|
||||||
if debug {
|
if debug {
|
||||||
retInfo = append(retInfo, fmt.Sprintf("missing entry %s[%s]", outLabels[mapIdx], expKey))
|
retInfo = append(retInfo, fmt.Sprintf("missing entry %s[%s]", outLabels[mapIdx], expKey))
|
||||||
}
|
}
|
||||||
|
|
||||||
valid = false
|
valid = false
|
||||||
|
|
||||||
goto checkFinished
|
goto checkFinished
|
||||||
}
|
}
|
||||||
if outVal != expVal { //ok entry
|
|
||||||
|
if outVal != expVal { // ok entry
|
||||||
if debug {
|
if debug {
|
||||||
retInfo = append(retInfo, fmt.Sprintf("mismatch %s[%s] %s != %s", outLabels[mapIdx], expKey, expVal, outVal))
|
retInfo = append(retInfo, fmt.Sprintf("mismatch %s[%s] %s != %s", outLabels[mapIdx], expKey, expVal, outVal))
|
||||||
}
|
}
|
||||||
|
|
||||||
valid = false
|
valid = false
|
||||||
|
|
||||||
goto checkFinished
|
goto checkFinished
|
||||||
}
|
}
|
||||||
|
|
||||||
if debug {
|
if debug {
|
||||||
retInfo = append(retInfo, fmt.Sprintf("ok %s[%s] %s == %s", outLabels[mapIdx], expKey, expVal, outVal))
|
retInfo = append(retInfo, fmt.Sprintf("ok %s[%s] %s == %s", outLabels[mapIdx], expKey, expVal, outVal))
|
||||||
}
|
}
|
||||||
|
|
||||||
valid = true
|
valid = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -309,6 +325,7 @@ checkFinished:
|
||||||
retInfo = append(retInfo, fmt.Sprintf("KO ! \n\t%s", strings.Join(retInfo, "\n\t")))
|
retInfo = append(retInfo, fmt.Sprintf("KO ! \n\t%s", strings.Join(retInfo, "\n\t")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return retInfo, valid
|
return retInfo, valid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -320,9 +337,10 @@ func testSubSet(testSet TestFile, pctx UnixParserCtx, nodes []Node) (bool, error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to process %s : %v", spew.Sdump(in), err)
|
log.Errorf("Failed to process %s : %v", spew.Sdump(in), err)
|
||||||
}
|
}
|
||||||
//log.Infof("Parser output : %s", spew.Sdump(out))
|
// log.Infof("Parser output : %s", spew.Sdump(out))
|
||||||
results = append(results, out)
|
results = append(results, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("parsed %d lines", len(testSet.Lines))
|
log.Infof("parsed %d lines", len(testSet.Lines))
|
||||||
log.Infof("got %d results", len(results))
|
log.Infof("got %d results", len(results))
|
||||||
|
|
||||||
|
@ -336,15 +354,17 @@ func testSubSet(testSet TestFile, pctx UnixParserCtx, nodes []Node) (bool, error
|
||||||
|
|
||||||
reCheck:
|
reCheck:
|
||||||
failinfo := []string{}
|
failinfo := []string{}
|
||||||
|
|
||||||
for ridx, result := range results {
|
for ridx, result := range results {
|
||||||
for eidx, expected := range testSet.Results {
|
for eidx, expected := range testSet.Results {
|
||||||
explain, match := matchEvent(expected, result, debug)
|
explain, match := matchEvent(expected, result, debug)
|
||||||
if match {
|
if match {
|
||||||
log.Infof("expected %d/%d matches result %d/%d", eidx, len(testSet.Results), ridx, len(results))
|
log.Infof("expected %d/%d matches result %d/%d", eidx, len(testSet.Results), ridx, len(results))
|
||||||
|
|
||||||
if len(explain) > 0 {
|
if len(explain) > 0 {
|
||||||
log.Printf("-> %s", explain[len(explain)-1])
|
log.Printf("-> %s", explain[len(explain)-1])
|
||||||
}
|
}
|
||||||
//don't do this at home : delete current element from list and redo
|
// don't do this at home : delete current element from list and redo
|
||||||
results[len(results)-1], results[ridx] = results[ridx], results[len(results)-1]
|
results[len(results)-1], results[ridx] = results[ridx], results[len(results)-1]
|
||||||
results = results[:len(results)-1]
|
results = results[:len(results)-1]
|
||||||
|
|
||||||
|
@ -352,34 +372,40 @@ reCheck:
|
||||||
testSet.Results = testSet.Results[:len(testSet.Results)-1]
|
testSet.Results = testSet.Results[:len(testSet.Results)-1]
|
||||||
|
|
||||||
goto reCheck
|
goto reCheck
|
||||||
} else {
|
|
||||||
failinfo = append(failinfo, explain...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
failinfo = append(failinfo, explain...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(results) > 0 {
|
if len(results) > 0 {
|
||||||
log.Printf("Errors : %s", strings.Join(failinfo, " / "))
|
log.Printf("Errors : %s", strings.Join(failinfo, " / "))
|
||||||
return false, fmt.Errorf("leftover results : %+v", results)
|
return false, fmt.Errorf("leftover results : %+v", results)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(testSet.Results) > 0 {
|
if len(testSet.Results) > 0 {
|
||||||
log.Printf("Errors : %s", strings.Join(failinfo, " / "))
|
log.Printf("Errors : %s", strings.Join(failinfo, " / "))
|
||||||
return false, fmt.Errorf("leftover expected results : %+v", testSet.Results)
|
return false, fmt.Errorf("leftover expected results : %+v", testSet.Results)
|
||||||
}
|
}
|
||||||
|
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func testFile(testSet []TestFile, pctx UnixParserCtx, nodes []Node) bool {
|
func testFile(testSet []TestFile, pctx UnixParserCtx, nodes []Node) bool {
|
||||||
log.Warning("Going to process one test set")
|
log.Warning("Going to process one test set")
|
||||||
|
|
||||||
for _, tf := range testSet {
|
for _, tf := range testSet {
|
||||||
//func testSubSet(testSet TestFile, pctx UnixParserCtx, nodes []Node) (bool, error) {
|
// func testSubSet(testSet TestFile, pctx UnixParserCtx, nodes []Node) (bool, error) {
|
||||||
testOk, err := testSubSet(tf, pctx, nodes)
|
testOk, err := testSubSet(tf, pctx, nodes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("test failed : %s", err)
|
log.Fatalf("test failed : %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !testOk {
|
if !testOk {
|
||||||
log.Fatalf("failed test : %+v", tf)
|
log.Fatalf("failed test : %+v", tf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -404,48 +430,61 @@ func TestGeneratePatternsDoc(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to load patterns : %s", err)
|
t.Fatalf("unable to load patterns : %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("-> %s", spew.Sdump(pctx))
|
log.Infof("-> %s", spew.Sdump(pctx))
|
||||||
/*don't judge me, we do it for the users*/
|
/*don't judge me, we do it for the users*/
|
||||||
p := make(PairList, len(pctx.Grok.Patterns))
|
p := make(PairList, len(pctx.Grok.Patterns))
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
|
|
||||||
for key, val := range pctx.Grok.Patterns {
|
for key, val := range pctx.Grok.Patterns {
|
||||||
p[i] = Pair{key, val}
|
p[i] = Pair{key, val}
|
||||||
p[i].Value = strings.ReplaceAll(p[i].Value, "{%{", "\\{\\%\\{")
|
p[i].Value = strings.ReplaceAll(p[i].Value, "{%{", "\\{\\%\\{")
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Sort(p)
|
sort.Sort(p)
|
||||||
|
|
||||||
f, err := os.OpenFile("./patterns-documentation.md", os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
|
f, err := os.OpenFile("./patterns-documentation.md", os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0o644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to open : %s", err)
|
t.Fatalf("failed to open : %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := f.WriteString("# Patterns documentation\n\n"); err != nil {
|
if _, err := f.WriteString("# Patterns documentation\n\n"); err != nil {
|
||||||
t.Fatal("failed to write to file")
|
t.Fatal("failed to write to file")
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := f.WriteString("You will find here a generated documentation of all the patterns loaded by crowdsec.\n"); err != nil {
|
if _, err := f.WriteString("You will find here a generated documentation of all the patterns loaded by crowdsec.\n"); err != nil {
|
||||||
t.Fatal("failed to write to file")
|
t.Fatal("failed to write to file")
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := f.WriteString("They are sorted by pattern length, and are meant to be used in parsers, in the form %{PATTERN_NAME}.\n"); err != nil {
|
if _, err := f.WriteString("They are sorted by pattern length, and are meant to be used in parsers, in the form %{PATTERN_NAME}.\n"); err != nil {
|
||||||
t.Fatal("failed to write to file")
|
t.Fatal("failed to write to file")
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := f.WriteString("\n\n"); err != nil {
|
if _, err := f.WriteString("\n\n"); err != nil {
|
||||||
t.Fatal("failed to write to file")
|
t.Fatal("failed to write to file")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, k := range p {
|
for _, k := range p {
|
||||||
if _, err := fmt.Fprintf(f, "## %s\n\nPattern :\n```\n%s\n```\n\n", k.Key, k.Value); err != nil {
|
if _, err := fmt.Fprintf(f, "## %s\n\nPattern :\n```\n%s\n```\n\n", k.Key, k.Value); err != nil {
|
||||||
t.Fatal("failed to write to file")
|
t.Fatal("failed to write to file")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%v\t%v\n", k.Key, k.Value)
|
fmt.Printf("%v\t%v\n", k.Key, k.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := f.WriteString("\n"); err != nil {
|
if _, err := f.WriteString("\n"); err != nil {
|
||||||
t.Fatal("failed to write to file")
|
t.Fatal("failed to write to file")
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := f.WriteString("# Documentation generation\n"); err != nil {
|
if _, err := f.WriteString("# Documentation generation\n"); err != nil {
|
||||||
t.Fatal("failed to write to file")
|
t.Fatal("failed to write to file")
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := f.WriteString("This documentation is generated by `pkg/parser` : `GO_WANT_TEST_DOC=1 go test -run TestGeneratePatternsDoc`\n"); err != nil {
|
if _, err := f.WriteString("This documentation is generated by `pkg/parser` : `GO_WANT_TEST_DOC=1 go test -run TestGeneratePatternsDoc`\n"); err != nil {
|
||||||
t.Fatal("failed to write to file")
|
t.Fatal("failed to write to file")
|
||||||
}
|
}
|
||||||
|
|
||||||
f.Close()
|
f.Close()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue