2016-02-19 16:14:46 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
Tests for the state runner
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Python Libs
|
2017-12-15 01:21:00 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2016-09-02 00:49:12 +00:00
|
|
|
import errno
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import signal
|
|
|
|
import tempfile
|
|
|
|
import textwrap
|
|
|
|
import yaml
|
2016-09-02 22:52:39 +00:00
|
|
|
import threading
|
|
|
|
from salt.ext.six.moves import queue
|
2016-02-19 16:14:46 +00:00
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-04-03 16:04:09 +00:00
|
|
|
from tests.support.case import ShellCase
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf
|
2017-04-03 16:04:09 +00:00
|
|
|
from tests.support.paths import TMP
|
2016-02-19 16:14:46 +00:00
|
|
|
|
2016-09-02 00:49:12 +00:00
|
|
|
# Import Salt Libs
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
import salt.utils.platform
|
2016-09-02 00:49:12 +00:00
|
|
|
import salt.utils.event
|
2017-07-18 16:31:01 +00:00
|
|
|
import salt.utils.files
|
2017-12-22 19:54:53 +00:00
|
|
|
import salt.utils.json
|
2017-12-15 01:21:00 +00:00
|
|
|
import salt.utils.stringutils
|
|
|
|
|
|
|
|
# Import 3rd-party libs
|
|
|
|
from salt.ext import six
|
2016-09-02 00:49:12 +00:00
|
|
|
|
2016-02-19 16:14:46 +00:00
|
|
|
|
2017-04-03 16:04:09 +00:00
|
|
|
class StateRunnerTest(ShellCase):
|
2016-02-19 16:14:46 +00:00
|
|
|
'''
|
|
|
|
Test the state runner.
|
|
|
|
'''
|
2016-09-02 22:52:39 +00:00
|
|
|
def add_to_queue(self, q, cmd):
|
|
|
|
'''
|
|
|
|
helper method to add salt-run
|
|
|
|
return data to a queue
|
|
|
|
'''
|
|
|
|
ret = self.run_run(cmd)
|
|
|
|
q.put(ret)
|
|
|
|
q.task_done()
|
2016-02-19 16:14:46 +00:00
|
|
|
|
|
|
|
def test_orchestrate_output(self):
|
|
|
|
'''
|
|
|
|
Ensure the orchestrate runner outputs useful state data.
|
|
|
|
|
|
|
|
In Issue #31330, the output only contains ['outputter:', ' highstate'],
|
|
|
|
and not the full stateful return. This tests ensures we don't regress in that
|
|
|
|
manner again.
|
|
|
|
|
|
|
|
Also test against some sample "good" output that would be included in a correct
|
|
|
|
orchestrate run.
|
|
|
|
'''
|
2016-09-02 05:04:18 +00:00
|
|
|
#ret_output = self.run_run_plus('state.orchestrate', 'orch.simple')['out']
|
|
|
|
ret_output = self.run_run('state.orchestrate orch.simple')
|
2016-02-19 16:14:46 +00:00
|
|
|
bad_out = ['outputter:', ' highstate']
|
|
|
|
good_out = [' Function: salt.state',
|
|
|
|
' Result: True',
|
|
|
|
'Succeeded: 1 (changed=1)',
|
|
|
|
'Failed: 0',
|
|
|
|
'Total states run: 1']
|
|
|
|
|
|
|
|
# First, check that we don't have the "bad" output that was displaying in
|
|
|
|
# Issue #31330 where only the highstate outputter was listed
|
|
|
|
self.assertIsNot(bad_out, ret_output)
|
|
|
|
|
|
|
|
# Now test that some expected good sample output is present in the return.
|
|
|
|
for item in good_out:
|
|
|
|
self.assertIn(item, ret_output)
|
|
|
|
|
2016-12-14 13:41:03 +00:00
|
|
|
def test_orchestrate_nested(self):
|
|
|
|
'''
|
|
|
|
test salt-run state.orchestrate and failhard with nested orchestration
|
|
|
|
'''
|
|
|
|
if os.path.exists('/tmp/ewu-2016-12-13'):
|
|
|
|
os.remove('/tmp/ewu-2016-12-13')
|
|
|
|
|
|
|
|
_, code = self.run_run(
|
|
|
|
'state.orchestrate nested-orch.outer',
|
|
|
|
with_retcode=True)
|
|
|
|
|
|
|
|
self.assertFalse(os.path.exists('/tmp/ewu-2016-12-13'))
|
|
|
|
self.assertNotEqual(code, 0)
|
|
|
|
|
2017-11-29 20:16:29 +00:00
|
|
|
def test_orchestrate_state_and_function_failure(self):
|
|
|
|
'''
|
|
|
|
Ensure that returns from failed minions are in the changes dict where
|
|
|
|
they belong, so they can be programatically analyzed.
|
|
|
|
|
|
|
|
See https://github.com/saltstack/salt/issues/43204
|
|
|
|
'''
|
|
|
|
self.run_run('saltutil.sync_modules')
|
2017-12-22 19:54:53 +00:00
|
|
|
ret = salt.utils.json.loads(
|
2017-11-29 20:16:29 +00:00
|
|
|
'\n'.join(
|
2017-12-15 01:21:00 +00:00
|
|
|
self.run_run('state.orchestrate orch.issue43204 --out=json')
|
2017-11-29 20:16:29 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
# Drill down to the changes dict
|
2017-12-15 01:21:00 +00:00
|
|
|
state_ret = ret['data']['master']['salt_|-Step01_|-Step01_|-state']['changes']
|
|
|
|
func_ret = ret['data']['master']['salt_|-Step02_|-runtests_helpers.nonzero_retcode_return_false_|-function']['changes']
|
2017-11-29 20:16:29 +00:00
|
|
|
|
|
|
|
# Remove duration and start time from the results, since they would
|
|
|
|
# vary with each run and that would make it impossible to test.
|
|
|
|
for item in ('duration', 'start_time'):
|
|
|
|
state_ret['ret']['minion']['test_|-test fail with changes_|-test fail with changes_|-fail_with_changes'].pop(item)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
state_ret,
|
|
|
|
{
|
2017-12-15 01:21:00 +00:00
|
|
|
'out': 'highstate',
|
|
|
|
'ret': {
|
|
|
|
'minion': {
|
|
|
|
'test_|-test fail with changes_|-test fail with changes_|-fail_with_changes': {
|
|
|
|
'__id__': 'test fail with changes',
|
|
|
|
'__run_num__': 0,
|
|
|
|
'__sls__': 'orch.issue43204.fail_with_changes',
|
|
|
|
'changes': {
|
|
|
|
'testing': {
|
|
|
|
'new': 'Something pretended to change',
|
|
|
|
'old': 'Unchanged'
|
2017-11-29 20:16:29 +00:00
|
|
|
}
|
|
|
|
},
|
2017-12-15 01:21:00 +00:00
|
|
|
'comment': 'Failure!',
|
|
|
|
'name': 'test fail with changes',
|
|
|
|
'result': False,
|
2017-11-29 20:16:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
func_ret,
|
2017-12-15 01:21:00 +00:00
|
|
|
{'out': 'highstate', 'ret': {'minion': False}}
|
2017-11-29 20:16:29 +00:00
|
|
|
)
|
|
|
|
|
2017-11-10 17:00:12 +00:00
|
|
|
def test_orchestrate_target_exists(self):
|
|
|
|
'''
|
|
|
|
test orchestration when target exists
|
|
|
|
while using multiple states
|
|
|
|
'''
|
|
|
|
ret = self.run_run('state.orchestrate orch.target-exists')
|
|
|
|
|
|
|
|
first = [' ID: core',
|
|
|
|
' Function: salt.state',
|
|
|
|
' Result: True']
|
|
|
|
|
|
|
|
second = [' ID: test-state',
|
|
|
|
' Function: salt.state',
|
|
|
|
' Result: True']
|
|
|
|
|
|
|
|
third = [' ID: cmd.run',
|
|
|
|
' Function: salt.function',
|
|
|
|
' Result: True']
|
|
|
|
|
|
|
|
ret_out = [first, second, third]
|
|
|
|
|
|
|
|
for out in ret_out:
|
|
|
|
for item in out:
|
|
|
|
self.assertIn(item, ret)
|
|
|
|
|
2017-11-29 03:34:19 +00:00
|
|
|
def test_orchestrate_retcode(self):
|
|
|
|
'''
|
|
|
|
Test orchestration with nonzero retcode set in __context__
|
|
|
|
'''
|
|
|
|
self.run_run('saltutil.sync_runners')
|
|
|
|
self.run_run('saltutil.sync_wheel')
|
|
|
|
ret = '\n'.join(self.run_run('state.orchestrate orch.retcode'))
|
|
|
|
|
|
|
|
for result in (' ID: test_runner_success\n'
|
|
|
|
' Function: salt.runner\n'
|
|
|
|
' Name: runtests_helpers.success\n'
|
|
|
|
' Result: True',
|
|
|
|
|
|
|
|
' ID: test_runner_failure\n'
|
|
|
|
' Function: salt.runner\n'
|
|
|
|
' Name: runtests_helpers.failure\n'
|
|
|
|
' Result: False',
|
|
|
|
|
|
|
|
' ID: test_wheel_success\n'
|
|
|
|
' Function: salt.wheel\n'
|
|
|
|
' Name: runtests_helpers.success\n'
|
|
|
|
' Result: True',
|
|
|
|
|
|
|
|
' ID: test_wheel_failure\n'
|
|
|
|
' Function: salt.wheel\n'
|
|
|
|
' Name: runtests_helpers.failure\n'
|
|
|
|
' Result: False'):
|
|
|
|
self.assertIn(result, ret)
|
|
|
|
|
2017-11-10 17:00:12 +00:00
|
|
|
def test_orchestrate_target_doesnt_exists(self):
|
|
|
|
'''
|
|
|
|
test orchestration when target doesnt exist
|
|
|
|
while using multiple states
|
|
|
|
'''
|
|
|
|
ret = self.run_run('state.orchestrate orch.target-doesnt-exists')
|
|
|
|
|
|
|
|
first = ['No minions matched the target. No command was sent, no jid was assigned.',
|
|
|
|
' ID: core',
|
|
|
|
' Function: salt.state',
|
|
|
|
' Result: False']
|
|
|
|
|
|
|
|
second = [' ID: test-state',
|
|
|
|
' Function: salt.state',
|
|
|
|
' Result: True']
|
|
|
|
|
|
|
|
third = [' ID: cmd.run',
|
|
|
|
' Function: salt.function',
|
|
|
|
' Result: True']
|
|
|
|
|
|
|
|
ret_out = [first, second, third]
|
|
|
|
|
|
|
|
for out in ret_out:
|
|
|
|
for item in out:
|
|
|
|
self.assertIn(item, ret)
|
|
|
|
|
2016-09-02 22:52:39 +00:00
|
|
|
def test_state_event(self):
|
|
|
|
'''
|
|
|
|
test to ensure state.event
|
|
|
|
runner returns correct data
|
|
|
|
'''
|
|
|
|
q = queue.Queue(maxsize=0)
|
|
|
|
|
|
|
|
cmd = 'state.event salt/job/*/new count=1'
|
|
|
|
expect = '"minions": ["minion"]'
|
|
|
|
server_thread = threading.Thread(target=self.add_to_queue, args=(q, cmd))
|
|
|
|
server_thread.setDaemon(True)
|
|
|
|
server_thread.start()
|
|
|
|
|
|
|
|
while q.empty():
|
|
|
|
self.run_salt('minion test.ping --static')
|
|
|
|
out = q.get()
|
2017-12-15 01:21:00 +00:00
|
|
|
self.assertIn(expect, six.text_type(out))
|
2016-09-02 22:52:39 +00:00
|
|
|
|
|
|
|
server_thread.join()
|
2016-02-19 16:14:46 +00:00
|
|
|
|
2016-09-07 16:05:44 +00:00
|
|
|
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
@skipIf(salt.utils.platform.is_windows(), '*NIX-only test')
|
2017-04-03 16:04:09 +00:00
|
|
|
class OrchEventTest(ShellCase):
|
2016-09-02 00:49:12 +00:00
|
|
|
'''
|
|
|
|
Tests for orchestration events
|
|
|
|
'''
|
|
|
|
def setUp(self):
|
2017-01-23 12:10:22 +00:00
|
|
|
self.timeout = 60
|
2016-09-02 00:49:12 +00:00
|
|
|
self.master_d_dir = os.path.join(self.get_config_dir(), 'master.d')
|
|
|
|
try:
|
|
|
|
os.makedirs(self.master_d_dir)
|
|
|
|
except OSError as exc:
|
|
|
|
if exc.errno != errno.EEXIST:
|
|
|
|
raise
|
|
|
|
|
|
|
|
self.conf = tempfile.NamedTemporaryFile(
|
|
|
|
mode='w',
|
|
|
|
suffix='.conf',
|
|
|
|
dir=self.master_d_dir,
|
|
|
|
delete=True,
|
|
|
|
)
|
2017-04-03 16:04:09 +00:00
|
|
|
self.base_env = tempfile.mkdtemp(dir=TMP)
|
2017-04-17 10:54:50 +00:00
|
|
|
self.addCleanup(shutil.rmtree, self.base_env)
|
|
|
|
self.addCleanup(self.conf.close)
|
|
|
|
for attr in ('timeout', 'master_d_dir', 'conf', 'base_env'):
|
|
|
|
self.addCleanup(delattr, self, attr)
|
2016-09-02 00:49:12 +00:00
|
|
|
# Force a reload of the configuration now that our temp config file has
|
|
|
|
# been removed.
|
2017-04-17 10:54:50 +00:00
|
|
|
self.addCleanup(self.run_run_plus, 'test.arg', __reload_config=True)
|
2016-09-02 00:49:12 +00:00
|
|
|
|
|
|
|
def alarm_handler(self, signal, frame):
|
|
|
|
raise Exception('Timeout of {0} seconds reached'.format(self.timeout))
|
|
|
|
|
|
|
|
def write_conf(self, data):
|
|
|
|
'''
|
|
|
|
Dump the config dict to the conf file
|
|
|
|
'''
|
|
|
|
self.conf.write(yaml.dump(data, default_flow_style=False))
|
|
|
|
self.conf.flush()
|
|
|
|
|
|
|
|
def test_jid_in_ret_event(self):
|
|
|
|
'''
|
|
|
|
Test to confirm that the ret event for the orchestration contains the
|
|
|
|
jid for the jobs spawned.
|
|
|
|
'''
|
|
|
|
self.write_conf({
|
|
|
|
'fileserver_backend': ['roots'],
|
|
|
|
'file_roots': {
|
|
|
|
'base': [self.base_env],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
state_sls = os.path.join(self.base_env, 'test_state.sls')
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(state_sls, 'w') as fp_:
|
2017-12-15 01:21:00 +00:00
|
|
|
fp_.write(salt.utils.stringutils.to_str(textwrap.dedent('''
|
2016-09-02 00:49:12 +00:00
|
|
|
date:
|
|
|
|
cmd.run
|
2017-12-15 01:21:00 +00:00
|
|
|
''')))
|
2016-09-02 00:49:12 +00:00
|
|
|
|
|
|
|
orch_sls = os.path.join(self.base_env, 'test_orch.sls')
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(orch_sls, 'w') as fp_:
|
2017-12-15 01:21:00 +00:00
|
|
|
fp_.write(salt.utils.stringutils.to_str(textwrap.dedent('''
|
2016-09-02 00:49:12 +00:00
|
|
|
date_cmd:
|
|
|
|
salt.state:
|
|
|
|
- tgt: minion
|
|
|
|
- sls: test_state
|
|
|
|
|
|
|
|
ping_minion:
|
|
|
|
salt.function:
|
|
|
|
- name: test.ping
|
|
|
|
- tgt: minion
|
|
|
|
|
|
|
|
fileserver.file_list:
|
|
|
|
salt.runner
|
|
|
|
|
|
|
|
config.values:
|
|
|
|
salt.wheel
|
2017-12-15 01:21:00 +00:00
|
|
|
''')))
|
2016-09-02 00:49:12 +00:00
|
|
|
|
|
|
|
listener = salt.utils.event.get_event(
|
|
|
|
'master',
|
|
|
|
sock_dir=self.master_opts['sock_dir'],
|
|
|
|
transport=self.master_opts['transport'],
|
|
|
|
opts=self.master_opts)
|
|
|
|
|
|
|
|
jid = self.run_run_plus(
|
|
|
|
'state.orchestrate',
|
|
|
|
'test_orch',
|
|
|
|
__reload_config=True).get('jid')
|
|
|
|
|
|
|
|
if jid is None:
|
|
|
|
raise Exception('jid missing from run_run_plus output')
|
|
|
|
|
|
|
|
signal.signal(signal.SIGALRM, self.alarm_handler)
|
|
|
|
signal.alarm(self.timeout)
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
event = listener.get_event(full=True)
|
|
|
|
if event is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if event['tag'] == 'salt/run/{0}/ret'.format(jid):
|
|
|
|
# Don't wrap this in a try/except. We want to know if the
|
|
|
|
# data structure is different from what we expect!
|
|
|
|
ret = event['data']['return']['data']['master']
|
|
|
|
for job in ret:
|
|
|
|
self.assertTrue('__jid__' in ret[job])
|
|
|
|
break
|
|
|
|
finally:
|
2017-04-17 10:54:50 +00:00
|
|
|
del listener
|
2016-09-02 00:49:12 +00:00
|
|
|
signal.alarm(0)
|