From 8bd504636bb978c2fdeda088e81b5e9a897a34d7 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Sat, 16 Feb 2019 15:31:28 +0100 Subject: hostname: additional test case fixes Conflicts: src/tests/test_host_name.py --- src/conf_mode/host_name.py | 11 +++++------ src/tests/test_host_name.py | 25 +++++++++++++++---------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/conf_mode/host_name.py b/src/conf_mode/host_name.py index 4b6ce76b0..0f8d3b719 100755 --- a/src/conf_mode/host_name.py +++ b/src/conf_mode/host_name.py @@ -35,7 +35,6 @@ config_file_hosts = '/etc/hosts' config_file_resolv = '/etc/resolv.conf' config_tmpl_hosts = """ -### Autogenerated by host_name.py ### 127.0.0.1 localhost {{ hostname }}{% if domain_name %}.{{ domain_name }}{% endif %} # The following lines are desirable for IPv6 capable hosts @@ -85,7 +84,6 @@ default_config_data = { } def get_config(): - """Get configuration""" conf = Config() hosts = copy.deepcopy(default_config_data) @@ -102,8 +100,8 @@ def get_config(): return hosts def verify(config): - """Verify configuration""" - # check for invalid host + if config is None: + return None # pattern $VAR(@) "^[[:alnum:]][-.[:alnum:]]*[[:alnum:]]$" ; "invalid host name $VAR(@)" hostname_regex = re.compile("^[A-Za-z0-9][-.A-Za-z0-9]*[A-Za-z0-9]$") @@ -128,7 +126,6 @@ def verify(config): return None def generate(config): - """Generate configuration files""" if config is None: return None @@ -158,7 +155,9 @@ def generate(config): return None def apply(config): - """Apply configuration""" + if config is None: + return None + fqdn = config['hostname'] if config['domain_name']: fqdn += '.' + config['domain_name'] diff --git a/src/tests/test_host_name.py b/src/tests/test_host_name.py index 8c5210d5f..c820641d1 100644 --- a/src/tests/test_host_name.py +++ b/src/tests/test_host_name.py @@ -19,6 +19,7 @@ import os import tempfile import unittest +import textwrap from unittest import TestCase, mock from vyos import ConfigError @@ -85,31 +86,35 @@ class TestHostName(TestCase): tests = [ {'name': 'has_old_entry', 'has_old_entry': True, - 'config': {"hostname": 'router', "domain": 'localdomain', "fqdn": 'router.localdomain'}, - 'expected': ['127.0.1.1', 'router.localdomain']}, + 'config': {"hostname": 'router', "domain_name": 'localdomain', "domain_search": '', "no_dhcp_ns": False, "nameserver": []}, + 'expected': ['127.0.0.1', 'localhost'] + }, {'name': 'no_old_entry', 'has_old_entry': False, - 'config': {"hostname": 'router', "domain": 'localdomain', "fqdn": 'router.localdomain'}, - 'expected': ['127.0.1.1', 'router.localdomain']}, + 'config': {"hostname": 'router', "domain_name": 'localdomain', "domain_search": 'vyos.io', "no_dhcp_ns": False, "nameserver": []}, + 'expected': ['127.0.0.1', 'localhost'] + }, ] for t in tests: with self.subTest(msg=t['name'], config=t['config'], has_old_entry=t['has_old_entry'], expected=t['expected']): m = mock.MagicMock(return_value=b'debian') with mock.patch('subprocess.check_output', m): - host_name.hosts_file = tempfile.mkstemp()[1] + host_name.config_file_hosts = tempfile.mkstemp()[1] + host_name.config_file_resolv = tempfile.mkstemp()[1] if t['has_old_entry']: - with open(host_name.hosts_file, 'w') as f: + with open(host_name.config_file_hosts, 'w') as f: f.writelines(['\n127.0.1.1 {} # VyOS entry'.format('debian')]) host_name.generate(t['config']) if len(t['expected']) > 0: - self.assertTrue(os.path.isfile(host_name.hosts_file)) - with open(host_name.hosts_file) as f: + self.assertTrue(os.path.isfile(host_name.config_file_hosts)) + with open(host_name.config_file_hosts ) as f: actual = f.read() self.assertEqual( t['expected'], actual.splitlines()[1].split()[0:2]) - os.remove(host_name.hosts_file) + os.remove(host_name.config_file_hosts ) + os.remove(host_name.config_file_resolv) else: - self.assertFalse(os.path.isfile(host_name.hosts_file)) + self.assertFalse(os.path.isfile(host_name.config_file_hosts )) def test_apply(self): -- cgit v1.2.3