-
Notifications
You must be signed in to change notification settings - Fork 32
/
common.go
579 lines (518 loc) · 17.5 KB
/
common.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
package integration
// TODO(Michał): Split this file and refactor some functions.
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/Juniper/asf/pkg/fileutil"
"github.com/Juniper/asf/pkg/format"
"github.com/Juniper/asf/pkg/logutil"
"github.com/Juniper/asf/pkg/models"
"github.com/Juniper/contrail/pkg/client"
"github.com/Juniper/contrail/pkg/keystone"
"github.com/Juniper/contrail/pkg/sync"
"github.com/Juniper/contrail/pkg/testutil"
"github.com/coreos/etcd/clientv3"
"github.com/flosch/pongo2"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
asfclient "github.com/Juniper/asf/pkg/client"
asfservices "github.com/Juniper/asf/pkg/services"
integrationetcd "github.com/Juniper/contrail/pkg/testutil/integration/etcd"
yaml "gopkg.in/yaml.v2"
)
// Integration tests constants.
const (
collectTimeout = 5 * time.Second
DefaultClientID = "default"
)
// TestMain is a function that can be called inside package specific TestMain
// to enable integration testing capabilities.
func TestMain(m *testing.M, s **APIServer) {
WithViperConfig(func() {
cacheDB, cancelEtcdEventProducer, err := RunCacheDB()
if err != nil {
logutil.FatalWithStackTrace(errors.Wrap(err, "failed to run Cache DB"))
}
defer testutil.LogFatalIfError(cancelEtcdEventProducer)
if viper.GetBool("sync.enabled") {
sync, err := sync.NewEtcdFeeder("integration-test-sync")
if err != nil {
logutil.FatalWithStackTrace(errors.Wrap(err, "failed to initialize Sync"))
}
ctx, cancel := context.WithCancel(context.Background())
errChan := RunConcurrently(RunnerFunc(func() error { return sync.Start(ctx) }))
defer CloseFatalIfError(CloserFunc(cancel), errChan)
<-sync.DumpDone()
}
if srv, err := NewRunningServer(&APIServerConfig{
RepoRootPath: "../../..",
EnableEtcdNotifier: true,
EnableVNCReplication: viper.GetBool("server.enable_vnc_replication"),
CacheDB: cacheDB,
}); err != nil {
logutil.FatalWithStackTrace(errors.Wrap(err, "failed to initialize API Server"))
} else {
*s = srv
}
defer testutil.LogFatalIfError((*s).Close)
if code := m.Run(); code != 0 {
os.Exit(code)
}
})
}
// RunTestFromTestsDirectory invokes integration test located in "tests" directory.
func RunTestFromTestsDirectory(t *testing.T, name string, server *APIServer) {
ts, err := LoadTest(fmt.Sprintf("./tests/%s.yml", format.CamelToSnake(name)), nil)
require.NoError(t, err, "failed to load test data")
RunCleanTestScenario(t, ts, server)
}
// RunTest invokes integration test located in given file.
func RunTest(t *testing.T, file string, server *APIServer) {
ts, err := LoadTest(file, nil)
require.NoError(t, err)
RunCleanTestScenario(t, ts, server)
}
// RunTestTemplate invokes integration test from template located in given file.
func RunTestTemplate(t *testing.T, file string, server *APIServer, context map[string]interface{}) {
ts, err := LoadTest(file, context)
require.NoError(t, err)
RunCleanTestScenario(t, ts, server)
}
// WithViperConfig initializes Viper configuration and logging and runs given test function.
func WithViperConfig(f func()) {
if err := initViperConfig(); err != nil {
logutil.FatalWithStackTrace(errors.Wrap(err, "failed to initialize Viper config"))
}
if err := logutil.Configure(viper.GetString("log_level")); err != nil {
logutil.FatalWithStackTrace(err)
}
logrus.Info("Starting integration tests")
f()
logrus.Info("Finished integration tests")
}
func initViperConfig() error {
viper.SetConfigName("test_config")
viper.SetConfigType("yml")
viper.AddConfigPath(".")
viper.AddConfigPath("../../../../sample")
viper.AddConfigPath("../../../sample")
viper.AddConfigPath("../../sample")
viper.AddConfigPath("../sample")
viper.AddConfigPath("./sample")
viper.AddConfigPath("./test_data")
viper.SetEnvPrefix("contrail")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
return viper.ReadInConfig()
}
// Event represents event received from etcd watch.
type Event struct {
Data map[string]interface{} `yaml:"data,omitempty"`
SyncOnly bool `yaml:"sync_only,omitempty"`
}
func convertEventsIntoMapList(events []Event) []map[string]interface{} {
m := make([]map[string]interface{}, len(events))
for i := range events {
m[i] = events[i].Data
}
return m
}
// Watchers map contains slices of events that should be emitted on
// etcd key matching the map key.
type Watchers map[string][]Event
// Waiters map contains slices of events that have to be emitted
// during single task.
type Waiters map[string][]Event
//Task has API request and expected response.
type Task struct {
Name string `yaml:"name,omitempty"`
Client string `yaml:"client,omitempty"`
Request *asfclient.Request `yaml:"request,omitempty"`
Expect interface{} `yaml:"expect,omitempty"`
Watchers Watchers `yaml:"watchers,omitempty"`
Waiters Waiters `yaml:"await,omitempty"`
}
// CleanTask defines clean task
type CleanTask struct {
Client string `yaml:"client,omitempty"`
Path string `yaml:"path,omitempty"`
FQName []string `yaml:"fq_name,omitempty"`
Kind string `yaml:"kind,omitempty"`
}
// TestScenario defines integration test scenario.
type TestScenario struct {
Name string `yaml:"name,omitempty"`
Description string `yaml:"description,omitempty"`
IntentCompilerEnabled bool `yaml:"intent_compiler_enabled,omitempty"`
Tables []string `yaml:"tables,omitempty"`
ClientConfigs map[string]*asfclient.HTTPConfig `yaml:"clients,omitempty"`
Clients ClientsList `yaml:"-"`
CleanTasks []CleanTask `yaml:"cleanup,omitempty"`
Workflow []*Task `yaml:"workflow,omitempty"`
Watchers Watchers `yaml:"watchers,omitempty"`
TestData interface{} `yaml:"test_data,omitempty"`
}
// ClientsList is the list of clients used in test
type ClientsList map[string]*client.HTTP
// LoadTest loads test scenario from given file.
func LoadTest(file string, ctx map[string]interface{}) (*TestScenario, error) {
template, err := pongo2.FromFile(file)
if err != nil {
return nil, errors.Wrap(err, "failed to read test data template")
}
content, err := template.ExecuteBytes(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to apply test data template")
}
ts := TestScenario{Clients: ClientsList{}}
if err = yaml.UnmarshalStrict(content, &ts); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal test scenario %q", file)
}
return &ts, nil
}
type trackedResource struct {
Path string
Client string
}
// RunCleanTestScenario runs test scenario from loaded yaml file, expects no resources leftovers
func RunCleanTestScenario(
t *testing.T,
ts *TestScenario,
server *APIServer,
) {
logrus.WithField("test-scenario", ts.Name).Debug("Running clean test scenario")
checkWatchers := StartWatchers(t, ts.Name, ts.Watchers)
stopIC := startIntentCompiler(t, ts, server)
defer stopIC()
ctx := context.Background()
clients := PrepareClients(ctx, t, ts, server)
tracked := runTestScenario(ctx, t, ts, clients, server.dbService)
cleanupTrackedResources(ctx, tracked, clients)
checkWatchers(t)
}
// RunDirtyTestScenario runs test scenario from loaded yaml file, leaves all resources after scenario
func RunDirtyTestScenario(t *testing.T, ts *TestScenario, server *APIServer) func() {
logrus.WithField("test-scenario", ts.Name).Debug("Running dirty test scenario")
ctx := context.Background()
clients := PrepareClients(ctx, t, ts, server)
tracked := runTestScenario(ctx, t, ts, clients, server.dbService)
cleanupFunc := func() {
cleanupTrackedResources(ctx, tracked, clients)
}
return cleanupFunc
}
func cleanupTrackedResources(ctx context.Context, tracked []trackedResource, clients map[string]*client.HTTP) {
for _, tr := range tracked {
response, err := clients[tr.Client].EnsureDeleted(ctx, tr.Path, nil)
if err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"url-path": tr.Path,
"http-client": tr.Client,
}).Warn("Deleting dirty resource failed - ignoring")
}
if response.StatusCode == http.StatusOK {
logrus.WithFields(logrus.Fields{
"url-path": tr.Path,
"http-client": tr.Client,
}).Warn("Test scenario has not deleted resource but should have deleted - test scenario is dirty")
}
}
}
// StartWaiters checks if there are emitted events described before.
func StartWaiters(t *testing.T, task string, waiters Waiters) func(t *testing.T) {
checks := []func(t *testing.T){}
ec := integrationetcd.NewEtcdClient(t)
for key := range waiters {
events := waiters[key]
getMissingEvents := ec.WaitForEvents(
key, convertEventsIntoMapList(events), collectTimeout, clientv3.WithPrefix(),
)
checks = append(checks, createWaiterChecker(task, getMissingEvents))
}
return func(t *testing.T) {
defer ec.Close(t)
for _, c := range checks {
c(t)
}
}
}
func createWaiterChecker(
task string, getMissingEvents func() ([]map[string]interface{}, error),
) func(t *testing.T) {
return func(t *testing.T) {
notFound, err := getMissingEvents()
assert.Equal(t, nil, err, "waiter for task %v got an error while collecting events: %v", task, err)
assert.Equal(t, 0, len(notFound), "etcd didn't emitted events %v for task %v", notFound, task)
}
}
// StartWatchers checks if events emitted to etcd match those given in watchers dict.
func StartWatchers(t *testing.T, task string, watchers Watchers, opts ...clientv3.OpOption) func(t *testing.T) {
checks := []func(t *testing.T){}
ec := integrationetcd.NewEtcdClient(t)
syncEnabled := viper.GetBool("sync.enabled")
for key := range watchers {
events := filterEvents(watchers[key], func(e Event) bool {
if e.SyncOnly {
return syncEnabled
}
return true
})
collect := ec.WatchKeyN(key, len(events), collectTimeout, append(opts, clientv3.WithPrefix())...)
checks = append(checks, createWatchChecker(task, collect, key, events))
}
return func(t *testing.T) {
defer ec.Close(t)
for _, c := range checks {
c(t)
}
}
}
func filterEvents(evs []Event, pred func(Event) bool) []Event {
result := make([]Event, 0, len(evs))
for _, e := range evs {
if pred(e) {
result = append(result, e)
}
}
return result
}
func createWatchChecker(task string, collect func() []string, key string, events []Event) func(t *testing.T) {
return func(t *testing.T) {
collected := collect()
eventCount := len(events)
assert.Equal(
t, eventCount, len(collected),
"etcd emitted not enough events on %s(got %v, expected %v)\n",
key, collected, events,
)
for i, e := range events[:len(collected)] {
c := collected[i]
var data interface{} = map[string]interface{}{}
if len(c) > 0 {
d := json.NewDecoder(bytes.NewReader([]byte(c)))
d.UseNumber()
err := d.Decode(&data)
assert.NoError(t, err)
}
testutil.AssertEqual(t, e.Data, data, fmt.Sprintf("task: %s\netcd event not equal for %s[%v]", task, key, i))
}
}
}
func startIntentCompiler(
t *testing.T,
ts *TestScenario,
server *APIServer,
) context.CancelFunc {
if ts.IntentCompilerEnabled {
etcdClient := integrationetcd.NewEtcdClient(t)
etcdClient.Clear(t)
return RunIntentCompilationService(t, server.URL())
}
return func() {}
}
// PrepareClients creates HTTP clients based on given configurations and logs them in if needed.
// It assigns created clients to given test scenario.
func PrepareClients(ctx context.Context, t *testing.T, ts *TestScenario, server *APIServer) ClientsList {
for k, c := range ts.ClientConfigs {
ts.Clients[k] = client.NewHTTP(&asfclient.HTTPConfig{
ID: c.ID,
Password: c.Password,
Endpoint: server.URL(),
AuthURL: server.URL() + keystone.LocalAuthPath,
Scope: c.Scope,
Insecure: c.Insecure,
})
if c.ID != "" {
err := ts.Clients[k].Login(ctx)
assert.NoError(t, err, fmt.Sprintf("client %q failed to login", c.ID))
}
}
return ts.Clients
}
func runTestScenario(
ctx context.Context,
t *testing.T,
ts *TestScenario,
clients ClientsList,
m asfservices.MetadataGetter,
) (tracked []trackedResource) {
for _, cleanTask := range ts.CleanTasks {
logrus.WithFields(logrus.Fields{
"test-scenario": ts.Name,
"clean-task": cleanTask,
}).Debug("Deleting existing resources before test scenario workflow")
err := performCleanup(ctx, cleanTask, getClientByID(cleanTask.Client, clients), m)
if err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"test-scenario": ts.Name,
"clean-task": cleanTask,
}).Warn("Failed to delete existing resource before running workflow - ignoring")
}
}
for _, task := range ts.Workflow {
logrus.WithFields(logrus.Fields{
"test-scenario": ts.Name,
"task": task.Name,
}).Info("Starting task")
checkWatchers := StartWatchers(t, task.Name, task.Watchers)
checkWaiters := StartWaiters(t, ts.Name, task.Waiters)
task.Request.Data = fileutil.YAMLtoJSONCompat(task.Request.Data)
clientID := DefaultClientID
if task.Client != "" {
clientID = task.Client
}
client, ok := clients[clientID]
if !assert.True(t, ok,
"Client %q not defined in task %q of scenario %q", clientID, task.Name, ts.Name) {
break
}
response, err := client.DoRequest(ctx, task.Request)
assert.NoError(
t,
err,
fmt.Sprintf("HTTP request failed in task %q of scenario %q", task.Name, ts.Name),
)
tracked = handleTestResponse(task, response.StatusCode, err, tracked)
task.Expect = fileutil.YAMLtoJSONCompat(task.Expect)
ok = testutil.AssertEqual(
t,
task.Expect,
task.Request.Output,
fmt.Sprintf("Invalid response body in task %q of scenario %q", task.Name, ts.Name),
)
checkWatchers(t)
checkWaiters(t)
if !ok {
break
}
}
// Reverse the order in tracked array so delete of nested resources is possible
// https://github.com/golang/go/wiki/SliceTricks#reversing
for left, right := 0, len(tracked)-1; left < right; left, right = left+1, right-1 {
tracked[left], tracked[right] = tracked[right], tracked[left]
}
return tracked
}
func performCleanup(
ctx context.Context,
cleanTask CleanTask,
client *client.HTTP,
m asfservices.MetadataGetter,
) error {
switch {
case client == nil:
return fmt.Errorf("failed to delete resource, got nil http client")
case cleanTask.Path != "":
return cleanPath(ctx, cleanTask.Path, client)
case cleanTask.Kind != "" && len(cleanTask.FQName) > 0:
return cleanByFQNameAndKind(ctx, cleanTask.FQName, cleanTask.Kind, client, m)
default:
return fmt.Errorf("invalid clean task %v", cleanTask)
}
}
func getClientByID(clientID string, clients ClientsList) *client.HTTP {
if clientID == "" {
clientID = DefaultClientID
}
return clients[clientID]
}
func cleanPath(ctx context.Context, path string, client *client.HTTP) error {
response, err := client.EnsureDeleted(ctx, path, nil)
if err != nil && response.StatusCode != http.StatusNotFound {
return errors.Wrapf(err, "failed to delete resource, got status code %v", response.StatusCode)
}
return nil
}
func cleanByFQNameAndKind(
ctx context.Context,
fqName []string,
kind string,
client *client.HTTP,
m asfservices.MetadataGetter,
) error {
metadata, err := m.GetMetadata(ctx, models.Metadata{
Type: kind,
FQName: fqName,
})
if err != nil {
return errors.Wrapf(err, "failed to fetch uuid for %s with fqName %s", kind, fqName)
}
return cleanPath(ctx, "/"+kind+"/"+metadata.UUID, client)
}
func extractResourcePathFromJSON(data interface{}) (path string) {
if data, ok := data.(map[string]interface{}); ok {
var uuid interface{}
if uuid, ok = data["uuid"]; !ok {
return path
}
if suuid, ok := uuid.(string); ok {
path = "/" + suuid
}
}
return path
}
func extractSyncOperation(syncOp map[string]interface{}, client string) []trackedResource {
resources := []trackedResource{}
var operIf, kindIf interface{}
var ok bool
if kindIf, ok = syncOp["kind"]; !ok {
return nil
}
if operIf, ok = syncOp["operation"]; !ok {
return nil
}
var oper, kind string
if oper, ok = operIf.(string); !ok {
return nil
}
if oper != "CREATE" {
return nil
}
if kind, ok = kindIf.(string); !ok {
return nil
}
if dataIf, ok := syncOp["data"]; ok {
if path := extractResourcePathFromJSON(dataIf); path != "" {
return append(resources, trackedResource{Path: "/" + kind + path, Client: client})
}
}
return resources
}
func handleTestResponse(task *Task, code int, rerr error, tracked []trackedResource) []trackedResource {
if task.Request.Output != nil && task.Request.Method == "POST" && code == http.StatusOK && rerr == nil {
clientID := DefaultClientID
if task.Client != "" {
clientID = task.Client
}
tracked = trackResponse(task.Request.Output, clientID, tracked)
}
return tracked
}
func trackResponse(respDataIf interface{}, clientID string, tracked []trackedResource) []trackedResource {
switch respData := respDataIf.(type) {
case []interface{}:
logrus.Warn("After-workflow Sync request cleanup is not supported")
for _, syncOpIf := range respData {
if syncOp, ok := syncOpIf.(map[string]interface{}); ok {
tracked = append(tracked, extractSyncOperation(syncOp, clientID)...)
}
}
case map[string]interface{}:
for k, v := range respData {
if path := extractResourcePathFromJSON(v); path != "" {
tracked = append(tracked, trackedResource{Path: "/" + k + path, Client: clientID})
}
}
}
return tracked
}