-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
rule_parser_test.go
92 lines (78 loc) · 2.27 KB
/
rule_parser_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
90
91
92
package sigma
import (
"github.com/google/go-cmp/cmp/cmpopts"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/bradleyjkemp/cupaloy/v2"
"github.com/google/go-cmp/cmp"
"gopkg.in/yaml.v3"
)
func TestParseRule(t *testing.T) {
err := filepath.Walk("./testdata/", func(path string, info os.FileInfo, err error) error {
if !strings.HasSuffix(path, ".rule.yml") {
return nil
}
t.Run(strings.TrimSuffix(filepath.Base(path), ".rule.yml"), func(t *testing.T) {
contents, err := ioutil.ReadFile(path)
if err != nil {
t.Fatalf("failed reading test input: %v", err)
}
rule, err := ParseRule(contents)
if err != nil {
t.Fatalf("error parsing rule: %v", err)
}
cupaloy.New(cupaloy.SnapshotSubdirectory("testdata")).SnapshotT(t, rule)
})
return nil
})
if err != nil {
t.Fatal(err)
}
}
func TestMarshalRule(t *testing.T) {
err := filepath.Walk("./testdata/", func(path string, info os.FileInfo, err error) error {
if !strings.HasSuffix(path, ".rule.yml") {
return nil
}
t.Run(strings.TrimSuffix(filepath.Base(path), ".rule.yml"), func(t *testing.T) {
contents, err := ioutil.ReadFile(path)
if err != nil {
t.Fatalf("failed reading test input: %v", err)
}
rule, err := ParseRule(contents)
if err != nil {
t.Fatalf("error parsing rule: %v", err)
}
// Create a new temporary file in our testing temp directory
stream, err := os.CreateTemp(t.TempDir(), filepath.Base(path))
if err != nil {
t.Fatalf("error creating temp rule file: %v", err)
}
defer os.Remove(stream.Name())
defer stream.Close()
// Save the rule to a temporary file
encoder := yaml.NewEncoder(stream)
if err := encoder.Encode(&rule); err != nil {
t.Fatalf("error encoding rule to file: %v", err)
}
// Return to the beginning of the stream
stream.Seek(0, os.SEEK_SET)
// Re-read the rule from the newly serialized file
var rule_copy Rule
decoder := yaml.NewDecoder(stream)
if err := decoder.Decode(&rule_copy); err != nil {
t.Fatalf("error decoding rule copy: %v", err)
}
if !cmp.Equal(rule, rule_copy, cmpopts.IgnoreUnexported(Condition{}, FieldMatcher{}, Search{})) {
t.Fatalf("rule and marshalled copy are not equal")
}
})
return nil
})
if err != nil {
t.Fatal(err)
}
}