-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsyslog_test.go
150 lines (114 loc) · 3.15 KB
/
syslog_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"testing"
"sync"
"github.com/stretchr/testify/assert"
"time"
)
func TestSyslogWriter(t *testing.T) {
// decrease exponential back-off base retry delay.
baseRetryDelay = 1 * time.Microsecond
assert.Nil(t, syslogWriter)
writer, _ := getSyslogWriter()
keep := writer
assert.NotNil(t, writer)
assert.NotNil(t, syslogWriter)
assert.Equal(t, writer, syslogWriter)
// force nil
syslogWriter = nil
// test init cannot be done twice
writer, _ = getSyslogWriter()
assert.Nil(t, writer)
assert.Nil(t, syslogWriter)
assert.Equal(t, writer, syslogWriter)
// re-initialize
syslogWriterOnce = sync.Once{}
// force nil
globalServerOptions.exportSyslogInfo = ExportSyslogInfo{
address: "1.2.3.4",
port: 22,
proto: "UDP",
program: "ipfix-forwarder",
}
w, err := getSyslogWriter()
// always returns a writer even with bad data.
assert.NotNil(t, w)
assert.Nil(t, err)
// put back
syslogWriter = keep
}
func TestExportSyslogError(t *testing.T) {
// decrease exponential back-off base retry delay.
baseRetryDelay = 1 * time.Microsecond
// force nil
syslogWriter = nil
err := exportSyslog("testing")
assert.Error(t, err, syslogErrMsg)
}
func TestExportSyslogEmptyError(t *testing.T) {
// decrease exponential back-off base retry delay.
baseRetryDelay = 1 * time.Microsecond
// force nil
syslogWriter = nil
err := exportSyslog("")
assert.Error(t, err, syslogErrMsg)
}
func TestExportSyslogWriterError(t *testing.T) {
// decrease exponential back-off base retry delay.
baseRetryDelay = 1 * time.Microsecond
globalServerOptions.exportSyslogInfo = ExportSyslogInfo{
address: "1.2.3.4",
port: 2055,
proto: "UDP",
program: "testing",
}
err := exportSyslog("whatever")
assert.Error(t, err, syslogErrMsg)
globalServerOptions.exportSyslogInfo = ExportSyslogInfo{}
}
func TestSyslogExportDisabled(t *testing.T) {
// decrease exponential back-off base retry delay.
baseRetryDelay = 1 * time.Microsecond
assert.False(t, isSyslogExportEnabled())
globalServerOptions = ServerOptions{}
assert.False(t, isSyslogExportEnabled())
globalServerOptions.exportSyslogInfo = ExportSyslogInfo{}
assert.False(t, isSyslogExportEnabled())
globalServerOptions.exportSyslogInfo = ExportSyslogInfo{
address: "",
port: 0,
proto: "",
program: "",
}
// still null cause empty struct
assert.False(t, isSyslogExportEnabled())
globalServerOptions.exportSyslogInfo = ExportSyslogInfo{
address: "",
port: 2055,
proto: "",
program: "",
}
// still null cause empty struct
assert.True(t, isSyslogExportEnabled())
// put it back to disabled for other tests
globalServerOptions.exportSyslogInfo = ExportSyslogInfo{}
}
func TestSyslogExportEnabled(t *testing.T) {
assert.False(t, isSyslogExportEnabled())
}
func TestExportSyslog(t *testing.T) {
// decrease exponential back-off base retry delay.
baseRetryDelay = 1 * time.Microsecond
// re-initialize
syslogWriterOnce = sync.Once{}
// force nil
globalServerOptions.exportSyslogInfo = ExportSyslogInfo{
address: "localhost",
port: 2055,
proto: "UDP",
program: "ipfix-forwarder",
}
err := exportSyslog("{}")
assert.Nil(t, err)
syslogWriter = nil
}