From cdc1748b8489251a45875736a99633740529ea0c Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Sun, 3 Jan 2021 10:24:20 +0100 Subject: dhcp: T3180: bugfix assignment of sliced ranges to config dict A reference to a dictionary key obtained by a for loop can not be used to update values inside that dictionaries key. You must use the original path to the nested dictionaries key. --- data/templates/dhcp-server/dhcpd.conf.tmpl | 2 +- smoketest/scripts/cli/test_service_dhcp-server.py | 72 +++++++++++++++++++++-- src/conf_mode/dhcp_server.py | 3 +- 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/data/templates/dhcp-server/dhcpd.conf.tmpl b/data/templates/dhcp-server/dhcpd.conf.tmpl index 56a5f4bcd..ff2e31998 100644 --- a/data/templates/dhcp-server/dhcpd.conf.tmpl +++ b/data/templates/dhcp-server/dhcpd.conf.tmpl @@ -35,12 +35,12 @@ option wpad-url code 252 = text; {% endfor %} {% endif %} -# Failover configuration {% if shared_network_name is defined and shared_network_name is not none %} {% for network, network_config in shared_network_name.items() if network_config.disable is not defined %} {% if network_config.subnet is defined and network_config.subnet is not none %} {% for subnet, subnet_config in network_config.subnet.items() %} {% if subnet_config.failover is defined and subnet_config.failover is defined and subnet_config.failover.name is defined and subnet_config.failover.name is not none %} +# Failover configuration for {{ subnet }} failover peer "{{ subnet_config.failover.name }}" { {% if subnet_config.failover.status == 'primary' %} primary; diff --git a/smoketest/scripts/cli/test_service_dhcp-server.py b/smoketest/scripts/cli/test_service_dhcp-server.py index 5bf4b100e..c9ec9ebc0 100755 --- a/smoketest/scripts/cli/test_service_dhcp-server.py +++ b/smoketest/scripts/cli/test_service_dhcp-server.py @@ -23,8 +23,9 @@ from vyos.configsession import ConfigSessionError from vyos.util import cmd from vyos.util import process_named_running from vyos.util import read_file -from vyos.template import inc_ip from vyos.template import address_from_cidr +from vyos.template import inc_ip +from vyos.template import dec_ip from vyos.template import netmask_from_cidr PROCESS_NAME = 'dhcpd' @@ -48,7 +49,7 @@ class TestServiceDHCPServer(unittest.TestCase): self.session.commit() del self.session - def test_single_pool_range(self): + def test_dhcp_single_pool_range(self): shared_net_name = 'SMOKE-1' range_0_start = inc_ip(subnet, 10) @@ -93,7 +94,7 @@ class TestServiceDHCPServer(unittest.TestCase): # Check for running process self.assertTrue(process_named_running(PROCESS_NAME)) - def test_single_pool_options(self): + def test_dhcp_single_pool_options(self): shared_net_name = 'SMOKE-0815' range_0_start = inc_ip(subnet, 10) @@ -197,7 +198,7 @@ class TestServiceDHCPServer(unittest.TestCase): # Check for running process self.assertTrue(process_named_running(PROCESS_NAME)) - def test_single_pool_static_mapping(self): + def test_dhcp_single_pool_static_mapping(self): shared_net_name = 'SMOKE-2' pool = base_path + ['shared-network-name', shared_net_name, 'subnet', subnet] @@ -246,7 +247,7 @@ class TestServiceDHCPServer(unittest.TestCase): # Check for running process self.assertTrue(process_named_running(PROCESS_NAME)) - def test_multi_pools(self): + def test_dhcp_multiple_pools(self): lease_time = '14400' for network in ['0', '1', '2', '3']: @@ -320,5 +321,66 @@ class TestServiceDHCPServer(unittest.TestCase): # Check for running process self.assertTrue(process_named_running(PROCESS_NAME)) + def test_dhcp_exclude_not_in_range(self): + # T3180: verify else path when slicing DHCP ranges and exclude address + # is not part of the DHCP range + range_0_start = inc_ip(subnet, 10) + range_0_stop = inc_ip(subnet, 20) + + pool = base_path + ['shared-network-name', 'EXCLUDE-TEST', 'subnet', subnet] + self.session.set(pool + ['default-router', router]) + self.session.set(pool + ['exclude', router]) + self.session.set(pool + ['range', '0', 'start', range_0_start]) + self.session.set(pool + ['range', '0', 'stop', range_0_stop]) + + # commit changes + self.session.commit() + + # VErify + config = read_file(DHCPD_CONF) + network = address_from_cidr(subnet) + netmask = netmask_from_cidr(subnet) + + self.assertIn(f'subnet {network} netmask {netmask}' + r' {', config) + self.assertIn(f'option routers {router};', config) + self.assertIn(f'range {range_0_start} {range_0_stop};', config) + + # Check for running process + self.assertTrue(process_named_running(PROCESS_NAME)) + + def test_dhcp_exclude_in_range(self): + # T3180: verify else path when slicing DHCP ranges and exclude address + # is not part of the DHCP range + range_0_start = inc_ip(subnet, 10) + range_0_stop = inc_ip(subnet, 100) + + # the DHCP exclude addresse is blanked out of the range which is done + # by slicing one range into two ranges + exclude_addr = inc_ip(range_0_start, 20) + range_0_stop_excl = dec_ip(exclude_addr, 1) + range_0_start_excl = inc_ip(exclude_addr, 1) + + pool = base_path + ['shared-network-name', 'EXCLUDE-TEST-2', 'subnet', subnet] + self.session.set(pool + ['default-router', router]) + self.session.set(pool + ['exclude', exclude_addr]) + self.session.set(pool + ['range', '0', 'start', range_0_start]) + self.session.set(pool + ['range', '0', 'stop', range_0_stop]) + + # commit changes + self.session.commit() + + # VErify + config = read_file(DHCPD_CONF) + network = address_from_cidr(subnet) + netmask = netmask_from_cidr(subnet) + + self.assertIn(f'subnet {network} netmask {netmask}' + r' {', config) + self.assertIn(f'option routers {router};', config) + self.assertIn(f'range {range_0_start} {range_0_stop_excl};', config) + self.assertIn(f'range {range_0_start_excl} {range_0_stop};', config) + + # Check for running process + self.assertTrue(process_named_running(PROCESS_NAME)) + if __name__ == '__main__': unittest.main(verbosity=2) diff --git a/src/conf_mode/dhcp_server.py b/src/conf_mode/dhcp_server.py index fee2afe24..392a69427 100755 --- a/src/conf_mode/dhcp_server.py +++ b/src/conf_mode/dhcp_server.py @@ -126,7 +126,8 @@ def get_config(config=None): new_range_dict.update({new_range_id : slice}) new_range_id +=1 - subnet_config.update({'range' : new_range_dict}) + dhcp['shared_network_name'][network]['subnet'][subnet].update( + {'range' : new_range_dict}) return dhcp -- cgit v1.2.3