2014-12-23 06:23:19 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2018-05-28 21:13:12 +00:00
|
|
|
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
|
2014-12-23 06:23:19 +00:00
|
|
|
'''
|
|
|
|
|
2015-01-08 02:32:28 +00:00
|
|
|
# Import Python libs
|
2018-01-16 21:48:58 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2015-01-08 02:32:28 +00:00
|
|
|
|
2014-12-23 06:23:19 +00:00
|
|
|
# Import Salt Testing Libs
|
2017-03-29 00:22:57 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase
|
|
|
|
from tests.support.mock import (
|
2014-12-23 06:23:19 +00:00
|
|
|
MagicMock,
|
|
|
|
mock_open,
|
|
|
|
patch,
|
|
|
|
)
|
|
|
|
# Import Salt Libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.hosts as hosts
|
2018-01-25 20:40:45 +00:00
|
|
|
import salt.utils.data
|
2017-10-14 01:52:56 +00:00
|
|
|
import salt.utils.platform
|
2018-01-25 20:40:45 +00:00
|
|
|
import salt.utils.stringutils
|
2015-08-26 00:12:45 +00:00
|
|
|
from salt.ext.six.moves import StringIO
|
2018-01-25 20:40:45 +00:00
|
|
|
from salt.ext import six
|
2014-12-23 06:23:19 +00:00
|
|
|
|
|
|
|
|
2017-03-29 00:22:57 +00:00
|
|
|
class HostsTestCase(TestCase, LoaderModuleMockMixin):
|
2014-12-23 06:23:19 +00:00
|
|
|
'''
|
|
|
|
TestCase for salt.modules.hosts
|
|
|
|
'''
|
2017-03-29 00:22:57 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {hosts: {}}
|
2014-12-23 06:23:19 +00:00
|
|
|
|
|
|
|
# 'list_hosts' function tests: 1
|
|
|
|
|
|
|
|
def test_list_hosts(self):
|
|
|
|
'''
|
|
|
|
Tests return the hosts found in the hosts file
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch('salt.modules.hosts._list_hosts',
|
|
|
|
MagicMock(return_value={'10.10.10.10': ['Salt1', 'Salt2']})):
|
|
|
|
self.assertDictEqual({'10.10.10.10': ['Salt1', 'Salt2']},
|
|
|
|
hosts.list_hosts())
|
2014-12-23 06:23:19 +00:00
|
|
|
|
|
|
|
# 'get_ip' function tests: 3
|
|
|
|
|
|
|
|
def test_get_ip(self):
|
|
|
|
'''
|
|
|
|
Tests return ip associated with the named host
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch('salt.modules.hosts._list_hosts',
|
|
|
|
MagicMock(return_value={'10.10.10.10': ['Salt1', 'Salt2']})):
|
|
|
|
self.assertEqual('10.10.10.10', hosts.get_ip('Salt1'))
|
2014-12-23 06:23:19 +00:00
|
|
|
|
2017-04-10 13:00:57 +00:00
|
|
|
self.assertEqual('', hosts.get_ip('Salt3'))
|
2014-12-23 06:23:19 +00:00
|
|
|
|
|
|
|
def test_get_ip_none(self):
|
|
|
|
'''
|
|
|
|
Tests return ip associated with the named host
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch('salt.modules.hosts._list_hosts', MagicMock(return_value='')):
|
|
|
|
self.assertEqual('', hosts.get_ip('Salt1'))
|
2014-12-23 06:23:19 +00:00
|
|
|
|
|
|
|
# 'get_alias' function tests: 2
|
|
|
|
|
|
|
|
def test_get_alias(self):
|
|
|
|
'''
|
|
|
|
Tests return the list of aliases associated with an ip
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch('salt.modules.hosts._list_hosts',
|
|
|
|
MagicMock(return_value={'10.10.10.10': ['Salt1', 'Salt2']})):
|
|
|
|
self.assertListEqual(['Salt1', 'Salt2'], hosts.get_alias('10.10.10.10'))
|
2014-12-23 06:23:19 +00:00
|
|
|
|
|
|
|
def test_get_alias_none(self):
|
|
|
|
'''
|
|
|
|
Tests return the list of aliases associated with an ip
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch('salt.modules.hosts._list_hosts',
|
|
|
|
MagicMock(return_value={'10.10.10.10': ['Salt1', 'Salt2']})):
|
|
|
|
self.assertListEqual([], hosts.get_alias('10.10.10.11'))
|
2014-12-23 06:23:19 +00:00
|
|
|
|
|
|
|
# 'has_pair' function tests: 1
|
|
|
|
|
|
|
|
def test_has_pair(self):
|
|
|
|
'''
|
|
|
|
Tests return True / False if the alias is set
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch('salt.modules.hosts._list_hosts',
|
|
|
|
MagicMock(return_value={'10.10.10.10': ['Salt1', 'Salt2']})):
|
|
|
|
self.assertTrue(hosts.has_pair('10.10.10.10', 'Salt1'))
|
2014-12-23 06:23:19 +00:00
|
|
|
|
2017-04-10 13:00:57 +00:00
|
|
|
self.assertFalse(hosts.has_pair('10.10.10.10', 'Salt3'))
|
2014-12-23 06:23:19 +00:00
|
|
|
|
2015-08-26 00:12:45 +00:00
|
|
|
# 'set_host' function tests: 3
|
2014-12-23 06:23:19 +00:00
|
|
|
|
|
|
|
def test_set_host(self):
|
|
|
|
'''
|
|
|
|
Tests true if the alias is set
|
|
|
|
'''
|
2017-09-08 23:21:01 +00:00
|
|
|
hosts_file = '/etc/hosts'
|
2017-09-26 19:11:57 +00:00
|
|
|
if salt.utils.platform.is_windows():
|
2017-09-08 23:21:01 +00:00
|
|
|
hosts_file = r'C:\Windows\System32\Drivers\etc\hosts'
|
|
|
|
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch('salt.modules.hosts.__get_hosts_filename',
|
2017-09-08 23:21:01 +00:00
|
|
|
MagicMock(return_value=hosts_file)), \
|
2017-04-10 13:00:57 +00:00
|
|
|
patch('os.path.isfile', MagicMock(return_value=False)), \
|
|
|
|
patch.dict(hosts.__salt__,
|
|
|
|
{'config.option': MagicMock(return_value=None)}):
|
2014-12-23 06:23:19 +00:00
|
|
|
self.assertFalse(hosts.set_host('10.10.10.10', 'Salt1'))
|
|
|
|
|
|
|
|
def test_set_host_true(self):
|
|
|
|
'''
|
|
|
|
Tests true if the alias is set
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch('salt.modules.hosts.__get_hosts_filename',
|
|
|
|
MagicMock(return_value='/etc/hosts')), \
|
|
|
|
patch('os.path.isfile', MagicMock(return_value=True)), \
|
2018-06-23 20:04:47 +00:00
|
|
|
patch('salt.utils.files.fopen', mock_open(b'')):
|
2014-12-23 06:23:19 +00:00
|
|
|
mock_opt = MagicMock(return_value=None)
|
|
|
|
with patch.dict(hosts.__salt__, {'config.option': mock_opt}):
|
|
|
|
self.assertTrue(hosts.set_host('10.10.10.10', 'Salt1'))
|
|
|
|
|
2015-08-26 00:12:45 +00:00
|
|
|
def test_set_host_true_remove(self):
|
|
|
|
'''
|
|
|
|
Test if an empty hosts value removes existing entries
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch('salt.modules.hosts.__get_hosts_filename',
|
|
|
|
MagicMock(return_value='/etc/hosts')), \
|
|
|
|
patch('os.path.isfile', MagicMock(return_value=True)):
|
|
|
|
data = ['\n'.join((
|
|
|
|
'1.1.1.1 foo.foofoo foo',
|
|
|
|
'2.2.2.2 bar.barbar bar',
|
|
|
|
'3.3.3.3 asdf.asdfadsf asdf',
|
|
|
|
'1.1.1.1 foofoo.foofoo foofoo',
|
|
|
|
))]
|
|
|
|
|
2018-01-25 20:40:45 +00:00
|
|
|
class TmpStringIO(StringIO, object):
|
2017-04-10 13:00:57 +00:00
|
|
|
def __init__(self, fn, mode='r'):
|
2018-01-25 20:40:45 +00:00
|
|
|
self.mode = mode
|
2017-04-10 13:00:57 +00:00
|
|
|
initial_value = data[0]
|
2018-01-25 20:40:45 +00:00
|
|
|
if 'w' in self.mode:
|
2017-04-10 13:00:57 +00:00
|
|
|
initial_value = ''
|
2018-01-25 20:40:45 +00:00
|
|
|
super(TmpStringIO, self).__init__(initial_value)
|
2017-04-10 13:00:57 +00:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def close(self):
|
2017-09-08 23:21:01 +00:00
|
|
|
# Don't save unless there's something there. In Windows
|
|
|
|
# the class gets initialized the first time with mode = w
|
|
|
|
# which sets the initial value to ''. When the class closes
|
|
|
|
# it clears out data and causes the test to fail.
|
|
|
|
# I don't know why it get's initialized with a mode of 'w'
|
|
|
|
# For the purposes of this test data shouldn't be empty
|
|
|
|
# This is a problem with this class and not with the hosts
|
|
|
|
# module
|
|
|
|
if self.getvalue():
|
|
|
|
data[0] = self.getvalue()
|
2017-04-10 13:00:57 +00:00
|
|
|
StringIO.close(self)
|
|
|
|
|
2018-01-25 20:40:45 +00:00
|
|
|
def read(self, *args):
|
|
|
|
ret = super(TmpStringIO, self).read(*args)
|
|
|
|
if six.PY3 and 'b' in self.mode:
|
|
|
|
return salt.utils.stringutils.to_bytes(ret)
|
|
|
|
else:
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def write(self, s, *args):
|
|
|
|
if six.PY3:
|
|
|
|
if 'b' in self.mode:
|
|
|
|
if not isinstance(s, bytes):
|
|
|
|
# Make this act like a binary filehandle
|
|
|
|
raise TypeError("a bytes-like object is required, not 'str'")
|
|
|
|
# The StringIO wants a str type, it won't take
|
|
|
|
# bytes. Convert before writing to it.
|
|
|
|
return super(TmpStringIO, self).write(
|
|
|
|
salt.utils.stringutils.to_str(s), *args)
|
|
|
|
else:
|
|
|
|
if not isinstance(s, str):
|
|
|
|
# Make this act like a non-binary filehandle
|
|
|
|
raise TypeError("write() argument must be str, not bytes")
|
|
|
|
return super(TmpStringIO, self).write(s, *args)
|
|
|
|
|
|
|
|
def readlines(self):
|
|
|
|
ret = super(TmpStringIO, self).readlines()
|
|
|
|
if six.PY3 and 'b' in self.mode:
|
|
|
|
return salt.utils.data.encode(ret)
|
|
|
|
else:
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def writelines(self, lines):
|
|
|
|
for line in lines:
|
|
|
|
self.write(line)
|
|
|
|
|
2017-04-10 13:00:57 +00:00
|
|
|
expected = '\n'.join((
|
|
|
|
'2.2.2.2 bar.barbar bar',
|
|
|
|
'3.3.3.3 asdf.asdfadsf asdf',
|
|
|
|
)) + '\n'
|
|
|
|
|
2017-07-18 16:31:01 +00:00
|
|
|
with patch('salt.utils.files.fopen', TmpStringIO):
|
2017-04-10 13:00:57 +00:00
|
|
|
mock_opt = MagicMock(return_value=None)
|
|
|
|
with patch.dict(hosts.__salt__, {'config.option': mock_opt}):
|
|
|
|
self.assertTrue(hosts.set_host('1.1.1.1', ' '))
|
2017-09-08 23:21:01 +00:00
|
|
|
|
2017-04-10 13:00:57 +00:00
|
|
|
self.assertEqual(data[0], expected)
|
2015-08-26 00:12:45 +00:00
|
|
|
|
2014-12-23 06:23:19 +00:00
|
|
|
# 'rm_host' function tests: 2
|
|
|
|
|
|
|
|
def test_rm_host(self):
|
|
|
|
'''
|
|
|
|
Tests if specified host entry gets removed from the hosts file
|
|
|
|
'''
|
2018-06-23 20:04:47 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open(b'')), \
|
2017-04-10 13:00:57 +00:00
|
|
|
patch('salt.modules.hosts.__get_hosts_filename',
|
|
|
|
MagicMock(return_value='/etc/hosts')), \
|
|
|
|
patch('salt.modules.hosts.has_pair',
|
|
|
|
MagicMock(return_value=True)), \
|
|
|
|
patch('os.path.isfile', MagicMock(return_value=True)):
|
2014-12-23 06:23:19 +00:00
|
|
|
mock_opt = MagicMock(return_value=None)
|
|
|
|
with patch.dict(hosts.__salt__, {'config.option': mock_opt}):
|
|
|
|
self.assertTrue(hosts.rm_host('10.10.10.10', 'Salt1'))
|
|
|
|
|
|
|
|
def test_rm_host_false(self):
|
|
|
|
'''
|
|
|
|
Tests if specified host entry gets removed from the hosts file
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch('salt.modules.hosts.has_pair', MagicMock(return_value=False)):
|
|
|
|
self.assertTrue(hosts.rm_host('10.10.10.10', 'Salt1'))
|
2014-12-23 06:23:19 +00:00
|
|
|
|
2014-12-23 10:13:17 +00:00
|
|
|
# 'add_host' function tests: 3
|
2014-12-23 06:23:19 +00:00
|
|
|
|
|
|
|
def test_add_host(self):
|
|
|
|
'''
|
|
|
|
Tests if specified host entry gets added from the hosts file
|
|
|
|
'''
|
2017-09-08 23:21:01 +00:00
|
|
|
hosts_file = '/etc/hosts'
|
2017-09-26 19:11:57 +00:00
|
|
|
if salt.utils.platform.is_windows():
|
2017-09-08 23:21:01 +00:00
|
|
|
hosts_file = r'C:\Windows\System32\Drivers\etc\hosts'
|
|
|
|
|
2017-07-18 16:31:01 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open()), \
|
2017-04-10 13:00:57 +00:00
|
|
|
patch('salt.modules.hosts.__get_hosts_filename',
|
2017-09-08 23:21:01 +00:00
|
|
|
MagicMock(return_value=hosts_file)):
|
2014-12-23 06:23:19 +00:00
|
|
|
mock_opt = MagicMock(return_value=None)
|
|
|
|
with patch.dict(hosts.__salt__, {'config.option': mock_opt}):
|
|
|
|
self.assertTrue(hosts.add_host('10.10.10.10', 'Salt1'))
|
|
|
|
|
|
|
|
def test_add_host_no_file(self):
|
|
|
|
'''
|
|
|
|
Tests if specified host entry gets added from the hosts file
|
|
|
|
'''
|
2017-07-18 16:31:01 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open()), \
|
2017-04-10 13:00:57 +00:00
|
|
|
patch('os.path.isfile', MagicMock(return_value=False)):
|
2014-12-23 06:23:19 +00:00
|
|
|
mock_opt = MagicMock(return_value=None)
|
|
|
|
with patch.dict(hosts.__salt__, {'config.option': mock_opt}):
|
|
|
|
self.assertFalse(hosts.add_host('10.10.10.10', 'Salt1'))
|
|
|
|
|
|
|
|
def test_add_host_create_entry(self):
|
|
|
|
'''
|
|
|
|
Tests if specified host entry gets added from the hosts file
|
|
|
|
'''
|
2017-07-18 16:31:01 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open()), \
|
2017-04-10 13:00:57 +00:00
|
|
|
patch('os.path.isfile', MagicMock(return_value=True)):
|
2014-12-23 06:23:19 +00:00
|
|
|
mock_opt = MagicMock(return_value=None)
|
|
|
|
with patch.dict(hosts.__salt__, {'config.option': mock_opt}):
|
|
|
|
self.assertTrue(hosts.add_host('10.10.10.10', 'Salt1'))
|