-
Notifications
You must be signed in to change notification settings - Fork 3
Add unit tests for SONiC config_generator orchestrator and service ca… #2237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
berendt
wants to merge
1
commit into
main
Choose a base branch
from
gh2221
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
tests/unit/tasks/conductor/sonic/_config_generator_helpers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Shared helpers for the SONiC ``config_generator`` unit-test split. | ||
|
|
||
| The leading underscore keeps pytest from collecting this module as a test file. | ||
| """ | ||
|
|
||
| import json | ||
| from types import SimpleNamespace | ||
| from unittest.mock import mock_open | ||
|
|
||
| from osism.tasks.conductor.sonic import config_generator | ||
| from osism.tasks.conductor.sonic.config_generator import TOP_LEVEL_SCAFFOLD_KEYS | ||
|
|
||
|
|
||
| def make_base_config(version=None): | ||
| """Build a base ``config_db.json`` scaffold with every top-level key the | ||
| orchestrator (and its mocked helpers) index into directly. | ||
|
|
||
| The key list is sourced from the production-side ``TOP_LEVEL_SCAFFOLD_KEYS`` | ||
| constant so the helper cannot drift when keys are added or removed. | ||
| Optionally seeds ``VERSIONS.DATABASE.VERSION`` so the version-handling | ||
| branch can be exercised explicitly. | ||
| """ | ||
|
|
||
| cfg = {key: {} for key in TOP_LEVEL_SCAFFOLD_KEYS} | ||
| if version is not None: | ||
| cfg["VERSIONS"] = {"DATABASE": {"VERSION": version}} | ||
| return cfg | ||
|
|
||
|
|
||
| def patch_base_config(mocker, *, exists=True, base_config=None, raise_on_open=None): | ||
| """Patch ``os.path.exists`` and ``builtins.open`` for the base-config load. | ||
|
|
||
| - ``exists=True`` and ``base_config`` provided → ``open`` returns that | ||
| JSON-encoded scaffold. | ||
| - ``exists=False`` → ``open`` is not patched (the orchestrator never | ||
| reaches the ``with open`` path). | ||
| - ``raise_on_open`` → ``open`` raises this exception (e.g. ``OSError``). | ||
| """ | ||
|
|
||
| mocker.patch.object(config_generator.os.path, "exists", return_value=exists) | ||
| if not exists: | ||
| return | ||
| if raise_on_open is not None: | ||
| mocker.patch("builtins.open", side_effect=raise_on_open) | ||
| return | ||
| cfg = base_config if base_config is not None else make_base_config() | ||
| mocker.patch("builtins.open", mock_open(read_data=json.dumps(cfg))) | ||
|
|
||
|
|
||
| def make_iface(name, *, mgmt_only=False, type_value=None, iface_id=None): | ||
| return SimpleNamespace( | ||
| id=iface_id if iface_id is not None else id(object()), | ||
| name=name, | ||
| mgmt_only=mgmt_only, | ||
| type=SimpleNamespace(value=type_value) if type_value is not None else None, | ||
| ) | ||
|
|
||
|
|
||
| def make_ip(address): | ||
| return SimpleNamespace(address=address) | ||
|
|
||
|
|
||
| def seed_metalbox_cache(metalbox_id=10, name="mb", *, interfaces): | ||
| """Set ``_metalbox_devices_cache`` directly (skip ``_load`` to keep tests | ||
| fast and independent of NetBox-shape concerns). | ||
|
|
||
| ``interfaces`` is a list of ``(interface_obj, is_vlan, [ip_strings])``. | ||
| """ | ||
| cache_entry = { | ||
| "device": SimpleNamespace(id=metalbox_id, name=name), | ||
| "interfaces": {}, | ||
| } | ||
| for iface, is_vlan, addresses in interfaces: | ||
| cache_entry["interfaces"][iface.id] = { | ||
| "interface": iface, | ||
| "is_vlan": is_vlan, | ||
| "ips": [make_ip(addr) for addr in addresses], | ||
| } | ||
| config_generator._metalbox_devices_cache = {metalbox_id: cache_entry} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
tests/unit/tasks/conductor/sonic/test_config_generator_dns.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Unit tests for ``_add_dns_configuration`` in ``config_generator``.""" | ||
|
|
||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
|
|
||
| from osism.tasks.conductor.sonic import config_generator | ||
| from osism.tasks.conductor.sonic.config_generator import _add_dns_configuration | ||
|
|
||
| pytestmark = pytest.mark.usefixtures("reset_config_generator_caches") | ||
|
|
||
|
|
||
| def test_add_dns_configuration_with_metalbox_ip(mocker): | ||
| mocker.patch.object( | ||
| config_generator, | ||
| "_get_metalbox_ip_for_device", | ||
| return_value="10.0.0.1", | ||
| ) | ||
| config = {"DNS_NAMESERVER": {}} | ||
|
|
||
| _add_dns_configuration(config, SimpleNamespace(name="leaf-1")) | ||
|
|
||
| assert config["DNS_NAMESERVER"] == {"10.0.0.1": {}} | ||
|
|
||
|
|
||
| def test_add_dns_configuration_no_metalbox_ip(mocker): | ||
| mocker.patch.object( | ||
| config_generator, "_get_metalbox_ip_for_device", return_value=None | ||
| ) | ||
| config = {"DNS_NAMESERVER": {}} | ||
|
|
||
| _add_dns_configuration(config, SimpleNamespace(name="leaf-1")) | ||
|
|
||
| assert config["DNS_NAMESERVER"] == {} | ||
|
|
||
|
|
||
| def test_add_dns_configuration_helper_exception_swallowed(mocker): | ||
| mocker.patch.object( | ||
| config_generator, | ||
| "_get_metalbox_ip_for_device", | ||
| side_effect=RuntimeError("kaboom"), | ||
| ) | ||
| config = {"DNS_NAMESERVER": {}} | ||
|
|
||
| _add_dns_configuration(config, SimpleNamespace(name="leaf-1")) | ||
|
|
||
| assert config["DNS_NAMESERVER"] == {} |
69 changes: 69 additions & 0 deletions
69
tests/unit/tasks/conductor/sonic/test_config_generator_log.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Unit tests for ``_add_log_server_configuration`` in ``config_generator``.""" | ||
|
|
||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
|
|
||
| from osism.tasks.conductor.sonic.config_generator import _add_log_server_configuration | ||
|
|
||
| pytestmark = pytest.mark.usefixtures("reset_config_generator_caches") | ||
|
|
||
|
|
||
| def _device_with_ctx(**ctx): | ||
| return SimpleNamespace(name="leaf-1", config_context=ctx) | ||
|
|
||
|
|
||
| def test_add_log_server_configuration_defaults(): | ||
| config = {} | ||
| device = _device_with_ctx(_segment_log_server_hosts=["10.1.1.1", "10.1.1.2"]) | ||
|
|
||
| _add_log_server_configuration(config, device) | ||
|
|
||
| for host in ("10.1.1.1", "10.1.1.2"): | ||
| assert config["SYSLOG_SERVER"][host] == { | ||
| "message-type": "log", | ||
| "protocol": "UDP", | ||
| "remote-port": "514", | ||
| "severity": "info", | ||
| "vrf_name": "mgmt", | ||
| } | ||
|
|
||
|
|
||
| def test_add_log_server_configuration_protocol_uppercased(): | ||
| config = {} | ||
| device = _device_with_ctx( | ||
| _segment_log_server_hosts=["10.1.1.1"], | ||
| _segment_log_server_proto="tcp", | ||
| ) | ||
|
|
||
| _add_log_server_configuration(config, device) | ||
|
|
||
| assert config["SYSLOG_SERVER"]["10.1.1.1"]["protocol"] == "TCP" | ||
|
|
||
|
|
||
| def test_add_log_server_configuration_custom_severity_and_vrf(): | ||
| config = {} | ||
| device = _device_with_ctx( | ||
| _segment_log_server_hosts=["10.1.1.1"], | ||
| _segment_log_server_severity="debug", | ||
| _segment_log_server_vrf="default", | ||
| ) | ||
|
|
||
| _add_log_server_configuration(config, device) | ||
|
|
||
| entry = config["SYSLOG_SERVER"]["10.1.1.1"] | ||
| assert entry["severity"] == "debug" | ||
| assert entry["vrf_name"] == "default" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("hosts_value", [[], None]) | ||
| def test_add_log_server_configuration_no_hosts_skips_section(hosts_value): | ||
| config = {} | ||
| ctx = {} if hosts_value is None else {"_segment_log_server_hosts": hosts_value} | ||
| device = _device_with_ctx(**ctx) | ||
|
|
||
| _add_log_server_configuration(config, device) | ||
|
|
||
| assert "SYSLOG_SERVER" not in config |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TOP_LEVEL_SCAFFOLD_KEYSis missing sections the orchestrator writes into directly:BGP_NEIGHBOR_AF(e.g. line 1020),BGP_NEIGHBOR(line 1202),BGP_GLOBALS_AF_NETWORK(line 1819),BGP_GLOBALS_AF(line 2014),BGP_GLOBALS_ROUTE_ADVERTISE(line 2035),VXLAN_TUNNEL,VXLAN_EVPN_NVO,VXLAN_TUNNEL_MAP(lines 2067–2086). With a missing or sparseconfig_db.json, any device with connected ports, Loopback0 routes, or VRFs with VNI will raiseKeyErrorat runtime. These eight keys need to be added to the tuple.