2012-04-04 12:00:52 -06:00
|
|
|
import hashlib
|
2017-06-01 15:45:54 -05:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import signal
|
|
|
|
import tempfile
|
|
|
|
import textwrap
|
2019-12-14 02:28:50 +00:00
|
|
|
import time
|
2016-08-23 19:33:45 +09:00
|
|
|
import uuid
|
2012-11-18 18:57:10 +00:00
|
|
|
|
2020-12-02 10:29:40 -08:00
|
|
|
import psutil # pylint: disable=3rd-party-module-not-gated
|
2020-04-10 08:30:25 +01:00
|
|
|
import pytest
|
2022-07-20 10:42:30 +01:00
|
|
|
from pytestshellutils.utils import ports
|
|
|
|
from saltfactories.utils.tempfiles import temp_file
|
|
|
|
|
2017-07-18 10:31:01 -06:00
|
|
|
import salt.utils.files
|
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-24 20:47:15 -05:00
|
|
|
import salt.utils.path
|
|
|
|
import salt.utils.platform
|
|
|
|
import salt.utils.stringutils
|
2017-04-03 17:04:09 +01:00
|
|
|
from tests.support.case import ModuleCase
|
2021-02-02 18:36:14 +00:00
|
|
|
from tests.support.helpers import with_tempfile
|
2018-12-07 17:52:49 +00:00
|
|
|
from tests.support.runtests import RUNTIME_VARS
|
2020-04-02 20:10:20 -05:00
|
|
|
|
2017-06-01 15:45:54 -05:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2012-04-03 23:15:37 -06:00
|
|
|
|
2020-04-10 08:30:25 +01:00
|
|
|
@pytest.mark.windows_whitelisted
|
2017-04-03 17:04:09 +01:00
|
|
|
class CPModuleTest(ModuleCase):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2015-05-03 18:10:39 -06:00
|
|
|
Validate the cp module
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
|
|
|
|
2019-12-03 18:25:35 +00:00
|
|
|
def run_function(self, *args, **kwargs): # pylint: disable=arguments-differ
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2018-01-18 16:13:08 -06:00
|
|
|
Ensure that results are decoded
|
|
|
|
|
|
|
|
TODO: maybe move this behavior to ModuleCase itself?
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2021-01-31 08:00:00 +00:00
|
|
|
return salt.utils.data.decode(super().run_function(*args, **kwargs))
|
2018-01-18 16:13:08 -06:00
|
|
|
|
2018-04-13 21:41:02 -05:00
|
|
|
@with_tempfile()
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2018-03-28 11:38:32 -06:00
|
|
|
def test_get_file(self, tgt):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-04-03 23:15:37 -06:00
|
|
|
cp.get_file
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-04-03 23:15:37 -06:00
|
|
|
self.run_function("cp.get_file", ["salt://grail/scene33", tgt])
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(tgt, "r") as scene:
|
2018-01-18 16:13:08 -06:00
|
|
|
data = salt.utils.stringutils.to_unicode(scene.read())
|
|
|
|
self.assertIn("KNIGHT: They're nervous, sire.", data)
|
|
|
|
self.assertNotIn("bacon", data)
|
2012-04-03 23:31:53 -06:00
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2016-08-05 10:46:24 -05:00
|
|
|
def test_get_file_to_dir(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-08-05 10:46:24 -05:00
|
|
|
cp.get_file
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2018-12-07 17:52:49 +00:00
|
|
|
tgt = os.path.join(RUNTIME_VARS.TMP, "")
|
2016-08-05 10:46:24 -05:00
|
|
|
self.run_function("cp.get_file", ["salt://grail/scene33", tgt])
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(tgt + "scene33", "r") as scene:
|
2018-01-18 16:13:08 -06:00
|
|
|
data = salt.utils.stringutils.to_unicode(scene.read())
|
|
|
|
self.assertIn("KNIGHT: They're nervous, sire.", data)
|
|
|
|
self.assertNotIn("bacon", data)
|
2016-08-05 10:46:24 -05:00
|
|
|
|
2018-04-13 21:41:02 -05:00
|
|
|
@with_tempfile()
|
2022-12-05 10:10:56 +00:00
|
|
|
@pytest.mark.skip_on_windows(reason="This test hangs on Windows on Py3")
|
2018-03-28 11:38:32 -06:00
|
|
|
def test_get_file_templated_paths(self, tgt):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-11-06 18:36:10 -08:00
|
|
|
cp.get_file
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-11-06 18:36:10 -08:00
|
|
|
self.run_function(
|
|
|
|
"cp.get_file",
|
|
|
|
[
|
2012-11-06 19:05:59 -08:00
|
|
|
"salt://{{grains.test_grain}}",
|
|
|
|
tgt.replace("cheese", "{{grains.test_grain}}"),
|
|
|
|
],
|
|
|
|
template="jinja",
|
|
|
|
)
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(tgt, "r") as cheese:
|
2018-01-18 16:13:08 -06:00
|
|
|
data = salt.utils.stringutils.to_unicode(cheese.read())
|
|
|
|
self.assertIn("Gromit", data)
|
|
|
|
self.assertNotIn("bacon", data)
|
2012-11-06 18:36:10 -08:00
|
|
|
|
2018-04-13 21:41:02 -05:00
|
|
|
@with_tempfile()
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2018-03-28 11:38:32 -06:00
|
|
|
def test_get_file_gzipped(self, tgt):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-11-08 16:09:38 -08:00
|
|
|
cp.get_file
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2018-12-07 17:52:49 +00:00
|
|
|
src = os.path.join(RUNTIME_VARS.FILES, "file", "base", "file.big")
|
2018-01-18 16:13:08 -06:00
|
|
|
with salt.utils.files.fopen(src, "rb") as fp_:
|
2023-10-20 16:59:02 +01:00
|
|
|
hash_str = hashlib.sha256(fp_.read()).hexdigest()
|
2012-11-08 16:09:38 -08:00
|
|
|
|
|
|
|
self.run_function("cp.get_file", ["salt://file.big", tgt], gzip=5)
|
2018-01-18 16:13:08 -06:00
|
|
|
with salt.utils.files.fopen(tgt, "rb") as scene:
|
2012-11-08 16:09:38 -08:00
|
|
|
data = scene.read()
|
2023-10-20 16:59:02 +01:00
|
|
|
self.assertEqual(hash_str, hashlib.sha256(data).hexdigest())
|
2018-01-18 16:13:08 -06:00
|
|
|
data = salt.utils.stringutils.to_unicode(data)
|
|
|
|
self.assertIn("KNIGHT: They're nervous, sire.", data)
|
|
|
|
self.assertNotIn("bacon", data)
|
2012-11-08 16:09:38 -08:00
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2012-11-14 18:06:53 -08:00
|
|
|
def test_get_file_makedirs(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-11-14 16:48:26 -08:00
|
|
|
cp.get_file
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2018-12-07 17:52:49 +00:00
|
|
|
tgt = os.path.join(RUNTIME_VARS.TMP, "make", "dirs", "scene33")
|
2012-11-14 16:48:26 -08:00
|
|
|
self.run_function("cp.get_file", ["salt://grail/scene33", tgt], makedirs=True)
|
2018-12-07 17:52:49 +00:00
|
|
|
self.addCleanup(
|
2012-11-14 18:06:53 -08:00
|
|
|
shutil.rmtree, os.path.join(RUNTIME_VARS.TMP, "make"), ignore_errors=True
|
2012-11-14 16:48:26 -08:00
|
|
|
)
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(tgt, "r") as scene:
|
2018-01-18 16:13:08 -06:00
|
|
|
data = salt.utils.stringutils.to_unicode(scene.read())
|
|
|
|
self.assertIn("KNIGHT: They're nervous, sire.", data)
|
|
|
|
self.assertNotIn("bacon", data)
|
2012-11-14 16:48:26 -08:00
|
|
|
|
2018-04-13 21:41:02 -05:00
|
|
|
@with_tempfile()
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2018-03-28 11:38:32 -06:00
|
|
|
def test_get_template(self, tgt):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-04-03 23:31:53 -06:00
|
|
|
cp.get_template
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-04-03 23:31:53 -06:00
|
|
|
self.run_function(
|
2017-02-25 20:11:06 -06:00
|
|
|
"cp.get_template", ["salt://grail/scene33", tgt], spam="bacon"
|
|
|
|
)
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(tgt, "r") as scene:
|
2018-01-18 16:13:08 -06:00
|
|
|
data = salt.utils.stringutils.to_unicode(scene.read())
|
|
|
|
self.assertIn("bacon", data)
|
|
|
|
self.assertNotIn("spam", data)
|
2012-04-03 23:46:45 -06:00
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2012-04-03 23:46:45 -06:00
|
|
|
def test_get_dir(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-04-03 23:46:45 -06:00
|
|
|
cp.get_dir
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2018-12-07 17:52:49 +00:00
|
|
|
tgt = os.path.join(RUNTIME_VARS.TMP, "many")
|
2012-04-03 23:46:45 -06:00
|
|
|
self.run_function("cp.get_dir", ["salt://grail", tgt])
|
|
|
|
self.assertIn("grail", os.listdir(tgt))
|
|
|
|
self.assertIn("36", os.listdir(os.path.join(tgt, "grail")))
|
2012-11-14 16:48:26 -08:00
|
|
|
self.assertIn("empty", os.listdir(os.path.join(tgt, "grail")))
|
|
|
|
self.assertIn("scene", os.listdir(os.path.join(tgt, "grail", "36")))
|
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2012-11-14 16:48:26 -08:00
|
|
|
def test_get_dir_templated_paths(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-11-14 16:48:26 -08:00
|
|
|
cp.get_dir
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2018-12-07 17:52:49 +00:00
|
|
|
tgt = os.path.join(RUNTIME_VARS.TMP, "many")
|
2012-11-14 16:48:26 -08:00
|
|
|
self.run_function(
|
|
|
|
"cp.get_dir",
|
|
|
|
["salt://{{grains.script}}", tgt.replace("many", "{{grains.alot}}")],
|
|
|
|
)
|
|
|
|
self.assertIn("grail", os.listdir(tgt))
|
|
|
|
self.assertIn("36", os.listdir(os.path.join(tgt, "grail")))
|
2012-04-03 23:46:45 -06:00
|
|
|
self.assertIn("empty", os.listdir(os.path.join(tgt, "grail")))
|
|
|
|
self.assertIn("scene", os.listdir(os.path.join(tgt, "grail", "36")))
|
2012-04-03 23:52:56 -06:00
|
|
|
|
2016-12-09 13:05:57 +02:00
|
|
|
# cp.get_url tests
|
|
|
|
|
2018-04-13 21:41:02 -05:00
|
|
|
@with_tempfile()
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2018-03-28 11:38:32 -06:00
|
|
|
def test_get_url(self, tgt):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-10-17 15:53:19 +03:00
|
|
|
cp.get_url with salt:// source given
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-10-17 15:10:22 +03:00
|
|
|
self.run_function("cp.get_url", ["salt://grail/scene33", tgt])
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(tgt, "r") as scene:
|
2018-01-18 16:13:08 -06:00
|
|
|
data = salt.utils.stringutils.to_unicode(scene.read())
|
|
|
|
self.assertIn("KNIGHT: They're nervous, sire.", data)
|
|
|
|
self.assertNotIn("bacon", data)
|
2012-04-03 23:52:56 -06:00
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2016-08-19 11:08:18 -05:00
|
|
|
def test_get_url_makedirs(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-08-19 11:08:18 -05:00
|
|
|
cp.get_url
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2018-12-07 17:52:49 +00:00
|
|
|
tgt = os.path.join(RUNTIME_VARS.TMP, "make", "dirs", "scene33")
|
2016-08-19 11:08:18 -05:00
|
|
|
self.run_function("cp.get_url", ["salt://grail/scene33", tgt], makedirs=True)
|
2018-12-07 17:52:49 +00:00
|
|
|
self.addCleanup(
|
|
|
|
shutil.rmtree, os.path.join(RUNTIME_VARS.TMP, "make"), ignore_errors=True
|
|
|
|
)
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(tgt, "r") as scene:
|
2018-01-18 16:13:08 -06:00
|
|
|
data = salt.utils.stringutils.to_unicode(scene.read())
|
|
|
|
self.assertIn("KNIGHT: They're nervous, sire.", data)
|
|
|
|
self.assertNotIn("bacon", data)
|
2016-08-19 11:08:18 -05:00
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2016-10-17 15:10:22 +03:00
|
|
|
def test_get_url_dest_empty(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-10-17 15:53:19 +03:00
|
|
|
cp.get_url with salt:// source given and destination omitted.
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-10-17 15:10:22 +03:00
|
|
|
ret = self.run_function("cp.get_url", ["salt://grail/scene33"])
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(ret, "r") as scene:
|
2018-01-18 16:13:08 -06:00
|
|
|
data = salt.utils.stringutils.to_unicode(scene.read())
|
|
|
|
self.assertIn("KNIGHT: They're nervous, sire.", data)
|
|
|
|
self.assertNotIn("bacon", data)
|
2016-10-17 15:10:22 +03:00
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2016-10-17 15:10:22 +03:00
|
|
|
def test_get_url_no_dest(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-10-17 15:53:19 +03:00
|
|
|
cp.get_url with salt:// source given and destination set as None
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-10-17 15:10:22 +03:00
|
|
|
tgt = None
|
|
|
|
ret = self.run_function("cp.get_url", ["salt://grail/scene33", tgt])
|
|
|
|
self.assertIn("KNIGHT: They're nervous, sire.", ret)
|
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2016-10-17 15:10:22 +03:00
|
|
|
def test_get_url_nonexistent_source(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-10-17 15:53:19 +03:00
|
|
|
cp.get_url with nonexistent salt:// source given
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-10-17 15:10:22 +03:00
|
|
|
tgt = None
|
|
|
|
ret = self.run_function("cp.get_url", ["salt://grail/nonexistent_scene", tgt])
|
|
|
|
self.assertEqual(ret, False)
|
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2016-08-05 10:46:24 -05:00
|
|
|
def test_get_url_to_dir(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-08-05 10:46:24 -05:00
|
|
|
cp.get_url with salt:// source
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2018-12-07 17:52:49 +00:00
|
|
|
tgt = os.path.join(RUNTIME_VARS.TMP, "")
|
2016-08-05 10:46:24 -05:00
|
|
|
self.run_function("cp.get_url", ["salt://grail/scene33", tgt])
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(tgt + "scene33", "r") as scene:
|
2018-01-18 16:13:08 -06:00
|
|
|
data = salt.utils.stringutils.to_unicode(scene.read())
|
|
|
|
self.assertIn("KNIGHT: They're nervous, sire.", data)
|
|
|
|
self.assertNotIn("bacon", data)
|
2016-08-05 10:46:24 -05:00
|
|
|
|
2018-04-13 21:41:02 -05:00
|
|
|
@with_tempfile()
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2018-03-28 11:38:32 -06:00
|
|
|
def test_get_url_https(self, tgt):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-10-17 15:53:19 +03:00
|
|
|
cp.get_url with https:// source given
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2024-10-31 09:52:00 -06:00
|
|
|
self.run_function(
|
|
|
|
"cp.get_url",
|
|
|
|
["https://packages.broadcom.com/artifactory/saltproject-generic/", tgt],
|
|
|
|
)
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(tgt, "r") as instructions:
|
2018-01-18 16:13:08 -06:00
|
|
|
data = salt.utils.stringutils.to_unicode(instructions.read())
|
2024-10-31 09:52:00 -06:00
|
|
|
self.assertIn("Index of saltproject", data)
|
|
|
|
self.assertIn("onedir", data)
|
|
|
|
self.assertIn("Artifactory Online Server", data)
|
2018-01-18 16:13:08 -06:00
|
|
|
self.assertNotIn("AYBABTU", data)
|
2020-04-02 20:10:20 -05:00
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2016-10-18 13:39:55 +03:00
|
|
|
def test_get_url_https_dest_empty(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-10-18 13:39:55 +03:00
|
|
|
cp.get_url with https:// source given and destination omitted.
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2024-10-31 09:52:00 -06:00
|
|
|
ret = self.run_function(
|
|
|
|
"cp.get_url",
|
|
|
|
["https://packages.broadcom.com/artifactory/saltproject-generic/"],
|
|
|
|
)
|
2019-05-09 11:36:57 +01:00
|
|
|
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(ret, "r") as instructions:
|
2018-01-18 16:13:08 -06:00
|
|
|
data = salt.utils.stringutils.to_unicode(instructions.read())
|
2024-10-31 09:52:00 -06:00
|
|
|
self.assertIn("Index of saltproject", data)
|
|
|
|
self.assertIn("onedir", data)
|
|
|
|
self.assertIn("Artifactory Online Server", data)
|
2018-01-18 16:13:08 -06:00
|
|
|
self.assertNotIn("AYBABTU", data)
|
2020-04-02 20:10:20 -05:00
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2016-10-17 17:23:03 +03:00
|
|
|
def test_get_url_https_no_dest(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-10-17 17:23:03 +03:00
|
|
|
cp.get_url with https:// source given and destination set as None
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2019-12-14 02:28:50 +00:00
|
|
|
timeout = 500
|
|
|
|
start = time.time()
|
|
|
|
sleep = 5
|
2016-10-17 17:23:03 +03:00
|
|
|
tgt = None
|
2019-12-14 02:28:50 +00:00
|
|
|
while time.time() - start <= timeout:
|
2024-10-31 09:52:00 -06:00
|
|
|
ret = self.run_function(
|
|
|
|
"cp.get_url",
|
|
|
|
["https://packages.broadcom.com/artifactory/saltproject-generic/", tgt],
|
|
|
|
)
|
2019-12-14 02:28:50 +00:00
|
|
|
if ret.find("HTTP 599") == -1:
|
|
|
|
break
|
2019-12-14 02:43:18 +00:00
|
|
|
time.sleep(sleep)
|
2019-12-14 02:28:50 +00:00
|
|
|
if ret.find("HTTP 599") != -1:
|
2024-10-31 09:52:00 -06:00
|
|
|
raise Exception(
|
|
|
|
"https://packages.broadcom.com/artifactory/saltproject-generic/ returned 599 error"
|
|
|
|
)
|
|
|
|
self.assertIn("Index of saltproject", ret)
|
|
|
|
self.assertIn("onedir", ret)
|
|
|
|
self.assertIn("Artifactory Online Server", ret)
|
2016-10-17 17:23:03 +03:00
|
|
|
self.assertNotIn("AYBABTU", ret)
|
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2016-10-18 13:39:55 +03:00
|
|
|
def test_get_url_file(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-10-18 13:39:55 +03:00
|
|
|
cp.get_url with file:// source given
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-10-18 13:39:55 +03:00
|
|
|
tgt = ""
|
2018-12-07 17:52:49 +00:00
|
|
|
src = os.path.join("file://", RUNTIME_VARS.FILES, "file", "base", "file.big")
|
2016-10-18 13:39:55 +03:00
|
|
|
ret = self.run_function("cp.get_url", [src, tgt])
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(ret, "r") as scene:
|
2018-01-18 16:13:08 -06:00
|
|
|
data = salt.utils.stringutils.to_unicode(scene.read())
|
|
|
|
self.assertIn("KNIGHT: They're nervous, sire.", data)
|
|
|
|
self.assertNotIn("bacon", data)
|
2016-10-18 13:39:55 +03:00
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2016-10-18 13:39:55 +03:00
|
|
|
def test_get_url_file_no_dest(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-10-18 13:39:55 +03:00
|
|
|
cp.get_url with file:// source given and destination set as None
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-10-18 13:39:55 +03:00
|
|
|
tgt = None
|
2018-12-07 17:52:49 +00:00
|
|
|
src = os.path.join("file://", RUNTIME_VARS.FILES, "file", "base", "file.big")
|
2016-10-18 13:39:55 +03:00
|
|
|
ret = self.run_function("cp.get_url", [src, tgt])
|
|
|
|
self.assertIn("KNIGHT: They're nervous, sire.", ret)
|
|
|
|
self.assertNotIn("bacon", ret)
|
|
|
|
|
2019-10-17 16:30:12 -07:00
|
|
|
@with_tempfile()
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2019-10-17 16:30:12 -07:00
|
|
|
def test_get_url_ftp(self, tgt):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2019-10-17 16:30:12 -07:00
|
|
|
cp.get_url with https:// source given
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2019-10-17 16:30:12 -07:00
|
|
|
self.run_function(
|
|
|
|
"cp.get_url",
|
|
|
|
[
|
2022-04-15 12:14:17 -06:00
|
|
|
"ftp://ftp.freebsd.org/pub/FreeBSD/releases/amd64/README.TXT",
|
2019-10-17 16:30:12 -07:00
|
|
|
tgt,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
with salt.utils.files.fopen(tgt, "r") as instructions:
|
|
|
|
data = salt.utils.stringutils.to_unicode(instructions.read())
|
2022-04-15 12:14:17 -06:00
|
|
|
self.assertIn("The official FreeBSD", data)
|
2019-10-17 16:30:12 -07:00
|
|
|
|
2016-12-09 13:05:57 +02:00
|
|
|
# cp.get_file_str tests
|
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2016-12-09 13:05:57 +02:00
|
|
|
def test_get_file_str_salt(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-12-09 13:05:57 +02:00
|
|
|
cp.get_file_str with salt:// source given
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-12-09 13:05:57 +02:00
|
|
|
src = "salt://grail/scene33"
|
|
|
|
ret = self.run_function("cp.get_file_str", [src])
|
|
|
|
self.assertIn("KNIGHT: They're nervous, sire.", ret)
|
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2016-12-09 13:05:57 +02:00
|
|
|
def test_get_file_str_nonexistent_source(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-12-09 13:05:57 +02:00
|
|
|
cp.get_file_str with nonexistent salt:// source given
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-12-09 13:05:57 +02:00
|
|
|
src = "salt://grail/nonexistent_scene"
|
|
|
|
ret = self.run_function("cp.get_file_str", [src])
|
|
|
|
self.assertEqual(ret, False)
|
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2016-12-09 13:05:57 +02:00
|
|
|
def test_get_file_str_https(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-12-09 13:05:57 +02:00
|
|
|
cp.get_file_str with https:// source given
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2024-10-31 09:52:00 -06:00
|
|
|
src = "https://packages.broadcom.com/artifactory/saltproject-generic/"
|
2016-12-09 13:05:57 +02:00
|
|
|
ret = self.run_function("cp.get_file_str", [src])
|
2024-10-31 09:52:00 -06:00
|
|
|
self.assertIn("Index of saltproject", ret)
|
|
|
|
self.assertIn("onedir", ret)
|
|
|
|
self.assertIn("Artifactory Online Server", ret)
|
2016-12-09 13:05:57 +02:00
|
|
|
self.assertNotIn("AYBABTU", ret)
|
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2016-12-09 13:05:57 +02:00
|
|
|
def test_get_file_str_local(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-12-09 13:05:57 +02:00
|
|
|
cp.get_file_str with file:// source given
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2018-12-07 17:52:49 +00:00
|
|
|
src = os.path.join("file://", RUNTIME_VARS.FILES, "file", "base", "file.big")
|
2016-12-09 13:05:57 +02:00
|
|
|
ret = self.run_function("cp.get_file_str", [src])
|
|
|
|
self.assertIn("KNIGHT: They're nervous, sire.", ret)
|
|
|
|
self.assertNotIn("bacon", ret)
|
|
|
|
|
|
|
|
# caching tests
|
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2012-04-03 23:58:34 -06:00
|
|
|
def test_cache_file(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-04-03 23:58:34 -06:00
|
|
|
cp.cache_file
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-04-03 23:58:34 -06:00
|
|
|
ret = self.run_function("cp.cache_file", ["salt://grail/scene33"])
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(ret, "r") as scene:
|
2018-01-18 16:13:08 -06:00
|
|
|
data = salt.utils.stringutils.to_unicode(scene.read())
|
|
|
|
self.assertIn("KNIGHT: They're nervous, sire.", data)
|
|
|
|
self.assertNotIn("bacon", data)
|
2012-04-04 00:06:41 -06:00
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2012-04-04 00:06:41 -06:00
|
|
|
def test_cache_files(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-04-04 00:06:41 -06:00
|
|
|
cp.cache_files
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-04-04 00:06:41 -06:00
|
|
|
ret = self.run_function(
|
2012-05-29 22:10:20 +05:30
|
|
|
"cp.cache_files", [["salt://grail/scene33", "salt://grail/36/scene"]]
|
2012-04-04 00:06:41 -06:00
|
|
|
)
|
|
|
|
for path in ret:
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(path, "r") as scene:
|
2018-01-18 16:13:08 -06:00
|
|
|
data = salt.utils.stringutils.to_unicode(scene.read())
|
|
|
|
self.assertIn("ARTHUR:", data)
|
|
|
|
self.assertNotIn("bacon", data)
|
2012-04-04 10:13:29 -06:00
|
|
|
|
2018-04-24 19:09:08 -07:00
|
|
|
@with_tempfile()
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2018-04-24 14:58:20 -07:00
|
|
|
def test_cache_master(self, tgt):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-04-04 10:13:29 -06:00
|
|
|
cp.cache_master
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2021-08-03 07:25:24 +01:00
|
|
|
ret = self.run_function(
|
|
|
|
"cp.cache_master",
|
|
|
|
[tgt],
|
|
|
|
)
|
2012-04-04 10:13:29 -06:00
|
|
|
for path in ret:
|
|
|
|
self.assertTrue(os.path.exists(path))
|
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2012-04-04 10:25:02 -06:00
|
|
|
def test_cache_local_file(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-04-04 10:25:02 -06:00
|
|
|
cp.cache_local_file
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2018-12-07 17:52:49 +00:00
|
|
|
src = os.path.join(RUNTIME_VARS.TMP, "random")
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(src, "w+") as fn_:
|
2018-01-18 16:13:08 -06:00
|
|
|
fn_.write(salt.utils.stringutils.to_str("foo"))
|
2012-04-04 10:25:02 -06:00
|
|
|
ret = self.run_function("cp.cache_local_file", [src])
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(ret, "r") as cp_:
|
2018-01-18 16:13:08 -06:00
|
|
|
self.assertEqual(salt.utils.stringutils.to_unicode(cp_.read()), "foo")
|
2020-04-02 20:10:20 -05:00
|
|
|
|
2022-12-05 09:57:32 +00:00
|
|
|
@pytest.mark.skip_if_binaries_missing("nginx")
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2021-02-02 18:36:14 +00:00
|
|
|
@pytest.mark.skip_if_not_root
|
2017-06-01 15:45:54 -05:00
|
|
|
def test_cache_remote_file(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2017-06-01 15:45:54 -05:00
|
|
|
cp.cache_file
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2022-05-31 17:35:38 +01:00
|
|
|
nginx_port = ports.get_unused_localhost_port()
|
2024-02-27 11:08:46 +00:00
|
|
|
url_prefix = f"http://localhost:{nginx_port}/"
|
2018-12-07 17:52:49 +00:00
|
|
|
temp_dir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
|
2017-06-01 15:45:54 -05:00
|
|
|
self.addCleanup(shutil.rmtree, temp_dir, ignore_errors=True)
|
|
|
|
nginx_root_dir = os.path.join(temp_dir, "root")
|
|
|
|
nginx_conf_dir = os.path.join(temp_dir, "conf")
|
|
|
|
nginx_conf = os.path.join(nginx_conf_dir, "nginx.conf")
|
|
|
|
nginx_pidfile = os.path.join(nginx_conf_dir, "nginx.pid")
|
|
|
|
file_contents = "Hello world!"
|
|
|
|
|
|
|
|
for dirname in (nginx_root_dir, nginx_conf_dir):
|
2019-09-18 16:59:36 -07:00
|
|
|
os.makedirs(dirname)
|
2017-06-01 15:45:54 -05:00
|
|
|
|
|
|
|
# Write the temp file
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(
|
|
|
|
os.path.join(nginx_root_dir, "actual_file"), "w"
|
|
|
|
) as fp_:
|
2018-01-18 16:13:08 -06:00
|
|
|
fp_.write(salt.utils.stringutils.to_str(file_contents))
|
2017-06-01 15:45:54 -05:00
|
|
|
|
|
|
|
# Write the nginx config
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(nginx_conf, "w") as fp_:
|
2018-01-18 16:13:08 -06:00
|
|
|
fp_.write(
|
|
|
|
textwrap.dedent(
|
|
|
|
salt.utils.stringutils.to_str(
|
2024-02-27 11:08:46 +00:00
|
|
|
f"""\
|
2017-06-01 15:45:54 -05:00
|
|
|
user root;
|
|
|
|
worker_processes 1;
|
|
|
|
error_log {nginx_conf_dir}/server_error.log;
|
|
|
|
pid {nginx_pidfile};
|
|
|
|
|
|
|
|
events {{
|
|
|
|
worker_connections 1024;
|
|
|
|
}}
|
|
|
|
|
|
|
|
http {{
|
|
|
|
include /etc/nginx/mime.types;
|
|
|
|
default_type application/octet-stream;
|
|
|
|
|
|
|
|
access_log {nginx_conf_dir}/access.log;
|
|
|
|
error_log {nginx_conf_dir}/error.log;
|
|
|
|
|
|
|
|
server {{
|
|
|
|
listen {nginx_port} default_server;
|
|
|
|
server_name cachefile.local;
|
|
|
|
root {nginx_root_dir};
|
|
|
|
|
|
|
|
location ~ ^/301$ {{
|
|
|
|
return 301 /actual_file;
|
|
|
|
}}
|
|
|
|
|
|
|
|
location ~ ^/302$ {{
|
|
|
|
return 302 /actual_file;
|
|
|
|
}}
|
|
|
|
}}
|
2024-02-27 11:08:46 +00:00
|
|
|
}}"""
|
2018-01-18 16:13:08 -06:00
|
|
|
)
|
2020-04-02 20:10:20 -05:00
|
|
|
)
|
|
|
|
)
|
2017-06-01 15:45:54 -05:00
|
|
|
|
|
|
|
self.run_function("cmd.run", [["nginx", "-c", nginx_conf]], python_shell=False)
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(nginx_pidfile) as fp_:
|
2017-06-01 15:45:54 -05:00
|
|
|
nginx_pid = int(fp_.read().strip())
|
|
|
|
nginx_proc = psutil.Process(pid=nginx_pid)
|
|
|
|
self.addCleanup(nginx_proc.send_signal, signal.SIGQUIT)
|
|
|
|
|
|
|
|
for code in ("", "301", "302"):
|
|
|
|
url = url_prefix + (code or "actual_file")
|
|
|
|
log.debug("attempting to cache %s", url)
|
|
|
|
ret = self.run_function("cp.cache_file", [url])
|
2018-02-27 13:32:57 -06:00
|
|
|
self.assertTrue(ret)
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(ret) as fp_:
|
2018-01-18 16:13:08 -06:00
|
|
|
cached_contents = salt.utils.stringutils.to_unicode(fp_.read())
|
2017-06-01 15:45:54 -05:00
|
|
|
self.assertEqual(cached_contents, file_contents)
|
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2012-04-04 10:28:57 -06:00
|
|
|
def test_list_states(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-04-04 10:28:57 -06:00
|
|
|
cp.list_states
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2020-12-02 10:29:40 -08:00
|
|
|
top_sls = """
|
|
|
|
base:
|
|
|
|
'*':
|
|
|
|
- core
|
|
|
|
"""
|
|
|
|
|
|
|
|
core_state = """
|
|
|
|
{}/testfile:
|
|
|
|
file:
|
|
|
|
- managed
|
|
|
|
- source: salt://testfile
|
|
|
|
- makedirs: true
|
|
|
|
""".format(
|
|
|
|
RUNTIME_VARS.TMP
|
|
|
|
)
|
|
|
|
|
2021-05-05 16:21:56 +01:00
|
|
|
with temp_file(
|
|
|
|
"top.sls", top_sls, RUNTIME_VARS.TMP_BASEENV_STATE_TREE
|
|
|
|
), temp_file("core.sls", core_state, RUNTIME_VARS.TMP_BASEENV_STATE_TREE):
|
2021-08-03 07:25:24 +01:00
|
|
|
ret = self.run_function(
|
|
|
|
"cp.list_states",
|
|
|
|
)
|
2020-12-02 10:29:40 -08:00
|
|
|
self.assertIn("core", ret)
|
|
|
|
self.assertIn("top", ret)
|
2012-04-04 10:39:45 -06:00
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2012-04-04 10:39:45 -06:00
|
|
|
def test_list_minion(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-04-04 10:39:45 -06:00
|
|
|
cp.list_minion
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-04-04 10:39:45 -06:00
|
|
|
self.run_function("cp.cache_file", ["salt://grail/scene33"])
|
|
|
|
ret = self.run_function("cp.list_minion")
|
|
|
|
found = False
|
2016-07-25 14:49:32 -07:00
|
|
|
search = "grail/scene33"
|
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-24 20:47:15 -05:00
|
|
|
if salt.utils.platform.is_windows():
|
2016-07-26 10:34:54 -07:00
|
|
|
search = r"grail\scene33"
|
2012-04-04 10:39:45 -06:00
|
|
|
for path in ret:
|
2016-07-25 14:49:32 -07:00
|
|
|
if search in path:
|
2012-04-04 10:39:45 -06:00
|
|
|
found = True
|
2017-03-07 18:48:55 +00:00
|
|
|
break
|
2012-04-04 10:39:45 -06:00
|
|
|
self.assertTrue(found)
|
2012-04-04 10:46:01 -06:00
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2012-04-04 10:46:01 -06:00
|
|
|
def test_is_cached(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-04-04 10:46:01 -06:00
|
|
|
cp.is_cached
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-04-04 10:46:01 -06:00
|
|
|
self.run_function("cp.cache_file", ["salt://grail/scene33"])
|
|
|
|
ret1 = self.run_function("cp.is_cached", ["salt://grail/scene33"])
|
|
|
|
self.assertTrue(ret1)
|
|
|
|
ret2 = self.run_function("cp.is_cached", ["salt://fasldkgj/poicxzbn"])
|
|
|
|
self.assertFalse(ret2)
|
2012-04-04 12:00:52 -06:00
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2012-04-04 12:00:52 -06:00
|
|
|
def test_hash_file(self):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2012-04-04 12:00:52 -06:00
|
|
|
cp.hash_file
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2016-08-10 16:12:08 +02:00
|
|
|
sha256_hash = self.run_function("cp.hash_file", ["salt://grail/scene33"])
|
2012-04-04 12:00:52 -06:00
|
|
|
path = self.run_function("cp.cache_file", ["salt://grail/scene33"])
|
2018-01-18 16:13:08 -06:00
|
|
|
with salt.utils.files.fopen(path, "rb") as fn_:
|
2016-08-02 12:29:58 -06:00
|
|
|
data = fn_.read()
|
2016-08-10 16:12:08 +02:00
|
|
|
self.assertEqual(sha256_hash["hsum"], hashlib.sha256(data).hexdigest())
|
2012-05-05 19:39:23 +05:30
|
|
|
|
2018-04-13 21:41:02 -05:00
|
|
|
@with_tempfile()
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2018-03-28 11:38:32 -06:00
|
|
|
def test_get_file_from_env_predefined(self, tgt):
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2013-11-01 12:44:56 +00:00
|
|
|
cp.get_file
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2018-12-07 17:52:49 +00:00
|
|
|
tgt = os.path.join(RUNTIME_VARS.TMP, "cheese")
|
2013-11-01 19:36:09 +00:00
|
|
|
try:
|
|
|
|
self.run_function("cp.get_file", ["salt://cheese", tgt])
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(tgt, "r") as cheese:
|
2018-01-18 16:13:08 -06:00
|
|
|
data = salt.utils.stringutils.to_unicode(cheese.read())
|
|
|
|
self.assertIn("Gromit", data)
|
|
|
|
self.assertNotIn("Comte", data)
|
2013-11-01 19:36:09 +00:00
|
|
|
finally:
|
|
|
|
os.unlink(tgt)
|
2013-11-01 12:44:56 +00:00
|
|
|
|
2018-04-13 21:41:02 -05:00
|
|
|
@with_tempfile()
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2018-04-04 11:43:03 -04:00
|
|
|
def test_get_file_from_env_in_url(self, tgt):
|
2018-12-07 17:52:49 +00:00
|
|
|
tgt = os.path.join(RUNTIME_VARS.TMP, "cheese")
|
2013-11-06 23:36:40 +00:00
|
|
|
try:
|
|
|
|
self.run_function("cp.get_file", ["salt://cheese?saltenv=prod", tgt])
|
2017-07-18 10:31:01 -06:00
|
|
|
with salt.utils.files.fopen(tgt, "r") as cheese:
|
2018-01-18 16:13:08 -06:00
|
|
|
data = salt.utils.stringutils.to_unicode(cheese.read())
|
|
|
|
self.assertIn("Gromit", data)
|
|
|
|
self.assertIn("Comte", data)
|
2013-11-06 23:36:40 +00:00
|
|
|
finally:
|
|
|
|
os.unlink(tgt)
|
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2016-08-12 20:06:20 +09:00
|
|
|
def test_push(self):
|
2018-12-07 17:52:49 +00:00
|
|
|
log_to_xfer = os.path.join(RUNTIME_VARS.TMP, uuid.uuid4().hex)
|
2024-02-25 10:37:08 +00:00
|
|
|
open( # pylint: disable=resource-leakage
|
|
|
|
log_to_xfer, "w", encoding="utf-8"
|
|
|
|
).close()
|
2016-08-12 20:06:20 +09:00
|
|
|
try:
|
2017-02-24 12:10:48 +00:00
|
|
|
self.run_function("cp.push", [log_to_xfer])
|
2016-08-12 20:06:20 +09:00
|
|
|
tgt_cache_file = os.path.join(
|
2018-12-07 17:52:49 +00:00
|
|
|
RUNTIME_VARS.TMP,
|
2017-02-24 12:10:48 +00:00
|
|
|
"master-minion-root",
|
|
|
|
"cache",
|
|
|
|
"minions",
|
|
|
|
"minion",
|
|
|
|
"files",
|
2018-12-07 17:52:49 +00:00
|
|
|
RUNTIME_VARS.TMP,
|
2017-02-24 12:10:48 +00:00
|
|
|
log_to_xfer,
|
|
|
|
)
|
2016-08-12 20:06:20 +09:00
|
|
|
self.assertTrue(
|
|
|
|
os.path.isfile(tgt_cache_file), "File was not cached on the master"
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
os.unlink(tgt_cache_file)
|
2018-08-28 21:14:05 -05:00
|
|
|
|
2021-01-31 08:57:48 +00:00
|
|
|
@pytest.mark.slow_test
|
2018-08-28 21:14:05 -05:00
|
|
|
def test_envs(self):
|
2018-08-31 12:32:24 -07:00
|
|
|
self.assertEqual(sorted(self.run_function("cp.envs")), sorted(["base", "prod"]))
|