-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
197 lines (155 loc) · 3.48 KB
/
server_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package syslogsidecar
import (
"strconv"
"testing"
syslogclient "github.com/RackSec/srslog"
"github.com/g41797/kissngoqueue"
"github.com/g41797/sputnik"
)
func defaultServerConfiguration() SyslogConfiguration {
result := SyslogConfiguration{}
result.ADDRTCP = "127.0.0.1:5141"
result.SEVERITYLEVEL = 7
return result
}
var seqNumber int
func next() int {
seqNumber += 1
return seqNumber
}
type srvTest struct {
t *testing.T
q *kissngoqueue.Queue[sputnik.Msg]
syslogconf SyslogConfiguration
srv *server
client *client
}
func newTest(t *testing.T) *srvTest {
result := new(srvTest)
result.q = kissngoqueue.NewQueue[sputnik.Msg]()
return result
}
func (test *srvTest) start() {
test.syslogconf = defaultServerConfiguration()
test.srv = newServer(test.syslogconf)
err := test.srv.initServer()
if err != nil {
test.t.Errorf("Init error %v", err)
}
test.client = newClient()
err = test.client.init()
if err != nil {
test.t.Errorf("Init client error %v", err)
}
test.srv.setupHandling(newCommunicator(test.q))
err = test.srv.start()
if err != nil {
test.t.Errorf("Start syslogd error %v", err)
}
}
func (test *srvTest) stop() {
test.client.finish()
err := test.srv.stop()
if err != nil {
test.t.Errorf("stop server error %v", err)
}
}
func (test *srvTest) exchange() {
next := next()
logText := strconv.Itoa(next)
err := test.client.log(logText)
if err != nil {
test.t.Errorf("send log message error %v", err)
}
msg, ok := test.q.Get()
if !ok {
test.t.Errorf("failed receive from test queue")
}
msgparts, err := UnpackToMap(msg)
Put(msg)
if err != nil {
test.t.Errorf("unpack message error %v", err)
}
recvlog, _ := msgparts["message"]
if logText != recvlog {
test.t.Errorf("Expected %s Received %s", logText, recvlog)
}
}
func (test *srvTest) stopServer() {
err := test.srv.stop()
if err != nil {
test.t.Errorf("stop server error %v", err)
}
}
var _ sputnik.BlockCommunicator = &MockCommunicator{}
type MockCommunicator struct {
q *kissngoqueue.Queue[sputnik.Msg]
}
func newCommunicator(q *kissngoqueue.Queue[sputnik.Msg]) *MockCommunicator {
mc := new(MockCommunicator)
mc.q = q
return mc
}
func (mc *MockCommunicator) Communicator(resp string) (bc sputnik.BlockCommunicator, exists bool) {
return nil, false
}
func (mc *MockCommunicator) Descriptor() sputnik.BlockDescriptor {
return sputnik.BlockDescriptor{}
}
func (mc *MockCommunicator) Send(msg sputnik.Msg) bool {
if msg == nil {
return false
}
if mc.q == nil {
return false
}
sok := mc.q.PutMT(msg)
return sok
}
type client struct {
syslogconf SyslogConfiguration
w *syslogclient.Writer
}
func newClientWIthConfig(cnf SyslogConfiguration) *client {
result := new(client)
result.syslogconf = cnf
return result
}
func newClient() *client {
return newClientWIthConfig(defaultServerConfiguration())
}
func (cl *client) init() error {
w, err := syslogclient.Dial("tcp", cl.syslogconf.ADDRTCP, syslogclient.LOG_ALERT, "test-client")
if err != nil {
return err
}
w.SetFormatter(syslogclient.RFC5424Formatter)
cl.w = w
return nil
}
func (cl *client) log(msg string) error {
return cl.w.Alert(msg)
}
func (cl *client) finish() {
if cl == nil {
return
}
if cl.w == nil {
return
}
cl.w.Close()
return
}
func Test_Init_Finish(t *testing.T) {
test := newTest(t)
test.start()
test.stop()
}
func Test_Exchange(t *testing.T) {
test := newTest(t)
test.start()
defer test.stop()
for i := 0; i < 1000; i++ {
test.exchange()
}
}