mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 08:58:59 +00:00
Get mock_open
working.
Kind of since we're not using `mock_open` from the mocked library. Were actually using the recipe to mock the builtin open as seen on http://goo.gl/dxMwPO
This commit is contained in:
parent
474e7590b9
commit
284a2abfcb
@ -21,13 +21,23 @@ from contextlib import contextmanager
|
|||||||
|
|
||||||
# Import Salt Testing libs
|
# Import Salt Testing libs
|
||||||
from salttesting import TestCase
|
from salttesting import TestCase
|
||||||
from salttesting.mock import MagicMock, patch, mock_open, call
|
from salttesting.mock import MagicMock, patch
|
||||||
from salttesting.helpers import ensure_in_syspath, TestsLoggingHandler
|
from salttesting.helpers import ensure_in_syspath, TestsLoggingHandler
|
||||||
from salt.exceptions import CommandExecutionError
|
from salt.exceptions import CommandExecutionError
|
||||||
|
|
||||||
|
ensure_in_syspath('../')
|
||||||
|
|
||||||
|
# Import salt libs
|
||||||
|
import salt.minion
|
||||||
|
import salt.utils
|
||||||
|
import integration
|
||||||
|
from salt import config as sconfig, version as salt_version
|
||||||
|
from salt.version import SaltStackVersion
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
mock_etc_hosts = (
|
|
||||||
|
MOCK_ETC_HOSTS = (
|
||||||
'##\n'
|
'##\n'
|
||||||
'# Host Database\n'
|
'# Host Database\n'
|
||||||
'#\n'
|
'#\n'
|
||||||
@ -38,30 +48,21 @@ mock_etc_hosts = (
|
|||||||
'127.0.0.1 localhost\n'
|
'127.0.0.1 localhost\n'
|
||||||
'10.0.0.100 foo.bar.net\n'
|
'10.0.0.100 foo.bar.net\n'
|
||||||
)
|
)
|
||||||
mock_etc_hostname = 'foo.bar.com\n'
|
MOCK_ETC_HOSTNAME = 'foo.bar.com\n'
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def _fopen_side_effect_etc_hosts(filename):
|
def _fopen_side_effect_etc_hosts(filename):
|
||||||
log.debug('Mock-reading {0}'.format(filename))
|
log.debug('Mock-reading {0}'.format(filename))
|
||||||
|
mock_open = MagicMock()
|
||||||
if filename == '/etc/hostname':
|
if filename == '/etc/hostname':
|
||||||
return mock_open(read_data=mock_etc_hostname)
|
mock_open.read.return_value = MOCK_ETC_HOSTNAME
|
||||||
#return OSError(2, "No such file or directory: '/etc/hostname'")
|
yield mock_open
|
||||||
return mock_open(
|
|
||||||
read_data=OSError(2, "No such file or directory: '/etc/hostname'")
|
|
||||||
)
|
|
||||||
elif filename == '/etc/hosts':
|
elif filename == '/etc/hosts':
|
||||||
return mock_open(read_data=mock_etc_hosts)
|
mock_open.read.return_value = MOCK_ETC_HOSTS
|
||||||
raise CommandExecutionError('Unhandled mock read for {0}'.format(filename))
|
yield mock_open
|
||||||
|
else:
|
||||||
|
raise CommandExecutionError('Unhandled mock read for {0}'.format(filename))
|
||||||
ensure_in_syspath('../')
|
|
||||||
|
|
||||||
# Import salt libs
|
|
||||||
import salt.minion
|
|
||||||
import salt.utils
|
|
||||||
import integration
|
|
||||||
from salt import config as sconfig, version as salt_version
|
|
||||||
from salt.version import SaltStackVersion
|
|
||||||
|
|
||||||
|
|
||||||
class ConfigTestCase(TestCase):
|
class ConfigTestCase(TestCase):
|
||||||
|
Loading…
Reference in New Issue
Block a user