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
51 changes: 50 additions & 1 deletion dockers/docker-dhcp-relay/cli-plugin-tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,57 @@ def mock_cfgdb():
'VLAN': {
'Vlan1000': {
'dhcp_servers': ['192.0.0.1']
}
},
'Vlan200': {},
},
'DHCPV4_RELAY': {
'Vlan1000': {
'dhcpv4_servers': ['192.0.0.1']
},
'Vlan200': {},
},
'DHCP_RELAY': {
'Vlan1000': {
'dhcpv6_servers': ['fc02:2000::1']
}
},
'DEVICE_METADATA': {
'localhost': {
'has_sonic_dhcpv4_relay' : "False"
}
},
'VRF': {
"default": {
'VRF': 'default'
},
"VrfRED": {
'VRF': 'VrfRED'
},
"VrfBLUE": {
'VRF': 'VrfBLUE'
}
},
'PORTCHANNEL_INTERFACE': {
"PortChannel5": {},
"PortChannel6": {},
"PortChannel5|192.168.0.1/31": {},
"PortChannel6|192.168.0.3/31": {}
},
'LOOPBACK_INTERFACE': {
"Loopback0": {},
"Loopback2": {},
"Loopback3": {},
"Loopback0|10.1.0.1/32": {},
"Loopback2|10.1.0.1/32": {},
"Loopback3|10.1.0.2/32": {}
},
'INTERFACE': {
"Ethernet0": {},
"Ethernet0|10.0.0.0/31": {},
"Ethernet8": {},
"Ethernet8|10.0.0.4/31": {},
"Ethernet12": {},
"Ethernet12|10.0.0.6/31": {},
}
}

Expand All @@ -34,6 +79,10 @@ def set_entry(table, key, data):
def get_keys(table):
return CONFIG[table].keys()

def get_table(table):
return CONFIG.get(table, {})

cfgdb.get_table = mock.Mock(side_effect=get_table)
cfgdb.get_entry = mock.Mock(side_effect=get_entry)
cfgdb.set_entry = mock.Mock(side_effect=set_entry)
cfgdb.get_keys = mock.Mock(side_effect=get_keys)
Expand Down
41 changes: 41 additions & 0 deletions dockers/docker-dhcp-relay/cli-plugin-tests/mock_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
"192.0.0.2"
]
}
},
"DHCPV4_RELAY": {
"Vlan1000": {
"dhcpv4_servers": [
"192.0.0.1",
"192.0.0.2"
]
}
}
}
}
Expand Down Expand Up @@ -170,5 +178,38 @@
}
}
}
],
[
"ipv4_dhcp",
{
"config_db": {
"DHCPV4_RELAY": {
"Vlan1000": {
"dhcpv4_servers": [
"192.0.0.1",
"192.0.0.2"
],
"source_interface": "Ethernet112",
"link_selection": "enable",
"server_vrf": "default",
"vrf_selection": "enable"
},
"Vlan1001": {
"vlanid": "1001",
"dhcpv4_servers": [
"192.0.0.3",
"192.0.0.4"
],
"agent_relay_mode": "discard",
"max_hop_count": "5"
}
},
'DEVICE_METADATA': {
'localhost': {
'has_sonic_dhcpv4_relay' : "False"
}
}
}
}
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
@pytest.fixture(scope="module")
def patch_import_module():
# We need to mock import module because clear_dhcp_relay.py has below import
# dhcp6_relay = importlib.import_module('show.plugins.dhcp-relay')
# dhcprelay = importlib.import_module('show.plugins.dhcp-relay')
# When install current container, sonic-application-extension would move below file to destination in switch
# Src: dockers/docker-dhcp-relay/cli/show/plugins/show_dhcp_relay.py
# Dst: python-package-patch/show/plugins/dhcp-relay.py
# The dst path doesn't exist in UT env, hence we need to mock it
fake_dhcp6_relay = MagicMock()
fake_dhcprelay = MagicMock()

with patch('importlib.import_module') as mock_import:
def side_effect(name):
if name == 'show.plugins.dhcp-relay':
return fake_dhcp6_relay
return fake_dhcprelay
return original_import_module(name) # fallback

mock_import.side_effect = side_effect
Expand All @@ -54,7 +54,7 @@ def test_clear_dhcp_relay_ipv6_counter(interface, patch_import_module):
gotten_interfaces = ["Ethernet0, Ethernet1"]

mock_counter = MagicMock()
clear_dhcp_relay.dhcp6_relay.DHCPv6_Counter.return_value = mock_counter
clear_dhcp_relay.dhcprelay.DHCPv6_Counter.return_value = mock_counter
mock_counter.get_interface.return_value = gotten_interfaces
clear_dhcp_relay.clear_dhcp_relay_ipv6_counter(interface)
if interface:
Expand Down Expand Up @@ -319,3 +319,30 @@ def test_is_write_db_paused(patch_import_module):
mock_db = MagicMock()
clear_dhcp_relay.is_write_db_paused(mock_db, "table_name", "vlan_interface")
mock_db.get.assert_called_once_with(ANY, "table_name|vlan_interface", "pause_write_to_db")

@pytest.mark.parametrize("direction, pkt_type, interface", [
(None, None, None),
("TX", "DISCOVER", "Vlan1000"),
("RX", "OFFER", None),
])
def test_clear_dhcp_relay_ipv4_vlan_counter(patch_import_module, direction, pkt_type, interface):
clear_dhcp_relay = patch_import_module
mock_counter = MagicMock()
clear_dhcp_relay.dhcprelay.DHCPv4_Counter.return_value = mock_counter
clear_dhcp_relay.clear_dhcp_relay_ipv4_vlan_counter(direction, pkt_type, interface)
mock_counter.clear_table.assert_called_once_with(direction, pkt_type, interface)

def test_dhcp4relay_clear_vlan_counters_command(patch_import_module):
clear_dhcp_relay = patch_import_module
runner = CliRunner()
# Patch the actual clear function to verify call
with patch.object(clear_dhcp_relay, "clear_dhcp_relay_ipv4_vlan_counter") as mock_clear:
result = runner.invoke(clear_dhcp_relay.dhcp4relay_clear.commands['dhcp4relay-vlan-counters'], ['-d', 'TX', 'Vlan1000'])
assert result.exit_code == 0

def test_register_adds_dhcp4relay_clear_vlan_counters(patch_import_module):
clear_dhcp_relay = patch_import_module
cli = MagicMock()
clear_dhcp_relay.register(cli)
# Check that dhcp4relay_clear_vlan_counters was registered as a command
cli.add_command.assert_any_call(clear_dhcp_relay.dhcp4relay_clear_vlan_counters)
Loading
Loading