Fix bug in svn.latest (closes #59069). (#59071)

This commit ensures that the --trust-server-cert-failures option is
correctly passed on to svn.diff when running svn.latest in test mode.
This commit is contained in:
Sebastian Marsching 2020-12-10 21:22:37 +01:00 committed by GitHub
parent d3606be389
commit 1148d34530
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 22 deletions

2
changelog/59069.fixed Normal file
View file

@ -0,0 +1,2 @@
Fixed an error when running svn.latest in test mode and using the trust_failures
option.

View file

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
Manage SVN repositories
=======================
@ -17,17 +16,11 @@ requisite to a pkg.installed state for the package which provides subversion
svn.latest:
- target: /tmp/swallow
"""
from __future__ import absolute_import, print_function, unicode_literals
# Import python libs
import logging
import os
# Import salt libs
from salt import exceptions
# Import 3rd party libs
from salt.ext import six
from salt.states.git import _fail, _neutral_test
log = logging.getLogger(__name__)
@ -108,12 +101,12 @@ def latest(
if os.path.exists(target) and not os.path.isdir(target):
return _fail(
ret, 'The path "{0}" exists and is not ' "a directory.".format(target)
ret, 'The path "{}" exists and is not ' "a directory.".format(target)
)
if __opts__["test"]:
if rev:
new_rev = six.text_type(rev)
new_rev = str(rev)
else:
new_rev = "HEAD"
@ -134,7 +127,7 @@ def latest(
svn_cmd = "svn.diff"
except exceptions.CommandExecutionError:
return _fail(
ret, ("{0} exists but is not a svn working copy.").format(target)
ret, ("{} exists but is not a svn working copy.").format(target)
)
current_rev = current_info[0]["Revision"]
@ -144,8 +137,11 @@ def latest(
if trust:
opts += ("--trust-server-cert",)
if trust_failures:
opts += ("--trust-server-cert-failures", trust_failures)
out = __salt__[svn_cmd](cwd, target, user, username, password, *opts)
return _neutral_test(ret, ("{0}").format(out))
return _neutral_test(ret, ("{}").format(out))
try:
current_info = __salt__["svn.info"](
cwd, target, user=user, username=username, password=password, fmt="dict"
@ -155,7 +151,7 @@ def latest(
pass
if rev:
opts += ("-r", six.text_type(rev))
opts += ("-r", str(rev))
if force:
opts += ("--force",)
@ -182,7 +178,7 @@ def latest(
fmt="dict",
)[0]["Revision"]
if current_rev != new_rev:
ret["changes"]["revision"] = "{0} => {1}".format(current_rev, new_rev)
ret["changes"]["revision"] = "{} => {}".format(current_rev, new_rev)
else:
out = __salt__[svn_cmd](cwd, name, basename, user, username, password, *opts)
@ -270,17 +266,17 @@ def export(
if not overwrite and os.path.exists(target) and not os.path.isdir(target):
return _fail(
ret, 'The path "{0}" exists and is not ' "a directory.".format(target)
ret, 'The path "{}" exists and is not ' "a directory.".format(target)
)
if __opts__["test"]:
if not os.path.exists(target):
return _neutral_test(
ret, ("{0} doesn't exist and is set to be checked out.").format(target)
ret, ("{} doesn't exist and is set to be checked out.").format(target)
)
svn_cmd = "svn.list"
rev = "HEAD"
out = __salt__[svn_cmd](cwd, target, user, username, password, *opts)
return _neutral_test(ret, ("{0}").format(out))
return _neutral_test(ret, ("{}").format(out))
if not rev:
rev = "HEAD"

View file

@ -1,16 +1,10 @@
# -*- coding: utf-8 -*-
"""
:codeauthor: Rahul Handay <rahulha@saltstack.com>
"""
# Import Python libs
from __future__ import absolute_import, print_function, unicode_literals
import os
# Import Salt Libs
import salt.states.svn as svn
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase
@ -73,6 +67,36 @@ class SvnTestCase(TestCase, LoaderModuleMockMixin):
},
)
def test_latest_trust_failures(self):
"""
Test that checks that the trust_failures option is handled
correctly when running svn.latest in test mode. This tests for the
bug reported as #59069.
"""
os_path_exists_mock = MagicMock(side_effect=[False, True])
svn_info_mock = MagicMock(return_value=[{"Revision": "42"}])
svn_diff_mock = MagicMock()
svn_neutral_test_mock = MagicMock()
with patch.object(os.path, "exists", os_path_exists_mock), patch.dict(
svn.__opts__, {"test": True}
), patch.dict(
svn.__salt__, {"svn.diff": svn_diff_mock, "svn.info": svn_info_mock}
), patch.object(
svn, "_neutral_test", svn_neutral_test_mock
):
svn.latest("salt", "/my/test/dir", trust_failures="unknown-ca")
svn_diff_mock.assert_called_with(
"/my/test",
"/my/test/dir",
None,
None,
None,
"-r",
"42:HEAD",
"--trust-server-cert-failures",
"unknown-ca",
)
def test_export(self):
"""
Test to export a file or directory from an SVN repository