2013-11-27 11:19:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-02-12 23:03:31 +00:00
|
|
|
'''
|
|
|
|
Test the hosts module
|
|
|
|
'''
|
2012-11-18 18:57:10 +00:00
|
|
|
# Import python libs
|
2011-12-30 23:21:08 +00:00
|
|
|
import os
|
2012-02-14 07:27:38 +00:00
|
|
|
import shutil
|
2011-12-30 23:21:08 +00:00
|
|
|
|
2013-06-27 11:08:45 +00:00
|
|
|
# Import Salt Testing libs
|
|
|
|
from salttesting.helpers import ensure_in_syspath
|
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
|
|
|
# Import salt libs
|
|
|
|
import integration
|
2012-11-18 18:57:10 +00:00
|
|
|
import salt.utils
|
2011-12-30 23:21:08 +00:00
|
|
|
|
2012-02-20 12:18:13 +00:00
|
|
|
HFN = os.path.join(integration.TMP, 'hosts')
|
2012-02-14 07:27:38 +00:00
|
|
|
|
2012-05-05 14:09:23 +00:00
|
|
|
|
2012-02-20 12:18:13 +00:00
|
|
|
class HostsModuleTest(integration.ModuleCase):
|
2012-02-14 07:27:38 +00:00
|
|
|
'''
|
|
|
|
Test the hosts module
|
|
|
|
'''
|
2013-04-22 23:00:51 +00:00
|
|
|
|
|
|
|
maxDiff = None
|
|
|
|
|
2012-02-14 07:27:38 +00:00
|
|
|
def __clean_hosts(self):
|
|
|
|
'''
|
|
|
|
Clean out the hosts file
|
|
|
|
'''
|
2012-02-20 12:18:13 +00:00
|
|
|
shutil.copyfile(os.path.join(integration.FILES, 'hosts'), HFN)
|
2011-12-30 23:21:08 +00:00
|
|
|
|
2012-02-14 07:27:38 +00:00
|
|
|
def __clear_hosts(self):
|
|
|
|
'''
|
|
|
|
Delete the tmp hosts file
|
|
|
|
'''
|
|
|
|
if os.path.isfile(HFN):
|
|
|
|
os.remove(HFN)
|
2011-12-30 23:21:08 +00:00
|
|
|
|
2012-02-14 07:27:38 +00:00
|
|
|
def tearDown(self):
|
|
|
|
'''
|
|
|
|
Make sure the tmp hosts file is gone
|
|
|
|
'''
|
|
|
|
self.__clear_hosts()
|
2011-12-30 23:21:08 +00:00
|
|
|
|
|
|
|
def test_list_hosts(self):
|
2012-02-14 07:27:38 +00:00
|
|
|
'''
|
|
|
|
hosts.list_hosts
|
|
|
|
'''
|
|
|
|
self.__clean_hosts()
|
|
|
|
hosts = self.run_function('hosts.list_hosts')
|
2011-12-30 23:21:08 +00:00
|
|
|
self.assertEqual(len(hosts), 6)
|
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'])
|
2011-12-30 23:21:08 +00:00
|
|
|
|
|
|
|
def test_list_hosts_nofile(self):
|
2012-02-14 07:27:38 +00:00
|
|
|
'''
|
|
|
|
hosts.list_hosts
|
|
|
|
without a hosts file
|
|
|
|
'''
|
|
|
|
if os.path.isfile(HFN):
|
|
|
|
os.remove(HFN)
|
|
|
|
hosts = self.run_function('hosts.list_hosts')
|
2011-12-30 23:21:08 +00:00
|
|
|
self.assertEqual(hosts, {})
|
|
|
|
|
|
|
|
def test_get_ip(self):
|
2012-02-14 07:27:38 +00:00
|
|
|
'''
|
|
|
|
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'
|
|
|
|
)
|
2012-02-14 07:27:38 +00:00
|
|
|
self.assertEqual(self.run_function('hosts.get_ip', ['othername']), '')
|
|
|
|
self.__clear_hosts()
|
|
|
|
self.assertEqual(self.run_function('hosts.get_ip', ['othername']), '')
|
2011-12-30 23:21:08 +00:00
|
|
|
|
|
|
|
def test_get_alias(self):
|
2012-02-14 07:27:38 +00:00
|
|
|
'''
|
|
|
|
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
|
|
|
)
|
2012-02-14 07:27:38 +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
|
|
|
)
|
2011-12-30 23:21:08 +00:00
|
|
|
|
|
|
|
def test_has_pair(self):
|
2012-02-14 07:27:38 +00:00
|
|
|
'''
|
|
|
|
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'])
|
|
|
|
)
|
2011-12-30 23:21:08 +00:00
|
|
|
|
|
|
|
def test_set_host(self):
|
2012-02-14 07:27:38 +00:00
|
|
|
'''
|
|
|
|
hosts.set_hosts
|
|
|
|
'''
|
|
|
|
self.__clean_hosts()
|
2013-04-22 23:00:51 +00:00
|
|
|
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'])
|
|
|
|
)
|
2013-04-22 23:00:51 +00:00
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('hosts.set_host', ['127.0.0.1', 'localhost'])
|
|
|
|
)
|
2012-02-14 07:27:38 +00:00
|
|
|
self.assertEqual(len(self.run_function('hosts.list_hosts')), 7)
|
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'
|
|
|
|
)
|
2011-12-30 23:21:08 +00:00
|
|
|
|
|
|
|
def test_add_host(self):
|
2012-02-14 07:27:38 +00:00
|
|
|
'''
|
|
|
|
hosts.add_host
|
|
|
|
'''
|
|
|
|
self.__clean_hosts()
|
2013-04-22 23:00:51 +00:00
|
|
|
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'])
|
|
|
|
)
|
2012-02-14 07:27:38 +00:00
|
|
|
self.assertEqual(len(self.run_function('hosts.list_hosts')), 7)
|
2013-04-22 23:00:51 +00:00
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('hosts.add_host', ['127.0.0.1', 'othernameip'])
|
2012-06-30 20:10:34 +00:00
|
|
|
)
|
2012-02-14 07:27:38 +00:00
|
|
|
self.assertEqual(len(self.run_function('hosts.list_hosts')), 7)
|
2011-12-30 23:21:08 +00:00
|
|
|
|
|
|
|
def test_rm_host(self):
|
2012-02-14 07:27:38 +00:00
|
|
|
self.__clean_hosts()
|
2013-04-23 11:42:03 +00:00
|
|
|
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'])
|
|
|
|
)
|
2012-04-08 18:33:30 +00:00
|
|
|
|
|
|
|
def test_add_host_formatting(self):
|
|
|
|
'''
|
|
|
|
Ensure that hosts.add_host isn't adding duplicates and that
|
|
|
|
it's formatting the output correctly
|
|
|
|
'''
|
2013-06-27 11:08:45 +00:00
|
|
|
# instead of using the 'clean' hosts file we're going to
|
2012-04-08 18:33:30 +00:00
|
|
|
# use an empty one so we can prove the syntax of the entries
|
|
|
|
# being added by the hosts module
|
|
|
|
self.__clear_hosts()
|
2012-11-18 18:57:10 +00:00
|
|
|
f = salt.utils.fopen(HFN, 'w')
|
2012-04-08 18:33:30 +00:00
|
|
|
f.close()
|
|
|
|
|
2013-04-23 11:42:03 +00:00
|
|
|
self.assertTrue(
|
|
|
|
self.run_function(
|
|
|
|
'hosts.add_host', ['192.168.1.3', 'host3.fqdn.com']
|
|
|
|
)
|
2012-06-30 20:10:34 +00:00
|
|
|
)
|
2013-04-23 11:42:03 +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
|
|
|
)
|
2013-04-23 11:42:03 +00:00
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('hosts.add_host', ['192.168.1.2', 'host2'])
|
2012-06-30 20:10:34 +00:00
|
|
|
)
|
2013-04-23 11:42:03 +00:00
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('hosts.add_host', ['192.168.1.2', 'oldhost2'])
|
2012-06-30 20:10:34 +00:00
|
|
|
)
|
2013-04-23 11:42:03 +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
|
|
|
)
|
2012-04-08 18:33:30 +00:00
|
|
|
|
|
|
|
# now read the lines and ensure they're formatted correctly
|
2013-04-23 11:42:03 +00:00
|
|
|
lines = salt.utils.fopen(HFN, 'r').read().splitlines()
|
2012-04-08 18:33:30 +00:00
|
|
|
self.assertEqual(lines, [
|
2013-04-23 11:42:03 +00:00
|
|
|
'192.168.1.3\t\thost3.fqdn.com',
|
2013-08-29 08:50:20 +00:00
|
|
|
'192.168.1.1\t\thost1.fqdn.com\t\thost1\t\thost1-reorder',
|
|
|
|
'192.168.1.2\t\thost2.fqdn.com\t\thost2\t\toldhost2\t\thost2-reorder',
|
2013-04-22 23:00:51 +00:00
|
|
|
])
|
2012-05-05 14:09:23 +00:00
|
|
|
|
2012-07-20 06:21:01 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(HostsModuleTest)
|