Update units to pass on macOS (#60435)

* Update units to pass on macOS. Fixes #27810

* raising=False
This commit is contained in:
Matt Martz 2019-08-12 15:13:07 -05:00 committed by GitHub
parent 8bca160363
commit 697b566971
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 35 additions and 23 deletions

View file

@ -11,7 +11,7 @@ wheel < 0.30.0 ; python_version < '2.7' # wheel 0.30.0 and later require python
yamllint != 1.8.0, < 1.14.0 ; python_version < '2.7' # yamllint 1.8.0 and 1.14.0+ require python 2.7+
pycrypto >= 2.6 # Need features found in 2.6 and greater
ncclient >= 0.5.2 # Need features added in 0.5.2 and greater
idna < 2.6 # requests requires idna < 2.6, but cryptography will cause the latest version to be installed instead
idna < 2.6, >= 2.5 # linode requires idna < 2.9, >= 2.5, requests requires idna < 2.6, but cryptography will cause the latest version to be installed instead
paramiko < 2.4.0 ; python_version < '2.7' # paramiko 2.4.0 drops support for python 2.6
paramiko < 2.5.0 ; python_version >= '2.7' # paramiko 2.5.0 requires cryptography 2.5.0+
pytest < 3.3.0 ; python_version < '2.7' # pytest 3.3.0 drops support for python 2.6

View file

@ -10,6 +10,7 @@ pytest
pytest-mock
pytest-xdist
python-memcached
pytz
pyvmomi
pyyaml
redis

View file

@ -7,6 +7,7 @@
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import os
import errno
import json
from itertools import product
@ -27,7 +28,7 @@ def atomic_am(am, mocker):
@pytest.fixture
def atomic_mocks(mocker):
def atomic_mocks(mocker, monkeypatch):
environ = dict()
mocks = {
'chmod': mocker.patch('os.chmod'),
@ -53,6 +54,9 @@ def atomic_mocks(mocker):
mocks['umask'].side_effect = [18, 0]
mocks['rename'].return_value = None
# normalize OS specific features
monkeypatch.delattr(os, 'chflags', raising=False)
yield mocks

View file

@ -101,19 +101,15 @@ def test_mode_unchanged_when_already_0660(am, mock_stats, mocker, mode, check_mo
@pytest.mark.parametrize('check_mode, stdin',
product((True, False), ({},)),
indirect=['stdin'])
def test_missing_lchmod_is_not_link(am, mock_stats, mocker, check_mode):
def test_missing_lchmod_is_not_link(am, mock_stats, mocker, monkeypatch, check_mode):
"""Some platforms have lchmod (*BSD) others do not (Linux)"""
am.check_mode = check_mode
original_hasattr = hasattr
def _hasattr(obj, name):
if obj == os and name == 'lchmod':
return False
return original_hasattr(obj, name)
monkeypatch.delattr(os, 'lchmod', raising=False)
mocker.patch('os.lstat', side_effect=[mock_stats['before'], mock_stats['after']])
mocker.patch.object(builtins, 'hasattr', side_effect=_hasattr)
mocker.patch('os.path.islink', return_value=False)
mocker.patch('os.path.exists', return_value=True)
m_chmod = mocker.patch('os.chmod', return_value=None)
@ -128,19 +124,15 @@ def test_missing_lchmod_is_not_link(am, mock_stats, mocker, check_mode):
@pytest.mark.parametrize('check_mode, stdin',
product((True, False), ({},)),
indirect=['stdin'])
def test_missing_lchmod_is_link(am, mock_stats, mocker, check_mode):
def test_missing_lchmod_is_link(am, mock_stats, mocker, monkeypatch, check_mode):
"""Some platforms have lchmod (*BSD) others do not (Linux)"""
am.check_mode = check_mode
original_hasattr = hasattr
def _hasattr(obj, name):
if obj == os and name == 'lchmod':
return False
return original_hasattr(obj, name)
monkeypatch.delattr(os, 'lchmod', raising=False)
mocker.patch('os.lstat', side_effect=[mock_stats['before'], mock_stats['after']])
mocker.patch.object(builtins, 'hasattr', side_effect=_hasattr)
mocker.patch('os.path.islink', return_value=True)
mocker.patch('os.path.exists', return_value=True)
m_chmod = mocker.patch('os.chmod', return_value=None)

View file

@ -19,8 +19,10 @@
from __future__ import (absolute_import, division)
__metaclass__ = type
import os
from units.compat import unittest
from units.compat.mock import Mock
from units.compat.mock import Mock, patch
class BaseFactsTest(unittest.TestCase):
@ -45,14 +47,18 @@ class BaseFactsTest(unittest.TestCase):
mock_module.get_bin_path = Mock(return_value=None)
return mock_module
def test_collect(self):
@patch('platform.system', return_value='Linux')
@patch('ansible.module_utils.facts.system.service_mgr.get_file_content', return_value='systemd')
def test_collect(self, mock_gfc, mock_ps):
module = self._mock_module()
fact_collector = self.collector_class()
facts_dict = fact_collector.collect(module=module, collected_facts=self.collected_facts)
self.assertIsInstance(facts_dict, dict)
return facts_dict
def test_collect_with_namespace(self):
@patch('platform.system', return_value='Linux')
@patch('ansible.module_utils.facts.system.service_mgr.get_file_content', return_value='systemd')
def test_collect_with_namespace(self, mock_gfc, mock_ps):
module = self._mock_module()
fact_collector = self.collector_class()
facts_dict = fact_collector.collect_with_namespace(module=module,

View file

@ -207,7 +207,9 @@ class TestCollectedFacts(unittest.TestCase):
def _mock_module(self, gather_subset=None):
return mock_module(gather_subset=self.gather_subset)
def setUp(self):
@patch('platform.system', return_value='Linux')
@patch('ansible.module_utils.facts.system.service_mgr.get_file_content', return_value='systemd')
def setUp(self, mock_gfc, mock_ps):
mock_module = self._mock_module()
collectors = self._collectors(mock_module)

View file

@ -247,7 +247,7 @@ class TestFileVaultSecret(unittest.TestCase):
def test_file_not_found(self):
tmp_file = tempfile.NamedTemporaryFile()
filename = tmp_file.name
filename = os.path.realpath(tmp_file.name)
tmp_file.close()
fake_loader = DictDataLoader({filename: 'sdfadf'})
@ -390,7 +390,7 @@ class TestGetFileVaultSecret(unittest.TestCase):
def test_file_not_found(self):
tmp_file = tempfile.NamedTemporaryFile()
filename = tmp_file.name
filename = os.path.realpath(tmp_file.name)
tmp_file.close()
fake_loader = DictDataLoader({filename: 'sdfadf'})

View file

@ -492,7 +492,7 @@ class TestVaultEditor(unittest.TestCase):
self.assertEqual(res, '/dev/null')
def test_real_path_symlink(self):
self._test_dir = self._create_test_dir()
self._test_dir = os.path.realpath(self._create_test_dir())
file_path = self._create_file(self._test_dir, 'test_file', content=b'this is a test file')
file_link_path = os.path.join(self._test_dir, 'a_link_to_test_file')

View file

@ -62,7 +62,8 @@ class MixinForMocks(object):
self.mock_block = MagicMock(name='MockBlock')
self.fake_role_loader = DictDataLoader({"/etc/ansible/roles/bogus_role/tasks/main.yml": """
# On macOS /etc is actually /private/etc, tests fail when performing literal /etc checks
self.fake_role_loader = DictDataLoader({os.path.join(os.path.realpath("/etc"), "ansible/roles/bogus_role/tasks/main.yml"): """
- shell: echo 'hello world'
"""})

View file

@ -15,6 +15,8 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import sys
import pytest
from ansible.errors import AnsibleError, AnsibleFilterError
@ -46,6 +48,7 @@ def assert_hash(expected, secret, algorithm, **settings):
assert excinfo.value.args[0] == "passlib must be installed to hash with '%s'" % algorithm
@pytest.mark.skipif(sys.platform.startswith('darwin'), reason='macOS requires passlib')
def test_encrypt_with_rounds_no_passlib():
with passlib_off():
assert_hash("$5$12345678$uAZsE3BenI2G.nA8DpTl.9Dc8JiqacI53pEqRr5ppT7",
@ -67,6 +70,7 @@ def test_encrypt_with_rounds():
secret="123", algorithm="sha512_crypt", salt="12345678", rounds=5000)
@pytest.mark.skipif(sys.platform.startswith('darwin'), reason='macOS requires passlib')
def test_encrypt_default_rounds_no_passlib():
with passlib_off():
assert_hash("$1$12345678$tRy4cXc3kmcfRZVj4iFXr/",
@ -89,9 +93,10 @@ def test_encrypt_default_rounds():
assert_hash("$6$12345678$LcV9LQiaPekQxZ.OfkMADjFdSO2k9zfbDQrHPVcYjSLqSdjLYpsgqviYvTEP/R41yPmhH3CCeEDqVhW1VHr3L.",
secret="123", algorithm="sha512_crypt", salt="12345678")
assert encrypt.CryptHash("md5_crypt").hash("123")
assert encrypt.PasslibHash("md5_crypt").hash("123")
@pytest.mark.skipif(sys.platform.startswith('darwin'), reason='macOS requires passlib')
def test_password_hash_filter_no_passlib():
with passlib_off():
assert not encrypt.PASSLIB_AVAILABLE
@ -127,6 +132,7 @@ def test_password_hash_filter_passlib():
assert get_encrypted_password("123", "pbkdf2_sha256")
@pytest.mark.skipif(sys.platform.startswith('darwin'), reason='macOS requires passlib')
def test_do_encrypt_no_passlib():
with passlib_off():
assert not encrypt.PASSLIB_AVAILABLE