/
manager_test.go
345 lines (271 loc) · 7.02 KB
/
manager_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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package licenser
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/elastic/beats/libbeat/logp"
)
type message struct {
license *License
err error
}
type mockFetcher struct {
sync.Mutex
bus chan message
last *message
}
func newMockFetcher() *mockFetcher {
return &mockFetcher{bus: make(chan message, 1)}
}
func (m *mockFetcher) Fetch() (*License, error) {
m.Lock()
defer m.Unlock()
for {
select {
case message := <-m.bus:
m.last = &message
// assume other calls to receive the same value,
// until we change it.
return message.license, message.err
default:
if m.last != nil {
return m.last.license, m.last.err
}
continue
}
}
}
func (m *mockFetcher) Insert(license *License, err error) {
m.bus <- message{license: license, err: err}
}
func (m *mockFetcher) Close() {
close(m.bus)
}
func TestRetrieveLicense(t *testing.T) {
i := &License{
UUID: mustUUIDV4().String(),
Type: Basic,
Mode: Basic,
Status: Active,
}
mock := newMockFetcher()
mock.Insert(i, nil)
defer mock.Close()
t.Run("return an error if the manager is stopped", func(t *testing.T) {
m := NewWithFetcher(mock, time.Duration(2*time.Second), time.Duration(1*time.Second))
m.Start()
m.Stop()
_, err := m.Get()
assert.Error(t, ErrManagerStopped, err)
})
t.Run("at startup when no license is retrieved return an error", func(t *testing.T) {
mck := newMockFetcher()
mck.Insert(nil, errors.New("not found"))
defer mck.Close()
m := NewWithFetcher(mck, time.Duration(2*time.Second), time.Duration(1*time.Second))
m.Start()
defer m.Stop()
_, err := m.Get()
assert.Error(t, ErrNoLicenseFound, err)
})
t.Run("at startup", func(t *testing.T) {
m := NewWithFetcher(mock, time.Duration(2*time.Second), time.Duration(1*time.Second))
m.Start()
defer m.Stop()
// Lets us find the first license.
time.Sleep(1 * time.Second)
_, err := m.Get()
assert.NoError(t, err)
})
t.Run("periodically", func(t *testing.T) {
period := time.Duration(1)
m := NewWithFetcher(mock, period, time.Duration(5*time.Second))
m.Start()
defer m.Stop()
// Lets us find the first license.
time.Sleep(1 * time.Second)
l, err := m.Get()
if !assert.NoError(t, err) {
return
}
if !assert.True(t, l.Is(Basic)) {
return
}
i := &License{
UUID: mustUUIDV4().String(),
Type: Platinum,
Mode: Platinum,
Status: Active,
}
mock.Insert(i, nil)
select {
case <-time.After(time.Duration(1 * time.Second)):
l, err := m.Get()
if !assert.NoError(t, err) {
return
}
assert.True(t, l.Is(Platinum))
}
})
}
func TestWatcher(t *testing.T) {
i := &License{
UUID: mustUUIDV4().String(),
Type: Basic,
Mode: Basic,
Status: Active,
}
t.Run("watcher must be uniquely registered", func(t *testing.T) {
mock := newMockFetcher()
mock.Insert(i, nil)
defer mock.Close()
m := NewWithFetcher(mock, time.Duration(2*time.Second), time.Duration(1*time.Second))
m.Start()
defer m.Stop()
w := CallbackWatcher{New: func(license License) {}}
err := m.AddWatcher(&w)
if assert.NoError(t, err) {
return
}
defer m.RemoveWatcher(&w)
err = m.AddWatcher(&w)
assert.Error(t, ErrWatcherAlreadyExist, err)
})
t.Run("cannot remove non existing watcher", func(t *testing.T) {
mock := newMockFetcher()
mock.Insert(i, nil)
defer mock.Close()
m := NewWithFetcher(mock, time.Duration(2*time.Second), time.Duration(1*time.Second))
m.Start()
defer m.Stop()
w := CallbackWatcher{New: func(license License) {}}
err := m.RemoveWatcher(&w)
assert.Error(t, ErrWatcherDoesntExist, err)
})
t.Run("adding a watcher trigger a a new license callback", func(t *testing.T) {
mock := newMockFetcher()
mock.Insert(i, nil)
defer mock.Close()
m := NewWithFetcher(mock, time.Duration(2*time.Second), time.Duration(1*time.Second))
m.Start()
defer m.Stop()
chanLicense := make(chan License)
defer close(chanLicense)
w := CallbackWatcher{
New: func(license License) {
chanLicense <- license
},
}
m.AddWatcher(&w)
defer m.RemoveWatcher(&w)
select {
case license := <-chanLicense:
assert.Equal(t, Basic, license.Get())
}
})
t.Run("periodically trigger a new license callback when the license change", func(t *testing.T) {
mock := newMockFetcher()
mock.Insert(i, nil)
defer mock.Close()
m := NewWithFetcher(mock, time.Duration(1), time.Duration(1*time.Second))
m.Start()
defer m.Stop()
chanLicense := make(chan License)
defer close(chanLicense)
w := CallbackWatcher{
New: func(license License) {
chanLicense <- license
},
}
m.AddWatcher(&w)
defer m.RemoveWatcher(&w)
c := 0
for {
select {
case license := <-chanLicense:
if c == 0 {
assert.Equal(t, Basic, license.Get())
mock.Insert(&License{
UUID: mustUUIDV4().String(),
Type: Platinum,
Mode: Platinum,
Status: Active,
}, nil)
c++
continue
}
assert.Equal(t, Platinum, license.Get())
return
}
}
})
t.Run("trigger OnManagerStopped when the manager is stopped", func(t *testing.T) {
mock := newMockFetcher()
mock.Insert(i, nil)
defer mock.Close()
m := NewWithFetcher(mock, time.Duration(1), time.Duration(1*time.Second))
m.Start()
var wg sync.WaitGroup
wg.Add(1)
w := CallbackWatcher{
Stopped: func() {
wg.Done()
},
}
m.AddWatcher(&w)
defer m.RemoveWatcher(&w)
m.Stop()
wg.Wait()
})
}
func TestWaitForLicense(t *testing.T) {
i := &License{
UUID: mustUUIDV4().String(),
Type: Basic,
Mode: Basic,
Status: Active,
}
t.Run("when license is available and valid", func(t *testing.T) {
mock := newMockFetcher()
mock.Insert(i, nil)
defer mock.Close()
m := NewWithFetcher(mock, time.Duration(1), time.Duration(1*time.Second))
m.Start()
defer m.Stop()
err := WaitForLicense(context.Background(), logp.NewLogger(""), m, CheckBasic)
assert.NoError(t, err)
})
t.Run("when license is available and not valid", func(t *testing.T) {
mock := newMockFetcher()
mock.Insert(i, nil)
defer mock.Close()
m := NewWithFetcher(mock, time.Duration(1), time.Duration(1*time.Second))
m.Start()
defer m.Stop()
err := WaitForLicense(context.Background(), logp.NewLogger(""), m, CheckLicenseCover(Platinum))
assert.Error(t, err)
})
t.Run("when license is not available we can still interrupt", func(t *testing.T) {
mock := newMockFetcher()
mock.Insert(i, nil)
defer mock.Close()
m := NewWithFetcher(mock, time.Duration(1), time.Duration(1*time.Second))
m.Start()
defer m.Stop()
ctx, cancel := context.WithCancel(context.Background())
executed := make(chan struct{})
go func() {
err := WaitForLicense(ctx, logp.NewLogger(""), m, CheckLicenseCover(Platinum))
assert.Error(t, err)
close(executed)
}()
cancel()
<-executed
})
}