-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathagent_test.go
356 lines (305 loc) · 9.39 KB
/
agent_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package agent_test // Different name from package agent to insure we test with the 'outside the package' experience
import (
"fmt"
"os"
"sync"
"syscall"
"testing"
"time"
. "github.com/onsi/gomega"
"go.ligato.io/cn-infra/v2/agent"
"go.ligato.io/cn-infra/v2/infra"
"go.ligato.io/cn-infra/v2/logging/logrus"
)
const (
initFailedErrorString = "Init failed"
afterInitFailedErrorString = "AfterInit failed"
closeFailedErrorString = "Close failed"
defaultPluginName = "testplugin"
)
func TestEmptyAgent(t *testing.T) {
RegisterTestingT(t)
agent := agent.NewAgent()
Expect(agent).NotTo(BeNil())
Expect(agent.Options()).NotTo(BeNil())
Expect(agent.Options().Plugins).To(BeNil())
err := agent.Start()
Expect(err).To(BeNil())
err = agent.Stop()
Expect(err).To(BeNil())
}
func TestStopBeforeStart(t *testing.T) {
RegisterTestingT(t)
agent := agent.NewAgent()
err := agent.Stop()
Expect(err).ToNot(BeNil())
err = agent.Start()
Expect(err).To(BeNil())
}
func TestWaitBeforeStart(t *testing.T) {
RegisterTestingT(t)
agent := agent.NewAgent()
err := agent.Wait()
Expect(err).ToNot(BeNil())
err = agent.Start()
Expect(err).To(BeNil())
}
func TestAfterBeforeStart(t *testing.T) {
RegisterTestingT(t)
agent := agent.NewAgent()
<-agent.After()
err := agent.Error()
Expect(err).ToNot(BeNil())
}
func TestAgentWithPlugin(t *testing.T) {
RegisterTestingT(t)
np1 := &TestPlugin{}
agent := agent.NewAgent(agent.Plugins(np1))
Expect(agent.Options()).NotTo(BeNil())
Expect(agent.Options().Plugins).ToNot(BeNil())
Expect(len(agent.Options().Plugins)).To(Equal(1))
Expect(agent.Options().Plugins[0]).To(Equal(np1))
err := agent.Start()
Expect(err).To(BeNil())
Expect(agent.Options().Plugins[0].(*TestPlugin).Initialized()).To(BeTrue())
Expect(agent.Options().Plugins[0].(*TestPlugin).AfterInitialized()).To(BeTrue())
Expect(agent.Options().Plugins[0].(*TestPlugin).Closed()).To(BeFalse())
err = agent.Stop()
Expect(agent.Options().Plugins[0].(*TestPlugin).Closed()).To(BeTrue())
Expect(err).To(BeNil())
}
func TestAgentWithPluginInitFailed(t *testing.T) {
RegisterTestingT(t)
agent := agent.NewAgent(agent.Plugins(NewTestPlugin(true, false, false)))
Expect(agent.Options()).NotTo(BeNil())
Expect(agent.Options().Plugins).ToNot(BeNil())
err := agent.Start()
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(Equal(initFailedErrorString))
Expect(agent.Options().Plugins[0].(*TestPlugin).Initialized()).To(BeTrue())
Expect(agent.Options().Plugins[0].(*TestPlugin).AfterInitialized()).To(BeFalse())
Expect(agent.Options().Plugins[0].(*TestPlugin).Closed()).To(BeFalse())
err = agent.Stop()
Expect(err).To(HaveOccurred())
err = agent.Wait()
Expect(err).To(HaveOccurred())
}
func TestAgentWithPluginAfterInitFailed(t *testing.T) {
RegisterTestingT(t)
agent := agent.NewAgent(agent.Plugins(NewTestPlugin(false, true, false)))
Expect(agent.Options()).NotTo(BeNil())
Expect(agent.Options().Plugins).ToNot(BeNil())
err := agent.Start()
Expect(err).To(HaveOccurred())
Expect(agent.Options().Plugins[0].(*TestPlugin).Initialized()).To(BeTrue())
Expect(agent.Options().Plugins[0].(*TestPlugin).AfterInitialized()).To(BeTrue())
Expect(agent.Options().Plugins[0].(*TestPlugin).Closed()).To(BeFalse())
err = agent.Stop()
Expect(err).To(HaveOccurred())
err = agent.Wait()
Expect(err).To(HaveOccurred())
}
func TestAgentWithPluginCloseFailed(t *testing.T) {
RegisterTestingT(t)
agent := agent.NewAgent(agent.Plugins(NewTestPlugin(false, false, true)))
Expect(agent.Options()).NotTo(BeNil())
Expect(agent.Options().Plugins).ToNot(BeNil())
err := agent.Start()
Expect(err).To(BeNil())
Expect(agent.Options().Plugins[0].(*TestPlugin).Initialized()).To(BeTrue())
Expect(agent.Options().Plugins[0].(*TestPlugin).AfterInitialized()).To(BeTrue())
Expect(agent.Options().Plugins[0].(*TestPlugin).Closed()).To(BeFalse())
err = agent.Stop()
Expect(err).To(HaveOccurred())
Expect(agent.Options().Plugins[0].(*TestPlugin).Closed()).To(BeTrue())
}
func TestAgentWithPluginWait(t *testing.T) {
RegisterTestingT(t)
agent := agent.NewAgent(agent.Plugins(&TestPlugin{}))
err := agent.Start()
Expect(err).To(BeNil())
waitOverCh := make(chan struct{})
go func() {
err = agent.Wait()
Expect(err).To(BeNil())
close(waitOverCh)
}()
time.Sleep(100 * time.Millisecond) // Make sure we aren't racing the go routing
select {
case <-waitOverCh:
Expect(false).To(BeTrue()) // Fail if we get here at all because waitOverCh should not be closed
default:
}
Expect(agent.Stop()).To(BeNil())
time.Sleep(100 * time.Millisecond) // Make sure we aren't racing the go routing
_, channelOpen := <-waitOverCh
Expect(channelOpen).To(BeFalse()) // ie, the wait returned
}
func TestAgentWithPluginsRun(t *testing.T) {
RegisterTestingT(t)
agent := agent.NewAgent(agent.Plugins(&TestPlugin{}))
waitOverCh := make(chan struct{})
go func() {
err := agent.Run()
Expect(err).To(BeNil())
close(waitOverCh)
}()
time.Sleep(100 * time.Millisecond) // Make sure we aren't racing the go routing
select {
case <-waitOverCh:
Expect(false).To(BeTrue()) // Fail if we get here at all because waitOverCh should not be closed
default:
}
Expect(agent.Stop()).To(BeNil())
time.Sleep(100 * time.Millisecond) // Make sure we aren't racing the go routing
_, channelOpen := <-waitOverCh
Expect(channelOpen).To(BeFalse()) // ie, the wait returned
}
func TestAgentWithPluginsRunFail(t *testing.T) {
RegisterTestingT(t)
agent := agent.NewAgent(agent.Plugins(NewTestPlugin(true, false, false)))
waitOverCh := make(chan struct{})
go func() {
err := agent.Run()
Expect(err).ToNot(BeNil())
close(waitOverCh)
}()
select {
case <-waitOverCh:
case <-time.After(100 * time.Millisecond):
Expect(false).To(BeTrue()) // Fail if we get here at all because waitOverCh should already be closed
}
}
func TestAgentWithPluginsReceiveSignal(t *testing.T) {
RegisterTestingT(t)
agent := agent.NewAgent(agent.Plugins(&TestPlugin{}))
err := agent.Start()
Expect(err).To(BeNil())
wg := sync.WaitGroup{}
wg.Add(1) // We have one go routine we are about to launch
logrus.DefaultLogger().Info("wg.Add(1)")
go func() {
logrus.DefaultLogger().Info("entering goroutine")
defer wg.Done() // Release the wg when we are done
err := agent.Wait()
Expect(err).To(BeNil())
logrus.DefaultLogger().Info("exiting goroutine")
}()
// Send the signal
syscall.Kill(os.Getpid(), syscall.SIGINT)
logrus.DefaultLogger().Info("wg.Wait()")
wg.Wait()
}
func TestAgentWithNamedPlugin(t *testing.T) {
RegisterTestingT(t)
p := NewTestPlugin(false, false, false)
p.SetName(defaultPluginName)
agent := agent.NewAgent(agent.Plugins(p))
Expect(agent).NotTo(BeNil())
Expect(agent.Options()).NotTo(BeNil())
Expect(agent.Options().Plugins).ToNot(BeNil())
Expect(len(agent.Options().Plugins)).To(Equal(1))
Expect(agent.Options().Plugins[0]).To(Equal(p))
err := agent.Start()
Expect(err).To(BeNil())
err = agent.Stop()
Expect(err).To(BeNil())
}
func TestAgentWithPluginNoAfterInit(t *testing.T) {
RegisterTestingT(t)
p := &TestPluginNoAfterInit{}
agent := agent.NewAgent(agent.Plugins(p))
Expect(agent).NotTo(BeNil())
Expect(agent.Options()).NotTo(BeNil())
Expect(agent.Options().Plugins).ToNot(BeNil())
Expect(len(agent.Options().Plugins)).To(Equal(1))
Expect(agent.Options().Plugins[0]).To(Equal(p))
err := agent.Start()
Expect(err).To(BeNil())
err = agent.Stop()
Expect(err).To(BeNil())
}
// Define the TestPluginNoAfterInit we will use for testing
type TestPluginNoAfterInit struct{}
func (*TestPluginNoAfterInit) Close() error {
return nil
}
func (*TestPluginNoAfterInit) Init() error {
return nil
}
func (*TestPluginNoAfterInit) String() string {
return defaultPluginName
}
// Define the TestPlugin we will use for testing
type TestPlugin struct {
failInit bool
failAfterInit bool
failClose bool
sync.Mutex
initCalled bool
afterInitCalled bool
closeCalled bool
infra.PluginName
}
func NewTestPlugin(failInit, failAfterInit, failClose bool) *TestPlugin {
return &TestPlugin{
failInit: failInit,
failAfterInit: failAfterInit,
failClose: failClose,
}
}
func (p *TestPlugin) Init() error {
p.Lock()
defer p.Unlock()
p.initCalled = true
if p.failInit {
return fmt.Errorf(initFailedErrorString)
}
return nil
}
func (p *TestPlugin) AfterInit() error {
p.Lock()
defer p.Unlock()
p.afterInitCalled = true
if p.failAfterInit {
return fmt.Errorf(afterInitFailedErrorString)
}
return nil
}
func (p *TestPlugin) Close() error {
p.Lock()
defer p.Unlock()
p.closeCalled = true
if p.failClose {
return fmt.Errorf(closeFailedErrorString)
}
return nil
}
func (p *TestPlugin) Initialized() bool {
p.Lock()
defer p.Unlock()
return p.initCalled
}
func (p *TestPlugin) AfterInitialized() bool {
p.Lock()
defer p.Unlock()
return p.afterInitCalled
}
func (p *TestPlugin) Closed() bool {
p.Lock()
defer p.Unlock()
return p.closeCalled
}