/
agent_test_helpers.go
368 lines (315 loc) · 12.5 KB
/
agent_test_helpers.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
363
364
365
366
367
368
package itest
import (
"encoding/json"
"github.com/ligato/cn-infra/core"
etcdmock "github.com/ligato/cn-infra/db/keyval/etcdv3/mocks"
httpmock "github.com/ligato/cn-infra/rpc/rest/mock"
"github.com/onsi/gomega"
"testing"
//etcdmock "github.com/ligato/cn-infra/db/keyval/etcdv3/mocks"
"bytes"
"fmt"
"github.com/golang/protobuf/proto"
agent_api "github.com/ligato/cn-infra/core"
"github.com/ligato/cn-infra/datasync"
"github.com/ligato/cn-infra/db/keyval/etcdv3"
"github.com/ligato/cn-infra/flavors/local"
"github.com/ligato/cn-infra/health/probe"
"github.com/ligato/cn-infra/logging/logmanager"
"github.com/ligato/cn-infra/rpc/rest"
"github.com/ligato/cn-infra/servicelabel"
sfccore "github.com/ligato/sfc-controller/controller/core"
"github.com/ligato/sfc-controller/controller/model/controller"
"github.com/ligato/sfc-controller/plugins/vnfdriver"
vppiface "github.com/ligato/vpp-agent/plugins/defaultplugins/ifplugin/model/interfaces"
"github.com/ligato/vpp-agent/plugins/defaultplugins/l2plugin/model/l2"
"io/ioutil"
"github.com/ligato/cn-infra/logging/logroot"
"time"
)
// AgentTestHelper is similar to what testing.T is in golang packages.
type AgentTestHelper struct {
// agent for sfcFlavor
sfcAgent *core.Agent
// sfc controller plugins with it's own connectivty to ETCD
sfcFalvor *Flavor
// testing purposes only connectivity to ETCD
tFlavor *TestingConFlavor
// agent for tFlavor
tAgent *core.Agent
httpMock *httpmock.HTTPMock
golangT *testing.T
stopDB func()
}
// Given is composition of multiple test step methods (see BDD Given keyword)
type Given struct {
agentT *AgentTestHelper
}
// When is composition of multiple test step methods (see BDD When keyword)
type When struct {
}
// Then is composition of multiple test step methods (see BDD Then keyword)
type Then struct {
agentT *AgentTestHelper
}
// DefaultSetup initializes the SFC Controller with embedded ETCD
func (t *AgentTestHelper) DefaultSetup(golangT *testing.T) {
gomega.RegisterTestingT(golangT)
t.golangT = golangT
tFlavorLocal := &local.FlavorLocal{ServiceLabel: servicelabel.Plugin{MicroserviceLabel: "test-utils"}}
etcdPlug, embedETCD := StartEmbeddedETCD(golangT, tFlavorLocal)
t.tFlavor = &TestingConFlavor{
FlavorLocal: tFlavorLocal,
ETCD: *etcdPlug,
}
t.stopDB = embedETCD.Stop
t.httpMock = MockHTTP()
t.sfcFalvor = &Flavor{
FlavorLocal: &local.FlavorLocal{ServiceLabel: servicelabel.Plugin{MicroserviceLabel: "sfc-controller"}},
HTTP: *rest.FromExistingServer(t.httpMock.SetHandler),
ETCD: *etcdPlug}
t.tAgent = core.NewAgent(tFlavorLocal.LoggerFor("tAgent"), 1*time.Second, t.tFlavor.Plugins()...)
err := t.tAgent.Start()
if err != nil {
panic(err)
}
t.sfcAgent = core.NewAgent(logroot.StandardLogger(), 2000*time.Second, t.sfcFalvor.Plugins()...)
}
// StartAgent in test (if there is error than panic => fail test)
func (t *Given) StartAgent() {
err := t.agentT.sfcAgent.Start()
if err != nil {
t.agentT.golangT.Fatal("error starting sfcAgent ", err)
}
}
// EmptyETCD deletes all keys in ETCD
func (t *Given) EmptyETCD() {
db := t.agentT.tFlavor.ETCD.NewBroker("" /*TODO use Root Const*/)
db.Delete("/", datasync.WithPrefix())
}
// ConfigSFCviaETCD puts SFC config to keyvalue store (e.g. ETCD)
func (t *Given) ConfigSFCviaETCD(cfg *sfccore.YamlConfig) {
db := t.agentT.tFlavor.ETCD.NewBroker("" /*TODO use Root Const*/)
for _, hostEntity := range cfg.HEs {
db.Put(controller.HostEntityNameKey(hostEntity.Name), &hostEntity)
}
for _, sfcEntity := range cfg.SFCs {
db.Put(controller.SfcEntityNameKey(sfcEntity.Name), &sfcEntity)
}
for _, extEntity := range cfg.EEs {
db.Put(controller.ExternalEntityNameKey(extEntity.Name), &extEntity)
}
}
// ConfigSFCviaREST posts SFC config via REST
func (t *Given) ConfigSFCviaREST(cfg *sfccore.YamlConfig) {
for _, hostEntity := range cfg.HEs {
data, _ := json.Marshal(hostEntity)
httpResp, _ := t.agentT.httpMock.NewRequest("POST", "http://127.0.0.1"+
controller.HostEntityNameKey(hostEntity.Name), bytes.NewReader(data))
gomega.Expect(httpResp.StatusCode).Should(gomega.BeEquivalentTo(200), "not HTTP 200")
data, _ = ioutil.ReadAll(httpResp.Body)
fmt.Println("xxx httpResp.Body post HEs ", string(data))
}
for _, sfcEntity := range cfg.SFCs {
data, _ := json.Marshal(sfcEntity)
httpResp, _ := t.agentT.httpMock.NewRequest("POST", "http://127.0.0.1"+
controller.SfcEntityNameKey(sfcEntity.Name), bytes.NewReader(data))
gomega.Expect(httpResp.StatusCode).Should(gomega.BeEquivalentTo(200), "not HTTP 200")
data, _ = ioutil.ReadAll(httpResp.Body)
fmt.Println("xxx httpResp.Body post SFCs ", string(data))
}
for _, extEntity := range cfg.EEs {
data, _ := json.Marshal(extEntity)
httpResp, _ := t.agentT.httpMock.NewRequest("POST", "http://127.0.0.1"+
controller.ExternalEntityNameKey(extEntity.Name), bytes.NewReader(data))
gomega.Expect(httpResp.StatusCode).Should(gomega.BeEquivalentTo(200), "not HTTP 200")
data, _ = ioutil.ReadAll(httpResp.Body)
fmt.Println("xxx httpResp.Body post EEs ", string(data))
}
}
// VppAgentcCfgContains
func (t *Then) VppAgentCfgContains(micorserviceLabel string, interfaceBDEtc ...proto.Message) {
db := t.agentT.tFlavor.ETCD.NewBroker(servicelabel.GetDifferentAgentPrefix(micorserviceLabel))
for _, expected := range interfaceBDEtc {
switch expected.(type) {
case *vppiface.Interfaces_Interface:
ifaceExpected := expected.(*vppiface.Interfaces_Interface)
ifaceExist := &vppiface.Interfaces_Interface{}
key := vppiface.InterfaceKey(ifaceExpected.Name)
found, _, err := db.GetValue(key, ifaceExist)
gomega.Expect(found).Should(gomega.BeTrue(), "interface not found "+key)
gomega.Expect(err).Should(gomega.BeNil(), "error reading "+key)
gomega.Expect(ifaceExist).Should(gomega.BeEquivalentTo(ifaceExpected), "error reading "+key)
case *l2.BridgeDomains_BridgeDomain:
bdExpected := expected.(*l2.BridgeDomains_BridgeDomain)
bdActual := &l2.BridgeDomains_BridgeDomain{}
key := l2.BridgeDomainKey(bdExpected.Name)
found, _, err := db.GetValue(key, bdActual)
gomega.Expect(found).Should(gomega.BeTrue(), "bd not found "+key)
gomega.Expect(err).Should(gomega.BeNil(), "error reading "+key)
gomega.Expect(bdActual).Should(gomega.BeEquivalentTo(bdExpected), "error reading "+key)
}
}
}
// HTTPGet simulates the HTTP call
func (t *Then) HTTPGetEntities(sfcCfg *sfccore.YamlConfig) {
{ //SFCs
url := "http://127.0.0.1/sfc-controller/api/v1/SFCs"
httpResp, err := t.agentT.httpMock.NewRequest("GET", url, nil)
gomega.Expect(err).Should(gomega.BeNil(), "error reading getting SFC entities")
gomega.Expect(httpResp.StatusCode).Should(gomega.BeEquivalentTo(200), "not HTTP 200")
data, _ := ioutil.ReadAll(httpResp.Body)
fmt.Println("xxx httpResp.Body SFCs ", string(data))
}
{ //HEs
url := "http://127.0.0.1/sfc-controller/api/v1/HEs"
httpResp, err := t.agentT.httpMock.NewRequest("GET", url, nil)
gomega.Expect(err).Should(gomega.BeNil(), "error reading getting HE entities")
gomega.Expect(httpResp.StatusCode).Should(gomega.BeEquivalentTo(200), "not HTTP 200")
data, _ := ioutil.ReadAll(httpResp.Body)
fmt.Println("xxx httpResp.Body HEs ", string(data))
}
{ //EEs
url := "http://127.0.0.1/sfc-controller/api/v1/EEs"
httpResp, err := t.agentT.httpMock.NewRequest("GET", url, nil)
gomega.Expect(err).Should(gomega.BeNil(), "error reading getting EE entities")
gomega.Expect(httpResp.StatusCode).Should(gomega.BeEquivalentTo(200), "not HTTP 200")
data, _ := ioutil.ReadAll(httpResp.Body)
fmt.Println("xxx httpResp.Body EEs ", string(data))
}
for _, expected := range sfcCfg.SFCs {
url := "http://127.0.0.1" + controller.SfcEntityNameKey(expected.Name)
fmt.Println("xxx url: " + url)
httpResp, err := t.agentT.httpMock.NewRequest("GET", url, nil)
gomega.Expect(err).Should(gomega.BeNil(), "error reading getting SFC entity")
gomega.Expect(httpResp.StatusCode).Should(gomega.BeEquivalentTo(200), "not HTTP 200")
data, _ := ioutil.ReadAll(httpResp.Body)
actual := &controller.SfcEntity{}
json.Unmarshal(data, actual)
gomega.Expect(actual).Should(gomega.BeEquivalentTo(&expected), "not eq sfc entities")
}
for _, expected := range sfcCfg.HEs {
url := "http://127.0.0.1" + controller.HostEntityNameKey(expected.Name)
httpResp, err := t.agentT.httpMock.NewRequest("GET", url, nil)
gomega.Expect(err).Should(gomega.BeNil(), "error reading getting SFC entity")
gomega.Expect(httpResp.StatusCode).Should(gomega.BeEquivalentTo(200), "not HTTP 200")
data, _ := ioutil.ReadAll(httpResp.Body)
actual := &controller.HostEntity{}
json.Unmarshal(data, actual)
gomega.Expect(actual).Should(gomega.BeEquivalentTo(&expected), "not eq host entities")
}
for _, expected := range sfcCfg.EEs {
url := "http://127.0.0.1" + controller.ExternalEntityNameKey(expected.Name)
httpResp, err := t.agentT.httpMock.NewRequest("GET", url, nil)
gomega.Expect(err).Should(gomega.BeNil(), "error reading getting SFC entity")
gomega.Expect(httpResp.StatusCode).Should(gomega.BeEquivalentTo(200), "not HTTP 200")
data, _ := ioutil.ReadAll(httpResp.Body)
actual := &controller.ExternalEntity{}
json.Unmarshal(data, actual)
gomega.Expect(actual).Should(gomega.BeEquivalentTo(&expected), "not eq external entities")
}
}
// Teardown stops the sfcAgent
func (t *AgentTestHelper) Teardown() {
if t.sfcAgent != nil {
err := t.sfcAgent.Stop()
if err != nil {
t.golangT.Fatal("error stoppig sfcAgent ", err)
}
}
if t.stopDB != nil {
t.stopDB()
}
if t.tAgent != nil {
err := t.tAgent.Stop()
if err != nil {
t.golangT.Fatal("error stoppig sfcAgent ", err)
}
}
}
// MockHTTP returns new instance of HTTP mock
// (usefull to avoid import aliases in other files)
func MockHTTP() *httpmock.HTTPMock {
return &httpmock.HTTPMock{}
}
// StartEmbeddedETCD initializes embedded ETCD & returns plugin instance for accessing it
func StartEmbeddedETCD(t *testing.T, flavorLocal *local.FlavorLocal) (*etcdv3.Plugin, *etcdmock.Embedded) {
embeddedETCD := etcdmock.Embedded{}
embeddedETCD.Start(t)
etcdClientLogger := flavorLocal.LoggerFor("embedEtcdClient")
etcdBytesCon, err := etcdv3.NewEtcdConnectionUsingClient(embeddedETCD.Client(), etcdClientLogger)
if err != nil {
panic(err)
}
return etcdv3.FromExistingConnection(etcdBytesCon, &flavorLocal.ServiceLabel), &embeddedETCD
}
// Flavor is set of common used generic plugins. This flavour can be used as a base
// for different flavours. The plugins are initialized in the same order as they appear
// in the structure.
type Flavor struct {
*local.FlavorLocal
HTTP rest.Plugin
HealthRPC probe.Plugin
LogMngRPC logmanager.Plugin
ETCD etcdv3.Plugin
Sfc sfccore.SfcControllerPluginHandler
VNFDriver vnfdriver.Plugin
injected bool
}
// Inject interconnects plugins - injects the dependencies. If it has been called
// already it is no op.
func (f *Flavor) Inject() bool {
if f.injected {
return false
}
f.FlavorLocal.Inject()
httpLogDeps := f.LogDeps("http")
f.HTTP.Deps.Log = httpLogDeps.Log
f.HTTP.Deps.PluginName = httpLogDeps.PluginName
logMngLogDeps := f.LogDeps("log-mng-rpc")
f.LogMngRPC.Deps.Log = logMngLogDeps.Log
f.LogMngRPC.Deps.PluginName = logMngLogDeps.PluginName
f.LogMngRPC.LogRegistry = f.FlavorLocal.LogRegistry()
f.LogMngRPC.HTTP = &f.HTTP
f.HealthRPC.Deps.PluginLogDeps = *f.LogDeps("health-rpc")
f.HealthRPC.Deps.HTTP = &f.HTTP
f.HealthRPC.Deps.StatusCheck = &f.StatusCheck
f.ETCD.Deps.PluginInfraDeps = *f.InfraDeps("etcdv3")
f.Sfc.Etcd = &f.ETCD
f.Sfc.HTTPmux = &f.HTTP
f.VNFDriver.Etcd = &f.ETCD
f.VNFDriver.HTTPmux = &f.HTTP
f.injected = true
return true
}
// Plugins returns all plugins from the flavour. The set of plugins is supposed
// to be passed to the sfcAgent constructor. The method calls inject to make sure that
// dependencies have been injected.
func (f *Flavor) Plugins() []*agent_api.NamedPlugin {
f.Inject()
return agent_api.ListPluginsInFlavor(f)
}
// TestingConFlavor - just ETCD connectivity
type TestingConFlavor struct {
*local.FlavorLocal
ETCD etcdv3.Plugin
injected bool
}
// Inject interconnects plugins - injects the dependencies. If it has been called
// already it is no op.
func (f *TestingConFlavor) Inject() bool {
if f.injected {
return false
}
f.FlavorLocal.Inject()
f.ETCD.Deps.PluginInfraDeps = *f.InfraDeps("etcdv3")
f.injected = true
return true
}
// Plugins returns all plugins from the flavour. The set of plugins is supposed
// to be passed to the sfcAgent constructor. The method calls inject to make sure that
// dependencies have been injected.
func (f *TestingConFlavor) Plugins() []*agent_api.NamedPlugin {
f.Inject()
return agent_api.ListPluginsInFlavor(f)
}