forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postfix_test.go
58 lines (45 loc) · 2.01 KB
/
postfix_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
package postfix
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGather(t *testing.T) {
td, err := ioutil.TempDir("", "")
require.NoError(t, err)
defer os.RemoveAll(td)
for _, q := range []string{"active", "hold", "incoming", "maildrop", "deferred/0/0", "deferred/F/F"} {
require.NoError(t, os.MkdirAll(filepath.FromSlash(td+"/"+q), 0755))
}
require.NoError(t, ioutil.WriteFile(filepath.FromSlash(td+"/active/01"), []byte("abc"), 0644))
require.NoError(t, ioutil.WriteFile(filepath.FromSlash(td+"/active/02"), []byte("defg"), 0644))
require.NoError(t, ioutil.WriteFile(filepath.FromSlash(td+"/hold/01"), []byte("abc"), 0644))
require.NoError(t, ioutil.WriteFile(filepath.FromSlash(td+"/incoming/01"), []byte("abcd"), 0644))
require.NoError(t, ioutil.WriteFile(filepath.FromSlash(td+"/deferred/0/0/01"), []byte("abc"), 0644))
require.NoError(t, ioutil.WriteFile(filepath.FromSlash(td+"/deferred/F/F/F1"), []byte("abc"), 0644))
p := Postfix{
QueueDirectory: td,
}
var acc testutil.Accumulator
require.NoError(t, p.Gather(&acc))
metrics := map[string]*testutil.Metric{}
for _, m := range acc.Metrics {
metrics[m.Tags["queue"]] = m
}
assert.Equal(t, int64(2), metrics["active"].Fields["length"])
assert.Equal(t, int64(7), metrics["active"].Fields["size"])
assert.InDelta(t, 0, metrics["active"].Fields["age"], 10)
assert.Equal(t, int64(1), metrics["hold"].Fields["length"])
assert.Equal(t, int64(3), metrics["hold"].Fields["size"])
assert.Equal(t, int64(1), metrics["incoming"].Fields["length"])
assert.Equal(t, int64(4), metrics["incoming"].Fields["size"])
assert.Equal(t, int64(0), metrics["maildrop"].Fields["length"])
assert.Equal(t, int64(0), metrics["maildrop"].Fields["size"])
assert.Equal(t, int64(0), metrics["maildrop"].Fields["age"])
assert.Equal(t, int64(2), metrics["deferred"].Fields["length"])
assert.Equal(t, int64(6), metrics["deferred"].Fields["size"])
}