mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Updates hosts.rm_host tests
This commit is contained in:
parent
be7f141ce1
commit
fc352b4b1e
2 changed files with 24 additions and 30 deletions
|
@ -1 +1 @@
|
|||
Now hosts.rm_host parses /etc/hosts that have comments in hosts entries and is able to remove hosts entries or aliases.
|
||||
Now hosts.rm_host can remove entries from /etc/hosts when this file have inline comments.
|
||||
|
|
|
@ -1,20 +1,14 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
|
||||
"""
|
||||
|
||||
# Import Python libs
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
# Import Salt Libs
|
||||
import salt.modules.hosts as hosts
|
||||
import salt.utils.data
|
||||
import salt.utils.platform
|
||||
import salt.utils.stringutils
|
||||
from salt.ext import six
|
||||
from salt.ext.six.moves import StringIO
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.mock import MagicMock, mock_open, patch
|
||||
from tests.support.unit import TestCase
|
||||
|
@ -150,13 +144,13 @@ class HostsTestCase(TestCase, LoaderModuleMockMixin):
|
|||
)
|
||||
]
|
||||
|
||||
class TmpStringIO(StringIO, object):
|
||||
class TmpStringIO(StringIO):
|
||||
def __init__(self, fn, mode="r"):
|
||||
self.mode = mode
|
||||
initial_value = data[0]
|
||||
if "w" in self.mode:
|
||||
initial_value = ""
|
||||
super(TmpStringIO, self).__init__(initial_value)
|
||||
super().__init__(initial_value)
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
@ -178,35 +172,30 @@ class HostsTestCase(TestCase, LoaderModuleMockMixin):
|
|||
StringIO.close(self)
|
||||
|
||||
def read(self, *args):
|
||||
ret = super(TmpStringIO, self).read(*args)
|
||||
ret = super().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
|
||||
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'"
|
||||
)
|
||||
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)
|
||||
# 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(TmpStringIO, self).readlines()
|
||||
ret = super().readlines()
|
||||
if six.PY3 and "b" in self.mode:
|
||||
return salt.utils.data.encode(ret)
|
||||
else:
|
||||
|
@ -234,7 +223,12 @@ class HostsTestCase(TestCase, LoaderModuleMockMixin):
|
|||
"""
|
||||
Tests if specified host entry gets removed from the hosts file
|
||||
"""
|
||||
with patch("salt.utils.files.fopen", mock_open(b"")), patch(
|
||||
hosts_content = (
|
||||
b"# one line comment\n"
|
||||
b"10.10.10.10 Salt1\n"
|
||||
b"9.9.9.9 Salt2 # comment\n"
|
||||
)
|
||||
with patch("salt.utils.files.fopen", mock_open(hosts_content)), patch(
|
||||
"salt.modules.hosts.__get_hosts_filename",
|
||||
MagicMock(return_value="/etc/hosts"),
|
||||
), patch("salt.modules.hosts.has_pair", MagicMock(return_value=True)), patch(
|
||||
|
|
Loading…
Add table
Reference in a new issue