Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions suzieq/poller/controller/source/base_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from suzieq.poller.controller.utils.inventory_utils import read_inventory
from suzieq.shared.exceptions import InventorySourceError

_DEFAULT_PORTS = {'http': 80, 'https': 443, 'ssh': 22}


class SourceModel(InventoryPluginModel):
"""Model for inventory source validation
Expand Down Expand Up @@ -233,14 +235,17 @@ def set_device(self, inventory: Dict[str, Dict]):
devtype = self._device.get('devtype')

for node in inventory.values():
transport_tmp = node.get('transport') or transport or 'ssh'
ignore_known_hosts_tmp = node.get('ignore_known_hosts')
node.update({
'jump_host': node.get('jump_host') or jump_host,
'jump_host_key_file': node.get('jump_host_key_file')
or jump_host_key_file,
'ignore_known_hosts': node.get('ignore_known_hosts')
or ignore_known_hosts,
'transport': node.get('transport') or transport or 'ssh',
'port': node.get('port') or port or 22,
'ignore_known_hosts': ignore_known_hosts_tmp if
ignore_known_hosts_tmp is not None else ignore_known_hosts,
'transport': transport_tmp,
'port': node.get('port') or port or
_DEFAULT_PORTS.get(transport_tmp),
'devtype': node.get('devtype') or devtype,
'slow_host': node.get('slow_host', '') or slow_host,
'per_cmd_auth': ((node.get('per_cmd_auth', '') != '')
Expand Down
99 changes: 92 additions & 7 deletions tests/unit/poller/controller/sources/test_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,63 @@
from suzieq.shared.utils import PollerTransport


_INVENTORY = [{
_INVENTORY = {
'native-ns.192.168.123.123.443':
{
'address': '192.168.123.123',
'hostname': None,
'namespace': 'native-ns',
'port': 443,
'transport': 'https'
},
'native-ns.192.168.123.164.443':
{
'address': '192.168.123.164',
'devtype': 'eos',
'hostname': None,
'namespace': 'native-ns',
'port': 443,
'ignore_known_hosts': False
},
'native-ns.192.168.123.111.443':
{
'address': '192.168.123.111',
'hostname': None,
'namespace': 'native-ns',
'transport': 'https'
},
'native-ns.192.168.123.110.22':
{
'address': '192.168.123.110',
'hostname': None,
'namespace': 'native-ns',
'transport': 'ssh'
},
'native-ns.192.168.123.143.443':
{
'address': '192.168.123.143',
'hostname': None,
'namespace': 'native-ns',
'transport': 'http',
'port': 443
},
'native-ns.192.168.123.171.22':
{
'address': '192.168.123.171',
'hostname': None,
'namespace': 'native-ns',
'transport': 'http'
}
}

_RESULT_INVENTORY = {
'native-ns.192.168.123.123.443':
{
'address': '192.168.123.123',
'hostname': None,
'namespace': 'native-ns',
'port': 443,
'transport': 'https'
},
'native-ns.192.168.123.164.443':
{
Expand All @@ -25,9 +74,42 @@
'hostname': None,
'namespace': 'native-ns',
'port': 443,
'transport': 'ssh',
'ignore_known_hosts': False
},
'native-ns.192.168.123.111.443':
{
'address': '192.168.123.111',
'hostname': None,
'namespace': 'native-ns',
'transport': 'https',
'port': 443
},
'native-ns.192.168.123.110.22':
{
'address': '192.168.123.110',
'hostname': None,
'namespace': 'native-ns',
'transport': 'ssh',
'port': 22
},
'native-ns.192.168.123.143.443':
{
'address': '192.168.123.143',
'hostname': None,
'namespace': 'native-ns',
'transport': 'http',
'port': 443
},
'native-ns.192.168.123.171.22':
{
'address': '192.168.123.171',
'hostname': None,
'namespace': 'native-ns',
'transport': 'http',
'port': 80
}
}]
}


def set_inventory_mock(self, inventory: Dict):
Expand All @@ -51,8 +133,9 @@ def set_inventory_mock(self, inventory: Dict):
@pytest.mark.poller_unit_tests
@pytest.mark.controller_unit_tests
@pytest.mark.asyncio
@pytest.mark.parametrize('inventory', _INVENTORY)
async def test_devices_set(inventory: Dict):
@pytest.mark.parametrize('inventory, result_inventory',
[(_INVENTORY, _RESULT_INVENTORY)])
async def test_devices_set(inventory: Dict, result_inventory: Dict):
"""Test devices are correctly set

Args:
Expand All @@ -66,7 +149,9 @@ async def test_devices_set(inventory: Dict):
'jump-host-key-file': None,
'devtype': 'panos',
'transport': PollerTransport.ssh,

'slow_host': False,
'per_cmd_auth': True,
'retries-on-auth-fail': 0
}
}

Expand All @@ -80,14 +165,14 @@ async def test_devices_set(inventory: Dict):

# emulate what the function Source.set_device should do
exp_inv = {}
for key, node in inventory.items():
for key, node in result_inventory.items():
Comment on lines -83 to +168
Copy link
Collaborator

@LucaNicosia LucaNicosia May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your fix is absolutely correct, but now you don't need to compare one element at a time anymore. You can simply compare inv with result_inventory

Suggested change
for key, node in inventory.items():
for key, node in result_inventory.items():
assert inv == result_inventory, 'inventories do not match'

exp_inv[key] = node.copy()
for k, v in config['device'].items():
k = k.replace('-', '_')
if k not in exp_inv[key]:
exp_inv[key][k] = v

assert inv == exp_inv
assert inv == exp_inv, 'inventory do not match'


@pytest.mark.controller_device
Expand Down