forked from aporeto-inc/trireme-lib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrireme_test.go
362 lines (294 loc) · 12.8 KB
/
trireme_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
357
358
359
360
361
362
package trireme
import (
"reflect"
"testing"
"github.com/aporeto-inc/trireme/collector"
"github.com/aporeto-inc/trireme/constants"
"github.com/aporeto-inc/trireme/enforcer"
"github.com/aporeto-inc/trireme/monitor"
"github.com/aporeto-inc/trireme/policy"
"github.com/aporeto-inc/trireme/supervisor"
)
func createMocks() (TestPolicyResolver, map[constants.PUType]supervisor.Supervisor, map[constants.PUType]enforcer.PolicyEnforcer, monitor.TestMonitor, collector.EventCollector) {
tresolver := NewTestPolicyResolver()
s := supervisor.NewTestSupervisor()
tsupervisor := map[constants.PUType]supervisor.Supervisor{constants.ContainerPU: s}
tenforcer := map[constants.PUType]enforcer.PolicyEnforcer{constants.ContainerPU: enforcer.NewTestPolicyEnforcer()}
tmonitor := monitor.NewTestMonitor()
tcollector := &collector.DefaultCollector{}
return tresolver, tsupervisor, tenforcer, tmonitor, tcollector
}
func doTestCreate(t *testing.T, trireme Trireme, tresolver TestPolicyResolver, tsupervisor supervisor.TestSupervisor, tenforcer enforcer.TestPolicyEnforcer, tmonitor monitor.TestMonitor, id string, runtime *policy.PURuntime) {
resolverCount := 0
supervisorCount := 0
enforcerCount := 0
tresolver.MockResolvePolicy(t, func(contextID string, RuntimeReader policy.RuntimeReader) (*policy.PUPolicy, error) {
t.Logf("Into ResolvePolicy")
if contextID != id {
t.Errorf("Id in Resolve was expected to be %s, but is %s", id, contextID)
}
if !reflect.DeepEqual(runtime, RuntimeReader) {
t.Errorf("Runtime given to Resolver is not the same. Received %v, expected %v", RuntimeReader, runtime)
}
ipaddrs := policy.ExtendedMap{policy.DefaultNamespace: "127.0.0.1"}
tpolicy := policy.NewPUPolicy("SomeId", policy.Police, nil, nil, nil, nil, nil, nil, ipaddrs, []string{"172.17.0.0/24"}, []string{})
resolverCount++
return tpolicy, nil
})
tsupervisor.MockSupervise(t, func(contextID string, puInfo *policy.PUInfo) error {
if contextID != id {
t.Errorf("Id in Supervisor was expected to be %s, but is %s", id, contextID)
}
if !reflect.DeepEqual(runtime, puInfo.Runtime) {
t.Errorf("Runtime given to Supervisor is not the same. Received %v, expected %v", puInfo.Runtime, runtime)
}
t.Logf("Into Supervise")
supervisorCount++
return nil
})
tenforcer.MockEnforce(t, func(contextID string, puInfo *policy.PUInfo) error {
if contextID != id {
t.Errorf("Id in Enforcer was expected to be %s, but is %s", id, contextID)
}
if !reflect.DeepEqual(runtime, puInfo.Runtime) {
t.Errorf("Runtime given to Enforcer is not the same. Received %v, expected %v", puInfo.Runtime, runtime)
}
t.Logf("Into Enforce")
enforcerCount++
return nil
})
e := trireme.SetPURuntime(id, runtime)
if e != nil {
t.Errorf("Error while setting the Runtime in Trireme, %s", e)
}
err := trireme.HandlePUEvent(id, monitor.EventStart)
if err != nil {
t.Errorf("Create was supposed to be nil, was %s", err)
}
if resolverCount != 1 {
t.Errorf("Create didn't go to Resolver")
}
if supervisorCount != 1 {
t.Errorf("Create didn't go to Supervisor")
}
if enforcerCount != 1 {
t.Errorf("Create didn't go to Enforcer")
}
}
func doTestDelete(t *testing.T, trireme Trireme, tresolver TestPolicyResolver, tsupervisor supervisor.TestSupervisor, tenforcer enforcer.TestPolicyEnforcer, tmonitor monitor.TestMonitor, id string, runtime *policy.PURuntime) {
resolverCount := 0
supervisorCount := 0
enforcerCount := 0
tresolver.MockHandlePUEvent(t, func(contextID string, eventType monitor.Event) {
t.Logf("Into HandleDeletePU")
if eventType == monitor.EventStop {
resolverCount++
}
})
tsupervisor.MockUnsupervise(t, func(contextID string) error {
t.Logf("Into Unsupervise")
supervisorCount++
return nil
})
tenforcer.MockUnenforce(t, func(ip string) error {
t.Logf("Into Unenforce")
enforcerCount++
return nil
})
err := trireme.HandlePUEvent(id, monitor.EventStop)
if err != nil {
t.Errorf("Delete was supposed to be nil, was %s", err)
}
if resolverCount != 1 {
t.Errorf("Delete didn't go to Resolver")
}
if supervisorCount != 1 {
t.Errorf("Delete didn't go to Supervisor")
}
if enforcerCount != 1 {
t.Errorf("Delete didn't go to Enforcer")
}
}
func doTestDeleteNotExist(t *testing.T, trireme Trireme, tresolver TestPolicyResolver, tsupervisor supervisor.TestSupervisor, tenforcer enforcer.TestPolicyEnforcer, tmonitor monitor.TestMonitor, id string, runtime *policy.PURuntime) {
resolverCount := 0
supervisorCount := 0
enforcerCount := 0
tresolver.MockHandlePUEvent(t, func(contextID string, eventType monitor.Event) {
t.Logf("Into HandleDeletePU")
resolverCount++
})
tsupervisor.MockUnsupervise(t, func(contextID string) error {
t.Logf("Into Unsupervise")
supervisorCount++
return nil
})
tenforcer.MockUnenforce(t, func(ip string) error {
t.Logf("Into Unenforce")
enforcerCount++
return nil
})
err := trireme.HandlePUEvent(id, monitor.EventStop)
if err == nil {
t.Errorf("Delete was not supposed to be nil, was nil")
}
}
func doTestUpdate(t *testing.T, trireme Trireme, tresolver TestPolicyResolver, tsupervisor supervisor.TestSupervisor, tenforcer enforcer.TestPolicyEnforcer, tmonitor monitor.TestMonitor, id string, initialRuntime *policy.PURuntime, updatedPolicy *policy.PUPolicy) {
supervisorCount := 0
enforcerCount := 0
tsupervisor.MockSupervise(t, func(contextID string, puInfo *policy.PUInfo) error {
if contextID != id {
t.Errorf("Id in Supervisor was expected to be %s, but is %s", id, contextID)
}
if !reflect.DeepEqual(puInfo.Runtime, initialRuntime) {
t.Errorf("Runtime given to Supervisor is not the same. Received %v, expected %v", puInfo.Runtime, initialRuntime)
}
if !reflect.DeepEqual(puInfo.Policy, updatedPolicy) {
t.Errorf("Policy given to Supervisor is not the same. Received %v, expected %v", puInfo.Policy, updatedPolicy)
}
t.Logf("Into Supervise Update")
supervisorCount++
return nil
})
tenforcer.MockEnforce(t, func(contextID string, puInfo *policy.PUInfo) error {
// if contextID != id {
// t.Errorf("Id in Enforcer was expected to be %s, but is %s", id, contextID)
// }
if !reflect.DeepEqual(puInfo.Runtime, initialRuntime) {
t.Errorf("Runtime given to Supervisor is not the same. Received %v, expected %v", puInfo.Runtime, initialRuntime)
}
if !reflect.DeepEqual(puInfo.Policy, updatedPolicy) {
t.Errorf("Policy given to Supervisor is not the same. Received %v, expected %v", puInfo.Policy, updatedPolicy)
}
t.Logf("Into Enforce Update")
enforcerCount++
return nil
})
err := trireme.UpdatePolicy(id, updatedPolicy)
if err != nil {
t.Errorf("Update was supposed to be nil, was %s", err)
}
if supervisorCount != 1 {
t.Errorf("Update didn't go to Supervisor")
}
if enforcerCount != 1 {
t.Errorf("Update didn't go to Enforcer")
}
}
func TestSimpleCreate(t *testing.T) {
tresolver, tsupervisor, tenforcer, tmonitor, tcollector := createMocks()
trireme := NewTrireme("serverID", tresolver, tsupervisor, tenforcer, tcollector)
if err := trireme.Start(); err != nil {
t.Errorf("Failed to start trireme")
}
contextID := "123123"
runtime := policy.NewPURuntimeWithDefaults()
doTestCreate(t, trireme, tresolver, tsupervisor[constants.ContainerPU].(supervisor.TestSupervisor), tenforcer[constants.ContainerPU].(enforcer.TestPolicyEnforcer), tmonitor, contextID, runtime)
}
func TestSimpleDelete(t *testing.T) {
tresolver, tsupervisor, tenforcer, tmonitor, tcollector := createMocks()
trireme := NewTrireme("serverID", tresolver, tsupervisor, tenforcer, tcollector)
if err := trireme.Start(); err != nil {
t.Errorf("Failed to start trireme")
}
contextID := "123123"
runtime := policy.NewPURuntimeWithDefaults()
doTestDeleteNotExist(t, trireme, tresolver, tsupervisor[constants.ContainerPU].(supervisor.TestSupervisor), tenforcer[constants.ContainerPU].(enforcer.TestPolicyEnforcer), tmonitor, contextID, runtime)
}
func TestCreateDelete(t *testing.T) {
tresolver, tsupervisor, tenforcer, tmonitor, tcollector := createMocks()
trireme := NewTrireme("serverID", tresolver, tsupervisor, tenforcer, tcollector)
if err := trireme.Start(); err != nil {
t.Errorf("Failed to start trireme")
}
contextID := "123123"
runtime := policy.NewPURuntimeWithDefaults()
doTestCreate(t, trireme, tresolver, tsupervisor[constants.ContainerPU].(supervisor.TestSupervisor), tenforcer[constants.ContainerPU].(enforcer.TestPolicyEnforcer), tmonitor, contextID, runtime)
doTestDelete(t, trireme, tresolver, tsupervisor[constants.ContainerPU].(supervisor.TestSupervisor), tenforcer[constants.ContainerPU].(enforcer.TestPolicyEnforcer), tmonitor, contextID, runtime)
}
func TestSimpleUpdate(t *testing.T) {
tresolver, tsupervisor, tenforcer, tmonitor, tcollector := createMocks()
trireme := NewTrireme("serverID", tresolver, tsupervisor, tenforcer, tcollector)
if err := trireme.Start(); err != nil {
t.Errorf("Failed to start trireme")
}
contextID := "123123"
runtime := policy.NewPURuntimeWithDefaults()
ipa := policy.ExtendedMap{
"bridge": "10.10.10.10",
}
runtime.SetIPAddresses(ipa)
doTestCreate(t, trireme, tresolver, tsupervisor[constants.ContainerPU].(supervisor.TestSupervisor), tenforcer[constants.ContainerPU].(enforcer.TestPolicyEnforcer), tmonitor, contextID, runtime)
// Generate a new Policy ...
ipl := policy.ExtendedMap{policy.DefaultNamespace: "127.0.0.1"}
tagsMap := policy.NewTagStoreFromMap(map[string]string{enforcer.TransmitterLabel: contextID})
newPolicy := policy.NewPUPolicy("", policy.Police, nil, nil, nil, nil, tagsMap, nil, ipl, []string{"172.17.0.0/24"}, []string{})
doTestUpdate(t, trireme, tresolver, tsupervisor[constants.ContainerPU].(supervisor.TestSupervisor), tenforcer[constants.ContainerPU].(enforcer.TestPolicyEnforcer), tmonitor, contextID, runtime, newPolicy)
}
func TestCache(t *testing.T) {
tresolver, tsupervisor, tenforcer, tmonitor, tcollector := createMocks()
trireme := NewTrireme("serverID", tresolver, tsupervisor, tenforcer, tcollector)
if err := trireme.Start(); err != nil {
t.Errorf("Failed to start trireme")
}
contextID := "123123"
runtime := policy.NewPURuntimeWithDefaults()
for i := 0; i < 5; i++ {
doTestCreate(t, trireme, tresolver, tsupervisor[constants.ContainerPU].(supervisor.TestSupervisor), tenforcer[constants.ContainerPU].(enforcer.TestPolicyEnforcer), tmonitor, contextID, runtime)
// Expecting cache to find it
cacheRuntime, err := trireme.PURuntime(contextID)
if err != nil {
t.Errorf("Cache failed. No Error expected, but error returned %v", err)
}
if !reflect.DeepEqual(runtime, cacheRuntime) {
t.Errorf("Cache failed. Expected %v, got %v", runtime, cacheRuntime)
}
doTestDelete(t, trireme, tresolver, tsupervisor[constants.ContainerPU].(supervisor.TestSupervisor), tenforcer[constants.ContainerPU].(enforcer.TestPolicyEnforcer), tmonitor, contextID, runtime)
// Expecting cache to not find it
_, err = trireme.PURuntime(contextID)
if err == nil {
t.Errorf("Cache succeeded. Error expected, but No error returned ")
}
}
}
func TestStop(t *testing.T) {
tresolver, tsupervisor, tenforcer, tmonitor, tcollector := createMocks()
trireme := NewTrireme("serverID", tresolver, tsupervisor, tenforcer, tcollector)
if err := trireme.Start(); err != nil {
t.Errorf("Failed to start trireme")
}
contextID := "123123"
runtime := policy.NewPURuntimeWithDefaults()
doTestCreate(t, trireme, tresolver, tsupervisor[constants.ContainerPU].(supervisor.TestSupervisor), tenforcer[constants.ContainerPU].(enforcer.TestPolicyEnforcer), tmonitor, contextID, runtime)
if err := trireme.Stop(); err != nil {
t.Errorf("Failed to stop trireme ")
}
if err := trireme.Start(); err != nil {
t.Errorf("Failed to start trireme")
}
doTestCreate(t, trireme, tresolver, tsupervisor[constants.ContainerPU].(supervisor.TestSupervisor), tenforcer[constants.ContainerPU].(enforcer.TestPolicyEnforcer), tmonitor, contextID, runtime)
}
func TestTransmitterLabel(t *testing.T) {
// If management ID is set, use it as the TransmitterLabel
mgmtID := "mgmt"
contextID := "mgmt"
containerInfo := policy.NewPUInfo(contextID, constants.ContainerPU)
addTransmitterLabel(contextID, containerInfo)
label, ok := containerInfo.Policy.Identity().Get(enforcer.TransmitterLabel)
if !ok {
t.Errorf("Expecting Transmitter label to be set but it is missing")
}
if label != mgmtID {
t.Errorf("Expecting Transmitter label to be set to MgmtID: %s , but was set to: %s", mgmtID, label)
}
// If management ID is not set, use contextID as the TransmitterLabel
contextID = "Context"
containerInfo = policy.NewPUInfo(contextID, constants.ContainerPU)
addTransmitterLabel(contextID, containerInfo)
label, ok = containerInfo.Policy.Identity().Get(enforcer.TransmitterLabel)
if !ok {
t.Errorf("Expecting Transmitter label to be set but it is missing")
}
if label != contextID {
t.Errorf("Expecting Transmitter label to be set to ContextID: %s , but was set to: %s", contextID, label)
}
}