Netbox Service Tests
Overview
The Netbox Service Tests (test_netbox_service.py) provide comprehensive testing coverage for NORFAB's Netbox integration service. These tests validate the Netbox worker's ability to interact with Netbox instances.
Test Summary
The test suite is organized into 17 test classes:
| Test Class |
Purpose |
Documentation |
| TestNetboxWorker |
Core service discovery and version checks |
Details |
| TestNetboxGrapQL |
GraphQL query operations |
Details |
| TestGetInterfaces |
Interface retrieval and filtering |
Details |
| TestGetDevices |
Device retrieval and filtering |
Details |
| TestGetConnections |
Device connection queries |
Details |
| TestGetNornirInventory |
Nornir inventory generation from Netbox |
Details |
| TestGetCircuits |
Circuit information retrieval |
Details |
| TestGetBgpPeerings |
BGP peering data |
Details |
| TestSyncDeviceFacts |
Device fact synchronization |
Details |
| TestSyncDeviceInterfaces |
Device interface synchronization |
Details |
| TestCreateDeviceInterfaces |
Device interface creation |
Details |
| TestSyncDeviceIP |
IP address synchronization |
Details |
| TestCreateIP |
IP address creation |
Details |
| TestNetboxCache |
Caching functionality |
Details |
| TestGetContainerlabInventory |
Containerlab inventory generation |
Details |
| TestCreatePrefix |
IP prefix creation |
Details |
| TestCreateIPBulk |
Bulk IP address creation |
Details |
Test Classes
TestNetboxWorker
Core Netbox service functionality and health checks.
Purpose: Validate basic service operations, version compatibility, and status monitoring.
Test Get Netbox Inventory
Source code in tests\test_netbox_service.py
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271 | def test_get_netbox_inventory(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_inventory",
workers="any",
)
pprint.pprint(ret)
for worker, res in ret.items():
assert not res["errors"], f"{worker} - received error"
assert all(
k in res["result"] for k in ["service", "instances"]
), f"{worker} - not all netbox inventory data returned"
assert all(
k in res["result"]["instances"] for k in ["dev", "preprod", "prod"]
), f"{worker} - not all netbox instances inventory data returned"
|
Test Get Netbox Version
Source code in tests\test_netbox_service.py
273
274
275
276
277
278
279
280
281
282
283
284
285
286 | def test_get_netbox_version(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_version",
workers="any",
)
pprint.pprint(ret)
for worker, res in ret.items():
assert not res["errors"], f"{worker} - received error"
assert all(
k in res["result"]
for k in ["platform", "pynetbox", "python", "requests"]
), f"{worker} - not all netbox version data returned"
|
Test Get Netbox Status
Source code in tests\test_netbox_service.py
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 | def test_get_netbox_status(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_netbox_status",
workers="any",
)
pprint.pprint(ret)
for worker, res in ret.items():
assert not res["errors"], f"{worker} - received error"
assert all(
k in res["result"] for k in ["dev", "preprod", "prod"]
), f"{worker} - not all netbox instances inventory data returned"
for instance, status_data in res["result"].items():
assert all(
k in status_data
for k in [
"django-version",
"error",
"netbox-version",
"plugins",
"python-version",
"rq-workers-running",
"status",
]
), f"{worker}:{instance} - not all netbox instances status data returned"
|
Test Get Netbox Compatibility
Source code in tests\test_netbox_service.py
315
316
317
318
319
320
321
322
323
324
325
326
327
328 | def test_get_netbox_compatibility(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_compatibility",
workers="any",
)
pprint.pprint(ret)
for worker, res in ret.items():
assert not res["errors"], f"{worker} - received error"
assert all(
k in res["result"] for k in ["dev", "preprod", "prod"]
), f"{worker} - not all netbox instances inventory data returned"
for instance, compatible in res["result"].items():
assert compatible == True, f"{worker}:{instance} - not compatible"
|
TestNetboxGrapQL
GraphQL query operations against Netbox instances.
Purpose: Validate GraphQL query execution, dry-run mode, and error handling.
Test Graphql Query String
Source code in tests\test_netbox_service.py
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351 | def test_graphql_query_string(self, nfclient):
ret = nfclient.run_job(
"netbox",
"graphql",
workers="any",
kwargs={"query_string": "query DeviceListQuery { device_list { name } }"},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert not res["errors"], f"{worker} - received error"
assert "device_list" in res["result"], f"{worker} no device list returned"
assert isinstance(
res["result"]["device_list"], list
), f"{worker} unexpected device list payload type, was expecting list"
assert (
len(res["result"]["device_list"]) > 0
), f"{worker} returned no devices in device list"
|
Test Graphql Query String With Instance
Source code in tests\test_netbox_service.py
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373 | def test_graphql_query_string_with_instance(self, nfclient):
ret = nfclient.run_job(
"netbox",
"graphql",
workers="any",
kwargs={
"query_string": "query DeviceListQuery { device_list { name } }",
"instance": "prod",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert not res["errors"], f"{worker} - received error"
assert "device_list" in res["result"], f"{worker} no device list returned"
assert isinstance(
res["result"]["device_list"], list
), f"{worker} unexpected device list payload type, was expecting list"
assert (
len(res["result"]["device_list"]) > 0
), f"{worker} returned no devices in device list"
|
Test Graphql Query String Dry Run
Source code in tests\test_netbox_service.py
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391 | def test_graphql_query_string_dry_run(self, nfclient):
ret = nfclient.run_job(
"netbox",
"graphql",
workers="any",
kwargs={
"query_string": "query DeviceListQuery { device_list { name } }",
"dry_run": True,
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert not res["errors"], f"{worker} - received error"
assert all(
k in res["result"] for k in ["headers", "data", "verify", "url"]
), f"{worker} - not all dry run data returned"
|
Test Graphql Query String Error
Source code in tests\test_netbox_service.py
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407 | def test_graphql_query_string_error(self, nfclient):
ret = nfclient.run_job(
"netbox",
"graphql",
workers="any",
kwargs={
"query_string": "query DeviceListQuery { device_list { name } ",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert res[
"errors"
], f"{worker} did not return errors for malformed graphql query"
|
Source code in tests\test_netbox_service.py
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 | def test_form_graphql_query_dry_run(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
if self.nb_version[0] == 4:
ret = nfclient.run_job(
"netbox",
"graphql",
workers="any",
kwargs={
"obj": "device_list",
"fields": ["name", "platform {name}"],
"filters": {"q": "ceos", "platform": "arista_eos"},
"dry_run": True,
},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert res["result"]["data"] == (
'{"query": "query {device_list(filters: {q: \\"ceos\\", platform: \\"arista_eos\\"}) {name platform {name}}}"}'
), f"{worker} did not return correct query string"
elif self.nb_version[0] == 3:
ret = nfclient.run_job(
"netbox",
"graphql",
workers="any",
kwargs={
"obj": "device_list",
"fields": ["name", "platform {name}"],
"filters": {"name__ic": "ceos", "platform": "arista_eos"},
"dry_run": True,
},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert res["result"]["data"] == (
'{"query": "query {device_list(name__ic: \\"ceos\\", platform: \\"arista_eos\\") {name platform {name}}}"}'
), f"{worker} did not return correct query string"
|
TestGetInterfaces
Interface retrieval and filtering operations.
Purpose: Validate device interface queries with various filters and parameters.
Test Get Interfaces
Source code in tests\test_netbox_service.py
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837 | def test_get_interfaces(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
clear_nb_cache("get_interfaces*", nfclient)
ret = nfclient.run_job(
"netbox",
"get_interfaces",
workers="any",
kwargs={"devices": ["ceos1", "fceos4"]},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert "ceos1" in res["result"], f"{worker} returned no results for ceos1"
assert "fceos4" in res["result"], f"{worker} returned no results for fceos4"
for device, interfaces in res["result"].items():
assert isinstance(
interfaces, dict
), f"{worker}:{device} did not return interfaces dictionary"
for intf_name, intf_data in interfaces.items():
assert all(
k in intf_data
for k in [
"enabled",
"description",
"mtu",
"parent",
"mode",
"untagged_vlan",
"vrf",
"tagged_vlans",
"tags",
"custom_fields",
"last_updated",
"bridge",
"child_interfaces",
"bridge_interfaces",
"member_interfaces",
"wwn",
"duplex",
"speed",
]
), f"{worker}:{device}:{intf_name} not all data returned"
if self.nb_version >= (4, 2, 0):
assert "mac_addresses" in intf_data
else:
assert "mac_address" in intf_data
|
Test Get Interfaces With Instance
Source code in tests\test_netbox_service.py
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885 | def test_get_interfaces_with_instance(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
clear_nb_cache("get_interfaces*", nfclient)
ret = nfclient.run_job(
"netbox",
"get_interfaces",
workers="any",
kwargs={"devices": ["ceos1", "fceos4"], "instance": "prod"},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert "ceos1" in res["result"], f"{worker} returned no results for ceos1"
assert "fceos4" in res["result"], f"{worker} returned no results for fceos4"
for device, interfaces in res["result"].items():
assert isinstance(
interfaces, dict
), f"{worker}:{device} did not return interfaces dictionary"
for intf_name, intf_data in interfaces.items():
assert all(
k in intf_data
for k in [
"enabled",
"description",
"mtu",
"parent",
"mode",
"untagged_vlan",
"vrf",
"tagged_vlans",
"tags",
"custom_fields",
"last_updated",
"bridge",
"child_interfaces",
"bridge_interfaces",
"member_interfaces",
"wwn",
"duplex",
"speed",
]
), f"{worker}:{device}:{intf_name} not all data returned"
if self.nb_version >= (4, 2, 0):
assert "mac_addresses" in intf_data
else:
assert "mac_address" in intf_data
|
Test Get Interfaces Dry Run
Source code in tests\test_netbox_service.py
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902 | def test_get_interfaces_dry_run(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
ret = nfclient.run_job(
"netbox",
"get_interfaces",
workers="any",
kwargs={"devices": ["ceos1", "fceos4"], "dry_run": True},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["result"] == {
"filter_params": {"device": ["ceos1", "fceos4"]}
}, f"{worker} did not return correct query string"
|
Test Get Interfaces Add Ip
Source code in tests\test_netbox_service.py
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932 | def test_get_interfaces_add_ip(self, nfclient):
clear_nb_cache("get_interfaces*", nfclient)
ret = nfclient.run_job(
"netbox",
"get_interfaces",
workers="any",
kwargs={"devices": ["ceos1", "fceos4"], "ip_addresses": True},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert "ceos1" in res["result"], f"{worker} returned no results for ceos1"
assert "fceos4" in res["result"], f"{worker} returned no results for fceos4"
for device, interfaces in res["result"].items():
assert isinstance(
interfaces, dict
), f"{worker}:{device} did not return interfaces dictionary"
for intf_name, intf_data in interfaces.items():
assert (
"ip_addresses" in intf_data
), f"{worker}:{device}:{intf_name} no IP addresses data returned"
for ip in intf_data["ip_addresses"]:
assert all(
k in ip
for k in [
"address",
"family",
]
), f"{worker}:{device}:{intf_name} not all IP data returned"
|
Test Get Interfaces Add Inventory Items
Source code in tests\test_netbox_service.py
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968 | def test_get_interfaces_add_inventory_items(self, nfclient):
clear_nb_cache("get_interfaces*", nfclient)
ret = nfclient.run_job(
"netbox",
"get_interfaces",
workers="any",
kwargs={
"devices": ["ceos1", "fceos4"],
"inventory_items": True,
},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert "ceos1" in res["result"], f"{worker} returned no results for ceos1"
assert "fceos4" in res["result"], f"{worker} returned no results for fceos4"
for device, interfaces in res["result"].items():
assert isinstance(
interfaces, dict
), f"{worker}:{device} did not return interfaces dictionary"
for intf_name, intf_data in interfaces.items():
assert (
"inventory_items" in intf_data
), f"{worker}:{device}:{intf_name} no inventory items data returned"
for item in intf_data["inventory_items"]:
assert all(
k in item
for k in [
"name",
"role",
"manufacturer",
"custom_fields",
"serial",
]
), f"{worker}:{device}:{intf_name} not all inventory item data returned"
|
Test Get Interfaces With Interface Regex
Source code in tests\test_netbox_service.py
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993 | def test_get_interfaces_with_interface_regex(self, nfclient):
clear_nb_cache("get_interfaces*", nfclient)
ret = nfclient.run_job(
"netbox",
"get_interfaces",
workers="any",
kwargs={
"devices": ["ceos1", "fceos4"],
"interface_regex": "loop.+",
},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert "ceos1" in res["result"], f"{worker} returned no results for ceos1"
assert "fceos4" in res["result"], f"{worker} returned no results for fceos4"
for device, interfaces in res["result"].items():
assert isinstance(
interfaces, dict
), f"{worker}:{device} did not return interfaces dictionary"
for intf_name, intf_data in interfaces.items():
assert (
"loopback" in intf_name.lower()
), f"{worker}:{device}:{intf_name} interface name does not match regex pattern"
|
TestGetDevices
Device retrieval and advanced filtering.
Purpose: Validate comprehensive device queries with filtering, selection, and data structure validation.
Test With Devices List
Source code in tests\test_netbox_service.py
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113 | def test_with_devices_list(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_devices",
workers="any",
kwargs={"devices": ["ceos1", "fceos4"]},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert "ceos1" in res["result"], f"{worker} returned no results for ceos1"
assert "fceos4" in res["result"], f"{worker} returned no results for fceos4"
for device, device_data in res["result"].items():
print(list(device_data.keys()))
assert isinstance(
device_data, dict
), f"{worker}:{device} did not return device data as dictionary"
assert all(
k in device_data for k in self.device_data_keys
), f"{worker}:{device} not all data returned"
assert (
"role" in device_data
), f"{worker}:{device} nodevice role info returned"
|
Test With Filters
Source code in tests\test_netbox_service.py
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145 | def test_with_filters(self, nfclient):
# REST API filter syntax: plain dicts with standard query params
ret = nfclient.run_job(
"netbox",
"get_devices",
workers="any",
kwargs={
"filters": [
{"name": ["ceos1", "fceos4"]},
{"name__ic": "390"},
]
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert "ceos1" in res["result"], f"{worker} returned no results for ceos1"
assert (
"fceos3_390" in res["result"]
), f"{worker} returned no results for fceos3_390"
assert "fceos4" in res["result"], f"{worker} returned no results for fceos4"
for device, device_data in res["result"].items():
assert isinstance(
device_data, dict
), f"{worker}:{device} did not return device data as dictionary"
assert all(
k in device_data for k in self.device_data_keys
), f"{worker}:{device} not all data returned"
assert (
"role" in device_data or "devcie_role" in device_data
), f"{worker}:{device} nodevice role info returned"
|
Test With Filters Dry Run
Source code in tests\test_netbox_service.py
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173 | def test_with_filters_dry_run(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_devices",
workers="any",
kwargs={
"filters": [
{"name": ["ceos1", "fceos4"]},
{"name__ic": "390"},
],
"dry_run": True,
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert not res["errors"], f"{worker} - received error"
assert (
"get_devices_dry_run" in res["result"]
), f"{worker} - dry run key missing from result"
dry_run_data = res["result"]["get_devices_dry_run"]
assert (
"filters" in dry_run_data
), f"{worker} - 'filters' key missing from dry run result"
assert isinstance(
dry_run_data["filters"], list
), f"{worker} - dry run filters should be a list"
|
Test Get Devices Cache
Source code in tests\test_netbox_service.py
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226 | @pytest.mark.parametrize("cache", cache_options)
def test_get_devices_cache(self, nfclient, cache):
# REST API filter syntax works across all Netbox versions
ret = nfclient.run_job(
"netbox",
"get_devices",
workers="any",
kwargs={
"devices": ["ceos1", "fceos4"],
"filters": [
{"name": ["ceos1", "fceos4"]},
{"name__ic": "390"},
],
"cache": cache,
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert "ceos1" in res["result"], f"{worker} returned no results for ceos1"
assert "fceos4" in res["result"], f"{worker} returned no results for fceos4"
for device, device_data in res["result"].items():
assert isinstance(
device_data, dict
), f"{worker}:{device} did not return device data as dictionary"
assert all(
k in device_data for k in self.device_data_keys
), f"{worker}:{device} not all data returned"
assert (
"role" in device_data or "devcie_role" in device_data
), f"{worker}:{device} nodevice role info returned"
|
TestGetConnections
Device connection and cable management queries.
Purpose: Validate device connection retrieval and relationship mapping.
Test Get Connections
Source code in tests\test_netbox_service.py
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544 | def test_get_connections(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_connections",
workers="any",
kwargs={
"devices": ["fceos4", "fceos5"],
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert "fceos5" in res["result"], f"{worker} returned no results for fceos5"
assert "fceos4" in res["result"], f"{worker} returned no results for fceos4"
assert (
"ConsolePort1" in res["result"]["fceos4"]
), f"{worker}:fceos4 no console ports data returned"
assert (
"ConsoleServerPort1" in res["result"]["fceos5"]
), f"{worker}:fceos5 no console server ports data returned"
assert (
"eth11.123" in res["result"]["fceos5"]
), f"{worker}:fceos5 no virtual ports data returned"
assert (
"Port-Channel1" in res["result"]["fceos4"]
), f"{worker}:fceos5 no lag ports data returned"
assert (
"Port-Channel1.101" in res["result"]["fceos4"]
), f"{worker}:fceos5 no lag virtual ports data returned"
for device, interfaces in res["result"].items():
assert isinstance(
interfaces, dict
), f"{worker}:{device} did not return interfaces dictionary"
for intf_name, intf_data in interfaces.items():
assert all(
k in intf_data
for k in [
"remote_device",
"remote_interface",
"remote_termination_type",
"termination_type",
]
), f"{worker}:{device}:{intf_name} not all data returned"
# verify provider network connection handling
if device == "fceos4" and intf_name == "eth201":
assert (
"provider" in intf_data
), f"{worker}:{device}:{intf_name} no provider data"
assert intf_data["remote_termination_type"] == "providernetwork"
assert intf_data["remote_device"] == None
assert intf_data["remote_interface"] == None
assert intf_data["remote_interface_label"] == None
# verify breakout handling
if device == "fceos5" and intf_name == "eth1":
assert (
intf_data["breakout"] == True
), f"{worker}:{device}:{intf_name} was expecting breakout connection"
assert isinstance(intf_data["remote_interface"], list)
assert len(intf_data["remote_interface"]) > 1
# verify virtual ports handling
if device == "fceos5" and intf_name == "eth11.123":
assert intf_data["remote_device"] == "fceos4"
assert intf_data["remote_device_status"] == "active"
assert intf_data["remote_interface"] == "eth11.123"
assert intf_data["termination_type"] == "virtual"
# verify lag ports handling
if device == "fceos4" and intf_name == "Port-Channel1":
assert intf_data["remote_device"] == "fceos5"
assert intf_data["remote_interface"] == "ae5"
assert intf_data["termination_type"] == "lag"
assert "remote_interface_label" not in intf_data
# verify lag virtual interfaces handling
if device == "fceos4" and intf_name == "Port-Channel1.101":
assert intf_data["remote_device"] == "fceos5"
assert intf_data["remote_interface"] == "ae5.101"
assert intf_data["termination_type"] == "virtual"
assert "remote_interface_label" not in intf_data
if device == "fceos5" and intf_name == "ae6.0":
assert intf_data["remote_device"] == "fceos4"
assert intf_data["remote_interface"] == "Port-Channel2"
assert intf_data["termination_type"] == "virtual"
assert intf_data["remote_termination_type"] == "lag"
if device == "fceos4" and intf_name == "eth103.0":
assert intf_data["remote_device"] == "fceos5"
assert intf_data["remote_interface"] == "eth103"
assert intf_data["termination_type"] == "virtual"
assert intf_data["remote_termination_type"] == "interface"
assert intf_data["remote_interface_label"] == ""
if device == "fceos4" and intf_name == "ConsolePort2":
assert intf_data["remote_device"] == "fceos5"
assert intf_data["remote_interface"] == "ConsoleServerPort2"
assert intf_data["termination_type"] == "consoleport"
assert (
intf_data["remote_termination_type"] == "consoleserverport"
)
assert intf_data["remote_interface_label"] == ""
|
Test Get Connections Physical Interface Regex
Source code in tests\test_netbox_service.py
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566 | def test_get_connections_physical_interface_regex(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_connections",
workers="any",
kwargs={
"devices": ["fceos4", "fceos5"],
"interface_regex": "eth10.*",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert "fceos5" in res["result"], f"{worker} returned no results for fceos5"
assert "fceos4" in res["result"], f"{worker} returned no results for fceos4"
for device, interfaces in res["result"].items():
assert interfaces, f"{worker}:{device} no connections data returned"
for intf_name, intf_data in interfaces.items():
assert intf_name.startswith(
"eth10"
), f"{worker}:{device}:{intf_name} not matching regex"
|
Test Get Connections Virtual Interface Regex
Source code in tests\test_netbox_service.py
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590 | def test_get_connections_virtual_interface_regex(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_connections",
workers="any",
kwargs={
"devices": ["fceos4", "fceos5"],
"interface_regex": "(Port-Channel1|ae5).101", # match Port-Channel1.101 and ae5.101
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert "fceos5" in res["result"], f"{worker} returned no results for fceos5"
assert "fceos4" in res["result"], f"{worker} returned no results for fceos4"
assert (
res["result"]["fceos4"]["Port-Channel1.101"]["remote_interface"]
== "ae5.101"
), "Unexpected interface name"
assert (
res["result"]["fceos5"]["ae5.101"]["remote_interface"]
== "Port-Channel1.101"
), "Unexpected interface name"
|
Test Get Connections Dry Run
Source code in tests\test_netbox_service.py
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606 | def test_get_connections_dry_run(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_connections",
workers="any",
kwargs={"devices": ["fceos4", "fceos5"], "dry_run": True},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert not res["errors"], f"{worker} - received error"
dry_run_payload = res["result"]
assert all(
k in dry_run_payload for k in ["headers", "data", "verify", "url"]
), f"{worker} - not all dry run data returned"
|
Test Get Connections And Cables
Source code in tests\test_netbox_service.py
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643 | def test_get_connections_and_cables(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_connections",
workers="any",
kwargs={"devices": ["fceos4", "fceos5"]},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert "fceos5" in res["result"], f"{worker} returned no results for fceos5"
assert "fceos4" in res["result"], f"{worker} returned no results for fceos4"
for device, interfaces in res["result"].items():
assert isinstance(
interfaces, dict
), f"{worker}:{device} did not return interfaces dictionary"
for intf_name, intf_data in interfaces.items():
if intf_data["termination_type"] in ["virtual", "lag"]:
continue
assert (
"cable" in intf_data
), f"{worker}:{device}:{intf_name} no cable data returned"
assert all(
k in intf_data["cable"]
for k in [
"custom_fields",
"label",
"peer_device",
"peer_interface",
"peer_termination_type",
"status",
"tags",
"tenant",
"type",
]
), f"{worker}:{device}:{intf_name} not all cable data returned"
|
TestGetNornirInventory
Nornir inventory generation from Netbox data.
Purpose: Validate conversion of Netbox data into Nornir-compatible inventory format.
Test With Devices
Source code in tests\test_netbox_service.py
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118 | def test_with_devices(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_nornir_inventory",
workers="any",
kwargs={"devices": ["ceos1", "fceos4", "nonexist"]},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert (
"ceos1" in res["result"]["hosts"]
), f"{worker} returned no results for ceos1"
assert (
"fceos4" in res["result"]["hosts"]
), f"{worker} returned no results for fceos4"
for device, data in res["result"]["hosts"].items():
assert all(
k in data for k in ["data", "hostname", "platform"]
), f"{worker}:{device} not all data returned"
|
Test With Filters
Source code in tests\test_netbox_service.py
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144 | def test_with_filters(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_nornir_inventory",
workers="any",
kwargs={
"filters": [
{"name": ["ceos1"]},
{"name__ic": "fceos"},
]
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert (
"ceos1" in res["result"]["hosts"]
), f"{worker} returned no results for ceos1"
assert (
"fceos4" in res["result"]["hosts"]
), f"{worker} returned no results for fceos4"
for device, data in res["result"]["hosts"].items():
assert all(
k in data for k in ["data", "hostname", "platform"]
), f"{worker}:{device} not all data returned"
|
Test Source Platform From Config Context
Source code in tests\test_netbox_service.py
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162 | def test_source_platform_from_config_context(self, nfclient):
# for iosxr1 platform data encoded in config context
ret = nfclient.run_job(
"netbox",
"get_nornir_inventory",
workers="any",
kwargs={"devices": ["iosxr1"]},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert (
"iosxr1" in res["result"]["hosts"]
), f"{worker} returned no results for iosxr1"
for device, data in res["result"]["hosts"].items():
assert all(
k in data for k in ["data", "hostname", "platform"]
), f"{worker}:{device} not all data returned"
|
Test With Devices Nbdata Is True
Source code in tests\test_netbox_service.py
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186 | def test_with_devices_nbdata_is_true(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_nornir_inventory",
workers="any",
kwargs={"devices": ["ceos1", "fceos4"], "nbdata": True},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert (
"ceos1" in res["result"]["hosts"]
), f"{worker} returned no results for ceos1"
assert (
"fceos4" in res["result"]["hosts"]
), f"{worker} returned no results for fceos4"
for device, data in res["result"]["hosts"].items():
assert all(
k in data for k in ["data", "hostname", "platform"]
), f"{worker}:{device} not all device data returned"
assert all(
k in data["data"] for k in self.device_data_keys
), f"{worker}:{device} not all nbdata returned"
|
Test With Devices Add Interfaces
Source code in tests\test_netbox_service.py
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216 | def test_with_devices_add_interfaces(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_nornir_inventory",
workers="any",
kwargs={"devices": ["ceos1", "fceos4"], "interfaces": True},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert (
"ceos1" in res["result"]["hosts"]
), f"{worker} returned no results for ceos1"
assert (
"fceos4" in res["result"]["hosts"]
), f"{worker} returned no results for fceos4"
for device, data in res["result"]["hosts"].items():
assert data["data"][
"interfaces"
], f"{worker}:{device} no interfaces data returned"
for intf_name, intf_data in data["data"]["interfaces"].items():
assert all(
k in intf_data
for k in [
"vrf",
"mode",
"description",
]
), f"{worker}:{device}:{intf_name} not all interface data returned"
|
Test With Devices Add Interfaces With Ip And Inventory
Source code in tests\test_netbox_service.py
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246 | def test_with_devices_add_interfaces_with_ip_and_inventory(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_nornir_inventory",
workers="any",
kwargs={
"devices": ["ceos1", "fceos4"],
"interfaces": {"ip_addresses": True, "inventory_items": True},
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert (
"ceos1" in res["result"]["hosts"]
), f"{worker} returned no results for ceos1"
assert (
"fceos4" in res["result"]["hosts"]
), f"{worker} returned no results for fceos4"
for device, data in res["result"]["hosts"].items():
assert data["data"][
"interfaces"
], f"{worker}:{device} no interfaces data returned"
for intf_name, intf_data in data["data"]["interfaces"].items():
assert (
"ip_addresses" in intf_data
), f"{worker}:{device}:{intf_name} no ip addresses data returned"
assert (
"inventory_items" in intf_data
), f"{worker}:{device}:{intf_name} no invetnory data returned"
|
Test With Devices Add Connections
Source code in tests\test_netbox_service.py
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275 | def test_with_devices_add_connections(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_nornir_inventory",
workers="any",
kwargs={"devices": ["fceos4", "fceos5"], "connections": True},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert (
"fceos5" in res["result"]["hosts"]
), f"{worker} returned no results for fceos5"
assert (
"fceos4" in res["result"]["hosts"]
), f"{worker} returned no results for fceos4"
for device, data in res["result"]["hosts"].items():
assert data["data"][
"connections"
], f"{worker}:{device} no connections data returned"
for intf_name, intf_data in data["data"]["connections"].items():
assert all(
k in intf_data
for k in [
"remote_interface",
"remote_device",
]
), f"{worker}:{device}:{intf_name} not all connection data returned"
|
Test With Devices Add Bgp Peerings
Source code in tests\test_netbox_service.py
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304 | def test_with_devices_add_bgp_peerings(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_nornir_inventory",
workers="any",
kwargs={"devices": ["fceos4", "fceos5"], "bgp_peerings": True},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert (
"fceos5" in res["result"]["hosts"]
), f"{worker} returned no results for fceos5"
assert (
"fceos4" in res["result"]["hosts"]
), f"{worker} returned no results for fceos4"
for device, data in res["result"]["hosts"].items():
assert data["data"][
"bgp_peerings"
], f"{worker}:{device} no bgp_peerings data returned"
for peering, peering_data in data["data"]["bgp_peerings"].items():
assert all(
k in peering_data
for k in [
"id",
"name",
]
), f"{worker}:{device}:{peering} not all peerings data returned"
|
TestGetCircuits
Circuit information retrieval from Netbox.
Purpose: Validate circuit data queries and filtering.
Test Get Circuits Dry Run
Source code in tests\test_netbox_service.py
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333 | def test_get_circuits_dry_run(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
if self.nb_version[0] == 3:
ret = nfclient.run_job(
"netbox",
"get_circuits",
workers="any",
kwargs={
"devices": ["fceos4", "fceos5"],
"dry_run": True,
},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert res["result"]["data"] == (
'{"query": "query {circuit_list(site: '
+ '[\\"saltnornir-lab\\"]) {cid tags {name} '
+ "provider {name} commit_rate description status "
+ "type {name} provider_account {name} tenant "
+ "{name} termination_a {id} termination_z {id} "
+ 'custom_fields comments}}"}'
), f"{worker} did not return correct query string"
|
Test Get Circuits
Source code in tests\test_netbox_service.py
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386 | def test_get_circuits(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_circuits",
workers="any",
kwargs={
"devices": ["fceos4", "fceos5"],
},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert "fceos5" in res["result"], f"{worker} returned no results for fceos5"
assert "fceos4" in res["result"], f"{worker} returned no results for fceos4"
for device, device_data in res["result"].items():
assert device_data, f"{worker}:{device} no circuit data returned"
for cid, cid_data in device_data.items():
if cid == "CID3":
assert all(
k in cid_data
for k in [
"tags",
"provider",
"commit_rate",
"description",
"status",
"type",
"provider_account",
"tenant",
"custom_fields",
"comments",
"provider_account",
"provider_network",
]
), f"{worker}:{device}:{cid} not all circuit data returned"
else:
assert all(
k in cid_data
for k in [
"tags",
"provider",
"commit_rate",
"description",
"status",
"type",
"provider_account",
"tenant",
"custom_fields",
"comments",
"remote_device",
"remote_interface",
]
), f"{worker}:{device}:{cid} not all circuit data returned"
|
Test Get Circuits By Cid
Source code in tests\test_netbox_service.py
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425 | def test_get_circuits_by_cid(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_circuits",
workers="any",
kwargs={"devices": ["fceos4", "fceos5"], "cid": ["CID1"]},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert "fceos5" in res["result"], f"{worker} returned no results for fceos5"
assert "fceos4" in res["result"], f"{worker} returned no results for fceos4"
for device, device_data in res["result"].items():
assert device_data, f"{worker}:{device} no circuit data returned"
for cid, cid_data in device_data.items():
assert (
cid == "CID1"
), f"{worker}:{device}:{cid} wrong circuit returned, was expecting 'CID1' only"
|
Test Get Circuits Cache
Source code in tests\test_netbox_service.py
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478 | @pytest.mark.parametrize("cache", cache_options)
def test_get_circuits_cache(self, nfclient, cache):
print(f"cache: {cache}")
ret = nfclient.run_job(
"netbox",
"get_circuits",
workers="any",
kwargs={"devices": ["fceos4", "fceos5"], "cache": cache},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert "fceos5" in res["result"], f"{worker} returned no results for fceos5"
assert "fceos4" in res["result"], f"{worker} returned no results for fceos4"
for device, device_data in res["result"].items():
assert device_data, f"{worker}:{device} no circuit data returned"
for cid, cid_data in device_data.items():
if cid == "CID3":
assert all(
k in cid_data
for k in [
"tags",
"provider",
"commit_rate",
"description",
"status",
"type",
"provider_account",
"tenant",
"custom_fields",
"comments",
"provider_account",
"provider_network",
]
), f"{worker}:{device}:{cid} not all circuit data returned"
else:
assert all(
k in cid_data
for k in [
"tags",
"provider",
"commit_rate",
"description",
"status",
"type",
"provider_account",
"tenant",
"custom_fields",
"comments",
"remote_device",
"remote_interface",
]
), f"{worker}:{device}:{cid} not all circuit data returned"
|
Test Get Circuits Cache Content
Source code in tests\test_netbox_service.py
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513 | def test_get_circuits_cache_content(self, nfclient):
circuits_cache = nfclient.run_job(
"netbox",
"cache_get",
workers="all",
kwargs={"keys": "get_circuits*"},
)
pprint.pprint(circuits_cache)
for worker, res in circuits_cache.items():
if "get_circuits::CID1" in res["result"]:
assert (
res["result"]["get_circuits::CID1"]["fceos4"]["remote_device"]
== "fceos5"
)
assert (
res["result"]["get_circuits::CID1"]["fceos5"]["remote_device"]
== "fceos4"
)
if "get_circuits::CID2" in res["result"]:
assert (
res["result"]["get_circuits::CID2"]["fceos4"]["remote_device"]
== "fceos5"
)
assert (
res["result"]["get_circuits::CID2"]["fceos5"]["remote_device"]
== "fceos4"
)
if "get_circuits::CID3" in res["result"]:
assert (
res["result"]["get_circuits::CID3"]["fceos4"]["provider_network"]
== "Provider1-Net1"
)
|
TestGetBgpPeerings
BGP peering information retrieval.
Purpose: Validate BGP peering data extraction and filtering.
Test Get Bgp Peerings
Test basic BGP peerings retrieval
Source code in tests\test_netbox_service.py
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570 | def test_get_bgp_peerings(self, nfclient):
"""Test basic BGP peerings retrieval"""
ret = nfclient.run_job(
"netbox",
"get_bgp_peerings",
workers="any",
kwargs={"devices": ["fceos4", "fceos5"]},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert res["result"], f"{worker} returned no results"
assert "fceos4" in res["result"], f"{worker} missing fceos4 in results"
assert "fceos5" in res["result"], f"{worker} missing fceos5 in results"
# Check that each device has a dictionary (may be empty if no BGP sessions)
for device, bgp_sessions in res["result"].items():
assert isinstance(
bgp_sessions, dict
), f"{worker}:{device} BGP sessions should be a dictionary"
# If there are BGP sessions, verify the structure
for session_name, session_data in bgp_sessions.items():
assert isinstance(
session_data, dict
), f"{worker}:{device}:{session_name} session data should be a dictionary"
# Verify required top-level fields
required_fields = [
"id",
"name",
"description",
"device",
"local_address",
"local_as",
"remote_address",
"remote_as",
"status",
"last_updated",
"created",
"url",
"display",
"site",
"tenant",
"tags",
"comments",
"custom_fields",
]
for field in required_fields:
assert (
field in session_data
), f"{worker}:{device}:{session_name} missing field '{field}'"
|
Test Get Bgp Peerings With Instance
Test BGP peerings retrieval with explicit instance
Source code in tests\test_netbox_service.py
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590 | def test_get_bgp_peerings_with_instance(self, nfclient):
"""Test BGP peerings retrieval with explicit instance"""
ret = nfclient.run_job(
"netbox",
"get_bgp_peerings",
workers="any",
kwargs={"devices": ["fceos4", "fceos5"], "instance": "prod"},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert res["result"], f"{worker} returned no results"
assert "fceos4" in res["result"], f"{worker} missing fceos4 in results"
assert "fceos5" in res["result"], f"{worker} missing fceos5 in results"
# Check that each device has a dictionary (may be empty if no BGP sessions)
for device, bgp_sessions in res["result"].items():
assert isinstance(
bgp_sessions, dict
), f"{worker}:{device} BGP sessions should be a dictionary"
|
Test Get Bgp Peerings Nonexistent Device
Test error handling for non-existent device
Source code in tests\test_netbox_service.py
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610 | def test_get_bgp_peerings_nonexistent_device(self, nfclient):
"""Test error handling for non-existent device"""
ret = nfclient.run_job(
"netbox",
"get_bgp_peerings",
workers="any",
kwargs={"devices": ["nonexistent-device-12345"]},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert res["result"], f"{worker} returned no results"
assert (
"nonexistent-device-12345" in res["result"]
), f"{worker} should have entry for nonexistent device"
# The result for non-existent device should be empty dict
assert (
res["result"]["nonexistent-device-12345"] == {}
), f"{worker} should return empty dict for nonexistent device"
|
Test Get Bgp Peerings Empty Devices List
Test with empty devices list
Source code in tests\test_netbox_service.py
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628 | def test_get_bgp_peerings_empty_devices_list(self, nfclient):
"""Test with empty devices list"""
ret = nfclient.run_job(
"netbox",
"get_bgp_peerings",
workers="any",
kwargs={"devices": []},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert isinstance(
res["result"], dict
), f"{worker} should return a dictionary"
assert (
len(res["result"]) == 0
), f"{worker} should return empty dict for empty devices list"
|
Test Get Bgp Peerings Cache True
Test cache content for BGP peerings
Source code in tests\test_netbox_service.py
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678 | def test_get_bgp_peerings_cache_true(self, nfclient):
"""Test cache content for BGP peerings"""
# get cache brief info
cache_before = nfclient.run_job(
"netbox",
"cache_list",
workers=["netbox-worker-1.1"],
kwargs={"keys": "get_bgp_peerings*", "details": True},
)
# Clear any existing cache
nfclient.run_job(
"netbox",
"cache_clear",
workers=["netbox-worker-1.1"],
kwargs={"keys": "get_bgp_peerings*"},
)
# cache data
nfclient.run_job(
"netbox",
"get_bgp_peerings",
workers=["netbox-worker-1.1"],
kwargs={"devices": ["fceos4", "fceos5"], "cache": True},
)
# Now retrieve cache content
cache_after = nfclient.run_job(
"netbox",
"cache_list",
workers=["netbox-worker-1.1"],
kwargs={"keys": "get_bgp_peerings*", "details": True},
)
print("cache_before:")
pprint.pprint(cache_before, width=200)
print("cache_after:")
pprint.pprint(cache_after, width=200)
for worker, res in cache_after.items():
for cache_item_after in res["result"]:
key = cache_item_after["key"]
for cache_item_before in cache_before[worker]["result"]:
if cache_item_before["key"] == key:
assert (
cache_item_before["creation"]
!= cache_item_after["creation"]
), f"{worker}:{key} cache not re-created"
|
Test Get Bgp Peerings Cache Refresh
Test cache content for BGP peerings
Source code in tests\test_netbox_service.py
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720 | def test_get_bgp_peerings_cache_refresh(self, nfclient):
"""Test cache content for BGP peerings"""
# get cache brief info
cache_before = nfclient.run_job(
"netbox",
"cache_list",
workers=["netbox-worker-1.1"],
kwargs={"keys": "get_bgp_peerings*", "details": True},
)
# cache data
nfclient.run_job(
"netbox",
"get_bgp_peerings",
workers=["netbox-worker-1.1"],
kwargs={"devices": ["fceos4", "fceos5"], "cache": "refresh"},
)
# Now retrieve cache content
cache_after = nfclient.run_job(
"netbox",
"cache_list",
workers=["netbox-worker-1.1"],
kwargs={"keys": "get_bgp_peerings*", "details": True},
)
print("cache_before:")
pprint.pprint(cache_before, width=200)
print("cache_after:")
pprint.pprint(cache_after, width=200)
for worker, res in cache_after.items():
for cache_item_after in res["result"]:
key = cache_item_after["key"]
for cache_item_before in cache_before[worker]["result"]:
if cache_item_before["key"] == key:
assert (
cache_item_before["creation"]
!= cache_item_after["creation"]
), f"{worker}:{key} cache not re-created"
|
Test Get Bgp Peerings Cache Force
Test cache force mode (use cache without checking)
Source code in tests\test_netbox_service.py
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743 | def test_get_bgp_peerings_cache_force(self, nfclient):
"""Test cache force mode (use cache without checking)"""
# First, populate cache
nfclient.run_job(
"netbox",
"get_bgp_peerings",
workers="any",
kwargs={"devices": ["fceos4"], "cache": True},
)
# Use cache="force" to retrieve from cache only
ret = nfclient.run_job(
"netbox",
"get_bgp_peerings",
workers="any",
kwargs={"devices": ["fceos4"], "cache": "force"},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert res["result"], f"{worker} returned no results"
assert "fceos4" in res["result"], f"{worker} missing fceos4 in results"
|
Test Get Bgp Peerings Cache False
Test with cache disabled
Source code in tests\test_netbox_service.py
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780 | def test_get_bgp_peerings_cache_false(self, nfclient):
"""Test with cache disabled"""
# Clear any existing cache
nfclient.run_job(
"netbox",
"cache_clear",
workers="all",
kwargs={"keys": "get_bgp_peerings*"},
)
# Fetch with cache=False
ret = nfclient.run_job(
"netbox",
"get_bgp_peerings",
workers="any",
kwargs={"devices": ["fceos4"], "cache": False},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert res["result"], f"{worker} returned no results"
assert "fceos4" in res["result"], f"{worker} missing fceos4 in results"
# Verify nothing was cached
bgp_cache = nfclient.run_job(
"netbox",
"cache_get",
workers="all",
kwargs={"keys": "get_bgp_peerings::fceos4"},
)
for worker, res in bgp_cache.items():
# Cache should be empty or the key should not exist
assert (
"get_bgp_peerings::fceos4" not in res["result"]
), f"{worker} should not have cached data when cache=False"
|
TestSyncDeviceFacts
Device fact synchronization from external sources to Netbox.
Purpose: Validate pushing collected device facts (from Nornir) back to Netbox.
Test Sync Device Facts Basic Update
Test basic sync with devices list - updates serial numbers
Source code in tests\test_netbox_service.py
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827 | def test_sync_device_facts_basic_update(self, nfclient):
"""Test basic sync with devices list - updates serial numbers"""
# Setup: update serial for spine to force a change
pynb = get_pynetbox(nfclient)
nb_device = pynb.dcim.devices.get(name="ceos-spine-1")
nb_device.serial = "123456"
nb_device.save()
# Execute sync job
ret = nfclient.run_job(
"netbox",
"sync_device_facts",
workers="any",
kwargs={
"datasource": "nornir",
"devices": ["ceos-spine-1", "ceos-spine-2"],
},
)
pprint.pprint(ret, width=200)
# Verify results
for worker, res in ret.items():
assert not res["failed"], f"{worker} failed - {res.get('errors')}"
assert (
"ceos-spine-1" in res["result"]
), f"{worker} returned no results for ceos-spine-1"
assert (
"ceos-spine-2" in res["result"]
), f"{worker} returned no results for ceos-spine-2"
# ceos-spine-1 should have been updated
assert res["result"]["ceos-spine-1"][
"sync_device_facts"
], "ceos-spine-1 no data returned"
assert res["result"]["ceos-spine-1"]["sync_device_facts"][
"serial"
], "ceos-spine-1 serial not synced"
# ceos-spine-2 should be in sync or updated
assert res["result"]["ceos-spine-2"][
"sync_device_facts"
], "ceos-spine-2 no data returned"
|
Test Sync Device Facts Already In Sync
Test when device facts are already synchronized
Source code in tests\test_netbox_service.py
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859 | def test_sync_device_facts_already_in_sync(self, nfclient):
"""Test when device facts are already synchronized"""
# First sync to ensure devices are up to date
nfclient.run_job(
"netbox",
"sync_device_facts",
workers="any",
kwargs={
"datasource": "nornir",
"devices": ["ceos-spine-1"],
},
)
# Second sync should show devices in sync
ret = nfclient.run_job(
"netbox",
"sync_device_facts",
workers="any",
kwargs={
"datasource": "nornir",
"devices": ["ceos-spine-1"],
},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert not res["failed"], f"{worker} failed"
assert (
res["result"]["ceos-spine-1"]["sync_device_facts"]
== "Device facts in sync"
), "Expected device to be in sync"
|
Test Sync Device Facts With Filters
Test sync using Nornir filters instead of device list
Source code in tests\test_netbox_service.py
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886 | def test_sync_device_facts_with_filters(self, nfclient):
"""Test sync using Nornir filters instead of device list"""
ret = nfclient.run_job(
"netbox",
"sync_device_facts",
workers="any",
kwargs={
"datasource": "nornir",
"FC": "spine",
},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert not res["failed"], f"{worker} failed - {res.get('errors')}"
assert (
"ceos-spine-1" in res["result"]
), f"{worker} returned no results for ceos-spine-1"
assert (
"ceos-spine-2" in res["result"]
), f"{worker} returned no results for ceos-spine-2"
for device, device_data in res["result"].items():
assert (
"sync_device_facts" in device_data
), f"{worker}:{device} no sync data"
|
Test Sync Device Facts Dry Run
Test dry run mode - should not modify NetBox
Source code in tests\test_netbox_service.py
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933 | def test_sync_device_facts_dry_run(self, nfclient):
"""Test dry run mode - should not modify NetBox"""
# Setup: change serial to create a difference
pynb = get_pynetbox(nfclient)
nb_device = pynb.dcim.devices.get(name="ceos-spine-1")
original_serial = nb_device.serial
nb_device.serial = "DRY-RUN-TEST-123"
nb_device.save()
# Execute dry run
ret = nfclient.run_job(
"netbox",
"sync_device_facts",
workers="any",
kwargs={
"datasource": "nornir",
"devices": ["ceos-spine-1", "ceos-spine-2"],
"dry_run": True,
},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert not res["failed"], f"{worker} failed"
assert res["dry_run"] is True, "dry_run flag not set in result"
assert (
"ceos-spine-1" in res["result"]
), f"{worker} returned no results for ceos-spine-1"
assert (
"ceos-spine-2" in res["result"]
), f"{worker} returned no results for ceos-spine-2"
for device, device_data in res["result"].items():
assert (
"sync_device_facts_dry_run" in device_data
), f"{worker}:{device} no dry run data"
# Verify NetBox was not modified
nb_device = pynb.dcim.devices.get(name="ceos-spine-1")
assert (
nb_device.serial == "DRY-RUN-TEST-123"
), "NetBox was modified during dry run"
# Cleanup
nb_device.serial = original_serial
nb_device.save()
|
Test Sync Device Facts With Diff
Test that diff is properly populated when changes are made
Source code in tests\test_netbox_service.py
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972 | def test_sync_device_facts_with_diff(self, nfclient):
"""Test that diff is properly populated when changes are made"""
# Setup: force a change
pynb = get_pynetbox(nfclient)
nb_device = pynb.dcim.devices.get(name="ceos-spine-1")
nb_device.serial = "OLD-SERIAL-123"
nb_device.save()
# Execute sync
ret = nfclient.run_job(
"netbox",
"sync_device_facts",
workers="any",
kwargs={
"datasource": "nornir",
"devices": ["ceos-spine-1"],
},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert not res["failed"], f"{worker} failed"
if (
res["result"]["ceos-spine-1"]["sync_device_facts"]
!= "Device facts in sync"
):
assert (
"ceos-spine-1" in res["diff"]
), "diff not populated for changed device"
assert (
"serial" in res["diff"]["ceos-spine-1"]
), "serial field not in diff"
assert (
"-" in res["diff"]["ceos-spine-1"]["serial"]
), "old value (-) not in diff"
assert (
"+" in res["diff"]["ceos-spine-1"]["serial"]
), "new value (+) not in diff"
|
Test Sync Device Facts With Branch
Test sync with NetBox branching plugin
Source code in tests\test_netbox_service.py
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006 | def test_sync_device_facts_with_branch(self, nfclient):
"""Test sync with NetBox branching plugin"""
delete_branch("sync_facts_test_branch", nfclient)
ret = nfclient.run_job(
"netbox",
"sync_device_facts",
workers="any",
kwargs={
"datasource": "nornir",
"devices": ["ceos-spine-1", "ceos-spine-2"],
"branch": "sync_facts_test_branch",
},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert not res["failed"], f"{worker} failed - {res.get('errors')}"
assert (
"ceos-spine-1" in res["result"]
), f"{worker} returned no results for ceos-spine-1"
assert (
"ceos-spine-2" in res["result"]
), f"{worker} returned no results for ceos-spine-2"
for device, device_data in res["result"].items():
assert "branch" in device_data, f"{worker}:{device} no branch info"
assert (
device_data["branch"] == "sync_facts_test_branch"
), "Wrong branch name"
# Cleanup
delete_branch("sync_facts_test_branch", nfclient)
|
Test Sync Device Facts With Custom Instance
Test sync with explicit NetBox instance
Source code in tests\test_netbox_service.py
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024 | def test_sync_device_facts_with_custom_instance(self, nfclient):
"""Test sync with explicit NetBox instance"""
ret = nfclient.run_job(
"netbox",
"sync_device_facts",
workers="any",
kwargs={
"datasource": "nornir",
"devices": ["ceos-spine-1"],
"instance": "prod",
},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert not res["failed"], f"{worker} failed"
assert "prod" in res["resources"], "instance not in resources"
|
Test Sync Device Facts With Batch Size
Test sync with custom batch size
Source code in tests\test_netbox_service.py
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043 | def test_sync_device_facts_with_batch_size(self, nfclient):
"""Test sync with custom batch size"""
ret = nfclient.run_job(
"netbox",
"sync_device_facts",
workers="any",
kwargs={
"datasource": "nornir",
"devices": ["ceos-spine-1", "ceos-spine-2"],
"batch_size": 1, # Process one device at a time
},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert not res["failed"], f"{worker} failed"
assert "ceos-spine-1" in res["result"], "ceos-spine-1 not processed"
assert "ceos-spine-2" in res["result"], "ceos-spine-2 not processed"
|
Test Sync Device Facts With Timeout
Test sync with custom timeout
Source code in tests\test_netbox_service.py
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061 | def test_sync_device_facts_with_timeout(self, nfclient):
"""Test sync with custom timeout"""
ret = nfclient.run_job(
"netbox",
"sync_device_facts",
workers="any",
kwargs={
"datasource": "nornir",
"devices": ["ceos-spine-1"],
"timeout": 120,
},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert not res["failed"], f"{worker} failed"
assert "ceos-spine-1" in res["result"], "ceos-spine-1 not processed"
|
Test Sync Device Facts Non Existing Device
Test error handling when device doesn't exist in NetBox
Source code in tests\test_netbox_service.py
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078 | def test_sync_device_facts_non_existing_device(self, nfclient):
"""Test error handling when device doesn't exist in NetBox"""
ret = nfclient.run_job(
"netbox",
"sync_device_facts",
workers="any",
kwargs={
"datasource": "nornir",
"devices": ["nonexistent-device-12345"],
},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
# Should fail or report error
assert res["errors"], "Should report error for non-existent device"
|
Test Sync Device Facts Empty Device List
Test sync with empty device list and no filters
Source code in tests\test_netbox_service.py
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098 | def test_sync_device_facts_empty_device_list(self, nfclient):
"""Test sync with empty device list and no filters"""
ret = nfclient.run_job(
"netbox",
"sync_device_facts",
workers="any",
kwargs={
"datasource": "nornir",
"devices": [],
},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
# Should complete without error, just process no devices
assert not res["failed"], f"{worker} failed"
assert (
res["result"] == {} or len(res["result"]) == 0
), "Should have empty result"
|
Test Sync Device Facts Single Device
Test sync with a single device
Source code in tests\test_netbox_service.py
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116 | def test_sync_device_facts_single_device(self, nfclient):
"""Test sync with a single device"""
ret = nfclient.run_job(
"netbox",
"sync_device_facts",
workers="any",
kwargs={
"datasource": "nornir",
"devices": ["ceos-spine-1"],
},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert not res["failed"], f"{worker} failed"
assert "ceos-spine-1" in res["result"], "Device not in result"
assert len(res["result"]) == 1, "Should only process one device"
|
TestSyncDeviceInterfaces
Interface status and configuration synchronization.
Purpose: Validate pushing interface data from network devices to Netbox.
Test Sync Device Interfaces
Clean TEST_SYNC interfaces from spines then sync. All TEST_SYNC interfaces
must be created from live data; result must carry live-run keys.
Source code in tests\test_netbox_service.py
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222 | def test_sync_device_interfaces(self, nfclient):
"""Clean TEST_SYNC interfaces from spines then sync. All TEST_SYNC interfaces
must be created from live data; result must carry live-run keys."""
self._cleanup(nfclient, self.SPINE_DEVICES)
ret = self._sync(nfclient, self.SPINE_DEVICES)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} failed - {res}"
for device in self.SPINE_DEVICES:
assert (
device in res["result"]
), f"{worker} returned no results for {device}"
for device, device_data in res["result"].items():
assert (
self.LIVE_RUN_KEYS <= device_data.keys()
), f"{worker}:{device} missing keys in result, got: {set(device_data)}"
assert device_data[
"created"
], f"{worker}:{device} no interfaces created after cleanup"
|
Test Sync Device Interfaces Dry Run
Clean TEST_SYNC from spines then dry_run sync. The plan must list
cleaned interfaces under 'create' and expose correct key/type structure.
Source code in tests\test_netbox_service.py
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276 | def test_sync_device_interfaces_dry_run(self, nfclient):
"""Clean TEST_SYNC from spines then dry_run sync. The plan must list
cleaned interfaces under 'create' and expose correct key/type structure."""
self._cleanup(nfclient, self.SPINE_DEVICES)
ret = self._sync(nfclient, self.SPINE_DEVICES, dry_run=True)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} failed - {res}"
for device in self.SPINE_DEVICES:
assert (
device in res["result"]
), f"{worker} returned no results for {device}"
for device, device_data in res["result"].items():
assert (
self.DRY_RUN_KEYS <= device_data.keys()
), f"{worker}:{device} dry-run result missing keys, got: {set(device_data)}"
assert isinstance(
device_data["create"], list
), f"{worker}:{device} create not a list"
assert isinstance(
device_data["delete"], list
), f"{worker}:{device} delete not a list"
assert isinstance(
device_data["update"], dict
), f"{worker}:{device} update not a dict"
assert isinstance(
device_data["in_sync"], list
), f"{worker}:{device} in_sync not a list"
assert device_data[
"create"
], f"{worker}:{device} create list is empty after cleanup"
|
Test Sync Device Interfaces Create
Clean TEST_SYNC from spine-2 then verify sync creates Loopback10
(TEST_SYNC_LOOPBACK_IPV4) and Loopback11 (TEST_SYNC_LOOPBACK_IPV6) from live data.
Source code in tests\test_netbox_service.py
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317 | def test_sync_device_interfaces_create(self, nfclient):
"""Clean TEST_SYNC from spine-2 then verify sync creates Loopback10
(TEST_SYNC_LOOPBACK_IPV4) and Loopback11 (TEST_SYNC_LOOPBACK_IPV6) from live data.
"""
self._cleanup(nfclient, ["ceos-spine-2"])
ret = self._sync(nfclient, ["ceos-spine-2"])
pprint.pprint(ret)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} failed - {res}"
device_data = res["result"]["ceos-spine-2"]
assert (
"Loopback10" in device_data["created"]
), f"{worker} Loopback10 not in created list"
assert (
"Loopback11" in device_data["created"]
), f"{worker} Loopback11 not in created list"
# Validate Loopback10 record in NetBox
nb_lb10 = self._get_nb_intf(nfclient, "ceos-spine-2", "Loopback10")
assert nb_lb10 is not None, "Loopback10 not found in NetBox after sync"
assert (
nb_lb10.description == "TEST_SYNC_LOOPBACK_IPV4"
), f"Loopback10 description mismatch: got {nb_lb10.description!r}"
assert (
nb_lb10.type.value == "virtual"
), f"Loopback10 type mismatch: got {nb_lb10.type.value!r}"
# Validate Loopback11 record in NetBox
nb_lb11 = self._get_nb_intf(nfclient, "ceos-spine-2", "Loopback11")
assert nb_lb11 is not None, "Loopback11 not found in NetBox after sync"
assert (
nb_lb11.description == "TEST_SYNC_LOOPBACK_IPV6"
), f"Loopback11 description mismatch: got {nb_lb11.description!r}"
assert (
nb_lb11.type.value == "virtual"
), f"Loopback11 type mismatch: got {nb_lb11.type.value!r}"
|
Test Sync Device Interfaces Update Description
Clean TEST_SYNC for spine-2, run sync to create Loopback10 with the
correct description, then corrupt it and verify sync restores it.
Field-level diff must be present in res['diff'].
Source code in tests\test_netbox_service.py
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447 | def test_sync_device_interfaces_update_description(self, nfclient):
"""Clean TEST_SYNC for spine-2, run sync to create Loopback10 with the
correct description, then corrupt it and verify sync restores it.
Field-level diff must be present in res['diff']."""
self._cleanup(nfclient, ["ceos-spine-2"])
# Create correct NB state from live data
setup = self._sync(nfclient, ["ceos-spine-2"])
for worker, res in setup.items():
assert not res["failed"], f"Setup sync failed for {worker}: {res['errors']}"
assert (
"Loopback10" in res["result"]["ceos-spine-2"]["created"]
), f"{worker} Loopback10 not created during setup"
# Corrupt description
intf_id = self._get_intf_id(nfclient, "ceos-spine-2", "Loopback10")
self._patch_intf(nfclient, intf_id, {"description": "corrupted-by-test"})
ret = self._sync(nfclient, ["ceos-spine-2"])
pprint.pprint(ret)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} failed - {res}"
device_data = res["result"]["ceos-spine-2"]
assert (
"Loopback10" in device_data["updated"]
), f"{worker} Loopback10 not in updated list after description corruption"
assert "ceos-spine-2" in res["diff"], f"{worker} diff not populated"
assert (
"Loopback10" in res["diff"]["ceos-spine-2"]["update"]
), f"{worker} Loopback10 field diff missing"
assert (
"description" in res["diff"]["ceos-spine-2"]["update"]["Loopback10"]
), f"{worker} description field missing from diff"
# Validate description restored in NetBox
nb_lb10 = self._get_nb_intf(nfclient, "ceos-spine-2", "Loopback10")
assert nb_lb10 is not None, "Loopback10 not found in NetBox after sync"
assert (
nb_lb10.description == "TEST_SYNC_LOOPBACK_IPV4"
), f"Loopback10 description not restored in NetBox: got {nb_lb10.description!r}"
|
Test Sync Device Interfaces Non Existing Device
Sync of a device not in NetBox should report an error.
Source code in tests\test_netbox_service.py
3728
3729
3730
3731
3732
3733
3734
3735 | def test_sync_device_interfaces_non_existing_device(self, nfclient):
"""Sync of a device not in NetBox should report an error."""
ret = self._sync(nfclient, ["nonexistent-device-12345"])
pprint.pprint(ret)
for worker, res in ret.items():
assert (
len(res["errors"]) > 0
), f"{worker} should have errors for nonexistent device"
|
Test Sync Device Interfaces With Branch
Clean TEST_SYNC from spines, delete the branch, then sync into a new branch.
Result must carry live-run keys and at least one interface must be created.
Source code in tests\test_netbox_service.py
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867 | def test_sync_device_interfaces_with_branch(self, nfclient):
"""Clean TEST_SYNC from spines, delete the branch, then sync into a new branch.
Result must carry live-run keys and at least one interface must be created."""
delete_branch("update_interfaces_branch_1", nfclient)
self._cleanup(nfclient, self.SPINE_DEVICES)
ret = self._sync(
nfclient,
self.SPINE_DEVICES,
branch="update_interfaces_branch_1",
)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} failed - {res}"
for device in self.SPINE_DEVICES:
assert (
device in res["result"]
), f"{worker} returned no results for {device}"
for device, device_data in res["result"].items():
assert (
self.LIVE_RUN_KEYS <= device_data.keys()
), f"{worker}:{device} missing keys in branch-run result"
|
TestCreateDeviceInterfaces
Device interface creation in Netbox.
Purpose: Validate programmatic interface creation and configuration.
Test Create Device Interfaces Single
Test creating a single interface on a device
Source code in tests\test_netbox_service.py
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894 | def test_create_device_interfaces_single(self, nfclient):
"""Test creating a single interface on a device"""
delete_interfaces(nfclient, "ceos-spine-1", "TestInterface1")
ret = nfclient.run_job(
"netbox",
"create_device_interfaces",
workers="any",
kwargs={
"devices": ["ceos-spine-1"],
"interface_name": "TestInterface1",
"interface_type": "virtual",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} failed - {res}"
assert (
"ceos-spine-1" in res["result"]
), f"{worker} returned no results for ceos-spine-1"
assert (
"TestInterface1" in res["result"]["ceos-spine-1"]["created"]
), f"{worker} did not create TestInterface1"
|
Test Create Device Interfaces Multiple Devices
Test creating interfaces on multiple devices
Source code in tests\test_netbox_service.py
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926 | def test_create_device_interfaces_multiple_devices(self, nfclient):
"""Test creating interfaces on multiple devices"""
delete_interfaces(nfclient, "ceos-spine-1", "TestInterface2")
delete_interfaces(nfclient, "ceos-spine-2", "TestInterface2")
ret = nfclient.run_job(
"netbox",
"create_device_interfaces",
workers="any",
kwargs={
"devices": ["ceos-spine-1", "ceos-spine-2"],
"interface_name": "TestInterface2",
"interface_type": "virtual",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} failed - {res}"
assert (
"ceos-spine-1" in res["result"]
), f"{worker} returned no results for ceos-spine-1"
assert (
"ceos-spine-2" in res["result"]
), f"{worker} returned no results for ceos-spine-2"
assert (
"TestInterface2" in res["result"]["ceos-spine-1"]["created"]
), f"{worker} did not create TestInterface2 on ceos-spine-1"
assert (
"TestInterface2" in res["result"]["ceos-spine-2"]["created"]
), f"{worker} did not create TestInterface2 on ceos-spine-2"
|
Test Create Device Interfaces With Range Numeric
Test creating interfaces with numeric range expansion
Source code in tests\test_netbox_service.py
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961 | def test_create_device_interfaces_with_range_numeric(self, nfclient):
"""Test creating interfaces with numeric range expansion"""
for i in range(1, 4):
delete_interfaces(nfclient, "ceos-spine-1", f"Loopback{i}")
ret = nfclient.run_job(
"netbox",
"create_device_interfaces",
workers="any",
kwargs={
"devices": ["ceos-spine-1"],
"interface_name": "Loopback[1-3]",
"interface_type": "virtual",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} failed - {res}"
assert (
"ceos-spine-1" in res["result"]
), f"{worker} returned no results for ceos-spine-1"
assert (
len(res["result"]["ceos-spine-1"]["created"]) == 3
), f"{worker} did not create 3 interfaces"
assert (
"Loopback1" in res["result"]["ceos-spine-1"]["created"]
), f"{worker} did not create Loopback1"
assert (
"Loopback2" in res["result"]["ceos-spine-1"]["created"]
), f"{worker} did not create Loopback2"
assert (
"Loopback3" in res["result"]["ceos-spine-1"]["created"]
), f"{worker} did not create Loopback3"
|
Test Create Device Interfaces With Range List
Test creating interfaces with comma-separated list expansion
Source code in tests\test_netbox_service.py
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993 | def test_create_device_interfaces_with_range_list(self, nfclient):
"""Test creating interfaces with comma-separated list expansion"""
delete_interfaces(nfclient, "ceos-spine-1", "ge-0/0/0")
delete_interfaces(nfclient, "ceos-spine-1", "xe-0/0/0")
ret = nfclient.run_job(
"netbox",
"create_device_interfaces",
workers="any",
kwargs={
"devices": ["ceos-spine-1"],
"interface_name": "[ge,xe]-0/0/0",
"interface_type": "1000base-t",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} failed - {res}"
assert (
"ceos-spine-1" in res["result"]
), f"{worker} returned no results for ceos-spine-1"
assert (
len(res["result"]["ceos-spine-1"]["created"]) == 2
), f"{worker} did not create 2 interfaces"
assert (
"ge-0/0/0" in res["result"]["ceos-spine-1"]["created"]
), f"{worker} did not create ge-0/0/0"
assert (
"xe-0/0/0" in res["result"]["ceos-spine-1"]["created"]
), f"{worker} did not create xe-0/0/0"
|
Test Create Device Interfaces With Multiple Ranges
Test creating interfaces with multiple range patterns
Source code in tests\test_netbox_service.py
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024 | def test_create_device_interfaces_with_multiple_ranges(self, nfclient):
"""Test creating interfaces with multiple range patterns"""
for prefix in ["ge", "xe"]:
for i in range(0, 2):
delete_interfaces(nfclient, "ceos-spine-1", f"{prefix}-0/0/{i}")
ret = nfclient.run_job(
"netbox",
"create_device_interfaces",
workers="any",
kwargs={
"devices": ["ceos-spine-1"],
"interface_name": "[ge,xe]-0/0/[0-1]",
"interface_type": "1000base-t",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} failed - {res}"
assert (
"ceos-spine-1" in res["result"]
), f"{worker} returned no results for ceos-spine-1"
assert (
len(res["result"]["ceos-spine-1"]["created"]) == 4
), f"{worker} did not create 4 interfaces"
assert "ge-0/0/0" in res["result"]["ceos-spine-1"]["created"]
assert "ge-0/0/1" in res["result"]["ceos-spine-1"]["created"]
assert "xe-0/0/0" in res["result"]["ceos-spine-1"]["created"]
assert "xe-0/0/1" in res["result"]["ceos-spine-1"]["created"]
|
Test Create Device Interfaces Multiple Names List
Test creating multiple interfaces passed as a list
Source code in tests\test_netbox_service.py
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052 | def test_create_device_interfaces_multiple_names_list(self, nfclient):
"""Test creating multiple interfaces passed as a list"""
delete_interfaces(nfclient, "ceos-spine-1", "TestIntf1")
delete_interfaces(nfclient, "ceos-spine-1", "TestIntf2")
ret = nfclient.run_job(
"netbox",
"create_device_interfaces",
workers="any",
kwargs={
"devices": ["ceos-spine-1"],
"interface_name": ["TestIntf1", "TestIntf2"],
"interface_type": "virtual",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} failed - {res}"
assert (
"ceos-spine-1" in res["result"]
), f"{worker} returned no results for ceos-spine-1"
assert (
len(res["result"]["ceos-spine-1"]["created"]) == 2
), f"{worker} did not create 2 interfaces"
assert "TestIntf1" in res["result"]["ceos-spine-1"]["created"]
assert "TestIntf2" in res["result"]["ceos-spine-1"]["created"]
|
Test Create Device Interfaces Skip Existing
Test that existing interfaces are skipped
Source code in tests\test_netbox_service.py
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094 | def test_create_device_interfaces_skip_existing(self, nfclient):
"""Test that existing interfaces are skipped"""
# First create the interface
ret1 = nfclient.run_job(
"netbox",
"create_device_interfaces",
workers="any",
kwargs={
"devices": ["ceos-spine-1"],
"interface_name": "TestExisting",
"interface_type": "virtual",
},
)
# Try to create it again
ret2 = nfclient.run_job(
"netbox",
"create_device_interfaces",
workers="any",
kwargs={
"devices": ["ceos-spine-1"],
"interface_name": "TestExisting",
"interface_type": "virtual",
},
)
pprint.pprint(ret2)
for worker, res in ret2.items():
assert res["failed"] == False, f"{worker} failed - {res}"
assert (
"ceos-spine-1" in res["result"]
), f"{worker} returned no results for ceos-spine-1"
assert (
"TestExisting" in res["result"]["ceos-spine-1"]["skipped"]
), f"{worker} did not skip existing TestExisting interface"
assert (
len(res["result"]["ceos-spine-1"]["created"]) == 0
), f"{worker} should not have created any interfaces"
# Cleanup
delete_interfaces(nfclient, "ceos-spine-1", "TestExisting")
|
Test Create Device Interfaces Dry Run
Test dry run mode
Source code in tests\test_netbox_service.py
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136 | def test_create_device_interfaces_dry_run(self, nfclient):
"""Test dry run mode"""
delete_interfaces(nfclient, "ceos-spine-1", "TestDryRun")
ret = nfclient.run_job(
"netbox",
"create_device_interfaces",
workers="any",
kwargs={
"devices": ["ceos-spine-1"],
"interface_name": "TestDryRun",
"interface_type": "virtual",
"dry_run": True,
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} failed - {res}"
assert (
"ceos-spine-1" in res["result"]
), f"{worker} returned no results for ceos-spine-1"
assert (
"TestDryRun" in res["result"]["ceos-spine-1"]["created"]
), f"{worker} did not mark TestDryRun for creation in dry run"
# Verify interface was not actually created
resp_get = nfclient.run_job(
"netbox",
"rest",
workers="any",
kwargs={
"method": "get",
"api": "/dcim/interfaces/",
"params": {"device": "ceos-spine-1", "name": "TestDryRun"},
},
)
worker, interfaces = tuple(resp_get.items())[0]
assert (
len(interfaces["result"]["results"]) == 0
), "Interface should not exist after dry run"
|
Test Create Device Interfaces With Branch
Test creating interfaces with a branch
Source code in tests\test_netbox_service.py
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167 | def test_create_device_interfaces_with_branch(self, nfclient):
"""Test creating interfaces with a branch"""
delete_branch("create_interfaces_branch_1", nfclient)
delete_interfaces(nfclient, "ceos-spine-1", "TestBranch")
ret = nfclient.run_job(
"netbox",
"create_device_interfaces",
workers="any",
kwargs={
"devices": ["ceos-spine-1"],
"interface_name": "TestBranch",
"interface_type": "virtual",
"branch": "create_interfaces_branch_1",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} failed - {res}"
assert (
"ceos-spine-1" in res["result"]
), f"{worker} returned no results for ceos-spine-1"
assert (
"TestBranch" in res["result"]["ceos-spine-1"]["created"]
), f"{worker} did not create TestBranch"
# Cleanup
delete_interfaces(nfclient, "ceos-spine-1", "TestBranch")
delete_branch("create_interfaces_branch_1", nfclient)
|
Test Create Device Interfaces Non Existing Device
Test handling of non-existing device
Source code in tests\test_netbox_service.py
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184 | def test_create_device_interfaces_non_existing_device(self, nfclient):
"""Test handling of non-existing device"""
ret = nfclient.run_job(
"netbox",
"create_device_interfaces",
workers="any",
kwargs={
"devices": ["nonexistent-device-12345"],
"interface_name": "TestInterface",
"interface_type": "virtual",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert len(res["errors"]) > 0, f"{worker} should have errors"
|
TestSyncDeviceIP
IP address and interface IP synchronization.
Purpose: Validate IP address assignment and synchronization to device interfaces.
Test Sync Device Ip
Clean IPs from both spines then sync. Both spine IPs must be created;
result must carry the correct RESULT_KEYS per device.
Source code in tests\test_netbox_service.py
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292 | def test_sync_device_ip(self, nfclient):
"""Clean IPs from both spines then sync. Both spine IPs must be created;
result must carry the correct RESULT_KEYS per device."""
self._cleanup(nfclient, self.SPINE_DEVICES)
ret = self._sync(nfclient, self.SPINE_DEVICES)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} failed - {res}"
for device in self.SPINE_DEVICES:
assert (
device in res["result"]
), f"{worker} returned no result for {device}"
for device, device_data in res["result"].items():
assert (
self.RESULT_KEYS <= device_data.keys()
), f"{worker}:{device} missing keys in result, got: {set(device_data)}"
assert device_data[
"created"
], f"{worker}:{device} no IPs created after cleanup"
# Validate both spine IPs exist in NetBox assigned to the correct interface
pynb = get_pynetbox(nfclient)
nb_spine1 = list(
pynb.ipam.ip_addresses.filter(address=self.SPINE1_IP, device="ceos-spine-1")
)
assert nb_spine1, f"{self.SPINE1_IP} not found in NetBox for ceos-spine-1"
assert (
nb_spine1[0].assigned_object is not None
), f"{self.SPINE1_IP} not assigned to any interface on ceos-spine-1"
assert (
nb_spine1[0].assigned_object.name == self.SPINE1_INTF
), f"{self.SPINE1_IP} assigned to {nb_spine1[0].assigned_object.name!r}, expected {self.SPINE1_INTF!r}"
nb_spine2 = list(
pynb.ipam.ip_addresses.filter(address=self.SPINE2_IP, device="ceos-spine-2")
)
assert nb_spine2, f"{self.SPINE2_IP} not found in NetBox for ceos-spine-2"
assert (
nb_spine2[0].assigned_object is not None
), f"{self.SPINE2_IP} not assigned to any interface on ceos-spine-2"
|
Test Sync Device Ip Dry Run
Clean IPs from both spines then dry_run. Result keys must be RESULT_KEYS
and 'created' must be non-empty (no actual NB writes).
Source code in tests\test_netbox_service.py
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322 | def test_sync_device_ip_dry_run(self, nfclient):
"""Clean IPs from both spines then dry_run. Result keys must be RESULT_KEYS
and 'created' must be non-empty (no actual NB writes)."""
self._cleanup(nfclient, self.SPINE_DEVICES)
ret = self._sync(nfclient, self.SPINE_DEVICES, dry_run=True)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} failed - {res}"
for device in self.SPINE_DEVICES:
assert (
device in res["result"]
), f"{worker} returned no result for {device}"
for device, device_data in res["result"].items():
assert (
self.RESULT_KEYS <= device_data.keys()
), f"{worker}:{device} dry-run result missing keys, got: {set(device_data)}"
assert device_data[
"created"
], f"{worker}:{device} dry-run created list is empty after cleanup"
# Verify dry-run made no writes — IPs must still be absent from NetBox
pynb = get_pynetbox(nfclient)
ips_in_nb = list(
pynb.ipam.ip_addresses.filter(address=self.SPINE1_IP, device="ceos-spine-1")
)
assert (
not ips_in_nb
), f"dry-run wrote IP {self.SPINE1_IP} to NetBox: {[str(i) for i in ips_in_nb]}"
|
Test Sync Device Ip With Branch
Clean spine IPs, delete the test branch, then sync into a new branch.
Result must carry RESULT_KEYS and at least one IP must be created.
Source code in tests\test_netbox_service.py
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724 | def test_sync_device_ip_with_branch(self, nfclient):
"""Clean spine IPs, delete the test branch, then sync into a new branch.
Result must carry RESULT_KEYS and at least one IP must be created."""
branch = "sync_device_ip_branch_1"
delete_branch(branch, nfclient)
self._cleanup(nfclient, self.SPINE_DEVICES)
ret = self._sync(nfclient, self.SPINE_DEVICES, branch=branch)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} failed - {res}"
for device in self.SPINE_DEVICES:
assert (
device in res["result"]
), f"{worker} returned no result for {device}"
for device, device_data in res["result"].items():
assert (
self.RESULT_KEYS <= device_data.keys()
), f"{worker}:{device} missing keys in branch-run result"
# Validate IPs were NOT written to the main NetBox context (branch-only writes)
pynb = get_pynetbox(nfclient)
nb_ips = list(
pynb.ipam.ip_addresses.filter(address=self.SPINE1_IP, device="ceos-spine-1")
)
assert not nb_ips, (
f"Branch sync wrote {self.SPINE1_IP} to main NetBox context (should be branch-only): "
f"{[str(i) for i in nb_ips]}"
)
delete_branch(branch, nfclient)
|
TestCreateIP
IP address creation in Netbox.
Purpose: Validate IP address allocation and creation.
Test Create Ip By Prefix
Source code in tests\test_netbox_service.py
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134 | def test_create_ip_by_prefix(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_ips("10.0.0.0/24", nfclient)
rand = random.randint(1, 1000)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"description": f"test create ip {rand}",
},
)
create_2 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"description": f"test create ip {rand}",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
print("create_2")
pprint.pprint(create_2, width=200)
for worker, res1 in create_1.items():
assert res1["failed"] == False, "Allocation failed"
assert res1["result"]["address"], f"Result has no ip {res1['result']}"
worker, res2 = tuple(create_2.items())[0]
assert (
res1["result"]["address"] == res2["result"]["address"]
), "Should have been same IP address"
assert (
res1["result"]["description"] == res2["result"]["description"]
), "Should have been same IP description"
|
Test Create Ip By Prefix Description
Source code in tests\test_netbox_service.py
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177 | def test_create_ip_by_prefix_description(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_ips("10.0.0.0/24", nfclient)
rand = random.randint(1, 1000)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "TEST NEXT IP PREFIX",
"description": f"test create ip {rand}",
},
)
create_2 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "TEST NEXT IP PREFIX",
"description": f"test create ip {rand}",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
print("create_2")
pprint.pprint(create_2, width=200)
for worker, res1 in create_1.items():
assert res1["failed"] == False, "Allocation failed"
assert res1["result"]["address"], f"Result has no ip {res1['result']}"
worker, res2 = tuple(create_2.items())[0]
assert (
res1["result"]["address"] == res2["result"]["address"]
), "Should have been same IP address"
assert (
res1["result"]["description"] == res2["result"]["description"]
), "Should have been same IP description"
|
Test Create Ip By Prefix Multiple
Source code in tests\test_netbox_service.py
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257 | def test_create_ip_by_prefix_multiple(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_ips("10.0.0.0/24", nfclient)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"description": f"test create ip {random.randint(1, 1000)}",
},
)
create_2 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"description": f"test create ip {random.randint(1, 1000)}",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
print("create_2")
pprint.pprint(create_2, width=200)
worker, res1 = tuple(create_1.items())[0]
worker, res2 = tuple(create_2.items())[0]
assert res1["failed"] == False, "Allocation failed"
assert res2["failed"] == False, "Allocation failed"
assert res1["result"]["address"] != res2["result"]["address"]
assert res1["result"]["description"] != res2["result"]["description"]
|
Test Create Ip Nonexist Prefix
Source code in tests\test_netbox_service.py
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276 | def test_create_ip_nonexist_prefix(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "1.2.3.0/24",
"description": f"test create ip {random.randint(1, 1000)}",
},
)
pprint.pprint(create_1, width=200)
worker, res1 = tuple(create_1.items())[0]
assert res1["failed"] == True, "Allocation not failed"
assert "Unable to source parent prefix from Netbox" in res1["messages"][0]
|
Test Create Ip By Prefix Device Interface
Source code in tests\test_netbox_service.py
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318 | def test_create_ip_by_prefix_device_interface(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_ips("10.0.0.0/24", nfclient)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fceos4",
"interface": "eth1",
},
)
create_2 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fceos4",
"interface": "eth1",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
print("create_2")
pprint.pprint(create_2, width=200)
worker, res1 = tuple(create_1.items())[0]
worker, res2 = tuple(create_2.items())[0]
assert res1["failed"] == False, "Allocation failed"
assert res2["failed"] == False, "Allocation failed"
assert (
res1["result"]["address"] == res2["result"]["address"]
), "Should be same IP"
|
Test Create Ip By Prefix Description Device Interface
This test should allocate two different IPs since device, interface, description given
Source code in tests\test_netbox_service.py
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363 | def test_create_ip_by_prefix_description_device_interface(self, nfclient):
"This test should allocate two different IPs since device, interface, description given"
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_ips("10.0.0.0/24", nfclient)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fceos4",
"interface": "eth1",
"description": "foo1",
},
)
create_2 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fceos4",
"interface": "eth1",
"description": "foo2",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
print("create_2")
pprint.pprint(create_2, width=200)
worker, res1 = tuple(create_1.items())[0]
worker, res2 = tuple(create_2.items())[0]
assert res1["failed"] == False, "Allocation failed"
assert res2["failed"] == False, "Allocation failed"
assert (
res1["result"]["address"] != res2["result"]["address"]
), "Should be different IP cause description is different"
|
Source code in tests\test_netbox_service.py
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395 | def test_create_ip_with_vrf_tags_tenant_role_dnsname_comments(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_ips("10.0.0.0/24", nfclient)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fceos4",
"interface": "eth1",
"description": "foo1",
"vrf": "VRF1",
"tags": ["NORFAB", "ACCESS"],
"tenant": "NORFAB",
"dns_name": "foo1.lab.local",
"role": "anycast",
"comments": "Some comments",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
worker, res1 = tuple(create_1.items())[0]
assert res1["failed"] == False, "Allocation failed"
assert res1["result"]["address"], "No ip allocated"
|
Test Create Ip Non Existing Device
Source code in tests\test_netbox_service.py
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418 | def test_create_ip_non_existing_device(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "does_not_exist",
"interface": "eth1",
"description": "foo1",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
worker, res1 = tuple(create_1.items())[0]
assert res1["failed"] == True, "Allocation should have failed"
|
Test Create Ip Non Existing Interface
Source code in tests\test_netbox_service.py
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441 | def test_create_ip_non_existing_interface(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fce0s4",
"interface": "does_not_exist",
"description": "foo1",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
worker, res1 = tuple(create_1.items())[0]
assert res1["failed"] == True, "Allocation should have failed"
|
Test Create Ip Is Primary
Source code in tests\test_netbox_service.py
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468 | def test_create_ip_is_primary(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_ips("10.0.0.0/24", nfclient)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "ceos-spine-1",
"interface": "Ethernet1",
"description": "foo1",
"is_primary": True,
},
)
print("create_1")
pprint.pprint(create_1, width=200)
worker, res1 = tuple(create_1.items())[0]
assert res1["failed"] == False, "Allocation failed"
assert res1["result"]["address"], "No ip allocated"
|
Test Create Ip Dry Run New Ip
Source code in tests\test_netbox_service.py
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511 | def test_create_ip_dry_run_new_ip(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_ips("10.0.0.0/24", nfclient)
if self.nb_version[0] == 4:
# test dry run for new ip
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "ceos-spine-1",
"interface": "Ethernet1",
"dry_run": True,
},
)
create_2 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={"prefix": "10.0.0.0/24", "description": "foo", "dry_run": True},
)
print("create_1")
pprint.pprint(create_1, width=200)
print("create_2")
pprint.pprint(create_2, width=200)
worker, res1 = tuple(create_1.items())[0]
worker, res2 = tuple(create_2.items())[0]
assert res1["failed"] == False, "Allocation failed"
assert res1["result"]["address"], "No ip allocated"
assert res1["dry_run"] is True, "No dry run flag set to true"
assert res1["status"] == "unchanged", "Unexpected status"
assert res2["failed"] == False, "Allocation failed"
assert res2["result"]["address"], "No ip allocated"
assert res2["dry_run"] is True, "No dry run flag set to true"
assert res2["status"] == "unchanged", "Unexpected status"
|
Test Create Ip Dry Run Existing Ip
Source code in tests\test_netbox_service.py
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556 | def test_create_ip_dry_run_existing_ip(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_ips("10.0.0.0/24", nfclient)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"description": "foobar",
},
)
create_2 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"description": "foobar",
"role": "anycast",
"dry_run": True,
},
)
print("create_1")
pprint.pprint(create_1, width=200)
print("create_2")
pprint.pprint(create_2, width=200)
worker, res1 = tuple(create_1.items())[0]
worker, res2 = tuple(create_2.items())[0]
assert res1["failed"] == False, "Allocation failed"
assert res1["result"]["address"], "No ip allocated"
assert res1["dry_run"] is False, "No dry run flag set to true"
assert res1["status"] == "created", "Unexpected status"
assert res2["failed"] == False, "Allocation failed"
assert res2["result"]["address"], "No ip allocated"
assert res2["dry_run"] is True, "No dry run flag set to true"
assert res2["status"] == "unchanged", "Unexpected status"
|
Test Create Ip With Nb Instance
Source code in tests\test_netbox_service.py
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584 | def test_create_ip_with_nb_instance(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_ips("10.0.0.0/24", nfclient)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"description": "foobar",
"instance": "dev",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
worker, res1 = tuple(create_1.items())[0]
assert res1["failed"] == False, "Allocation failed"
assert res1["result"]["address"], "No ip allocated"
assert res1["dry_run"] is False, "No dry run flag set to true"
assert res1["status"] == "created", "Unexpected status"
assert "dev" in res1["resources"]
|
Test Create Ip With Branch
Source code in tests\test_netbox_service.py
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620 | def test_create_ip_with_branch(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_branch("create_ip_1", nfclient)
delete_ips("10.0.0.0/24", nfclient)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fceos4",
"interface": "eth1",
"description": "foo1",
"vrf": "VRF1",
"tags": ["NORFAB", "ACCESS"],
"tenant": "NORFAB",
"dns_name": "foo1.lab.local",
"role": "anycast",
"comments": "Some comments",
"branch": "create_ip_1",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
worker, res1 = tuple(create_1.items())[0]
assert res1["failed"] == False, "Allocation failed"
assert res1["result"]["address"], "No ip allocated"
assert (
res1["result"]["branch"] == "create_ip_1"
), "No branch info in results"
|
Test Create Ip With Mask Len
Source code in tests\test_netbox_service.py
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639 | def test_create_ip_with_mask_len(self, nfclient):
delete_prefixes_within("10.0.0.0/24", nfclient)
delete_ips("10.0.0.0/24", nfclient)
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fceos4",
"interface": "Port-Channel1",
"mask_len": 31,
},
)
print("create_1")
pprint.pprint(create_1, width=200)
worker, res1 = tuple(create_1.items())[0]
assert res1["result"]["address"] == "10.0.0.0/31", "Wrong ip allocated"
|
Test Create Ip With Mask Len Dry Run
Source code in tests\test_netbox_service.py
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660 | def test_create_ip_with_mask_len_dry_run(self, nfclient):
delete_prefixes_within("10.0.0.0/24", nfclient)
delete_ips("10.0.0.0/24", nfclient)
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fceos4",
"interface": "Port-Channel1",
"mask_len": 31,
"dry_run": True,
},
)
print("create_1")
pprint.pprint(create_1, width=200)
worker, res1 = tuple(create_1.items())[0]
# dry run will allocate first ip within /24 as opposed to /31
assert res1["result"]["address"] == "10.0.0.1/24", "Wrong ip allocated"
|
Test Create Ip Check Create Peer Ip
Source code in tests\test_netbox_service.py
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690 | def test_create_ip_check_create_peer_ip(self, nfclient):
delete_prefixes_within("10.0.0.0/24", nfclient)
delete_ips("10.0.0.0/24", nfclient)
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fceos4",
"interface": "Port-Channel1.101",
"mask_len": 31,
},
)
print("create_1")
pprint.pprint(create_1, width=200)
worker, res1 = tuple(create_1.items())[0]
assert res1["result"]["address"] == "10.0.0.0/31", "Wrong ip allocated"
assert (
res1["result"]["peer"]["address"] == "10.0.0.1/31"
), "Wrong ip allocated for peer"
assert (
res1["result"]["peer"]["device"] == "fceos5"
), "Wrong ip allocated for peer"
assert (
res1["result"]["peer"]["interface"] == "ae5.101"
), "Wrong ip allocated for peer"
|
Test Create Ip Check Create Peer Ip With Branch
Source code in tests\test_netbox_service.py
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723 | def test_create_ip_check_create_peer_ip_with_branch(self, nfclient):
delete_prefixes_within("10.0.0.0/24", nfclient)
delete_ips("10.0.0.0/24", nfclient)
delete_branch("create_ip_with_peer", nfclient)
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fceos4",
"interface": "Port-Channel1.101",
"mask_len": 31,
"branch": "create_ip_with_peer",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
worker, res1 = tuple(create_1.items())[0]
assert res1["result"]["address"] == "10.0.0.0/31", "Wrong ip allocated"
assert res1["result"]["branch"] == "create_ip_with_peer", "Wrong branch"
assert (
res1["result"]["peer"]["address"] == "10.0.0.1/31"
), "Wrong ip allocated for peer"
assert (
res1["result"]["peer"]["device"] == "fceos5"
), "Wrong ip allocated for peer"
assert (
res1["result"]["peer"]["interface"] == "ae5.101"
), "Wrong ip allocated for peer"
|
Test Create Ip Check Skip Create Peer Ip
Source code in tests\test_netbox_service.py
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748 | def test_create_ip_check_skip_create_peer_ip(self, nfclient):
delete_prefixes_within("10.0.0.0/24", nfclient)
delete_ips("10.0.0.0/24", nfclient)
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fceos4",
"interface": "Port-Channel1.101",
"mask_len": 31,
"create_peer_ip": False,
},
)
print("create_1")
pprint.pprint(create_1, width=200)
worker, res1 = tuple(create_1.items())[0]
assert res1["result"]["address"] == "10.0.0.0/31", "Wrong ip allocated"
assert (
"peer" not in res1["result"]
), "SHould have been skipping peer ip creation"
|
Test Create Ip Use Peer Ip
Source code in tests\test_netbox_service.py
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784 | def test_create_ip_use_peer_ip(self, nfclient):
delete_prefixes_within("10.0.0.0/24", nfclient)
delete_ips("10.0.0.0/24", nfclient)
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fceos4",
"interface": "Port-Channel1.101",
"mask_len": 31,
"create_peer_ip": False,
},
)
create_2 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fceos5",
"interface": "ae5.101",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
print("create_2")
pprint.pprint(create_2, width=200)
worker, res1 = tuple(create_1.items())[0]
worker, res2 = tuple(create_2.items())[0]
assert res1["result"]["address"] == "10.0.0.0/31", "Wrong ip allocated"
assert res2["result"]["address"] == "10.0.0.1/31", "Wrong ip allocated"
|
Test Create Ip With Link Peer Dry Run
Source code in tests\test_netbox_service.py
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820 | def test_create_ip_with_link_peer_dry_run(self, nfclient):
delete_prefixes_within("10.0.0.0/24", nfclient)
delete_ips("10.0.0.0/24", nfclient)
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fceos4",
"interface": "Port-Channel1.101",
"mask_len": 31,
},
)
create_2 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fceos5",
"interface": "ae5.101",
"dry_run": True,
},
)
print("create_1")
pprint.pprint(create_1, width=200)
print("create_2")
pprint.pprint(create_2, width=200)
worker, res1 = tuple(create_1.items())[0]
worker, res2 = tuple(create_2.items())[0]
assert res1["result"]["address"] == "10.0.0.0/31", "Wrong ip allocated"
assert res2["result"]["address"] == "10.0.0.1/31", "Wrong ip allocated"
|
Test Create Ip With Link Peer Within Parent
Source code in tests\test_netbox_service.py
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854 | def test_create_ip_with_link_peer_within_parent(self, nfclient):
delete_prefixes_within("10.0.0.0/24", nfclient)
delete_ips("10.0.0.0/24", nfclient)
create_1 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fceos4",
"interface": "Port-Channel1.101",
},
)
create_2 = nfclient.run_job(
"netbox",
"create_ip",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"device": "fceos5",
"interface": "ae5.101",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
print("create_2")
pprint.pprint(create_2, width=200)
worker, res1 = tuple(create_1.items())[0]
worker, res2 = tuple(create_2.items())[0]
assert res1["result"]["address"] == "10.0.0.1/24", "Wrong ip allocated"
assert res2["result"]["address"] == "10.0.0.2/24", "Wrong ip allocated"
|
TestNetboxCache
Caching functionality and cache management.
Purpose: Validate cache operations for improved query performance.
Test Cache List
Source code in tests\test_netbox_service.py
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885 | def test_cache_list(self, nfclient):
# populate the cache
ret = nfclient.run_job(
"netbox",
"get_devices",
workers="all",
kwargs={"cache": True, "devices": ["ceos1", "fceos4", "fceos5"]},
)
# verify cache is present
ret = nfclient.run_job(
"netbox",
"cache_list",
workers="all",
kwargs={},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} - cache operation failed"
assert len(res["result"]) > 0, f"{worker} - cache is empty"
assert isinstance(
res["result"], list
), f"{worker} - cache list result is not a list"
assert "get_devices::fceos5" in res["result"]
assert "get_devices::ceos1" in res["result"]
assert "get_devices::fceos4" in res["result"]
|
Test Cache List Details
Source code in tests\test_netbox_service.py
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915 | def test_cache_list_details(self, nfclient):
# populate the cache
ret = nfclient.run_job(
"netbox",
"get_devices",
workers="all",
kwargs={"cache": True, "devices": ["ceos1", "fceos4", "fceos5"]},
)
# verify cache is present
ret = nfclient.run_job(
"netbox",
"cache_list",
workers="all",
kwargs={"details": True},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} - cache operation failed"
assert len(res["result"]) > 0, f"{worker} - cache is empty"
assert isinstance(
res["result"], list
), f"{worker} - cache list result is not a list"
for item in res["result"]:
assert all(
key in item for key in ["age", "creation", "expires", "key"]
), f"{worker} - not all cache list data details returned"
|
Test Cache List Filter
Source code in tests\test_netbox_service.py
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945 | def test_cache_list_filter(self, nfclient):
# populate the cache
ret = nfclient.run_job(
"netbox",
"get_devices",
workers="all",
kwargs={"cache": True, "devices": ["ceos1", "fceos4", "fceos5"]},
)
# verify cache is present
ret = nfclient.run_job(
"netbox",
"cache_list",
workers="all",
kwargs={"keys": "*ceos1*"},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} - cache operation failed"
assert len(res["result"]) > 0, f"{worker} - cache is empty"
assert isinstance(
res["result"], list
), f"{worker} - cache list result is not a list"
for key in res["result"]:
assert (
"ceos1" in key
), f"{worker} - key '{key}' does not contain 'ceos1' pattern "
|
Test Cache Clear All
Source code in tests\test_netbox_service.py
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997 | def test_cache_clear_all(self, nfclient):
# populate the cache
ret_populate = nfclient.run_job(
"netbox",
"get_devices",
workers="all",
kwargs={"cache": True, "devices": ["ceos1", "fceos4", "fceos5"]},
)
# clear cache
ret_clear = nfclient.run_job(
"netbox",
"cache_clear",
workers="all",
kwargs={"keys": "*"},
)
# list cache
ret_list = nfclient.run_job(
"netbox",
"cache_list",
workers="all",
kwargs={"keys": "*"},
)
print("\nret_populate:")
pprint.pprint(ret_populate, width=150)
print("\nret_clear:")
pprint.pprint(ret_clear, width=150)
print("\nret_list:")
pprint.pprint(ret_list, width=150)
for worker, res in ret_populate.items():
assert (
res["failed"] == False
), f"{worker} - get_devices populate operation failed"
for worker, res in ret_clear.items():
assert res["failed"] == False, f"{worker} - cache clear operation failed"
assert (
len(res["result"]) > 0
), f"{worker} - did not return list of cleared keys"
assert isinstance(
res["result"], list
), f"{worker} - cache clear result is not a list"
for worker, res in ret_list.items():
assert res["failed"] == False, f"{worker} - cache list operation failed"
assert len(res["result"]) == 0, f"{worker} - cache is not empty"
|
Test Cache Clear Key
Source code in tests\test_netbox_service.py
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070 | def test_cache_clear_key(self, nfclient):
# populate the cache
ret_populate = nfclient.run_job(
"netbox",
"get_devices",
workers="all",
kwargs={"cache": True, "devices": ["ceos1", "fceos4", "fceos5"]},
)
# list cache
ret_list_before = nfclient.run_job(
"netbox",
"cache_list",
workers="all",
kwargs={"keys": "*"},
)
# clear cache
ret_clear = nfclient.run_job(
"netbox",
"cache_clear",
workers="all",
kwargs={"key": "get_devices::ceos1"},
)
# list cache
ret_list_after = nfclient.run_job(
"netbox",
"cache_list",
workers="all",
kwargs={"keys": "*"},
)
print("\nret_populate:")
pprint.pprint(ret_populate, width=150)
print("\nret_list_before:")
pprint.pprint(ret_list_before, width=150)
print("\nret_clear:")
pprint.pprint(ret_clear, width=150)
print("\nret_list_after:")
pprint.pprint(ret_list_after, width=150)
for worker, res in ret_populate.items():
assert (
res["failed"] == False
), f"{worker} - get_devices populate operation failed"
for worker, res in ret_list_before.items():
assert res["failed"] == False, f"{worker} - cache list operation failed"
assert len(res["result"]) > 0, f"{worker} - cache is empty"
assert (
"get_devices::ceos1" in res["result"]
), f"{worker} - cache does not have get_devices::ceos1 key"
for worker, res in ret_clear.items():
assert res["failed"] == False, f"{worker} - cache clear operation failed"
assert (
len(res["result"]) > 0
), f"{worker} - did not return list of cleared keys"
assert isinstance(
res["result"], list
), f"{worker} - cache clear result is not a list"
for worker, res in ret_list_after.items():
assert res["failed"] == False, f"{worker} - cache list operation failed"
assert len(res["result"]) > 0, f"{worker} - cache is empty"
assert (
"get_devices::ceos1" not in res["result"]
), f"{worker} - cache still has get_devices::ceos1 key"
|
Test Cache Get Key
Source code in tests\test_netbox_service.py
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099 | def test_cache_get_key(self, nfclient):
# populate the cache
ret = nfclient.run_job(
"netbox",
"get_devices",
workers="all",
kwargs={"cache": True, "devices": ["ceos1", "fceos4", "fceos5"]},
)
# verify cache is present
ret = nfclient.run_job(
"netbox",
"cache_get",
workers="all",
kwargs={"key": "get_devices::ceos1"},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} - cache operation failed"
assert len(res["result"]) > 0, f"{worker} - cache is empty"
assert isinstance(
res["result"], dict
), f"{worker} - cache get result is not a dict"
assert res["result"][
"get_devices::ceos1"
], f"{worker} - cache get result key data is empty"
|
Test Cache Get Keys
Source code in tests\test_netbox_service.py
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129 | def test_cache_get_keys(self, nfclient):
# populate the cache
ret = nfclient.run_job(
"netbox",
"get_devices",
workers="all",
kwargs={"cache": True, "devices": ["ceos1", "fceos4", "fceos5"]},
)
# verify cache is present
ret = nfclient.run_job(
"netbox",
"cache_get",
workers="all",
kwargs={"keys": "*ceos1*"},
)
pprint.pprint(ret, width=200)
for worker, res in ret.items():
assert res["failed"] == False, f"{worker} - cache operation failed"
assert len(res["result"]) > 0, f"{worker} - cache is empty"
assert isinstance(
res["result"], dict
), f"{worker} - cache get result is not a dict"
for key in res["result"].keys():
assert (
"ceos1" in key
), f"{worker} - cache key '{key}' does not contain ceos1 pattern"
|
Test Cache False
Source code in tests\test_netbox_service.py
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176 | def test_cache_false(self, nfclient):
# clear cache
ret_clear = nfclient.run_job(
"netbox",
"cache_clear",
workers="all",
kwargs={"keys": "*"},
)
# query data with cache set to False
ret_query = nfclient.run_job(
"netbox",
"get_devices",
workers="all",
kwargs={"cache": False, "devices": ["ceos1", "fceos4", "fceos5"]},
)
# verify cache is empty
ret_list = nfclient.run_job(
"netbox",
"cache_list",
workers="all",
kwargs={"keys": "*"},
)
print("\nret_clear:")
pprint.pprint(ret_clear, width=150)
print("\nret_query:")
pprint.pprint(ret_query, width=150)
print("\nret_list:")
pprint.pprint(ret_list, width=150)
for worker, res in ret_clear.items():
assert res["failed"] == False, f"{worker} - cache clear operation failed"
for worker, res in ret_query.items():
assert res["failed"] == False, f"{worker} - query netbox operation failed"
for worker, res in ret_list.items():
assert res["failed"] == False, f"{worker} - cache list operation failed"
assert isinstance(
res["result"], list
), f"{worker} - cache get result is not a dict"
assert len(res["result"]) == 0, f"{worker} - cache is not empty"
|
Test Cache Refresh
Source code in tests\test_netbox_service.py
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257 | def test_cache_refresh(self, nfclient):
# clear cache
ret_clear = nfclient.run_job(
"netbox",
"cache_clear",
workers="all",
kwargs={"keys": "*"},
)
# query data with cache set to True
ret_query_true = nfclient.run_job(
"netbox",
"get_devices",
workers="all",
kwargs={"cache": True, "devices": ["ceos1", "fceos4", "fceos5"]},
)
# get cache creation time
ret_list_1st = nfclient.run_job(
"netbox",
"cache_list",
workers="all",
kwargs={"keys": "*", "details": True},
)
# query data with cache set to refresh
ret_query_refresh = nfclient.run_job(
"netbox",
"get_devices",
workers="all",
kwargs={"cache": "refresh", "devices": ["ceos1", "fceos4", "fceos5"]},
)
# get cache creation time
ret_list_2nd = nfclient.run_job(
"netbox",
"cache_list",
workers="all",
kwargs={"keys": "*", "details": True},
)
print("\nret_clear:")
pprint.pprint(ret_clear, width=150)
print("\nret_query_true:")
pprint.pprint(ret_query_true, width=150)
print("\nret_list_1st:")
pprint.pprint(ret_list_1st, width=150)
print("\nret_query_refresh:")
pprint.pprint(ret_query_refresh, width=150)
print("\nret_list_2nd:")
pprint.pprint(ret_list_2nd, width=150)
# verify no errors
for worker, res in ret_clear.items():
assert res["failed"] == False, f"{worker} - cache clear operation failed"
for worker, res in ret_query_true.items():
assert res["failed"] == False, f"{worker} - query netbox operation failed"
for worker, res in ret_list_1st.items():
assert res["failed"] == False, f"{worker} - ret_list_1st operation failed"
for worker, res in ret_query_refresh.items():
assert (
res["failed"] == False
), f"{worker} - ret_query_refresh operation failed"
for worker, res in ret_list_2nd.items():
assert res["failed"] == False, f"{worker} - ret_list_2nd operation failed"
# compare 2nd list items expiration time is after the 1st one
for worker_2nd, res_2nd in ret_list_2nd.items():
for item_2nd in res_2nd["result"]:
for item_1st in ret_list_1st[worker_2nd]["result"]:
if item_2nd["key"] == item_1st["key"]:
assert item_2nd["expires"] > item_1st["expires"]
|
TestGetContainerlabInventory
Containerlab topology generation from Netbox.
Purpose: Validate containerlab topology file generation from Netbox inventory.
Test Get Containerlab Inventory Devices
Source code in tests\test_netbox_service.py
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288 | def test_get_containerlab_inventory_devices(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_containerlab_inventory",
workers="any",
kwargs={
"devices": ["fceos4", "fceos5"],
"lab_name": "foobar",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert not res["errors"], f"{worker} - received error"
assert all(
k in res["result"] for k in ["mgmt", "name", "topology"]
), f"{worker} - not all clab inventory data returned"
assert res["result"]["topology"][
"links"
], f"{worker} - no clab inventory links data returned"
assert res["result"]["topology"][
"nodes"
], f"{worker} - no clab inventory nodes data returned"
assert (
res["result"]["name"] == "foobar"
), f"{worker} - clab inventory name is not foobar"
|
Test Get Containerlab Inventory Non Existing Devices
Source code in tests\test_netbox_service.py
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313 | def test_get_containerlab_inventory_non_existing_devices(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_containerlab_inventory",
workers="any",
kwargs={
"devices": ["fceosabc", "fceosxyz"],
"lab_name": "foobar",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["errors"], f"{worker} - received no error"
assert res["failed"] == True, f"{worker} - should have failed"
assert all(
k in res["result"] for k in ["mgmt", "name", "topology"]
), f"{worker} - not all clab inventory data returned"
assert (
res["result"]["topology"]["links"] == []
), f"{worker} - clab inventory links data returned"
assert (
res["result"]["topology"]["nodes"] == {}
), f"{worker} - clab inventory nodes data returned"
|
Test Get Containerlab Inventory By Tenant
Source code in tests\test_netbox_service.py
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354 | def test_get_containerlab_inventory_by_tenant(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
if self.nb_version >= (4, 3, 0):
ret = nfclient.run_job(
"netbox",
"get_containerlab_inventory",
workers="any",
kwargs={
"tenant": "NORFAB",
"lab_name": "foobar",
},
)
else:
ret = nfclient.run_job(
"netbox",
"get_containerlab_inventory",
workers="any",
kwargs={
"tenant": "norfab",
"lab_name": "foobar",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert not res["errors"], f"{worker} - received error"
assert all(
k in res["result"] for k in ["mgmt", "name", "topology"]
), f"{worker} - not all clab inventory data returned"
assert res["result"]["topology"][
"links"
], f"{worker} - no clab inventory links data returned"
assert res["result"]["topology"][
"nodes"
], f"{worker} - no clab inventory nodes data returned"
assert (
res["result"]["name"] == "foobar"
), f"{worker} - clab inventory name is not foobar"
|
Test Get Containerlab Inventory By Filters
Source code in tests\test_netbox_service.py
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395 | def test_get_containerlab_inventory_by_filters(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
if self.nb_version >= (4, 3, 0):
ret = nfclient.run_job(
"netbox",
"get_containerlab_inventory",
workers="any",
kwargs={
"filters": [{"name__ic": "ceos-spine", "status": "active"}],
"lab_name": "foobar",
},
)
else:
ret = nfclient.run_job(
"netbox",
"get_containerlab_inventory",
workers="any",
kwargs={
"filters": [{"q": "ceos-spine", "status": "active"}],
"lab_name": "foobar",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert not res["errors"], f"{worker} - received error"
assert all(
k in res["result"] for k in ["mgmt", "name", "topology"]
), f"{worker} - not all clab inventory data returned"
assert res["result"]["topology"][
"links"
], f"{worker} - no clab inventory links data returned"
assert (
len(res["result"]["topology"]["nodes"]) == 2
), f"{worker} - clab inventory nodes data wromg"
assert (
res["result"]["name"] == "foobar"
), f"{worker} - clab inventory name is not foobar"
|
Test Get Containerlab Inventory With Nb Instance
Source code in tests\test_netbox_service.py
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423 | def test_get_containerlab_inventory_with_nb_instance(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_containerlab_inventory",
workers="any",
kwargs={
"devices": ["fceos4", "fceos5"],
"lab_name": "foobar",
"instance": "preprod",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert not res["errors"], f"{worker} - received error"
assert all(
k in res["result"] for k in ["mgmt", "name", "topology"]
), f"{worker} - not all clab inventory data returned"
assert res["result"]["topology"][
"links"
], f"{worker} - no clab inventory links data returned"
assert res["result"]["topology"][
"nodes"
], f"{worker} - no clab inventory nodes data returned"
assert (
res["result"]["name"] == "foobar"
), f"{worker} - clab inventory name is not foobar"
|
Test Get Containerlab Inventory With Image
Source code in tests\test_netbox_service.py
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449 | def test_get_containerlab_inventory_with_image(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_containerlab_inventory",
workers="any",
kwargs={
"devices": ["ceos1"],
"lab_name": "foobar",
"image": "ceos:latest",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert not res["errors"], f"{worker} - received error"
assert all(
k in res["result"] for k in ["mgmt", "name", "topology"]
), f"{worker} - not all clab inventory data returned"
assert res["result"]["topology"][
"nodes"
], f"{worker} - no clab inventory nodes data returned"
for node_name, node_data in res["result"]["topology"]["nodes"].items():
assert (
node_data["image"] == "ceos:latest"
), f"{worker} - node {node_name} image is not ceos:latest"
|
Test Get Containerlab Inventory Run Out Of Ports
Source code in tests\test_netbox_service.py
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466 | def test_get_containerlab_inventory_run_out_of_ports(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_containerlab_inventory",
workers="any",
kwargs={
"devices": ["fceos4", "fceos5"],
"lab_name": "foobar",
"ports": [10000, 10005],
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["errors"], f"{worker} - received no error"
assert res["failed"] == True, f"{worker} - should have failed"
|
Test Get Containerlab Inventory Run Out Of Ips
Source code in tests\test_netbox_service.py
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483 | def test_get_containerlab_inventory_run_out_of_ips(self, nfclient):
ret = nfclient.run_job(
"netbox",
"get_containerlab_inventory",
workers="any",
kwargs={
"devices": ["fceos4", "fceos5", "ceos1"],
"lab_name": "foobar",
"ipv4_subnet": "172.100.100.0/30",
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert res["errors"], f"{worker} - received no error"
assert res["failed"] == True, f"{worker} - should have failed"
|
Test Get Containerlab Inventory With Ports Map
Source code in tests\test_netbox_service.py
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527 | def test_get_containerlab_inventory_with_ports_map(self, nfclient):
ports_map = {
"fceos4": [
"10007:22/tcp",
"10008:23/tcp",
"10009:80/tcp",
"10010:161/udp",
"10011:443/tcp",
"10012:830/tcp",
"10013:8080/tcp",
]
}
ret = nfclient.run_job(
"netbox",
"get_containerlab_inventory",
workers="any",
kwargs={
"devices": ["fceos4", "fceos5"],
"lab_name": "foobar",
"ports_map": ports_map,
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert not res["errors"], f"{worker} - received error"
assert all(
k in res["result"] for k in ["mgmt", "name", "topology"]
), f"{worker} - not all clab inventory data returned"
assert res["result"]["topology"][
"nodes"
], f"{worker} - no clab inventory nodes data returned"
assert (
res["result"]["topology"]["nodes"]["fceos4"]["ports"]
== ports_map["fceos4"]
), f"{worker} - ports map not applied for fceos4"
assert res["result"]["topology"]["nodes"]["fceos5"][
"ports"
], f"{worker} - no ports allocated for fceos5"
assert all(
p.startswith("1200")
for p in res["result"]["topology"]["nodes"]["fceos5"]["ports"]
), f"{worker} - ports for fceos5 allocated correctly"
|
Test Get Containerlab Inventory With Cache
Source code in tests\test_netbox_service.py
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556 | @pytest.mark.parametrize("cache", cache_options)
def test_get_containerlab_inventory_with_cache(self, nfclient, cache):
ret = nfclient.run_job(
"netbox",
"get_containerlab_inventory",
workers="any",
kwargs={
"devices": ["fceos4", "fceos5"],
"lab_name": "foobar",
"cache": cache,
},
)
pprint.pprint(ret)
for worker, res in ret.items():
assert not res["errors"], f"{worker} - received error"
assert all(
k in res["result"] for k in ["mgmt", "name", "topology"]
), f"{worker} - not all clab inventory data returned"
assert res["result"]["topology"][
"links"
], f"{worker} - no clab inventory links data returned"
assert res["result"]["topology"][
"nodes"
], f"{worker} - no clab inventory nodes data returned"
assert (
res["result"]["name"] == "foobar"
), f"{worker} - clab inventory name is not foobar"
|
TestCreatePrefix
IP prefix creation and management.
Purpose: Validate IP prefix allocation and creation.
Test Create Prefix
Source code in tests\test_netbox_service.py
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607 | def test_create_prefix(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_prefixes_within("10.1.0.0/24", nfclient)
rand = random.randint(1, 1000)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.1.0.0/24",
"prefixlen": 30,
"description": f"test create prefix {rand}",
},
)
create_2 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.1.0.0/24",
"prefixlen": 30,
"description": f"test create prefix {rand}",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
print("create_2")
pprint.pprint(create_2, width=200)
for worker, res1 in create_1.items():
assert res1["failed"] == False, "Allocation failed"
assert res1["result"][
"prefix"
], f"Result has no prefix {res1['result']}"
worker, res2 = tuple(create_2.items())[0]
assert (
res1["result"]["prefix"] == res2["result"]["prefix"]
), "Should have been same prefix"
assert (
res1["result"]["description"] == res2["result"]["description"]
), "Should have been same prefix description"
|
Test Create Prefix Multiple
Source code in tests\test_netbox_service.py
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650 | def test_create_prefix_multiple(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_prefixes_within("10.1.0.0/24", nfclient)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.1.0.0/24",
"prefixlen": 30,
"description": f"test create prefix {random.randint(1, 100)}",
},
)
create_2 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.1.0.0/24",
"prefixlen": 30,
"description": f"test create prefix {random.randint(200, 300)}",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
print("create_2")
pprint.pprint(create_2, width=200)
for worker, res1 in create_1.items():
assert res1["failed"] == False, "Allocation failed"
assert res1["result"][
"prefix"
], f"Result has no prefix {res1['result']}"
worker, res2 = tuple(create_2.items())[0]
assert (
res1["result"]["prefix"] != res2["result"]["prefix"]
), "Should have been different prefix"
|
Test Create Prefix Non Exist Parent
Source code in tests\test_netbox_service.py
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674 | def test_create_prefix_non_exist_parent(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.1.123.0/24",
"prefixlen": 30,
"description": f"test create prefix {random.randint(1, 100)}",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
for worker, res1 in create_1.items():
assert res1["failed"] == True, "Allocation not failed"
assert (
"Unable to source parent prefix from Netbox" in res1["messages"][0]
), "Result has no errors"
|
Test Create Prefix With Vrf
Should create single prefix and handle deduplication within vrf
Source code in tests\test_netbox_service.py
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739 | def test_create_prefix_with_vrf(self, nfclient):
"""Should create single prefix and handle deduplication within vrf"""
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_prefixes_within("10.2.0.0/24", nfclient)
rand = random.randint(1, 1000)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.2.0.0/24",
"prefixlen": 30,
"description": f"test create prefix {rand}",
"vrf": "VRF1",
},
)
create_2 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.2.0.0/24",
"prefixlen": 30,
"description": f"test create prefix {rand}",
"vrf": "VRF1",
},
)
create_3 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.2.0.0/24",
"prefixlen": 30,
"description": f"test create prefix {rand+1}",
"vrf": "VRF1",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
print("create_2")
pprint.pprint(create_2, width=200)
print("create_3")
pprint.pprint(create_3, width=200)
for worker, res1 in create_1.items():
assert res1["failed"] == False, "Allocation failed"
assert res1["result"][
"prefix"
], f"Result has no prefix {res1['result']}"
worker, res1 = tuple(create_1.items())[0]
worker, res2 = tuple(create_2.items())[0]
worker, res3 = tuple(create_3.items())[0]
assert (
res1["result"]["prefix"] == res2["result"]["prefix"]
), "Should have been same prefix"
assert (
res1["result"]["prefix"] != res3["result"]["prefix"]
), "Should have been different prefix"
|
Test Create Prefix With Parent Vrf Mismatch
Source code in tests\test_netbox_service.py
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765 | def test_create_prefix_with_parent_vrf_mismatch(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_prefixes_within("10.1.0.0/24", nfclient)
rand = random.randint(1, 1000)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.1.0.0/24",
"prefixlen": 30,
"description": f"test create prefix {rand}",
"vrf": "VRF1",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
for worker, res1 in create_1.items():
assert res1["failed"] == True, "Allocation not failed"
assert "NetboxAllocationError" in res1["errors"][0]
|
Test Create Prefix By Parent Prefix Name
Source code in tests\test_netbox_service.py
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812 | def test_create_prefix_by_parent_prefix_name(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_prefixes_within("10.1.0.0/24", nfclient)
rand = random.randint(1, 1000)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "TEST CREATE PREFIXES",
"prefixlen": 30,
"description": f"test create prefix {rand}",
},
)
create_2 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "TEST CREATE PREFIXES",
"prefixlen": 30,
"description": f"test create prefix {rand}",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
print("create_2")
pprint.pprint(create_2, width=200)
for worker, res1 in create_1.items():
assert res1["failed"] == False, "Allocation failed"
assert res1["result"][
"prefix"
], f"Result has no prefix {res1['result']}"
worker, res2 = tuple(create_2.items())[0]
assert (
res1["result"]["prefix"] == res2["result"]["prefix"]
), "Should have been same prefix"
assert (
res1["result"]["description"] == res2["result"]["description"]
), "Should have been same prefix description"
|
Test Create Prefix Within Vrf By Parent Prefix Name
Source code in tests\test_netbox_service.py
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927 | def test_create_prefix_within_vrf_by_parent_prefix_name(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_prefixes_within("10.2.0.0/24", nfclient)
rand = random.randint(1, 1000)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "TEST CREATE PREFIXES WITH VRF",
"prefixlen": 30,
"description": f"test create prefix {rand}",
"vrf": "VRF1",
},
)
create_2 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "TEST CREATE PREFIXES WITH VRF",
"prefixlen": 30,
"description": f"test create prefix {rand}",
"vrf": "VRF1",
},
)
create_3 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "TEST CREATE PREFIXES WITH VRF",
"prefixlen": 30,
"description": f"test create prefix {rand+1}",
"vrf": "VRF1",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
print("create_2")
pprint.pprint(create_2, width=200)
print("create_3")
pprint.pprint(create_3, width=200)
for worker, res1 in create_1.items():
assert res1["failed"] == False, "Allocation failed"
assert res1["result"][
"prefix"
], f"Result has no prefix {res1['result']}"
worker, res1 = tuple(create_1.items())[0]
worker, res2 = tuple(create_2.items())[0]
worker, res3 = tuple(create_3.items())[0]
assert (
res1["result"]["prefix"] == res2["result"]["prefix"]
), "Should have been same prefix"
assert (
res1["result"]["description"] == res2["result"]["description"]
), "Should have been same prefix description"
assert (
res1["result"]["prefix"] != res3["result"]["prefix"]
), "Should have been different prefix"
|
Test Create Prefix Dry Run Empty Parent
Source code in tests\test_netbox_service.py
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955 | def test_create_prefix_dry_run_empty_parent(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_prefixes_within("10.1.0.0/24", nfclient)
rand = random.randint(1, 1000)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.1.0.0/24",
"prefixlen": 30,
"description": f"test create prefix {rand}",
"dry_run": True,
},
)
print("create_1")
pprint.pprint(create_1, width=200)
for worker, res1 in create_1.items():
assert res1["failed"] == False, "Allocation failed"
assert res1["dry_run"] == True
assert res1["status"] == "unchanged"
assert res1["result"]["prefix"] == "10.1.0.0/30"
|
Test Create Prefix Dry Run Parent Has Children
Source code in tests\test_netbox_service.py
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993 | def test_create_prefix_dry_run_parent_has_children(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_prefixes_within("10.1.0.0/24", nfclient)
rand = random.randint(1, 1000)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.1.0.0/24",
"prefixlen": 31,
"description": f"test create prefix {rand}",
},
)
create_dry_run = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.1.0.0/24",
"prefixlen": 30,
"description": f"test create prefix {rand+1}",
"dry_run": True,
},
)
print("create_dry_run")
pprint.pprint(create_dry_run, width=200)
for worker, res1 in create_dry_run.items():
assert res1["failed"] == False, "Allocation failed"
assert res1["dry_run"] == True
assert res1["status"] == "unchanged"
assert res1["result"]["prefix"] == "10.1.0.4/30"
|
Test Create Prefix Dry Run Prefix Exists
Source code in tests\test_netbox_service.py
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031 | def test_create_prefix_dry_run_prefix_exists(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_prefixes_within("10.1.0.0/24", nfclient)
rand = random.randint(1, 1000)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.1.0.0/24",
"prefixlen": 30,
"description": f"test create prefix {rand}",
},
)
create_dry_run = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.1.0.0/24",
"prefixlen": 30,
"description": f"test create prefix {rand}",
"dry_run": True,
},
)
print("create_dry_run")
pprint.pprint(create_dry_run, width=200)
for worker, res1 in create_dry_run.items():
assert res1["failed"] == False, "Allocation failed"
assert res1["dry_run"] == True
assert res1["status"] == "unchanged"
assert res1["result"]["prefix"] == "10.1.0.0/30"
|
Test Create Prefix Test Length Mismatch
We creating first prefix, next creating prefix with same
description but different prefix length
Source code in tests\test_netbox_service.py
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070 | def test_create_prefix_test_length_mismatch(self, nfclient):
"""We creating first prefix, next creating prefix with same
description but different prefix length"""
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_prefixes_within("10.1.0.0/24", nfclient)
rand = random.randint(1, 1000)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.1.0.0/24",
"prefixlen": 31,
"description": f"test create prefix {rand}",
},
)
create_2 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.1.0.0/24",
"prefixlen": 30,
"description": f"test create prefix {rand}",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
print("create_2")
pprint.pprint(create_2, width=200)
for worker, res in create_2.items():
assert res["failed"] == True, "Allocation not failed"
assert "NetboxAllocationError" in res["errors"][0]
|
Test Create Prefix With Attributes
Source code in tests\test_netbox_service.py
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137 | def test_create_prefix_with_attributes(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_prefixes_within("10.2.0.0/24", nfclient)
rand = random.randint(1, 1000)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.2.0.0/24",
"prefixlen": 30,
"description": f"test create prefix {rand}",
"site": "NORFAB-LAB",
"tenant": "NORFAB",
"role": "PREFIX_ROLE_1",
"comments": "Some important comment",
"vrf": "VRF1",
"tags": ["NORFAB"],
"status": "reserved",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
for worker, res1 in create_1.items():
assert res1["failed"] == False, "Allocation failed"
assert all(
k in res1["diff"]
for k in [
"comments",
"description",
"role",
"site",
"status",
"tags",
"tenant",
]
)
# retrieve created prefix details from Netbox
nb_prefix = nfclient.run_job(
"netbox",
"rest",
workers="any",
kwargs={
"method": "get",
"api": "/ipam/prefixes/",
"params": {"prefix": res1["result"]["prefix"]},
},
)
print("nb_prefix:")
pprint.pprint(nb_prefix, width=200)
worker, created_prefix = tuple(nb_prefix.items())[0]
created_prefix = created_prefix["result"]["results"][0]
assert created_prefix["role"]["name"] == "PREFIX_ROLE_1"
assert created_prefix["scope"]["name"] == "NORFAB-LAB"
assert created_prefix["tags"][0]["name"] == "NORFAB"
assert created_prefix["tenant"]["name"] == "NORFAB"
assert created_prefix["vrf"]["name"] == "VRF1"
assert created_prefix["description"]
assert created_prefix["comments"]
|
Test Create Prefix With Attributes Updates
Source code in tests\test_netbox_service.py
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221 | def test_create_prefix_with_attributes_updates(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_prefixes_within("10.2.0.0/24", nfclient)
rand = random.randint(1, 1000)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.2.0.0/24",
"prefixlen": 30,
"description": f"test create prefix {rand}",
"site": "NORFAB-LAB",
"tenant": "NORFAB",
"role": "PREFIX_ROLE_1",
"comments": "Some important comment",
"tags": ["NORFAB"],
"status": "reserved",
},
)
create_2 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.2.0.0/24",
"description": f"test create prefix {rand}",
"site": "SALTNORNIR-LAB",
"tenant": "SALTNORNIR",
"role": "PREFIX_ROLE_2",
"comments": "Some important comments updates",
"tags": ["ACCESS"],
"status": "active",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
print("create_2")
pprint.pprint(create_2, width=200)
# verify has changes
for worker, res1 in create_2.items():
assert res1["failed"] == False, "Allocation failed"
assert all(
k in res1["diff"]
for k in [
"comments",
"role",
"site",
"status",
"tags",
"tenant",
]
)
# retrieve created prefix details from Netbox
nb_prefix = nfclient.run_job(
"netbox",
"rest",
workers="any",
kwargs={
"method": "get",
"api": "/ipam/prefixes/",
"params": {"prefix": res1["result"]["prefix"]},
},
)
print("nb_prefix:")
pprint.pprint(nb_prefix, width=200)
worker, created_prefix = tuple(nb_prefix.items())[0]
created_prefix = created_prefix["result"]["results"][0]
assert created_prefix["role"]["name"] == "PREFIX_ROLE_2"
assert created_prefix["scope"]["name"] == "SALTNORNIR-LAB"
for tag in created_prefix["tags"]:
assert tag["name"] == "NORFAB" or tag["name"] == "ACCESS"
assert created_prefix["tenant"]["name"] == "SALTNORNIR"
assert created_prefix["vrf"]["name"] == "VRF1"
assert created_prefix["description"] == f"test create prefix {rand}"
assert created_prefix["comments"] == "Some important comments updates"
|
Test Create Prefix With Branch
Source code in tests\test_netbox_service.py
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257 | def test_create_prefix_with_branch(self, nfclient):
if self.nb_version is None:
self.nb_version = get_nb_version(nfclient)
delete_branch("create_prefix_1", nfclient)
delete_prefixes_within("10.2.0.0/24", nfclient)
rand = random.randint(1, 1000)
if self.nb_version[0] == 4:
create_1 = nfclient.run_job(
"netbox",
"create_prefix",
workers="any",
kwargs={
"parent": "10.2.0.0/24",
"prefixlen": 30,
"description": f"test create prefix {rand}",
"site": "NORFAB-LAB",
"tenant": "NORFAB",
"role": "PREFIX_ROLE_1",
"comments": "Some important comment",
"vrf": "VRF1",
"tags": ["NORFAB"],
"status": "reserved",
"branch": "create_prefix_1",
},
)
print("create_1")
pprint.pprint(create_1, width=200)
for worker, res1 in create_1.items():
assert res1["failed"] == False, "Allocation with branch failed"
assert (
res1["result"]["branch"] == "create_prefix_1"
), "No branch details in result"
|
TestCreateIPBulk
Bulk IP address creation operations.
Purpose: Validate efficient bulk IP creation for large datasets.
Test Create Ip Bulk
Source code in tests\test_netbox_service.py
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343 | def test_create_ip_bulk(self, nfclient):
delete_prefixes_within("10.0.0.0/24", nfclient)
delete_ips("10.0.0.0/24", nfclient)
create_1 = nfclient.run_job(
"netbox",
"create_ip_bulk",
workers="any",
kwargs={
"prefix": "10.0.0.0/24",
"devices": ["fceos4", "fceos5"],
"interface_regex": "eth103.0|eth11.123|Port-Channel1.101|ae5.101",
"mask_len": 31,
},
)
print("create_1")
pprint.pprint(create_1, width=200)
for worker, res1 in create_1.items():
assert res1["failed"] == False, "Allocation failed"
assert res1["result"] == {
"fceos4": {
"Port-Channel1.101": {
"address": "10.0.0.0/31",
"description": "",
"device": "fceos4",
"interface": "Port-Channel1.101",
"peer": {
"address": "10.0.0.1/31",
"description": "",
"device": "fceos5",
"interface": "ae5.101",
"vrf": "None",
},
"vrf": "None",
},
"eth103.0": {
"address": "10.0.0.2/31",
"description": "",
"device": "fceos4",
"interface": "eth103.0",
"peer": {
"address": "10.0.0.3/31",
"description": "",
"device": "fceos5",
"interface": "eth103",
"vrf": "None",
},
"vrf": "None",
},
"eth11.123": {
"address": "10.0.0.4/31",
"description": "",
"device": "fceos4",
"interface": "eth11.123",
"peer": {
"address": "10.0.0.5/31",
"description": "",
"device": "fceos5",
"interface": "eth11.123",
"vrf": "None",
},
"vrf": "None",
},
},
"fceos5": {
"ae5.101": {
"address": "10.0.0.1/31",
"description": "",
"device": "fceos5",
"interface": "ae5.101",
"vrf": "None",
},
"eth11.123": {
"address": "10.0.0.5/31",
"description": "",
"device": "fceos5",
"interface": "eth11.123",
"vrf": "None",
},
},
}
|
Test Execution
Configuration
Netbox tests require configuration in netbox_data.py:
NB_URL = "http://netbox-instance:8000"
NB_API_TOKEN = "your-api-token"
Tests operate against three Netbox instances configured in inventory: dev, preprod, and prod.
Running Tests
# Run all Netbox tests
pytest tests/test_netbox_service.py -v
# Run specific test class
pytest tests/test_netbox_service.py::TestGetDevices -v
# Run specific test method
pytest tests/test_netbox_service.py::TestGetDevices::test_get_devices_with_filters -v
Test Utilities
Helper functions available in test file:
get_nb_version(nfclient, instance=None) - Get Netbox version
delete_branch(branch, nfclient) - Delete transaction branch
delete_interfaces(nfclient, device, interface) - Delete interface
delete_prefixes_within(prefix, nfclient) - Delete prefix subtree
delete_ips(prefix, nfclient) - Delete IPs in prefix
get_pynetbox(nfclient) - Get pynetbox API instance
Troubleshooting
- Connection errors: Verify Netbox is running and
netbox_data.py is configured correctly
- Fixture timeout: Increase sleep time in fixture if workers need more time to start
- Cache issues: Clear cache between test runs if stale data is observed
- Branch isolation: Always delete test branches after use to prevent state pollution