salt/tests/unit/modules/test_dnsutil.py

117 lines
4.7 KiB
Python
Raw Normal View History

2014-02-12 21:53:58 +00:00
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Nicole Thomas <nicole@saltstack.com>`
'''
# Import python libs
from __future__ import absolute_import
2014-11-22 12:07:38 +00:00
import logging
2014-02-12 21:53:58 +00:00
# Import Salt Testing Libs
from tests.support.unit import TestCase, skipIf
from tests.support.mock import (
MagicMock,
patch,
mock_open,
call,
NO_MOCK,
NO_MOCK_REASON
)
2014-02-12 21:53:58 +00:00
# Import Salt Libs
import salt.modules.dnsutil as dnsutil
2014-02-12 21:53:58 +00:00
2014-11-22 12:07:38 +00:00
log = logging.getLogger(__name__)
2014-02-18 19:36:09 +00:00
mock_hosts_file = '##\n' \
'# Host Database\n' \
'#\n' \
'# localhost is used to configure the loopback interface\n' \
'# when the system is booting. Do not change this entry.\n' \
'##\n' \
'127.0.0.1 localhost\n' \
'255.255.255.255 broadcasthost\n' \
'::1 localhost\n' \
'fe80::1%lo0 localhost'
2014-02-12 21:53:58 +00:00
mock_hosts_file_rtn = {'::1': ['localhost'], '255.255.255.255': ['broadcasthost'],
'127.0.0.1': ['localhost'], 'fe80::1%lo0': ['localhost']}
2014-02-18 19:36:09 +00:00
mock_soa_zone = '$TTL 3D\n' \
'@ IN SOA land-5.com. root.land-5.com. (\n' \
'199609203 ; Serial\n' \
'28800 ; Refresh\n' \
'7200 ; Retry\n' \
'604800 ; Expire\n' \
'86400) ; Minimum TTL\n' \
'NS land-5.com.\n\n' \
2014-02-18 17:45:43 +00:00
'1 PTR localhost.'
if NO_MOCK is False:
mock_calls_list = [call.read(), call.write('##\n'),
call.write('# Host Database\n'),
call.write('#\n'),
call.write('# localhost is used to configure the loopback interface\n'),
call.write('# when the system is booting. Do not change this entry.\n'),
call.write('##\n'),
call.write('127.0.0.1 localhost'),
call.write('\n'),
call.write('255.255.255.255 broadcasthost'),
call.write('\n'),
call.write('::1 localhost'),
call.write('\n'),
call.write('fe80::1%lo0 localhost'),
Clean up open filehandles (#35359) * salt/crypt.py: clean up open filehandles * salt/fileclient.py: clean up open filehandles * salt/grains/core.py: clean up open filehandles * salt/modules/cp.py: clean up open filehandles * salt/modules/data.py: clean up open filehandles * salt/modules/dnsutil.py: clean up open filehandles * salt/modules/dockerng.py: clean up open filehandles * salt/modules/inspectlib/collector.py: clean up open filehandles * salt/modules/file.py: clean up open filehandles * salt/modules/hosts.py: clean up open filehandles * salt/modules/incron.py: clean up open filehandles * salt/modules/dpkg.py: clean up open filehandles * salt/modules/linux_sysctl.py: clean up open filehandles * salt/modules/netbsd_sysctl.py: clean up open filehandles * salt/modules/network.py: clean up open filehandles * salt/modules/nftables.py: clean up open filehandles * salt/modules/openbsd_sysctl.py: clean up open filehandles * salt/modules/rh_ip.py: clean up open filehandles * salt/modules/portage_config.py: clean up open filehandles * salt/modules/status.py: clean up open filehandles * salt/modules/tls.py: clean up open filehandles * salt/modules/xapi.py: clean up open filehandles * salt/modules/x509.py: clean up open filehandles * salt/modules/virt.py: clean up open filehandles * salt/modules/zcbuildout.py: clean up open filehandles * salt/returners/local_cache.py: clean up open filehandles * salt/utils/cloud.py: clean up open filehandles * salt/states/pkgrepo.py: clean up open filehandles * salt/states/x509.py: clean up open filehandles * salt/transport/mixins/auth.py: clean up open filehandles * salt/utils/__init__.py: clean up open filehandles * salt/states/pkg.py: clean up open filehandles * salt/utils/minion.py: clean up open filehandles * salt/utils/openstack/nova.py: clean up open filehandles * salt/utils/openstack/swift.py: clean up open filehandles * salt/utils/process.py: clean up open filehandles * salt/utils/templates.py: clean up open filehandles * salt/utils/virt.py: clean up open filehandles * tests/integration/__init__.py: clean up open filehandles * tests/integration/cli/grains.py: clean up open filehandles * tests/integration/client/standard.py: clean up open filehandles * tests/integration/modules/hosts.py: clean up open filehandles * tests/unit/utils/vt_test.py: clean up open filehandles * tests/integration/shell/enabled.py: clean up open filehandles * tests/integration/states/cmd.py: clean up open filehandles * tests/integration/states/file.py: clean up open filehandles * tests/integration/states/match.py: clean up open filehandles * tests/unit/config_test.py: clean up open filehandles * tests/unit/templates/jinja_test.py: clean up open filehandles * tests/unit/utils/find_test.py: clean up open filehandles * tests/integration/modules/state.py: clean up open filehandles * Update dnsutil_test to reflect changes in fopen usage
2016-08-11 16:45:24 +00:00
call.write('\n')]
2014-02-12 21:53:58 +00:00
@skipIf(NO_MOCK, NO_MOCK_REASON)
2014-02-12 21:53:58 +00:00
class DNSUtilTestCase(TestCase):
def test_parse_hosts(self):
with patch('salt.utils.fopen', mock_open(read_data=mock_hosts_file)):
self.assertEqual(dnsutil.parse_hosts(), {'::1': ['localhost'],
'255.255.255.255': ['broadcasthost'],
'127.0.0.1': ['localhost'],
'fe80::1%lo0': ['localhost']})
@patch('salt.modules.dnsutil.parse_hosts', MagicMock(return_value=mock_hosts_file_rtn))
def test_hosts_append(self):
with patch('salt.utils.fopen', mock_open(read_data=mock_hosts_file)) as m_open:
dnsutil.hosts_append('/etc/hosts', '127.0.0.1', 'ad1.yuk.co,ad2.yuk.co')
helper_open = m_open()
helper_open.write.assert_called_once_with('\n127.0.0.1 ad1.yuk.co ad2.yuk.co')
2014-02-14 19:42:59 +00:00
def test_hosts_remove(self):
2014-02-18 19:36:09 +00:00
to_remove = 'ad1.yuk.co'
new_mock_file = mock_hosts_file + '\n127.0.0.1 ' + to_remove + '\n'
2014-02-14 19:42:59 +00:00
with patch('salt.utils.fopen', mock_open(read_data=new_mock_file)) as m_open:
2014-02-18 19:36:09 +00:00
dnsutil.hosts_remove('/etc/hosts', to_remove)
2014-02-14 19:42:59 +00:00
helper_open = m_open()
calls_list = helper_open.method_calls
self.assertEqual(calls_list, mock_calls_list)
2014-02-18 17:45:43 +00:00
@skipIf(True, 'Waiting on bug report fixes')
def test_parse_zone(self):
with patch('salt.utils.fopen', mock_open(read_data=mock_soa_zone)):
2014-11-22 12:07:38 +00:00
log.debug(mock_soa_zone)
log.debug(dnsutil.parse_zone('/var/lib/named/example.com.zone'))
2014-02-18 19:36:09 +00:00
def test_to_seconds_hour(self):
self.assertEqual(dnsutil._to_seconds('4H'), 14400,
msg='Did not detect valid hours as invalid')
def test_to_seconds_day(self):
self.assertEqual(dnsutil._to_seconds('1D'), 86400,
msg='Did not detect valid day as invalid')
def test_to_seconds_week(self):
self.assertEqual(dnsutil._to_seconds('2W'), 604800,
msg='Did not set time greater than one week to one week')
def test_to_seconds_empty(self):
self.assertEqual(dnsutil._to_seconds(''), 604800,
msg='Did not set empty time to one week')
def test_to_seconds_large(self):
self.assertEqual(dnsutil._to_seconds('604801'), 604800,
msg='Did not set time greater than one week to one week')