-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfailmail_test.go
75 lines (62 loc) · 2.18 KB
/
failmail_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
package main
import (
"io/ioutil"
"os"
"path"
"testing"
)
func TestBatchConfig(t *testing.T) {
msg := makeReceivedMessage(t, "Subject: that test\r\nX-Batch: 100\r\n\r\ntest body\r\n")
batch := (&Config{BatchExpr: `{{match "^(this|that)" (.Header.Get "Subject")}}`}).Batch()
if key, err := batch(msg); key != "that" || err != nil {
t.Errorf("expected message batch 'that', got %#v, %s", key, err)
}
batch = (&Config{BatchExpr: `{{replace "^(this|that)" (.Header.Get "Subject") "*"}}`}).Batch()
if key, err := batch(msg); key != "* test" || err != nil {
t.Errorf("expected message batch '* test', got %#v, %s", key, err)
}
batch = (&Config{BatchExpr: `{{.Header.Get "X-Batch"}}`}).Batch()
if key, err := batch(msg); key != "100" || err != nil {
t.Errorf("expected message batch '100', got %#v, %s", key, err)
}
}
func TestGroupConfig(t *testing.T) {
msg := makeReceivedMessage(t, "Subject: that test\r\nX-Batch: 100\r\n\r\ntest body\r\n")
group := (&Config{GroupExpr: `{{match "^(this|that)" (.Header.Get "Subject")}}`}).Group()
if key, err := group(msg); key != "that" || err != nil {
t.Errorf("expected message group 'that', got %#v, %s", key, err)
}
group = (&Config{GroupExpr: `{{replace "^(this|that)" (.Header.Get "Subject") "*"}}`}).Group()
if key, err := group(msg); key != "* test" || err != nil {
t.Errorf("expected message group '* test', got %#v, %s", key, err)
}
}
type TestUpstream struct {
Sends []OutgoingMessage
ReturnError error
}
func (t *TestUpstream) Send(msg OutgoingMessage) error {
if t.ReturnError != nil {
return t.ReturnError
}
t.Sends = append(t.Sends, msg)
return nil
}
func TestWritePidfile(t *testing.T) {
testDir, cleanup := makeTestDir(t)
defer cleanup()
pidfile := path.Join(testDir, "test.pid")
writePidfile(pidfile)
if _, err := os.Stat(pidfile); err != nil && os.IsNotExist(err) {
t.Errorf("no pidfile found at %s", pidfile)
} else if err != nil && !os.IsNotExist(err) {
t.Errorf("unexpected error looking for pidfile: %s", err)
}
}
func makeTestDir(t *testing.T) (string, func()) {
tmp, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("couldn't create temp dir: %s", err)
}
return tmp, func() { os.RemoveAll(tmp) }
}