2012-08-04 18:58:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-12-11 10:23:37 +00:00
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)`
|
2013-09-16 16:24:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
tests.integration.shell.minion
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2012-12-11 10:23:37 +00:00
|
|
|
'''
|
2012-08-04 18:58:32 +00:00
|
|
|
|
2013-10-15 21:09:37 +00:00
|
|
|
# Import python libs
|
2014-11-21 19:05:13 +00:00
|
|
|
from __future__ import absolute_import
|
2016-06-02 22:51:54 +00:00
|
|
|
import getpass
|
2013-10-15 21:09:37 +00:00
|
|
|
import os
|
2016-05-09 17:57:13 +00:00
|
|
|
import sys
|
|
|
|
import platform
|
2013-10-15 21:09:37 +00:00
|
|
|
import signal
|
|
|
|
import shutil
|
2016-05-09 17:57:13 +00:00
|
|
|
import logging
|
2013-10-15 21:09:37 +00:00
|
|
|
|
2013-06-27 12:25:43 +00:00
|
|
|
# Import Salt Testing libs
|
2017-02-27 15:59:04 +00:00
|
|
|
import tests.integration.utils
|
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 CODE_DIR, TMP
|
|
|
|
from tests.support.mixins import ShellCaseCommonTestsMixin
|
|
|
|
from tests.integration.utils import testprogram
|
2013-06-27 12:25:43 +00:00
|
|
|
|
2017-02-27 15:59:04 +00:00
|
|
|
# Import 3rd-party 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
|
|
|
from salt.ext import six
|
2016-06-29 20:30:18 +00:00
|
|
|
|
2012-08-04 18:58:32 +00:00
|
|
|
# Import salt libs
|
2017-07-18 16:31:01 +00:00
|
|
|
import salt.utils.files
|
2017-12-28 04:31:50 +00:00
|
|
|
import salt.utils.yaml
|
2012-08-04 18:58:32 +00:00
|
|
|
|
2016-05-09 17:57:13 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEBUG = True
|
2012-08-04 18:58:32 +00:00
|
|
|
|
|
|
|
|
2017-04-03 16:04:09 +00:00
|
|
|
class MinionTest(ShellCase, testprogram.TestProgramCase, ShellCaseCommonTestsMixin):
|
2016-05-09 17:57:13 +00:00
|
|
|
'''
|
|
|
|
Various integration tests for the salt-minion executable.
|
|
|
|
'''
|
2012-08-04 18:58:32 +00:00
|
|
|
_call_binary_ = 'salt-minion'
|
2016-05-09 17:57:13 +00:00
|
|
|
|
|
|
|
_test_minions = (
|
|
|
|
'minion',
|
|
|
|
'subminion',
|
|
|
|
)
|
|
|
|
|
2013-10-15 21:09:37 +00:00
|
|
|
def test_issue_7754(self):
|
|
|
|
old_cwd = os.getcwd()
|
2017-04-03 16:04:09 +00:00
|
|
|
config_dir = os.path.join(TMP, 'issue-7754')
|
2013-10-15 21:09:37 +00:00
|
|
|
if not os.path.isdir(config_dir):
|
|
|
|
os.makedirs(config_dir)
|
|
|
|
|
|
|
|
os.chdir(config_dir)
|
|
|
|
|
|
|
|
config_file_name = 'minion'
|
|
|
|
pid_path = os.path.join(config_dir, '{0}.pid'.format(config_file_name))
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(self.get_config_file_path(config_file_name), 'r') as fhr:
|
2017-12-28 04:31:50 +00:00
|
|
|
config = salt.utils.yaml.safe_load(fhr)
|
2014-11-26 17:55:30 +00:00
|
|
|
config['log_file'] = 'file:///tmp/log/LOG_LOCAL3'
|
|
|
|
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(os.path.join(config_dir, config_file_name), 'w') as fhw:
|
2017-12-28 04:31:50 +00:00
|
|
|
salt.utils.yaml.safe_dump(config, fhw, default_flow_style=False)
|
2013-10-15 21:09:37 +00:00
|
|
|
|
2014-01-19 07:52:57 +00:00
|
|
|
ret = self.run_script(
|
2013-10-15 21:09:37 +00:00
|
|
|
self._call_binary_,
|
2014-07-24 16:25:57 +00:00
|
|
|
'--disable-keepalive --config-dir {0} --pid-file {1} -l debug'.format(
|
2013-10-15 21:09:37 +00:00
|
|
|
config_dir,
|
|
|
|
pid_path
|
|
|
|
),
|
|
|
|
timeout=5,
|
2014-01-19 07:52:57 +00:00
|
|
|
catch_stderr=True,
|
|
|
|
with_retcode=True
|
2013-10-15 21:09:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Now kill it if still running
|
|
|
|
if os.path.exists(pid_path):
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(pid_path) as fhr:
|
2014-11-26 23:36:26 +00:00
|
|
|
try:
|
|
|
|
os.kill(int(fhr.read()), signal.SIGKILL)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2013-10-15 21:09:37 +00:00
|
|
|
try:
|
|
|
|
self.assertFalse(os.path.isdir(os.path.join(config_dir, 'file:')))
|
|
|
|
finally:
|
2015-07-28 11:07:04 +00:00
|
|
|
self.chdir(old_cwd)
|
2013-10-15 21:09:37 +00:00
|
|
|
if os.path.isdir(config_dir):
|
|
|
|
shutil.rmtree(config_dir)
|
|
|
|
|
2016-05-09 17:57:13 +00:00
|
|
|
def _run_initscript(
|
|
|
|
self,
|
|
|
|
init_script,
|
|
|
|
minions,
|
|
|
|
minion_running,
|
|
|
|
action,
|
|
|
|
exitstatus=None,
|
|
|
|
message=''
|
|
|
|
):
|
|
|
|
'''
|
|
|
|
Wrapper that runs the initscript for the configured minions and
|
|
|
|
verifies the results.
|
|
|
|
'''
|
2016-07-28 18:12:47 +00:00
|
|
|
user = getpass.getuser()
|
2016-05-09 17:57:13 +00:00
|
|
|
ret = init_script.run(
|
|
|
|
[action],
|
|
|
|
catch_stderr=True,
|
|
|
|
with_retcode=True,
|
2016-07-28 18:12:47 +00:00
|
|
|
env={
|
|
|
|
'SALTMINION_CONFIGS': '\n'.join([
|
|
|
|
'{0} {1}'.format(user, minion.abs_path(minion.config_dir)) for minion in minions
|
|
|
|
]),
|
|
|
|
},
|
2016-05-09 17:57:13 +00:00
|
|
|
timeout=90,
|
|
|
|
)
|
|
|
|
|
2016-07-28 18:12:47 +00:00
|
|
|
for line in ret[0]:
|
|
|
|
log.debug('script: salt-minion: stdout: {0}'.format(line))
|
|
|
|
for line in ret[1]:
|
|
|
|
log.debug('script: salt-minion: stderr: {0}'.format(line))
|
|
|
|
log.debug('exit status: {0}'.format(ret[2]))
|
|
|
|
|
2016-08-01 21:25:15 +00:00
|
|
|
if six.PY3:
|
|
|
|
std_out = b'\nSTDOUT:'.join(ret[0])
|
|
|
|
std_err = b'\nSTDERR:'.join(ret[1])
|
|
|
|
else:
|
|
|
|
std_out = '\nSTDOUT:'.join(ret[0])
|
|
|
|
std_err = '\nSTDERR:'.join(ret[1])
|
|
|
|
|
2016-05-09 17:57:13 +00:00
|
|
|
# Check minion state
|
|
|
|
for minion in minions:
|
|
|
|
self.assertEqual(
|
|
|
|
minion.is_running(),
|
|
|
|
minion_running,
|
2016-07-28 18:12:47 +00:00
|
|
|
'script action "{0}" should result in minion "{1}" {2} and is not.\nSTDOUT:{3}\nSTDERR:{4}'.format(
|
|
|
|
action,
|
2016-05-09 17:57:13 +00:00
|
|
|
minion.name,
|
|
|
|
["stopped", "running"][minion_running],
|
2016-08-01 21:25:15 +00:00
|
|
|
std_out,
|
|
|
|
std_err,
|
2016-05-09 17:57:13 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if exitstatus is not None:
|
|
|
|
self.assertEqual(
|
|
|
|
ret[2],
|
|
|
|
exitstatus,
|
|
|
|
'script action "{0}" {1} exited {2}, must be {3}\nSTDOUT:{4}\nSTDERR:{5}'.format(
|
|
|
|
action,
|
|
|
|
message,
|
|
|
|
ret[2],
|
|
|
|
exitstatus,
|
2016-08-01 21:25:15 +00:00
|
|
|
std_out,
|
|
|
|
std_err,
|
2016-05-09 17:57:13 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def _initscript_setup(self, minions):
|
|
|
|
'''Re-usable setup for running salt-minion tests'''
|
|
|
|
|
|
|
|
_minions = []
|
|
|
|
for mname in minions:
|
2016-07-28 18:12:47 +00:00
|
|
|
pid_file = 'salt-{0}.pid'.format(mname)
|
2016-05-09 17:57:13 +00:00
|
|
|
minion = testprogram.TestDaemonSaltMinion(
|
|
|
|
name=mname,
|
2016-06-28 21:10:07 +00:00
|
|
|
root_dir='init_script',
|
|
|
|
config_dir=os.path.join('etc', mname),
|
2016-05-09 17:57:13 +00:00
|
|
|
parent_dir=self._test_dir,
|
2016-07-28 18:12:47 +00:00
|
|
|
pid_file=pid_file,
|
|
|
|
configs={
|
2016-07-28 20:50:53 +00:00
|
|
|
'minion': {
|
|
|
|
'map': {
|
|
|
|
'pidfile': os.path.join('var', 'run', pid_file),
|
|
|
|
'sock_dir': os.path.join('var', 'run', 'salt', mname),
|
|
|
|
'log_file': os.path.join('var', 'log', 'salt', mname),
|
2016-06-28 21:10:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-05-09 17:57:13 +00:00
|
|
|
)
|
|
|
|
# Call setup here to ensure config and script exist
|
|
|
|
minion.setup()
|
|
|
|
_minions.append(minion)
|
|
|
|
|
|
|
|
# Need salt-call, salt-minion for wrapper script
|
2016-06-28 21:10:07 +00:00
|
|
|
salt_call = testprogram.TestProgramSaltCall(root_dir='init_script', parent_dir=self._test_dir)
|
2016-05-09 17:57:13 +00:00
|
|
|
# Ensure that run-time files are generated
|
|
|
|
salt_call.setup()
|
2016-06-28 21:10:07 +00:00
|
|
|
sysconf_dir = os.path.dirname(_minions[0].abs_path(_minions[0].config_dir))
|
2016-05-09 17:57:13 +00:00
|
|
|
cmd_env = {
|
2016-06-28 21:10:07 +00:00
|
|
|
'PATH': ':'.join([salt_call.abs_path(salt_call.script_dir), os.getenv('PATH')]),
|
2016-05-09 17:57:13 +00:00
|
|
|
'SALTMINION_DEBUG': '1' if DEBUG else '',
|
|
|
|
'SALTMINION_PYTHON': sys.executable,
|
|
|
|
'SALTMINION_SYSCONFDIR': sysconf_dir,
|
2016-06-28 21:10:07 +00:00
|
|
|
'SALTMINION_BINDIR': _minions[0].abs_path(_minions[0].script_dir),
|
2016-05-09 17:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default_dir = os.path.join(sysconf_dir, 'default')
|
|
|
|
if not os.path.exists(default_dir):
|
|
|
|
os.makedirs(default_dir)
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(os.path.join(default_dir, 'salt'), 'w') as defaults:
|
2016-05-09 17:57:13 +00:00
|
|
|
# Test suites is quite slow - extend the timeout
|
|
|
|
defaults.write(
|
|
|
|
'TIMEOUT=60\n'
|
|
|
|
'TICK=1\n'
|
|
|
|
)
|
|
|
|
|
|
|
|
init_script = testprogram.TestProgram(
|
|
|
|
name='init:salt-minion',
|
2017-04-03 16:04:09 +00:00
|
|
|
program=os.path.join(CODE_DIR, 'pkg', 'rpm', 'salt-minion'),
|
2016-05-09 17:57:13 +00:00
|
|
|
env=cmd_env,
|
|
|
|
)
|
|
|
|
|
|
|
|
return _minions, salt_call, init_script
|
|
|
|
|
2016-08-02 22:40:30 +00:00
|
|
|
@skipIf(True, 'Disabled. Test suite hanging')
|
2016-05-09 17:57:13 +00:00
|
|
|
def test_linux_initscript(self):
|
|
|
|
'''
|
|
|
|
Various tests of the init script to verify that it properly controls a salt minion.
|
|
|
|
'''
|
|
|
|
|
|
|
|
pform = platform.uname()[0].lower()
|
|
|
|
if pform not in ('linux',):
|
|
|
|
self.skipTest('salt-minion init script is unavailable on {1}'.format(platform))
|
|
|
|
|
2016-06-28 21:10:07 +00:00
|
|
|
minions, _, init_script = self._initscript_setup(self._test_minions)
|
2016-05-09 17:57:13 +00:00
|
|
|
|
|
|
|
try:
|
2016-06-02 22:51:54 +00:00
|
|
|
# These tests are grouped together, rather than split into individual test functions,
|
|
|
|
# because subsequent tests leverage the state from the previous test which minimizes
|
|
|
|
# setup for each test.
|
|
|
|
|
2016-05-09 17:57:13 +00:00
|
|
|
# I take visual readability with aligned columns over strict PEP8
|
|
|
|
# (bad-whitespace) Exactly one space required after comma
|
|
|
|
# pylint: disable=C0326
|
2016-07-28 18:12:47 +00:00
|
|
|
ret = self._run_initscript(init_script, minions[:1], False, 'bogusaction', 2)
|
|
|
|
ret = self._run_initscript(init_script, minions[:1], False, 'reload', 3) # Not implemented
|
|
|
|
ret = self._run_initscript(init_script, minions[:1], False, 'stop', 0, 'when not running')
|
|
|
|
ret = self._run_initscript(init_script, minions[:1], False, 'status', 3, 'when not running')
|
|
|
|
ret = self._run_initscript(init_script, minions[:1], False, 'condrestart', 7, 'when not running')
|
|
|
|
ret = self._run_initscript(init_script, minions[:1], False, 'try-restart', 7, 'when not running')
|
|
|
|
ret = self._run_initscript(init_script, minions, True, 'start', 0, 'when not running')
|
|
|
|
|
|
|
|
ret = self._run_initscript(init_script, minions, True, 'status', 0, 'when running')
|
2016-05-09 17:57:13 +00:00
|
|
|
# Verify that PIDs match
|
2016-07-28 18:12:47 +00:00
|
|
|
mpids = {}
|
|
|
|
for line in ret[0]:
|
2016-08-02 00:32:28 +00:00
|
|
|
segs = line.decode(__salt_system_encoding__).split()
|
2016-07-28 18:12:47 +00:00
|
|
|
minfo = segs[0].split(':')
|
|
|
|
mpids[minfo[-1]] = int(segs[-1]) if segs[-1].isdigit() else None
|
|
|
|
for minion in minions:
|
2016-05-09 17:57:13 +00:00
|
|
|
self.assertEqual(
|
|
|
|
minion.daemon_pid,
|
2016-07-28 18:12:47 +00:00
|
|
|
mpids[minion.name],
|
2016-05-09 17:57:13 +00:00
|
|
|
'PID in "{0}" is {1} and does not match status PID {2}'.format(
|
2016-07-28 18:12:47 +00:00
|
|
|
minion.abs_path(minion.pid_path),
|
2016-05-09 17:57:13 +00:00
|
|
|
minion.daemon_pid,
|
2016-07-28 18:12:47 +00:00
|
|
|
mpids[minion.name],
|
2016-05-09 17:57:13 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2016-07-28 18:12:47 +00:00
|
|
|
ret = self._run_initscript(init_script, minions, True, 'start', 0, 'when running')
|
|
|
|
ret = self._run_initscript(init_script, minions, True, 'condrestart', 0, 'when running')
|
|
|
|
ret = self._run_initscript(init_script, minions, True, 'try-restart', 0, 'when running')
|
|
|
|
ret = self._run_initscript(init_script, minions, False, 'stop', 0, 'when running')
|
2016-05-09 17:57:13 +00:00
|
|
|
|
|
|
|
finally:
|
|
|
|
# Ensure that minions are shutdown
|
|
|
|
for minion in minions:
|
|
|
|
minion.shutdown()
|
|
|
|
|
2016-06-02 00:21:49 +00:00
|
|
|
def test_exit_status_unknown_user(self):
|
|
|
|
'''
|
|
|
|
Ensure correct exit status when the minion is configured to run as an unknown user.
|
|
|
|
'''
|
|
|
|
|
|
|
|
minion = testprogram.TestDaemonSaltMinion(
|
|
|
|
name='unknown_user',
|
2016-09-06 22:30:08 +00:00
|
|
|
configs={'minion': {'map': {'user': 'some_unknown_user_xyz'}}},
|
2016-06-02 00:21:49 +00:00
|
|
|
parent_dir=self._test_dir,
|
|
|
|
)
|
|
|
|
# Call setup here to ensure config and script exist
|
|
|
|
minion.setup()
|
|
|
|
stdout, stderr, status = minion.run(
|
|
|
|
args=['-d'],
|
|
|
|
catch_stderr=True,
|
|
|
|
with_retcode=True,
|
|
|
|
)
|
2017-06-08 05:28:21 +00:00
|
|
|
try:
|
|
|
|
self.assert_exit_status(
|
|
|
|
status, 'EX_NOUSER',
|
|
|
|
message='unknown user not on system',
|
|
|
|
stdout=stdout,
|
|
|
|
stderr=tests.integration.utils.decode_byte_list(stderr)
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
# Although the start-up should fail, call shutdown() to set the
|
|
|
|
# internal _shutdown flag and avoid the registered atexit calls to
|
|
|
|
# cause timeout exeptions and respective traceback
|
|
|
|
minion.shutdown()
|
2016-06-02 00:21:49 +00:00
|
|
|
|
2016-06-02 00:21:49 +00:00
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def test_exit_status_unknown_argument(self):
|
|
|
|
'''
|
|
|
|
Ensure correct exit status when an unknown argument is passed to salt-minion.
|
|
|
|
'''
|
|
|
|
|
|
|
|
minion = testprogram.TestDaemonSaltMinion(
|
|
|
|
name='unknown_argument',
|
|
|
|
parent_dir=self._test_dir,
|
|
|
|
)
|
|
|
|
# Call setup here to ensure config and script exist
|
|
|
|
minion.setup()
|
|
|
|
stdout, stderr, status = minion.run(
|
|
|
|
args=['-d', '--unknown-argument'],
|
|
|
|
catch_stderr=True,
|
|
|
|
with_retcode=True,
|
|
|
|
)
|
2017-06-08 05:28:21 +00:00
|
|
|
try:
|
|
|
|
self.assert_exit_status(
|
|
|
|
status, 'EX_USAGE',
|
|
|
|
message='unknown argument',
|
|
|
|
stdout=stdout,
|
|
|
|
stderr=tests.integration.utils.decode_byte_list(stderr)
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
# Although the start-up should fail, call shutdown() to set the
|
|
|
|
# internal _shutdown flag and avoid the registered atexit calls to
|
|
|
|
# cause timeout exeptions and respective traceback
|
|
|
|
minion.shutdown()
|
2016-06-02 00:21:49 +00:00
|
|
|
|
2016-06-02 00:21:49 +00:00
|
|
|
def test_exit_status_correct_usage(self):
|
|
|
|
'''
|
|
|
|
Ensure correct exit status when salt-minion starts correctly.
|
|
|
|
'''
|
|
|
|
|
|
|
|
minion = testprogram.TestDaemonSaltMinion(
|
|
|
|
name='correct_usage',
|
|
|
|
parent_dir=self._test_dir,
|
|
|
|
)
|
|
|
|
# Call setup here to ensure config and script exist
|
|
|
|
minion.setup()
|
|
|
|
stdout, stderr, status = minion.run(
|
|
|
|
args=['-d'],
|
|
|
|
catch_stderr=True,
|
|
|
|
with_retcode=True,
|
|
|
|
)
|
2016-06-02 22:51:54 +00:00
|
|
|
self.assert_exit_status(
|
|
|
|
status, 'EX_OK',
|
|
|
|
message='correct usage',
|
|
|
|
stdout=stdout, stderr=stderr
|
|
|
|
)
|
2017-06-08 05:28:21 +00:00
|
|
|
minion.shutdown(wait_for_orphans=3)
|