2012-08-04 21:28:51 +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.cp
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2012-12-11 10:23:37 +00:00
|
|
|
'''
|
2012-08-04 21:28:51 +00:00
|
|
|
|
|
|
|
# Import python libs
|
2014-11-21 19:05:13 +00:00
|
|
|
from __future__ import absolute_import
|
2017-05-14 19:20:27 +00:00
|
|
|
import errno
|
2012-08-04 21:28:51 +00:00
|
|
|
import os
|
2012-10-14 21:31:58 +00:00
|
|
|
import pipes
|
2013-10-15 21:09:37 +00:00
|
|
|
import shutil
|
2017-05-14 19:20:27 +00:00
|
|
|
import tempfile
|
2012-08-04 21:28:51 +00:00
|
|
|
|
2017-04-03 16:04:09 +00:00
|
|
|
# Import 3rd-party libs
|
|
|
|
import yaml
|
|
|
|
|
2013-06-27 12:21:16 +00:00
|
|
|
# Import Salt Testing libs
|
2017-04-03 16:04:09 +00:00
|
|
|
from tests.support.case import ShellCase
|
|
|
|
from tests.support.paths import TMP
|
|
|
|
from tests.support.mixins import ShellCaseCommonTestsMixin
|
2013-06-27 12:21:16 +00:00
|
|
|
|
2012-08-04 21:28:51 +00:00
|
|
|
# Import salt libs
|
2017-07-18 16:31:01 +00:00
|
|
|
import salt.utils.files
|
2012-08-04 21:28:51 +00:00
|
|
|
|
2014-11-22 11:02:59 +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
|
2014-11-22 11:02:59 +00:00
|
|
|
|
2012-08-04 21:28:51 +00:00
|
|
|
|
2017-04-03 16:04:09 +00:00
|
|
|
class CopyTest(ShellCase, ShellCaseCommonTestsMixin):
|
2012-08-04 21:28:51 +00:00
|
|
|
|
|
|
|
_call_binary_ = 'salt-cp'
|
|
|
|
|
|
|
|
def test_cp_testfile(self):
|
|
|
|
'''
|
|
|
|
test salt-cp
|
|
|
|
'''
|
2012-09-03 22:24:08 +00:00
|
|
|
minions = []
|
2012-11-16 17:15:39 +00:00
|
|
|
for line in self.run_salt('--out yaml "*" test.ping'):
|
2012-09-03 22:24:08 +00:00
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
data = yaml.load(line)
|
2016-03-15 20:55:35 +00:00
|
|
|
minions.extend(data.keys())
|
2012-09-03 22:24:08 +00:00
|
|
|
|
|
|
|
self.assertNotEqual(minions, [])
|
|
|
|
|
|
|
|
testfile = os.path.abspath(
|
|
|
|
os.path.join(
|
|
|
|
os.path.dirname(os.path.dirname(__file__)),
|
|
|
|
'files', 'file', 'base', 'testfile'
|
|
|
|
)
|
|
|
|
)
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(testfile, 'r') as fh_:
|
2014-11-26 17:50:47 +00:00
|
|
|
testfile_contents = fh_.read()
|
2012-09-03 22:24:08 +00:00
|
|
|
|
2012-10-14 21:31:58 +00:00
|
|
|
for idx, minion in enumerate(minions):
|
|
|
|
ret = self.run_salt(
|
2012-11-16 17:15:39 +00:00
|
|
|
'--out yaml {0} file.directory_exists {1}'.format(
|
2017-04-03 16:04:09 +00:00
|
|
|
pipes.quote(minion), TMP
|
2012-10-14 21:31:58 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
data = yaml.load('\n'.join(ret))
|
|
|
|
if data[minion] is False:
|
|
|
|
ret = self.run_salt(
|
2012-11-16 17:15:39 +00:00
|
|
|
'--out yaml {0} file.makedirs {1}'.format(
|
2012-10-14 21:31:58 +00:00
|
|
|
pipes.quote(minion),
|
2017-04-03 16:04:09 +00:00
|
|
|
TMP
|
2012-10-14 21:31:58 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
data = yaml.load('\n'.join(ret))
|
|
|
|
self.assertTrue(data[minion])
|
|
|
|
|
2012-08-04 21:28:51 +00:00
|
|
|
minion_testfile = os.path.join(
|
2017-04-03 16:04:09 +00:00
|
|
|
TMP, 'cp_{0}_testfile'.format(idx)
|
2012-10-14 21:31:58 +00:00
|
|
|
)
|
|
|
|
|
2015-04-15 17:59:25 +00:00
|
|
|
ret = self.run_cp('--out pprint {0} {1} {2}'.format(
|
2012-10-14 21:31:58 +00:00
|
|
|
pipes.quote(minion),
|
|
|
|
pipes.quote(testfile),
|
|
|
|
pipes.quote(minion_testfile)
|
|
|
|
))
|
|
|
|
|
|
|
|
data = yaml.load('\n'.join(ret))
|
2014-11-22 11:02:59 +00:00
|
|
|
for part in six.itervalues(data):
|
2012-10-14 21:31:58 +00:00
|
|
|
self.assertTrue(part[minion_testfile])
|
|
|
|
|
|
|
|
ret = self.run_salt(
|
2012-11-16 17:15:39 +00:00
|
|
|
'--out yaml {0} file.file_exists {1}'.format(
|
2012-10-14 21:31:58 +00:00
|
|
|
pipes.quote(minion),
|
|
|
|
pipes.quote(minion_testfile)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
data = yaml.load('\n'.join(ret))
|
|
|
|
self.assertTrue(data[minion])
|
|
|
|
|
|
|
|
ret = self.run_salt(
|
2012-11-16 17:15:39 +00:00
|
|
|
'--out yaml {0} file.contains {1} {2}'.format(
|
2012-10-14 21:31:58 +00:00
|
|
|
pipes.quote(minion),
|
|
|
|
pipes.quote(minion_testfile),
|
|
|
|
pipes.quote(testfile_contents)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
data = yaml.load('\n'.join(ret))
|
|
|
|
self.assertTrue(data[minion])
|
|
|
|
ret = self.run_salt(
|
2012-11-16 17:15:39 +00:00
|
|
|
'--out yaml {0} file.remove {1}'.format(
|
2012-10-14 21:31:58 +00:00
|
|
|
pipes.quote(minion),
|
|
|
|
pipes.quote(minion_testfile)
|
|
|
|
)
|
2012-08-04 21:28:51 +00:00
|
|
|
)
|
2012-10-14 21:31:58 +00:00
|
|
|
data = yaml.load('\n'.join(ret))
|
|
|
|
self.assertTrue(data[minion])
|
2012-08-04 21:28:51 +00:00
|
|
|
|
2013-10-15 21:09:37 +00:00
|
|
|
def test_issue_7754(self):
|
2017-04-03 16:04:09 +00:00
|
|
|
config_dir = os.path.join(TMP, 'issue-7754')
|
2013-10-15 21:09:37 +00:00
|
|
|
|
2017-05-14 19:20:27 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(config_dir)
|
|
|
|
except OSError as exc:
|
|
|
|
if exc.errno != errno.EEXIST:
|
|
|
|
raise
|
2013-10-15 21:09:37 +00:00
|
|
|
|
|
|
|
config_file_name = 'master'
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(self.get_config_file_path(config_file_name), 'r') as fhr:
|
2014-11-26 21:07:39 +00:00
|
|
|
config = yaml.load(fhr.read())
|
|
|
|
config['log_file'] = 'file:///dev/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:
|
2014-11-26 21:07:39 +00:00
|
|
|
fhw.write(
|
|
|
|
yaml.dump(config, default_flow_style=False)
|
|
|
|
)
|
2013-10-15 21:09:37 +00:00
|
|
|
|
|
|
|
try:
|
2017-05-14 19:20:27 +00:00
|
|
|
fd_, fn_ = tempfile.mkstemp()
|
|
|
|
os.close(fd_)
|
|
|
|
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(fn_, 'w') as fp_:
|
2017-05-14 19:20:27 +00:00
|
|
|
fp_.write('Hello world!\n')
|
|
|
|
|
|
|
|
ret = self.run_script(
|
|
|
|
self._call_binary_,
|
|
|
|
'--out pprint --config-dir {0} \'*\' {1} {0}/{2}'.format(
|
|
|
|
config_dir,
|
|
|
|
fn_,
|
|
|
|
os.path.basename(fn_),
|
|
|
|
),
|
|
|
|
catch_stderr=True,
|
|
|
|
with_retcode=True
|
|
|
|
)
|
|
|
|
|
2014-01-20 11:09:56 +00:00
|
|
|
self.assertIn('minion', '\n'.join(ret[0]))
|
|
|
|
self.assertIn('sub_minion', '\n'.join(ret[0]))
|
2013-10-15 21:09:37 +00:00
|
|
|
self.assertFalse(os.path.isdir(os.path.join(config_dir, 'file:')))
|
2014-01-19 07:25:57 +00:00
|
|
|
except AssertionError:
|
2014-01-20 11:09:56 +00:00
|
|
|
if os.path.exists('/dev/log') and ret[2] != 2:
|
|
|
|
# If there's a syslog device and the exit code was not 2, 'No
|
|
|
|
# such file or directory', raise the error
|
|
|
|
raise
|
2014-01-19 07:25:57 +00:00
|
|
|
self.assertIn(
|
|
|
|
'Failed to setup the Syslog logging handler', '\n'.join(ret[1])
|
|
|
|
)
|
|
|
|
self.assertEqual(ret[2], 2)
|
2013-10-15 21:09:37 +00:00
|
|
|
finally:
|
2017-05-14 19:20:27 +00:00
|
|
|
try:
|
|
|
|
os.remove(fn_)
|
|
|
|
except OSError as exc:
|
|
|
|
if exc.errno != errno.ENOENT:
|
|
|
|
raise
|
2013-10-15 21:09:37 +00:00
|
|
|
if os.path.isdir(config_dir):
|
|
|
|
shutil.rmtree(config_dir)
|