Skip to content

Commit e4d3173

Browse files
authored
sonic: make the port_config path configurable (#2563)
The directory holding the per-HWSKU port_config .ini files was hardcoded to /etc/sonic/port_config, which only exists inside the conductor image -- the Containerfile copies files/sonic/port_config there. A generator run from a checkout instead, as in local development, finds nothing at that path, and planting the files under /etc/sonic to work around it needs root. Introduce a SONIC_PORT_CONFIG_PATH setting in osism.settings, following the existing SONIC_* environment variable convention, and wire constants.PORT_CONFIG_PATH to it. The default is the previous hardcoded path, so container behaviour is identical; setting the variable points the generator at files/sonic/port_config/ in the checkout instead. Tests cover the default, the environment override, and the settings-to-constants wiring. Assisted-by: Claude:claude-sonnet-5 Signed-off-by: Roger Luethi <luethi@osism.tech>
1 parent d4141f4 commit e4d3173

4 files changed

Lines changed: 41 additions & 1 deletion

File tree

osism/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ def read_secret(secret_name):
7878
SONIC_EXPORT_SUFFIX = os.getenv("SONIC_EXPORT_SUFFIX", "_config_db.json")
7979
SONIC_EXPORT_IDENTIFIER = os.getenv("SONIC_EXPORT_IDENTIFIER", "serial-number")
8080

81+
# Directory holding the per-HWSKU port_config .ini files (bundled in the
82+
# repo under files/sonic/port_config and installed by the Dockerfile)
83+
SONIC_PORT_CONFIG_PATH = os.getenv("SONIC_PORT_CONFIG_PATH", "/etc/sonic/port_config")
84+
8185
# SONiC ZTP firmware configuration
8286
#
8387
# The ZTP firmware install uses a dynamic-url built from

osism/tasks/conductor/sonic/constants.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
"""Constants and mappings for SONiC configuration."""
44

5+
from osism import settings
6+
57
# Tag to add AF L2VPN EVPN to BGP neighbor
68
BGP_AF_L2VPN_EVPN_TAG = "bgp-af-l2vpn-evpn"
79

@@ -87,7 +89,7 @@
8789
}
8890

8991
# Path to SONiC port configuration files
90-
PORT_CONFIG_PATH = "/etc/sonic/port_config"
92+
PORT_CONFIG_PATH = settings.SONIC_PORT_CONFIG_PATH
9193

9294
# List of supported vendors
9395
SUPPORTED_VENDORS = [

tests/unit/tasks/conductor/sonic/test_constants.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# SPDX-License-Identifier: Apache-2.0
22

3+
import importlib
4+
35
import pytest
46

7+
from osism import settings as settings_module
8+
from osism.tasks.conductor.sonic import constants as constants_module
59
from osism.tasks.conductor.sonic.constants import (
610
BGP_AF_L2VPN_EVPN_TAG,
711
DEFAULT_LOCAL_AS_PREFIX,
@@ -151,3 +155,19 @@ def test_supported_hwskus_entry_invariants(hwsku):
151155
assert "-" in hwsku
152156
vendor = hwsku.split("-")[0]
153157
assert vendor in SUPPORTED_VENDORS
158+
159+
160+
# ---------------------------------------------------------------------------
161+
# PORT_CONFIG_PATH settings wiring
162+
# ---------------------------------------------------------------------------
163+
164+
165+
def test_port_config_path_follows_settings():
166+
original = settings_module.SONIC_PORT_CONFIG_PATH
167+
try:
168+
settings_module.SONIC_PORT_CONFIG_PATH = "/custom/port_config"
169+
reloaded = importlib.reload(constants_module)
170+
assert reloaded.PORT_CONFIG_PATH == "/custom/port_config"
171+
finally:
172+
settings_module.SONIC_PORT_CONFIG_PATH = original
173+
importlib.reload(constants_module)

tests/unit/test_settings.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,20 @@ def test_sonic_export_identifier_override(reload_settings, monkeypatch):
583583
assert settings_module.SONIC_EXPORT_IDENTIFIER == "asset-tag"
584584

585585

586+
def test_sonic_port_config_path_default(reload_settings, monkeypatch):
587+
monkeypatch.delenv("SONIC_PORT_CONFIG_PATH", raising=False)
588+
reload_settings()
589+
590+
assert settings_module.SONIC_PORT_CONFIG_PATH == "/etc/sonic/port_config"
591+
592+
593+
def test_sonic_port_config_path_override(reload_settings, monkeypatch):
594+
monkeypatch.setenv("SONIC_PORT_CONFIG_PATH", "/tmp/port_config")
595+
reload_settings()
596+
597+
assert settings_module.SONIC_PORT_CONFIG_PATH == "/tmp/port_config"
598+
599+
586600
# ---------------------------------------------------------------------------
587601
# NETBOX_SECONDARIES
588602
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)