def test_set_host(self): tmp = self.tmp_hosts_file(self.hostspath) list_hosts.hosts_filename = tmp set_host.hosts_filename = tmp assert set_host('192.168.1.123', 'newip') self.assertTrue(has_pair('192.168.1.123', 'newip')) self.assertEqual(len(list_hosts()), 7) assert set_host('127.0.0.1', 'localhost') self.assertFalse(has_pair('127.0.0.1', 'myname'), 'should remove second entry')
def test_set_host(self): ''' Tests true if the alias is set ''' mock_opt = MagicMock(return_value=None) with patch.dict(hosts.__salt__, {'config.option': mock_opt}): self.assertFalse(hosts.set_host('10.10.10.10', 'Salt1'))
def test_set_host(self): """ Tests true if the alias is set """ mock_opt = MagicMock(return_value=None) with patch.dict(hosts.__salt__, {"config.option": mock_opt}): self.assertFalse(hosts.set_host("10.10.10.10", "Salt1"))
def test_set_host_true(self): ''' Tests true if the alias is set ''' with patch('salt.utils.fopen', mock_open()): 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'))
def test_set_host_true(self): """ Tests true if the alias is set """ with patch("salt.utils.fopen", mock_open()): 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"))
def test_set_host(self): ''' Tests true if the alias is set ''' with patch('salt.modules.hosts.__get_hosts_filename', MagicMock(return_value='/etc/hosts')), \ patch('os.path.isfile', MagicMock(return_value=False)), \ patch.dict(hosts.__salt__, {'config.option': MagicMock(return_value=None)}): self.assertFalse(hosts.set_host('10.10.10.10', 'Salt1'))
def test_set_host_true(self): ''' Tests true if the alias is set ''' with patch('salt.modules.hosts.__get_hosts_filename', MagicMock(return_value='/etc/hosts')), \ patch('os.path.isfile', MagicMock(return_value=True)), \ patch('salt.utils.files.fopen', mock_open()): 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'))
def test_set_host_true_remove(self): ''' Test if an empty hosts value removes existing entries ''' 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', )) ] class TmpStringIO(StringIO): def __init__(self, fn, mode='r'): initial_value = data[0] if 'w' in mode: initial_value = '' StringIO.__init__(self, initial_value) def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.close() def close(self): # 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() StringIO.close(self) expected = '\n'.join(( '2.2.2.2 bar.barbar bar', '3.3.3.3 asdf.asdfadsf asdf', )) + '\n' with patch('salt.utils.fopen', TmpStringIO): mock_opt = MagicMock(return_value=None) with patch.dict(hosts.__salt__, {'config.option': mock_opt}): self.assertTrue(hosts.set_host('1.1.1.1', ' ')) self.assertEqual(data[0], expected)
def test_set_host_true(self): """ Tests true if the alias is set """ with patch( "salt.modules.hosts.__get_hosts_filename", MagicMock(return_value="/etc/hosts"), ), patch("os.path.isfile", MagicMock(return_value=True)), patch("salt.utils.files.fopen", mock_open(b"")): 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"))
def test_set_host(self): ''' Tests true if the alias is set ''' hosts_file = '/etc/hosts' if salt.utils.is_windows(): hosts_file = r'C:\Windows\System32\Drivers\etc\hosts' with patch('salt.modules.hosts.__get_hosts_filename', MagicMock(return_value=hosts_file)), \ patch('os.path.isfile', MagicMock(return_value=False)), \ patch.dict(hosts.__salt__, {'config.option': MagicMock(return_value=None)}): self.assertFalse(hosts.set_host('10.10.10.10', 'Salt1'))
def test_set_host(self): """ Tests true if the alias is set """ hosts_file = "/etc/hosts" if salt.utils.platform.is_windows(): hosts_file = r"C:\Windows\System32\Drivers\etc\hosts" with patch( "salt.modules.hosts.__get_hosts_filename", MagicMock(return_value=hosts_file), ), patch("os.path.isfile", MagicMock(return_value=False)), patch.dict( hosts.__salt__, {"config.option": MagicMock(return_value=None)}): self.assertFalse(hosts.set_host("10.10.10.10", "Salt1"))
def test_set_host_true_remove(self): ''' Test if an empty hosts value removes existing entries ''' 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', ))] class TmpStringIO(StringIO): def __init__(self, fn, mode='r'): initial_value = data[0] if 'w' in mode: initial_value = '' StringIO.__init__(self, initial_value) def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.close() def close(self): data[0] = self.getvalue() StringIO.close(self) expected = '\n'.join(( '2.2.2.2 bar.barbar bar', '3.3.3.3 asdf.asdfadsf asdf', )) + '\n' with patch('salt.utils.files.fopen', TmpStringIO): mock_opt = MagicMock(return_value=None) with patch.dict(hosts.__salt__, {'config.option': mock_opt}): self.assertTrue(hosts.set_host('1.1.1.1', ' ')) self.assertEqual(data[0], expected)
def test_set_host_true_remove(self): """ Test if an empty hosts value removes existing entries """ 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", ) ) ] class TmpStringIO(StringIO): def __init__(self, fn, mode="r"): initial_value = data[0] if "w" in mode: initial_value = "" StringIO.__init__(self, initial_value) def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.close() def close(self): data[0] = self.getvalue() StringIO.close(self) expected = "\n".join(("2.2.2.2 bar.barbar bar", "3.3.3.3 asdf.asdfadsf asdf")) + "\n" with patch("salt.utils.fopen", TmpStringIO): mock_opt = MagicMock(return_value=None) with patch.dict(hosts.__salt__, {"config.option": mock_opt}): self.assertTrue(hosts.set_host("1.1.1.1", " ")) self.assertEqual(data[0], expected)
def test_set_host_true_remove(self): """ Test if an empty hosts value removes existing entries """ 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", )) ] class TmpStringIO(io.StringIO): def __init__(self, fn, mode="r"): self.mode = mode initial_value = data[0] if "w" in self.mode: initial_value = "" super().__init__(initial_value) def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.close() def close(self): # 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() io.StringIO.close(self) def read(self, *args): ret = super().read(*args) if "b" in self.mode: return salt.utils.stringutils.to_bytes(ret) else: return ret def write(self, s, *args): 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().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().write(s, *args) def readlines(self): ret = super().readlines() if "b" in self.mode: return salt.utils.data.encode(ret) else: return ret def writelines(self, lines): for line in lines: self.write(line) expected = ("\n".join(( "2.2.2.2 bar.barbar bar", "3.3.3.3 asdf.asdfadsf asdf", )) + "\n") with patch("salt.utils.files.fopen", TmpStringIO): mock_opt = MagicMock(return_value=None) with patch.dict(hosts.__salt__, {"config.option": mock_opt}): self.assertTrue(hosts.set_host("1.1.1.1", " ")) self.assertEqual(data[0], expected)