mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 17:09:03 +00:00
Merge pull request #44317 from Ch3LL/ssh_test
Add state tests and state request system to salt-ssh
This commit is contained in:
commit
cc08ad2edc
@ -289,6 +289,143 @@ def apply_(mods=None,
|
||||
return highstate(**kwargs)
|
||||
|
||||
|
||||
def request(mods=None,
|
||||
**kwargs):
|
||||
'''
|
||||
.. versionadded:: 2017.7.3
|
||||
|
||||
Request that the local admin execute a state run via
|
||||
`salt-call state.run_request`
|
||||
All arguments match state.apply
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' state.request
|
||||
salt '*' state.request test
|
||||
salt '*' state.request test,pkgs
|
||||
'''
|
||||
kwargs['test'] = True
|
||||
ret = apply_(mods, **kwargs)
|
||||
notify_path = os.path.join(__opts__['cachedir'], 'req_state.p')
|
||||
serial = salt.payload.Serial(__opts__)
|
||||
req = check_request()
|
||||
req.update({kwargs.get('name', 'default'): {
|
||||
'test_run': ret,
|
||||
'mods': mods,
|
||||
'kwargs': kwargs
|
||||
}
|
||||
})
|
||||
cumask = os.umask(0o77)
|
||||
try:
|
||||
if salt.utils.is_windows():
|
||||
# Make sure cache file isn't read-only
|
||||
__salt__['cmd.run']('attrib -R "{0}"'.format(notify_path))
|
||||
with salt.utils.fopen(notify_path, 'w+b') as fp_:
|
||||
serial.dump(req, fp_)
|
||||
except (IOError, OSError):
|
||||
msg = 'Unable to write state request file {0}. Check permission.'
|
||||
log.error(msg.format(notify_path))
|
||||
os.umask(cumask)
|
||||
return ret
|
||||
|
||||
|
||||
def check_request(name=None):
|
||||
'''
|
||||
.. versionadded:: 2017.7.3
|
||||
|
||||
Return the state request information, if any
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' state.check_request
|
||||
'''
|
||||
notify_path = os.path.join(__opts__['cachedir'], 'req_state.p')
|
||||
serial = salt.payload.Serial(__opts__)
|
||||
if os.path.isfile(notify_path):
|
||||
with salt.utils.fopen(notify_path, 'rb') as fp_:
|
||||
req = serial.load(fp_)
|
||||
if name:
|
||||
return req[name]
|
||||
return req
|
||||
return {}
|
||||
|
||||
|
||||
def clear_request(name=None):
|
||||
'''
|
||||
.. versionadded:: 2017.7.3
|
||||
|
||||
Clear out the state execution request without executing it
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' state.clear_request
|
||||
'''
|
||||
notify_path = os.path.join(__opts__['cachedir'], 'req_state.p')
|
||||
serial = salt.payload.Serial(__opts__)
|
||||
if not os.path.isfile(notify_path):
|
||||
return True
|
||||
if not name:
|
||||
try:
|
||||
os.remove(notify_path)
|
||||
except (IOError, OSError):
|
||||
pass
|
||||
else:
|
||||
req = check_request()
|
||||
if name in req:
|
||||
req.pop(name)
|
||||
else:
|
||||
return False
|
||||
cumask = os.umask(0o77)
|
||||
try:
|
||||
if salt.utils.is_windows():
|
||||
# Make sure cache file isn't read-only
|
||||
__salt__['cmd.run']('attrib -R "{0}"'.format(notify_path))
|
||||
with salt.utils.fopen(notify_path, 'w+b') as fp_:
|
||||
serial.dump(req, fp_)
|
||||
except (IOError, OSError):
|
||||
msg = 'Unable to write state request file {0}. Check permission.'
|
||||
log.error(msg.format(notify_path))
|
||||
os.umask(cumask)
|
||||
return True
|
||||
|
||||
|
||||
def run_request(name='default', **kwargs):
|
||||
'''
|
||||
.. versionadded:: 2017.7.3
|
||||
|
||||
Execute the pending state request
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' state.run_request
|
||||
'''
|
||||
req = check_request()
|
||||
if name not in req:
|
||||
return {}
|
||||
n_req = req[name]
|
||||
if 'mods' not in n_req or 'kwargs' not in n_req:
|
||||
return {}
|
||||
req[name]['kwargs'].update(kwargs)
|
||||
if 'test' in n_req['kwargs']:
|
||||
n_req['kwargs'].pop('test')
|
||||
if req:
|
||||
ret = apply_(n_req['mods'], **n_req['kwargs'])
|
||||
try:
|
||||
os.remove(os.path.join(__opts__['cachedir'], 'req_state.p'))
|
||||
except (IOError, OSError):
|
||||
pass
|
||||
return ret
|
||||
return {}
|
||||
|
||||
|
||||
def highstate(test=None, **kwargs):
|
||||
'''
|
||||
Retrieve the state data from the salt master for this minion and execute it
|
||||
|
4
tests/integration/files/file/base/ssh_state_tests.sls
Normal file
4
tests/integration/files/file/base/ssh_state_tests.sls
Normal file
@ -0,0 +1,4 @@
|
||||
ssh-file-test:
|
||||
file.managed:
|
||||
- name: /tmp/test
|
||||
- contents: 'test'
|
72
tests/integration/ssh/test_state.py
Normal file
72
tests/integration/ssh/test_state.py
Normal file
@ -0,0 +1,72 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Import Python libs
|
||||
from __future__ import absolute_import
|
||||
import os
|
||||
import shutil
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.case import SSHCase
|
||||
|
||||
SSH_SLS = 'ssh_state_tests'
|
||||
|
||||
|
||||
class SSHStateTest(SSHCase):
|
||||
'''
|
||||
testing the state system with salt-ssh
|
||||
'''
|
||||
def _check_dict_ret(self, ret, val, exp_ret):
|
||||
for key, value in ret.items():
|
||||
self.assertEqual(value[val], exp_ret)
|
||||
|
||||
def _check_request(self, empty=False):
|
||||
check = self.run_function('state.check_request', wipe=False)
|
||||
if empty:
|
||||
self.assertFalse(bool(check))
|
||||
else:
|
||||
self._check_dict_ret(ret=check['default']['test_run']['local']['return'],
|
||||
val='__sls__', exp_ret=SSH_SLS)
|
||||
|
||||
def test_state_apply(self):
|
||||
'''
|
||||
test state.apply with salt-ssh
|
||||
'''
|
||||
ret = self.run_function('state.apply', [SSH_SLS])
|
||||
self._check_dict_ret(ret=ret, val='__sls__', exp_ret=SSH_SLS)
|
||||
|
||||
check_file = self.run_function('file.file_exists', ['/tmp/test'])
|
||||
self.assertTrue(check_file)
|
||||
|
||||
def test_state_request_check_clear(self):
|
||||
'''
|
||||
test state.request system with salt-ssh
|
||||
while also checking and clearing request
|
||||
'''
|
||||
request = self.run_function('state.request', [SSH_SLS], wipe=False)
|
||||
self._check_dict_ret(ret=request, val='__sls__', exp_ret=SSH_SLS)
|
||||
|
||||
self._check_request()
|
||||
|
||||
clear = self.run_function('state.clear_request', wipe=False)
|
||||
self._check_request(empty=True)
|
||||
|
||||
def test_state_run_request(self):
|
||||
'''
|
||||
test state.request system with salt-ssh
|
||||
while also running the request later
|
||||
'''
|
||||
request = self.run_function('state.request', [SSH_SLS], wipe=False)
|
||||
self._check_dict_ret(ret=request, val='__sls__', exp_ret=SSH_SLS)
|
||||
|
||||
run = self.run_function('state.run_request', wipe=False)
|
||||
|
||||
check_file = self.run_function('file.file_exists', ['/tmp/test'], wipe=False)
|
||||
self.assertTrue(check_file)
|
||||
|
||||
def tearDown(self):
|
||||
'''
|
||||
make sure to clean up any old ssh directories
|
||||
'''
|
||||
salt_dir = self.run_function('config.get', ['thin_dir'], wipe=False)
|
||||
if os.path.exists(salt_dir):
|
||||
shutil.rmtree(salt_dir)
|
@ -130,6 +130,9 @@ TEST_SUITES = {
|
||||
'returners':
|
||||
{'display_name': 'Returners',
|
||||
'path': 'integration/returners'},
|
||||
'ssh-int':
|
||||
{'display_name': 'SSH Integration',
|
||||
'path': 'integration/ssh'},
|
||||
'spm':
|
||||
{'display_name': 'SPM',
|
||||
'path': 'integration/spm'},
|
||||
@ -412,6 +415,14 @@ class SaltTestsuiteParser(SaltCoverageTestingParser):
|
||||
'SSH server on your machine. In certain environments, this '
|
||||
'may be insecure! Default: False'
|
||||
)
|
||||
self.test_selection_group.add_option(
|
||||
'--ssh-int',
|
||||
dest='ssh-int',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help='Run salt-ssh integration tests. Requires to be run with --ssh'
|
||||
'to spin up the SSH server on your machine.'
|
||||
)
|
||||
self.test_selection_group.add_option(
|
||||
'-A',
|
||||
'--api',
|
||||
|
@ -124,11 +124,13 @@ class ShellTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
|
||||
arg_str = '-c {0} {1}'.format(self.get_config_dir(), arg_str)
|
||||
return self.run_script('salt', arg_str, with_retcode=with_retcode, catch_stderr=catch_stderr)
|
||||
|
||||
def run_ssh(self, arg_str, with_retcode=False, timeout=25, catch_stderr=False):
|
||||
def run_ssh(self, arg_str, with_retcode=False, timeout=25,
|
||||
catch_stderr=False, wipe=False):
|
||||
'''
|
||||
Execute salt-ssh
|
||||
'''
|
||||
arg_str = '-c {0} -i --priv {1} --roster-file {2} localhost {3} --out=json'.format(
|
||||
arg_str = '{0} -c {1} -i --priv {2} --roster-file {3} localhost {4} --out=json'.format(
|
||||
' -W' if wipe else '',
|
||||
self.get_config_dir(),
|
||||
os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'key_test'),
|
||||
os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'roster'),
|
||||
@ -453,11 +455,13 @@ class ShellCase(ShellTestCase, AdaptedConfigurationTestCaseMixin, ScriptPathMixi
|
||||
catch_stderr=catch_stderr,
|
||||
timeout=timeout)
|
||||
|
||||
def run_ssh(self, arg_str, with_retcode=False, catch_stderr=False, timeout=60): # pylint: disable=W0221
|
||||
def run_ssh(self, arg_str, with_retcode=False, catch_stderr=False,
|
||||
timeout=60, wipe=True): # pylint: disable=W0221
|
||||
'''
|
||||
Execute salt-ssh
|
||||
'''
|
||||
arg_str = '-ldebug -W -c {0} -i --priv {1} --roster-file {2} --out=json localhost {3}'.format(
|
||||
arg_str = '-ldebug{0} -c {1} -i --priv {2} --roster-file {3} --out=json localhost {4}'.format(
|
||||
' -W' if wipe else '',
|
||||
self.get_config_dir(),
|
||||
os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'key_test'),
|
||||
os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'roster'),
|
||||
@ -797,11 +801,12 @@ class SSHCase(ShellCase):
|
||||
def _arg_str(self, function, arg):
|
||||
return '{0} {1}'.format(function, ' '.join(arg))
|
||||
|
||||
def run_function(self, function, arg=(), timeout=90, **kwargs):
|
||||
def run_function(self, function, arg=(), timeout=90, wipe=True, **kwargs):
|
||||
'''
|
||||
We use a 90s timeout here, which some slower systems do end up needing
|
||||
'''
|
||||
ret = self.run_ssh(self._arg_str(function, arg), timeout=timeout)
|
||||
ret = self.run_ssh(self._arg_str(function, arg), timeout=timeout,
|
||||
wipe=wipe)
|
||||
try:
|
||||
return json.loads(ret)['localhost']
|
||||
except Exception:
|
||||
|
Loading…
Reference in New Issue
Block a user