diff --git a/vmware_nsxlib/tests/unit/v3/nsxlib_testcase.py b/vmware_nsxlib/tests/unit/v3/nsxlib_testcase.py index 6ad95741..0fb4c366 100644 --- a/vmware_nsxlib/tests/unit/v3/nsxlib_testcase.py +++ b/vmware_nsxlib/tests/unit/v3/nsxlib_testcase.py @@ -43,6 +43,9 @@ PLUGIN_SCOPE = "plugin scope" PLUGIN_TAG = "plugin tag" PLUGIN_VER = "plugin ver" +DNS_NAMESERVERS = ['1.1.1.1'] +DNS_DOMAIN = 'openstacklocal' + def _mock_nsxlib(): def _return_id_key(*args, **kwargs): @@ -107,7 +110,10 @@ def get_default_nsxlib_config(): nsx_api_managers=[], plugin_scope=PLUGIN_SCOPE, plugin_tag=PLUGIN_TAG, - plugin_ver=PLUGIN_VER) + plugin_ver=PLUGIN_VER, + dns_nameservers=DNS_NAMESERVERS, + dns_domain=DNS_DOMAIN + ) def get_nsxlib_config_with_client_cert(): diff --git a/vmware_nsxlib/tests/unit/v3/test_native_dhcp.py b/vmware_nsxlib/tests/unit/v3/test_native_dhcp.py new file mode 100644 index 00000000..6356710c --- /dev/null +++ b/vmware_nsxlib/tests/unit/v3/test_native_dhcp.py @@ -0,0 +1,88 @@ +# Copyright (c) 2017 VMware, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from vmware_nsxlib.tests.unit.v3 import nsxlib_testcase +from vmware_nsxlib.v3 import native_dhcp + + +# TODO(asarfaty): Add more test cases here +class TestNativeDhcp(nsxlib_testcase.NsxLibTestCase): + """Tests for vmware_nsxlib.v3.native_dhcp.NsxLibNativeDhcp.""" + + def setUp(self, *args, **kwargs): + super(TestNativeDhcp, self).setUp() + self.handler = native_dhcp.NsxLibNativeDhcp( + self.nsxlib.client, + nsxlib_testcase.get_default_nsxlib_config()) + self.net_dns_domain = 'a.com' + self.subnet_dns_nameserver = '1.1.1.1' + self.default_dns_domain = 'b.com' + self.default_dns_nameserver = '2.2.2.2' + + def _get_server_config(self, with_net_dns=True, with_default_dns=True): + net = {'name': 'dummy', + 'id': 'dummy'} + subnet = {'dns_nameservers': None, + 'gateway_ip': '2.2.2.2', + 'cidr': '5.5.0.0/24', + 'host_routes': []} + port = {'fixed_ips': [{'ip_address': '5.5.0.1'}]} + tags = [] + if with_net_dns: + net['dns_domain'] = {'dns_domain': self.net_dns_domain} + subnet['dns_nameservers'] = [self.subnet_dns_nameserver] + if with_default_dns: + result = self.handler.build_server_config( + net, subnet, port, tags, + default_dns_nameservers=[self.default_dns_nameserver], + default_dns_domain=self.default_dns_domain) + else: + result = self.handler.build_server_config(net, subnet, port, tags) + return result + + def test_build_server_config_dns_from_net_no_defaults(self): + # Verify that net/subnet dns params are used if exist + result = self._get_server_config(with_net_dns=True, + with_default_dns=False) + self.assertEqual(self.net_dns_domain, result['domain_name']) + self.assertEqual([self.subnet_dns_nameserver], + result['dns_nameservers']) + + def test_build_server_config_dns_from_net_with_defaults(self): + # Verify that net/subnet dns params are used if exist, even if there + # are defaults + result = self._get_server_config(with_net_dns=True, + with_default_dns=True) + self.assertEqual(self.net_dns_domain, result['domain_name']) + self.assertEqual([self.subnet_dns_nameserver], + result['dns_nameservers']) + + def test_build_server_config_dns_from_defaults(self): + # Verify that default dns params are used if net/subnet dns params + # are missing + result = self._get_server_config(with_net_dns=False, + with_default_dns=True) + self.assertEqual(self.default_dns_domain, result['domain_name']) + self.assertEqual([self.default_dns_nameserver], + result['dns_nameservers']) + + def test_build_server_config_dns_from_config(self): + # Verify that config dns params are used if net/subnet and default + # dns params are missing + result = self._get_server_config(with_net_dns=False, + with_default_dns=False) + self.assertEqual(nsxlib_testcase.DNS_DOMAIN, result['domain_name']) + self.assertEqual(nsxlib_testcase.DNS_NAMESERVERS, + result['dns_nameservers']) diff --git a/vmware_nsxlib/v3/native_dhcp.py b/vmware_nsxlib/v3/native_dhcp.py index ac2fbb38..6d4fef0a 100644 --- a/vmware_nsxlib/v3/native_dhcp.py +++ b/vmware_nsxlib/v3/native_dhcp.py @@ -22,13 +22,19 @@ from vmware_nsxlib.v3 import utils class NsxLibNativeDhcp(utils.NsxLibApiBase): - def build_server_config(self, network, subnet, port, tags): + def build_server_config(self, network, subnet, port, tags, + default_dns_nameservers=None, + default_dns_domain=None): # Prepare the configuration for a new logical DHCP server. server_ip = "%s/%u" % (port['fixed_ips'][0]['ip_address'], netaddr.IPNetwork(subnet['cidr']).prefixlen) dns_nameservers = subnet['dns_nameservers'] if not dns_nameservers or not validators.is_attr_set(dns_nameservers): - dns_nameservers = self.nsxlib_config.dns_nameservers + # use the default one , or the globally configured one + if default_dns_nameservers is not None: + dns_nameservers = default_dns_nameservers + else: + dns_nameservers = self.nsxlib_config.dns_nameservers gateway_ip = subnet['gateway_ip'] if not validators.is_attr_set(gateway_ip): gateway_ip = None @@ -55,8 +61,14 @@ class NsxLibNativeDhcp(utils.NsxLibApiBase): name = utils.get_name_and_uuid(network['name'] or 'dhcpserver', network['id']) dns_domain = network.get('dns_domain') - domain_name = dns_domain['dns_domain'] if dns_domain else ( - self.nsxlib_config.dns_domain) + if dns_domain: + domain_name = dns_domain['dns_domain'] + else: + # use the default one , or the globally configured one + if default_dns_domain is not None: + domain_name = default_dns_domain + else: + domain_name = self.nsxlib_config.dns_domain return {'name': name, 'server_ip': server_ip,