Newer
Older
from common.Constants import DEFAULT_CONTEXT_UUID, DEFAULT_TOPOLOGY_UUID
from common.orm.Database import Database
from common.orm.Factory import get_database_backend, BackendEnum
from context.client.ContextClient import ContextClient
from context.proto.context_pb2 import ConfigActionEnum, Context, ContextId, Device, DeviceDriverEnum, DeviceId, DeviceOperationalStatusEnum, Empty, Link, LinkId, Service, ServiceId, ServiceStatusEnum, ServiceTypeEnum, Topology, TopologyId
from context.service.grpc_server.ContextService import ContextService
from context.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD #, RESTAPI_SERVICE_PORT, \
# RESTAPI_BASE_URL
#from context.service.rest_server.Server import Server
#from context.service.rest_server.resources.Context import Context
#from .populate_database import populate_example

Lluis Gifre Renom
committed
grpc_port = 10000 + GRPC_SERVICE_PORT # avoid privileged ports
#restapi_port = 10000 + RESTAPI_SERVICE_PORT # avoid privileged ports
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG)
## use "copy.deepcopy" to prevent propagating forced changes during tests
CONTEXT_ID = {'context_uuid': {'uuid': DEFAULT_CONTEXT_UUID}}
CONTEXT = {'context_id': copy.deepcopy(CONTEXT_ID)}
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
TOPOLOGY_ID = {
'context_id': copy.deepcopy(CONTEXT_ID),
'topology_uuid': {'uuid': DEFAULT_TOPOLOGY_UUID},
}
TOPOLOGY = {
'topology_id': copy.deepcopy(TOPOLOGY_ID),
'device_ids': [],
'link_ids': [],
}
DEVICE1_UUID = 'DEV1'
DEVICE1_ID = {'device_uuid': {'uuid': DEVICE1_UUID}}
DEVICE1 = {
'device_id': copy.deepcopy(DEVICE1_ID),
'device_type': 'packet-router',
'device_config': {'config_rules': [
{'action': ConfigActionEnum.CONFIGACTION_SET,
'resource_key': 'device/resource-1/value',
'resource_value': 'value1'},
{'action': ConfigActionEnum.CONFIGACTION_SET,
'resource_key': 'device/resource-2/value',
'resource_value': 'value2'},
{'action': ConfigActionEnum.CONFIGACTION_SET,
'resource_key': 'device/resource-3/value',
'resource_value': 'value3'},
]},
'device_operational_status': DeviceOperationalStatusEnum.DEVICEOPERATIONALSTATUS_ENABLED,
'device_drivers': [DeviceDriverEnum.DEVICEDRIVER_OPENCONFIG, DeviceDriverEnum.DEVICEDRIVER_P4],
'device_endpoints': [
{'endpoint_id': {
'topology_id': copy.deepcopy(TOPOLOGY_ID),
'device_id': copy.deepcopy(DEVICE1_ID),
'endpoint_uuid': {'uuid': 'EP1'},
}, 'endpoint_type': 'port-packet-100G'},
{'endpoint_id': {
'topology_id': copy.deepcopy(TOPOLOGY_ID),
'device_id': copy.deepcopy(DEVICE1_ID),
'endpoint_uuid': {'uuid': 'EP2'},
}, 'endpoint_type': 'port-packet-100G'},
{'endpoint_id': {
'topology_id': copy.deepcopy(TOPOLOGY_ID),
'device_id': copy.deepcopy(DEVICE1_ID),
'endpoint_uuid': {'uuid': 'EP3'},
}, 'endpoint_type': 'port-packet-100G'},
],
}
DEVICE2_UUID = 'DEV2'
DEVICE2_ID = {'device_uuid': {'uuid': DEVICE2_UUID}}
DEVICE2 = {
'device_id': copy.deepcopy(DEVICE2_ID),
'device_type': 'packet-router',
'device_config': {'config_rules': [
{'action': ConfigActionEnum.CONFIGACTION_SET,
'resource_key': 'device/resource-1/value',
'resource_value': 'value4'},
{'action': ConfigActionEnum.CONFIGACTION_SET,
'resource_key': 'device/resource-2/value',
'resource_value': 'value5'},
{'action': ConfigActionEnum.CONFIGACTION_SET,
'resource_key': 'device/resource-3/value',
'resource_value': 'value6'},
]},
'device_operational_status': DeviceOperationalStatusEnum.DEVICEOPERATIONALSTATUS_ENABLED,
'device_drivers': [DeviceDriverEnum.DEVICEDRIVER_OPENCONFIG, DeviceDriverEnum.DEVICEDRIVER_P4],
'device_endpoints': [
{'endpoint_id': {
'topology_id': copy.deepcopy(TOPOLOGY_ID),
'device_id': copy.deepcopy(DEVICE2_ID),
'endpoint_uuid': {'uuid': 'EP1'},
}, 'endpoint_type': 'port-packet-100G'},
{'endpoint_id': {
'topology_id': copy.deepcopy(TOPOLOGY_ID),
'device_id': copy.deepcopy(DEVICE2_ID),
'endpoint_uuid': {'uuid': 'EP2'},
}, 'endpoint_type': 'port-packet-100G'},
{'endpoint_id': {
'topology_id': copy.deepcopy(TOPOLOGY_ID),
'device_id': copy.deepcopy(DEVICE2_ID),
'endpoint_uuid': {'uuid': 'EP3'},
}, 'endpoint_type': 'port-packet-100G'},
],
}
LINK_UUID = 'DEV1/EP2 ==> DEV2/EP1'
LINK_ID = {'link_uuid': {'uuid': LINK_UUID}}
LINK = {
'link_id': copy.deepcopy(LINK_ID),
'link_endpoint_ids' : [
{'topology_id': copy.deepcopy(TOPOLOGY_ID),
'device_id': copy.deepcopy(DEVICE1_ID),
'endpoint_uuid': {'uuid' : 'EP2'}},
{'topology_id': copy.deepcopy(TOPOLOGY_ID),
'device_id': copy.deepcopy(DEVICE2_ID),
'endpoint_uuid': {'uuid' : 'EP1'}},
]
}
SERVICE_UUID = 'SVC:DEV1/EP2-DEV2/EP1'
SERVICE_ID = {
'context_id': copy.deepcopy(CONTEXT_ID),
'service_uuid': {'uuid': SERVICE_UUID},
}
SERVICE = {
'service_id': copy.deepcopy(SERVICE_ID),
'service_type': ServiceTypeEnum.SERVICETYPE_L3NM,
'service_endpoint_ids' : [
{'topology_id': copy.deepcopy(TOPOLOGY_ID),
'device_id': copy.deepcopy(DEVICE1_ID),
'endpoint_uuid': {'uuid' : 'EP2'}},
{'topology_id': copy.deepcopy(TOPOLOGY_ID),
'device_id': copy.deepcopy(DEVICE2_ID),
'endpoint_uuid': {'uuid' : 'EP1'}},
],
'service_constraints': [
{'constraint_type': 'latency_ms', 'constraint_value': '15.2'},
{'constraint_type': 'jitter_us', 'constraint_value': '1.2'},
],
'service_status': {'service_status': ServiceStatusEnum.SERVICESTATUS_ACTIVE},
'service_config': {'config_rules': [
{'action': ConfigActionEnum.CONFIGACTION_SET,
'resource_key': 'service/resource-1/value',
'resource_value': 'value7'},
{'action': ConfigActionEnum.CONFIGACTION_SET,
'resource_key': 'service/resource-2/value',
'resource_value': 'value8'},
{'action': ConfigActionEnum.CONFIGACTION_SET,
'resource_key': 'service/resource-3/value',
'resource_value': 'value9'},
]},
}
database_backend = get_database_backend(engine=BackendEnum.INMEMORY)
_database = Database(database_backend)
return _database
@pytest.fixture(scope='session')
def context_service(context_database : Database): # pylint: disable=redefined-outer-name

