salt/tests/integration/modules/test_hosts.py

216 lines
6.2 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2012-02-12 23:03:31 +00:00
'''
Test the hosts module
'''
# Import python libs
from __future__ import absolute_import
import os
import shutil
# Import Salt Testing libs
from tests.support.case import ModuleCase
from tests.support.paths import FILES, TMP
# Import salt libs
import salt.utils.files
HFN = os.path.join(TMP, 'hosts')
class HostsModuleTest(ModuleCase):
'''
Test the hosts module
'''
maxDiff = None
def __clean_hosts(self):
'''
Clean out the hosts file
'''
shutil.copyfile(os.path.join(FILES, 'hosts'), HFN)
def __clear_hosts(self):
'''
Delete the tmp hosts file
'''
if os.path.isfile(HFN):
os.remove(HFN)
def tearDown(self):
'''
Make sure the tmp hosts file is gone
'''
self.__clear_hosts()
def test_list_hosts(self):
'''
hosts.list_hosts
'''
self.__clean_hosts()
hosts = self.run_function('hosts.list_hosts')
self.assertEqual(len(hosts), 10)
2012-07-23 19:00:54 +00:00
self.assertEqual(hosts['::1'], ['ip6-localhost', 'ip6-loopback'])
self.assertEqual(hosts['127.0.0.1'], ['localhost', 'myname'])
def test_list_hosts_nofile(self):
'''
hosts.list_hosts
without a hosts file
'''
if os.path.isfile(HFN):
os.remove(HFN)
hosts = self.run_function('hosts.list_hosts')
self.assertEqual(hosts, {})
def test_get_ip(self):
'''
hosts.get_ip
'''
self.__clean_hosts()
2012-06-30 20:10:34 +00:00
self.assertEqual(
self.run_function('hosts.get_ip', ['myname']), '127.0.0.1'
)
self.assertEqual(self.run_function('hosts.get_ip', ['othername']), '')
self.__clear_hosts()
self.assertEqual(self.run_function('hosts.get_ip', ['othername']), '')
def test_get_alias(self):
'''
hosts.get_alias
'''
self.__clean_hosts()
2012-06-30 20:10:34 +00:00
self.assertEqual(
self.run_function('hosts.get_alias', ['127.0.0.1']),
2012-07-23 19:00:54 +00:00
['localhost', 'myname']
2012-06-30 20:10:34 +00:00
)
self.assertEqual(
self.run_function('hosts.get_alias', ['127.0.0.2']),
2012-07-23 19:00:54 +00:00
[]
2012-06-30 20:10:34 +00:00
)
self.__clear_hosts()
2012-06-30 20:10:34 +00:00
self.assertEqual(
self.run_function('hosts.get_alias', ['127.0.0.1']),
2012-07-23 19:00:54 +00:00
[]
2012-06-30 20:10:34 +00:00
)
def test_has_pair(self):
'''
hosts.has_pair
'''
self.__clean_hosts()
2012-06-30 20:10:34 +00:00
self.assertTrue(
self.run_function('hosts.has_pair', ['127.0.0.1', 'myname'])
)
self.assertFalse(
self.run_function('hosts.has_pair', ['127.0.0.1', 'othername'])
)
def test_set_host(self):
'''
hosts.set_hosts
'''
self.__clean_hosts()
self.assertTrue(
self.run_function('hosts.set_host', ['192.168.1.123', 'newip'])
)
2012-06-30 20:10:34 +00:00
self.assertTrue(
self.run_function('hosts.has_pair', ['192.168.1.123', 'newip'])
)
self.assertTrue(
self.run_function('hosts.set_host', ['127.0.0.1', 'localhost'])
)
self.assertEqual(len(self.run_function('hosts.list_hosts')), 11)
2012-06-30 20:10:34 +00:00
self.assertFalse(
self.run_function('hosts.has_pair', ['127.0.0.1', 'myname']),
'should remove second entry'
)
def test_add_host(self):
'''
hosts.add_host
'''
self.__clean_hosts()
self.assertTrue(
self.run_function('hosts.add_host', ['192.168.1.123', 'newip'])
)
2012-06-30 20:10:34 +00:00
self.assertTrue(
self.run_function('hosts.has_pair', ['192.168.1.123', 'newip'])
)
self.assertEqual(len(self.run_function('hosts.list_hosts')), 11)
self.assertTrue(
self.run_function('hosts.add_host', ['127.0.0.1', 'othernameip'])
2012-06-30 20:10:34 +00:00
)
2014-09-03 14:43:52 +00:00
self.assertEqual(len(self.run_function('hosts.list_hosts')), 11)
def test_rm_host(self):
self.__clean_hosts()
self.assertTrue(
self.run_function('hosts.has_pair', ['127.0.0.1', 'myname'])
)
self.assertTrue(
self.run_function('hosts.rm_host', ['127.0.0.1', 'myname'])
)
self.assertFalse(
self.run_function('hosts.has_pair', ['127.0.0.1', 'myname'])
)
self.assertTrue(
self.run_function('hosts.rm_host', ['127.0.0.1', 'unknown'])
)
def test_add_host_formatting(self):
'''
Ensure that hosts.add_host isn't adding duplicates and that
it's formatting the output correctly
'''
# instead of using the 'clean' hosts file we're going to
# use an empty one so we can prove the syntax of the entries
# being added by the hosts module
self.__clear_hosts()
with salt.utils.files.fopen(HFN, 'w'):
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
pass
self.assertTrue(
self.run_function(
'hosts.add_host', ['192.168.1.3', 'host3.fqdn.com']
)
2012-06-30 20:10:34 +00:00
)
self.assertTrue(
self.run_function(
'hosts.add_host', ['192.168.1.1', 'host1.fqdn.com']
)
)
self.assertTrue(
self.run_function('hosts.add_host', ['192.168.1.1', 'host1'])
)
self.assertTrue(
self.run_function(
'hosts.add_host', ['192.168.1.2', 'host2.fqdn.com']
)
2012-06-30 20:10:34 +00:00
)
self.assertTrue(
self.run_function('hosts.add_host', ['192.168.1.2', 'host2'])
2012-06-30 20:10:34 +00:00
)
self.assertTrue(
self.run_function('hosts.add_host', ['192.168.1.2', 'oldhost2'])
2012-06-30 20:10:34 +00:00
)
self.assertTrue(
self.run_function(
'hosts.add_host', ['192.168.1.2', 'host2-reorder']
)
)
self.assertTrue(
self.run_function(
'hosts.add_host', ['192.168.1.1', 'host1-reorder']
)
2012-06-30 20:10:34 +00:00
)
# now read the lines and ensure they're formatted correctly
with salt.utils.files.fopen(HFN, 'r') as fp_:
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
lines = fp_.read().splitlines()
self.assertEqual(lines, [
'192.168.1.3\t\thost3.fqdn.com',
'192.168.1.1\t\thost1.fqdn.com host1 host1-reorder',
'192.168.1.2\t\thost2.fqdn.com host2 oldhost2 host2-reorder',
])