|
| 1 | +""" |
| 2 | +Generic helper functions for gearbox testing. |
| 3 | +
|
| 4 | +This module provides reusable utility functions for gearbox-related tests, |
| 5 | +including port management and configuration setup. |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | + |
| 10 | + |
| 11 | +class TestGearboxHelper: |
| 12 | + """Helper class for gearbox-related test operations.""" |
| 13 | + |
| 14 | + @staticmethod |
| 15 | + def get_first_gearbox_port(gearbox): |
| 16 | + """ |
| 17 | + Get the first port from Gearbox object (reads from _GEARBOX_TABLE in APPL_DB). |
| 18 | +
|
| 19 | + Args: |
| 20 | + gearbox: Gearbox fixture |
| 21 | +
|
| 22 | + Returns: |
| 23 | + tuple: (port_name, phy_id) - First available gearbox port and its PHY ID |
| 24 | + """ |
| 25 | + assert len(gearbox.interfaces) > 0, "No interfaces found in gearbox" |
| 26 | + |
| 27 | + # Get first interface |
| 28 | + first_idx = next(iter(gearbox.interfaces)) |
| 29 | + first_intf = gearbox.interfaces[first_idx] |
| 30 | + |
| 31 | + port_name = first_intf.get("name") |
| 32 | + phy_id = first_intf.get("phy_id") |
| 33 | + |
| 34 | + assert port_name, "First interface has no 'name' field" |
| 35 | + assert phy_id is not None, "First interface has no 'phy_id' field" |
| 36 | + |
| 37 | + return port_name, phy_id |
| 38 | + |
| 39 | + @staticmethod |
| 40 | + def configure_gearbox_macsec_support(dvs, gearbox, phy_id=None, macsec_supported=None): |
| 41 | + """ |
| 42 | + Configure MACsec support on a gearbox PHY by modifying gearbox_config.json and restarting DVS. |
| 43 | +
|
| 44 | + This is necessary because: |
| 45 | + 1. gearsyncd reads gearbox_config.json only at startup |
| 46 | + 2. PortsOrch caches _GEARBOX_TABLE only at startup (initGearbox) |
| 47 | + 3. MACsecOrch reads from PortsOrch's cache, not from _GEARBOX_TABLE |
| 48 | + 4. Full DVS restart is the only reliable way to reload the configuration |
| 49 | + because partial service restarts cause inconsistent port state |
| 50 | +
|
| 51 | + Args: |
| 52 | + dvs: Docker Virtual Switch instance |
| 53 | + gearbox: Gearbox fixture |
| 54 | + phy_id: PHY ID (string, e.g., "1"). If None, uses the first PHY from Gearbox object. |
| 55 | + macsec_supported: None (remove field), True, or False |
| 56 | + """ |
| 57 | + # If phy_id not provided, use the first PHY from Gearbox object |
| 58 | + if phy_id is None: |
| 59 | + assert len(gearbox.phys) > 0, "No PHYs found in gearbox" |
| 60 | + phy_id = next(iter(gearbox.phys)) |
| 61 | + print(f"No phy_id provided, using first PHY: {phy_id}") |
| 62 | + |
| 63 | + # Resolve symlink to get actual config path |
| 64 | + config_path = "/usr/share/sonic/hwsku/gearbox_config.json" |
| 65 | + rc, actual_path = dvs.runcmd(f"readlink -f {config_path}") |
| 66 | + if rc == 0 and actual_path.strip(): |
| 67 | + config_path = actual_path.strip() |
| 68 | + |
| 69 | + # Read current config |
| 70 | + rc, config_json = dvs.runcmd(f"cat {config_path}") |
| 71 | + assert rc == 0, f"Failed to read gearbox_config.json from {config_path}" |
| 72 | + config = json.loads(config_json) |
| 73 | + |
| 74 | + phy_id = int(phy_id) |
| 75 | + |
| 76 | + # Find and modify the PHY configuration |
| 77 | + phy_found = False |
| 78 | + for phy in config.get("phys", []): |
| 79 | + if phy.get("phy_id") == phy_id: |
| 80 | + phy_found = True |
| 81 | + if macsec_supported is None: |
| 82 | + # Remove the field if it exists |
| 83 | + if "macsec_supported" in phy: |
| 84 | + del phy["macsec_supported"] |
| 85 | + else: |
| 86 | + # Set the field |
| 87 | + phy["macsec_supported"] = macsec_supported |
| 88 | + break |
| 89 | + |
| 90 | + assert phy_found, f"PHY {phy_id} not found in gearbox_config.json" |
| 91 | + |
| 92 | + # Write modified config back using heredoc |
| 93 | + config_str = json.dumps(config, indent=2) |
| 94 | + heredoc = "__GEARBOX_JSON__" |
| 95 | + rc, _ = dvs.runcmd( |
| 96 | + "bash -lc 'cat > {path} <<\"{tag}\"\n{payload}\n{tag}\n'".format( |
| 97 | + path=config_path, |
| 98 | + tag=heredoc, |
| 99 | + payload=config_str, |
| 100 | + ) |
| 101 | + ) |
| 102 | + assert rc == 0, f"Failed to write modified config to {config_path}" |
| 103 | + |
| 104 | + # Restart DVS to reload configuration |
| 105 | + dvs.restart() |
0 commit comments