Lluis Gifre Renom
committed
context_database, port=grpc_port, max_workers=GRPC_MAX_WORKERS, grace_period=GRPC_GRACE_PERIOD)
_service.start()
yield _service
_service.stop()
@pytest.fixture(scope='session')
def context_client(context_service : ContextService): # pylint: disable=redefined-outer-name

Lluis Gifre Renom
committed
_client = ContextClient(address='127.0.0.1', port=grpc_port)
def test_context_instances(
context_client : ContextClient, context_database : Database): # pylint: disable=redefined-outer-name
response = context_client.ListContextIds(Empty())
assert len(response.context_ids) == 0
response = context_client.ListContexts(Empty())
assert len(response.contexts) == 0
response = context_client.GetContext(ContextId(**CONTEXT_ID))
assert len(response.context_id.context_uuid.uuid) == 0
assert len(response.topology_ids) == 0
assert len(response.service_ids) == 0
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry)) # pragma: no cover
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 0
response = context_client.SetContext(Context(**CONTEXT))
assert response.context_uuid.uuid == DEFAULT_CONTEXT_UUID
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry))
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 2
response = context_client.GetContext(ContextId(**CONTEXT_ID))
assert response.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert len(response.topology_ids) == 0
assert len(response.service_ids) == 0
response = context_client.ListContextIds(Empty())
assert len(response.context_ids) == 1
assert response.context_ids[0].context_uuid.uuid == DEFAULT_CONTEXT_UUID
response = context_client.ListContexts(Empty())
assert len(response.contexts) == 1
assert response.contexts[0].context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert len(response.contexts[0].topology_ids) == 0
assert len(response.contexts[0].service_ids) == 0
context_client.RemoveContext(ContextId(**CONTEXT_ID))
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
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry)) # pragma: no cover
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 0
def test_topology_instances(
context_client : ContextClient, context_database : Database): # pylint: disable=redefined-outer-name
context_database.clear_all()
response = context_client.SetContext(Context(**CONTEXT))
assert response.context_uuid.uuid == DEFAULT_CONTEXT_UUID
response = context_client.ListTopologyIds(ContextId(**CONTEXT_ID))
assert len(response.topology_ids) == 0
response = context_client.ListTopologies(ContextId(**CONTEXT_ID))
assert len(response.topologies) == 0
response = context_client.GetTopology(TopologyId(**TOPOLOGY_ID))
assert len(response.topology_id.topology_uuid.uuid) == 0
assert len(response.topology_id.context_id.context_uuid.uuid) == 0
assert len(response.device_ids) == 0
assert len(response.link_ids) == 0
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry))
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 2
response = context_client.SetTopology(Topology(**TOPOLOGY))
assert response.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert response.topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry))
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 5
response = context_client.ListTopologyIds(ContextId(**CONTEXT_ID))
assert len(response.topology_ids) == 1
assert response.topology_ids[0].context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert response.topology_ids[0].topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID
response = context_client.ListTopologies(ContextId(**CONTEXT_ID))
assert len(response.topologies) == 1
assert response.topologies[0].topology_id.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert response.topologies[0].topology_id.topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID
assert len(response.topologies[0].device_ids) == 0
assert len(response.topologies[0].link_ids) == 0
response = context_client.GetTopology(TopologyId(**TOPOLOGY_ID))
assert response.topology_id.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert response.topology_id.topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID
assert len(response.device_ids) == 0
assert len(response.link_ids) == 0
context_client.RemoveTopology(TopologyId(**TOPOLOGY_ID))
context_client.RemoveContext(ContextId(**CONTEXT_ID))
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry)) # pragma: no cover
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 0
def test_device_instances(
context_client : ContextClient, context_database : Database): # pylint: disable=redefined-outer-name
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
context_database.clear_all()
response = context_client.SetContext(Context(**CONTEXT))
assert response.context_uuid.uuid == DEFAULT_CONTEXT_UUID
response = context_client.SetTopology(Topology(**TOPOLOGY))
assert response.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert response.topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID
response = context_client.ListDeviceIds(Empty())
assert len(response.device_ids) == 0
response = context_client.ListDevices(Empty())
assert len(response.devices) == 0
response = context_client.GetDevice(DeviceId(**DEVICE1_ID))
assert len(response.device_id.device_uuid.uuid) == 0
assert len(response.device_type) == 0
assert len(response.device_config.config_rules) == 0
assert response.device_operational_status == DeviceOperationalStatusEnum.DEVICEOPERATIONALSTATUS_UNDEFINED
assert len(response.device_drivers) == 0
assert len(response.device_endpoints) == 0
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry))
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 5
response = context_client.SetDevice(Device(**DEVICE1))
assert response.device_uuid.uuid == DEVICE1_UUID
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry))
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 25
response = context_client.ListDeviceIds(Empty())
assert len(response.device_ids) == 1
assert response.device_ids[0].device_uuid.uuid == DEVICE1_UUID
response = context_client.ListDevices(Empty())
assert len(response.devices) == 1
assert response.devices[0].device_id.device_uuid.uuid == DEVICE1_UUID
assert response.devices[0].device_type == 'packet-router'
assert len(response.devices[0].device_config.config_rules) == 3
assert response.devices[0].device_operational_status == DeviceOperationalStatusEnum.DEVICEOPERATIONALSTATUS_ENABLED
assert len(response.devices[0].device_drivers) == 2
assert len(response.devices[0].device_endpoints) == 3
response = context_client.GetDevice(DeviceId(**DEVICE1_ID))
assert response.device_id.device_uuid.uuid == DEVICE1_UUID
assert response.device_type == 'packet-router'
assert len(response.device_config.config_rules) == 3
assert response.device_operational_status == DeviceOperationalStatusEnum.DEVICEOPERATIONALSTATUS_ENABLED
assert len(response.device_drivers) == 2
assert len(response.device_endpoints) == 3
TOPOLOGY_WITH_DEVICE = copy.deepcopy(TOPOLOGY)
TOPOLOGY_WITH_DEVICE['device_ids'].append(DEVICE1_ID)
response = context_client.SetTopology(Topology(**TOPOLOGY_WITH_DEVICE))
assert response.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert response.topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID
response = context_client.GetTopology(TopologyId(**TOPOLOGY_ID))
assert response.topology_id.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert response.topology_id.topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID
assert len(response.device_ids) == 1
assert response.device_ids[0].device_uuid.uuid == DEVICE1_UUID
assert len(response.link_ids) == 0
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry)) # pragma: no cover
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 25
context_client.RemoveDevice(DeviceId(**DEVICE1_ID))
context_client.RemoveTopology(TopologyId(**TOPOLOGY_ID))
context_client.RemoveContext(ContextId(**CONTEXT_ID))
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry)) # pragma: no cover
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 0
def test_link_instances(
context_client : ContextClient, context_database : Database): # pylint: disable=redefined-outer-name
context_database.clear_all()
response = context_client.SetContext(Context(**CONTEXT))
assert response.context_uuid.uuid == DEFAULT_CONTEXT_UUID
response = context_client.SetTopology(Topology(**TOPOLOGY))
assert response.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert response.topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID
response = context_client.ListLinkIds(Empty())
assert len(response.link_ids) == 0
response = context_client.ListLinks(Empty())
assert len(response.links) == 0
response = context_client.GetLink(LinkId(**LINK_ID))
assert len(response.link_id.link_uuid.uuid) == 0
assert len(response.link_endpoint_ids) == 0
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry))
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 5
response = context_client.SetLink(Link(**LINK))
assert response.link_uuid.uuid == LINK_UUID
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry))
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 24
response = context_client.ListLinkIds(Empty())
assert len(response.link_ids) == 1
assert response.link_ids[0].link_uuid.uuid == LINK_UUID
response = context_client.ListLinks(Empty())
assert len(response.links) == 1
assert response.links[0].link_id.link_uuid.uuid == LINK_UUID
assert len(response.links[0].link_endpoint_ids) == 0
response = context_client.GetLink(LinkId(**LINK_ID))
assert response.link_id.link_uuid.uuid == LINK_UUID
assert len(response.link_endpoint_ids) == 0
TOPOLOGY_WITH_LINK = copy.deepcopy(TOPOLOGY)
TOPOLOGY_WITH_LINK['link_ids'].append(LINK_ID)
response = context_client.SetTopology(Topology(**TOPOLOGY_WITH_LINK))
assert response.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert response.topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID
response = context_client.GetTopology(TopologyId(**TOPOLOGY_ID))
assert response.topology_id.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert response.topology_id.topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID
assert len(response.device_ids) == 2
assert response.device_ids[0].device_uuid.uuid == DEVICE1_UUID
assert response.device_ids[1].device_uuid.uuid == DEVICE2_UUID
assert len(response.link_ids) == 1
assert response.link_ids[0].link_uuid.uuid == LINK_UUID
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry)) # pragma: no cover
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 27
context_client.RemoveLink(LinkId(**LINK_ID))
context_client.RemoveDevice(DeviceId(**DEVICE1_ID))
context_client.RemoveDevice(DeviceId(**DEVICE2_ID))
context_client.RemoveTopology(TopologyId(**TOPOLOGY_ID))
context_client.RemoveContext(ContextId(**CONTEXT_ID))
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry)) # pragma: no cover
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 0
def t_e_s_t_service_instances(
context_client : ContextClient, context_database : Database): # pylint: disable=redefined-outer-name
context_database.clear_all()
response = context_client.SetContext(Context(**CONTEXT))
assert response.context_uuid.uuid == DEFAULT_CONTEXT_UUID
response = context_client.ListServiceIds(ContextId(**CONTEXT_ID))
assert len(response.service_ids) == 0
response = context_client.ListServices(ContextId(**CONTEXT_ID))
assert len(response.services) == 0
response = context_client.GetService(ServiceId(**SERVICE_ID))
assert len(response.service_id.service_uuid.uuid) == 0
assert len(response.service_id.context_id.context_uuid.uuid) == 0
assert response.service_type == ServiceTypeEnum.SERVICETYPE_UNKNOWN
assert len(response.service_endpoint_ids) == 0
assert len(response.service_constraints) == 0
assert response.service_status.service_status == ServiceStatusEnum.SERVICESTATUS_UNDEFINED
assert len(response.service_config.config_rules) == 0
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry))
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 2
response = context_client.SetService(Service(**SERVICE))
assert response.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert response.service_uuid.uuid == SERVICE_UUID
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry))
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 5
response = context_client.ListServiceIds(ContextId(**CONTEXT_ID))
assert len(response.service_ids) == 1
assert response.service_ids[0].context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert response.service_ids[0].service_uuid.uuid == SERVICE_UUID
response = context_client.ListServices(ContextId(**CONTEXT_ID))
assert len(response.services) == 1
assert response.services[0].service_id.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert response.services[0].service_id.service_uuid.uuid == SERVICE_UUID
assert response.services[0].service_type == ServiceTypeEnum.SERVICETYPE_L3NM
assert len(response.services[0].service_endpoint_ids) == 0
assert len(response.services[0].service_constraints) == 0
assert response.services[0].service_status.service_status == ServiceStatusEnum.SERVICESTATUS_ACTIVE
assert len(response.services[0].service_config.config_rules) == 0
response = context_client.GetService(ServiceId(**SERVICE_ID))
assert len(response.service_id.service_uuid.uuid) == 0
assert len(response.service_id.context_id.context_uuid.uuid) == 0
assert len(response.service_type) == 0
assert len(response.service_endpoint_ids) == 0
assert len(response.service_constraints) == 0
assert response.service_status.service_status == ServiceStatusEnum.SERVICESTATUS_ACTIVE
assert len(response.service_config.config_rules) == 0
context_client.RemoveService(ServiceId(**SERVICE_ID))
context_client.RemoveContext(ContextId(**CONTEXT_ID))
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry)) # pragma: no cover
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 0
#@pytest.fixture(scope='session')
#def context_service_rest(context_database : Database):
# _rest_server = Server(port=restapi_port, base_url=RESTAPI_BASE_URL)
# _rest_server.add_resource(Context, '/context', endpoint='api.context', resource_class_args=(context_database,))
# _rest_server.start()
# time.sleep(1) # bring time for the server to start
# yield _rest_server
# _rest_server.shutdown()
# _rest_server.join()
#
#def test_get_topology_completed_rest_api(context_service_rest : Server):
# # should work
# request_url = 'http://127.0.0.1:{}{}/context'.format(restapi_port, RESTAPI_BASE_URL)
# LOGGER.warning('Request: GET {}'.format(str(request_url)))
# reply = requests.get(request_url)
# LOGGER.warning('Reply: {}'.format(str(reply.text)))
# assert reply.status_code == 200, 'Reply failed with code {}'.format(reply.status_code)
# json_reply = reply.json()
# topology = MessageToDict(
# Topology(**json_reply['topologies'][0]),
# including_default_value_fields=True, preserving_proto_field_name=True,
# use_integers_for_enums=False)
# validate_topology(topology)
# validate_topology_has_devices(topology)
# validate_topology_has_links(topology)
#