Skip to content

Commit

Permalink
fix: Tests and mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
johannwagner committed Nov 19, 2024
1 parent dcc1f62 commit 919dfe9
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 22 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ cosmo.yml
.direnv
leaf*.json

cosmo_data.yaml
machines/**

.coverage
Expand Down
8 changes: 4 additions & 4 deletions cosmo/clients/netbox_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,8 @@ def _fetch_data(self, kwargs):
name: {starts_with: "lo"},
type: {exact:"loopback"}
}) {
id,
name,
child_interfaces {
id,
name,
vrf {
id
Expand Down Expand Up @@ -119,9 +117,11 @@ def _merge_into(self, data: object, query_data):
continue
device_name = interface['device']['name']

l_ipv4 = next(filter(lambda l: l['family']['value'] == 4, child_interface['ip_addresses']), None)
l_ipv6 = next(filter(lambda l: l['family']['value'] == 6, child_interface['ip_addresses']), None)
loopbacks[device_name] = {
"ipv4": next(filter(lambda l: l['family']['value'] == 4, child_interface['ip_addresses']))['address'],
"ipv6": next(filter(lambda l: l['family']['value'] == 6, child_interface['ip_addresses']))['address']
"ipv4": l_ipv4['address'] if l_ipv4 else None,
"ipv6": l_ipv6['address'] if l_ipv6 else None
}


Expand Down
5 changes: 4 additions & 1 deletion cosmo/tests/test_case_1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ device_list:
serial: 4242
staticroute_set: []
l2vpn_list: []
vrf_list: []
vrf_list: []
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
5 changes: 4 additions & 1 deletion cosmo/tests/test_case_2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,7 @@ device_list:
serial: 4242
staticroute_set: []
l2vpn_list: []
vrf_list: []
vrf_list: []
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
3 changes: 3 additions & 0 deletions cosmo/tests/test_case_bgpcpe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,6 @@ vrf_list:
- name: target:9136:407
name: L3VPN
rd: null
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
5 changes: 4 additions & 1 deletion cosmo/tests/test_case_fec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,7 @@ device_list:
serial: 4242
staticroute_set: [ ]
l2vpn_list: [ ]
vrf_list: [ ]
vrf_list: [ ]
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
5 changes: 4 additions & 1 deletion cosmo/tests/test_case_ips.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,7 @@ device_list:
serial: 4242
staticroute_set: [ ]
l2vpn_list: [ ]
vrf_list: [ ]
vrf_list: [ ]
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
5 changes: 2 additions & 3 deletions cosmo/tests/test_case_l2x_err_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ device_list:
l2vpn_list: []
vrf_list: []
loopbacks:
TEST0001:
ipv4: "45.139.136.10/24"
ipv6: ""
TEST0001:
ipv4: 45.139.136.10/24
16 changes: 16 additions & 0 deletions cosmo/tests/test_case_l3vpn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,19 @@ vrf_list:
- name: target:9136:407
name: L3VPN
rd: null
# This has some double use and gets put into NetboxClient and RouterSerializer.
# Therefore it has the same data twice in different formats.
loopback_interface_list:
- name: "lo0"
device:
name: "TEST0001"
child_interfaces:
- name: "lo0.0"
vrf: null
ip_addresses:
- address: 45.139.136.10/24
family:
value: 4
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
5 changes: 4 additions & 1 deletion cosmo/tests/test_case_lag.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,7 @@ device_list:
serial: 4242
staticroute_set: [ ]
l2vpn_list: [ ]
vrf_list: [ ]
vrf_list: [ ]
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
5 changes: 2 additions & 3 deletions cosmo/tests/test_case_local_l2x.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,5 @@ l2vpn_list:
type: EVPL
vrf_list: []
loopbacks:
TEST0001:
ipv4: "45.139.136.10/24"
ipv6: ""
TEST0001:
ipv4: 45.139.136.10/24
5 changes: 5 additions & 0 deletions cosmo/tests/test_case_mpls_evpn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,8 @@ l2vpn_list:
type: mpls-evpn

vrf_list: []
loopbacks:
TEST0001:
ipv4: "45.139.136.11/24"
TEST0002:
ipv4: "45.139.136.10/24"
4 changes: 4 additions & 0 deletions cosmo/tests/test_case_policer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,7 @@ vrf_list:
- name: target:9136:399
name: L3VPN
rd: null
loopbacks:
TEST0001:
ipv4: 62.176.224.24/32
ipv6: 2a01:580:6000::23/128
7 changes: 6 additions & 1 deletion cosmo/tests/test_case_vpws.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,9 @@ l2vpn_list:
id: '184383'
id: '866'
type: VPWS
vrf_list: []
vrf_list: []
loopbacks:
TEST0002:
ipv4: 45.139.136.10/24
TEST0001:
ipv4: 45.139.136.11/24
3 changes: 3 additions & 0 deletions cosmo/tests/test_case_vrf_staticroute.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,6 @@ vrf_list:
- name: target:9136:111000000
name: L3VPN-TEST
rd: null
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
13 changes: 8 additions & 5 deletions cosmo/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,22 @@ def patchPostFunc(url, json, **kwargs):
"device_list",
"vrf_list",
"l2vpn_list",
"interface_list"
]
retVal = dict()

for rl in request_lists:
if rl in q:
if "bgp_cpe" in q:
retVal['interface_list'] = patchKwArgs.get("connected_devices_interface_list", [])
elif "loopback" in q:
retVal['interface_list'] = patchKwArgs.get("loopback_interface_list", [])
elif rl in q:
retVal[rl] = patchKwArgs.get(rl, [])

return ResponseMock(200, {"data": retVal})

postMock1 = mocker.patch('requests.get', side_effect=patchGetFunc)
postMock2 = mocker.patch('requests.post', side_effect=patchPostFunc)
return [postMock1, postMock2]
getMock = mocker.patch('requests.get', side_effect=patchGetFunc)
postMock = mocker.patch('requests.post', side_effect=patchPostFunc)
return [getMock, postMock]


class ResponseMock:
Expand Down

0 comments on commit 919dfe9

Please sign in to comment.