This should not be returning a string type for stderr, since
Popen.communicate() returns a bytestring. In Python 2, the str and bytes
types are the same, but the same is not true in Python 3.
Additionally, the mocking here seems not to consider the fact that Popen
is called when runas is used to get the runas user's environment. Since
this test doesn't care about the output, we can just make the mocked
Popen.communicate() return an empty bytestring.
This makes the following changes:
- The `append_newline` argument to the `file.blockreplace`
remote-execution function has been modified so that if its value is
`None`, it only appends a newline when the content block does not end
in one.
- A couple of fixes were made to newline handling. The existing code
normalized the newlines in the content block, replacing them with
os.linesep. However, when the file contains newlines that don't match
the OS (i.e. POSIX newlines in a file on a Windows box, or Windows
newlines on a Linux/Mac/BSD/etc. box), then we would still end up with
mixed newlines. The line separator is now detected when we read in the
original file, and the detected line separator is used when writing
the content block. Additionally, the same newline mismatch was
possible when appending/prepending the content block. This has been
fixed by using a common function for appending, prepending, and
replacing the content block.
- Support for the `append_newline` argument has been added to the
`file.blockreplace` state. The default value for the state is `None`.
A `versionchanged` has been added to the remote execution function to
let users know that the Fluorine release will change the default value
of that variable.
- 20 new integration tests have been written to test the
`file.blockreplace` state.
pip.installed state calls pip.freeze to check for existing installation
and to verify installation post-install. This patch propagates the
env_vars passed to pip.installed to the pip.freeze function. It also modifies
existing pip unit tests to test new functionality and adds an integration test
that verifies the expected correct behavior
Fixes#46127
When the Kubernetes client is not installed, the import of
salt.modules.kubernetes will still succeed, but HAS_LIBS will be set to
False (since the library import will be covered by a try-except clause).
Therefore expect the salt.modules.kubernetes to always succeed and check
kubernetes.HAS_LIBS instead for the presence of the kubernetes library.
Signed-off-by: Benjamin Drung <benjamin.drung@profitbricks.com>
`_get_extra_opts()` and `_get_branch_option()` were unnecessarily
quoting the value, causing it to be interpreted as a literal quote by
`subprocess.Popen()`.
Also, because there were separate helpers for repo options,
disableexcludes, branch options, and extra options, and specifically
because `_get_extra_opts()` parses *all* kwargs, any of the options from
the other helper funcs would end up being added to the command line
twice if `_get_extra_opts()` was used.
This commit consolidates all of the kwarg inspection and CLI opts
construction to a single helper function. It also adds unit tests to
make sure that we are formatting our commands properly.
Additionally, it makes a minor fix in `refresh_db()` which was not
accounted for when we changed the osmajorrelease grain to an integer in
2017.7.0.
When running the unit tests with the locale set to POSIX, some Unicode
tests fail:
$ LC_ALL=POSIX python3 ./tests/runtests.py --unit
[...]
======================================================================
ERROR: test_list_products (unit.modules.test_zypper.ZypperTestCase)
[CPU:0.0%|MEM:73.2%]
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests/unit/modules/test_zypper.py", line 236, in
test_list_products
'stdout': get_test_data(filename)
File "tests/unit/modules/test_zypper.py", line 53, in get_test_data
return rfh.read()
File "/usr/lib/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position
828: ordinal not in range(128)
======================================================================
ERROR: test_non_ascii (unit.templates.test_jinja.TestGetTemplate)
[CPU:0.0%|MEM:73.2%]
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests/unit/templates/test_jinja.py", line 341, in test_non_ascii
result = fp.read()
File "/usr/lib/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5:
ordinal not in range(128)
======================================================================
ERROR: test_non_ascii_encoding
(unit.templates.test_jinja.TestGetTemplate)
[CPU:0.0%|MEM:73.2%]
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests/unit/templates/test_jinja.py", line 303, in
test_non_ascii_encoding
fp_.read(),
File "/usr/lib/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5:
ordinal not in range(128)
----------------------------------------------------------------------
Therefore open files in binary mode and explicitly decode them with
utf-8 instead of their default locale.
When boto and moto are not installed, the BotoVpcTestCaseBase() should
be skipped. _get_boto_version() and _get_moto_version() require that
boto and moto are installed, but are called anyway in the skipIf
condition.
The test_run unit tests end result when a command is not found. When
_run() is called without setting cwd, it will use the home directory as
working directory. When the home directory does not exist, the unit test
will fail:
Traceback (most recent call last):
File "tests/unit/modules/test_cmdmod.py", line 231, in test_run
ret = cmdmod._run('foo', use_vt=True).get('stderr')
File "salt/modules/cmdmod.py", line 536, in _run
.format(cwd)
salt.exceptions.CommandExecutionError: Specified cwd
'/sbuild-nonexistent' either not absolute or does not exist
Therefore set cwd to the current directory since the working directory
is not used in this test case.
This fixes one failing test of #45627.
Signed-off-by: Benjamin Drung <benjamin.drung@profitbricks.com>