forked from bitflipp/gerberos
/
testing.go
110 lines (92 loc) · 2.08 KB
/
testing.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package gerberos
import (
"errors"
"io"
"os"
"os/exec"
"reflect"
"strconv"
"testing"
"time"
)
var (
errFault = errors.New("fault")
)
type testFaultyExecutor struct {
// Actuator
name string
args []string
// Effect
output string
exitCode int
err error
}
func (e *testFaultyExecutor) Execute(name string, args ...string) (string, int, error) {
return e.ExecuteWithStd(nil, nil, name, args...)
}
func (e *testFaultyExecutor) ExecuteWithStd(stdin io.Reader, stdout io.Writer, name string, args ...string) (string, int, error) {
if name == e.name && reflect.DeepEqual(args, e.args) {
return e.output, e.exitCode, e.err
}
de := &defaultExecutor{}
return de.ExecuteWithStd(stdin, stdout, name, args...)
}
func NewTestFaultyExecutor(output string, exitCode int, err error, name string, args ...string) *testFaultyExecutor {
return &testFaultyExecutor{
name: name,
args: args,
output: output,
exitCode: exitCode,
err: err,
}
}
func testError(t *testing.T, err error) {
t.Helper()
if err == nil {
t.Error("expected error")
}
}
func testNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
}
func newTestConfiguration() (*Configuration, error) {
c := &Configuration{}
return c, c.ReadFile("test/configuration.toml")
}
func newTestRunner() (*Runner, error) {
c, err := newTestConfiguration()
if err != nil {
return nil, err
}
return NewRunner(c), nil
}
func newTestValidRule() *Rule {
return &Rule{
Action: []string{"ban", "1h"},
Regexp: []string{`%ip%\s%id%`},
Source: []string{"test"},
Aggregate: []string{"1s", `a\s%id%`, `%id%\sb`},
Occurrences: []string{"5", "10s"},
name: "test",
}
}
func newTestOccurrences() *occurrences {
return newOccurrences(100*time.Millisecond, 10)
}
func testCountChildren() (int, error) {
cmd := exec.Command("pgrep", "-P", strconv.Itoa(os.Getpid()))
b, err := cmd.CombinedOutput()
if err != nil {
return 0, nil // Probably no children
}
child := 0
for i := range b {
if b[i] == '\n' {
child++
}
}
return child, nil
}