diff options
author | Sam Doran <sdoran@redhat.com> | 2022-09-06 15:30:04 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-06 14:30:04 -0500 |
commit | 8ebca4a6a3a9836ce515a4dc60044c52465ef170 (patch) | |
tree | 6734222b0ab058cc9082d6a8d1b14f5cf750de76 /test/units | |
parent | fd19ff231055c439c6a2e9bb590fef09818b2afc (diff) | |
download | ansible-8ebca4a6a3a9836ce515a4dc60044c52465ef170.tar.gz |
fetch_file - properly split files with multi-part file extensions (#75257)
Diffstat (limited to 'test/units')
-rw-r--r-- | test/units/module_utils/urls/test_fetch_file.py | 39 | ||||
-rw-r--r-- | test/units/module_utils/urls/test_split.py | 77 |
2 files changed, 107 insertions, 9 deletions
diff --git a/test/units/module_utils/urls/test_fetch_file.py b/test/units/module_utils/urls/test_fetch_file.py index d536ffaf3a..ed112270d9 100644 --- a/test/units/module_utils/urls/test_fetch_file.py +++ b/test/units/module_utils/urls/test_fetch_file.py @@ -1,24 +1,45 @@ # -*- coding: utf-8 -*- -# (c) 2018 Matt Martz <matt@sivel.net> +# Copyright: Contributors to the Ansible project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import absolute_import, division, print_function __metaclass__ = type +import os + from ansible.module_utils.urls import fetch_file import pytest from units.compat.mock import MagicMock -def test_fetch_file(mocker): - tempfile = mocker.patch('ansible.module_utils.urls.tempfile') - tempfile.NamedTemporaryFile.side_effect = RuntimeError('boom') - - module = MagicMock() +class FakeTemporaryFile: + def __init__(self, name): + self.name = name + + +@pytest.mark.parametrize( + 'url, prefix, suffix, expected', ( + ('http://ansible.com/foo.tar.gz?foo=%s' % ('bar' * 100), 'foo', '.tar.gz', 'foo.tar.gz'), + ('https://www.gnu.org/licenses/gpl-3.0.txt', 'gpl-3.0', '.txt', 'gpl-3.0.txt'), + ('http://pyyaml.org/download/libyaml/yaml-0.2.5.tar.gz', 'yaml-0.2.5', '.tar.gz', 'yaml-0.2.5.tar.gz'), + ( + 'https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-linux64.tar.gz', + 'geckodriver-v0.26.0-linux64', + '.tar.gz', + 'geckodriver-v0.26.0-linux64.tar.gz' + ), + ) +) +def test_file_multiple_extensions(mocker, url, prefix, suffix, expected): + module = mocker.Mock() module.tmpdir = '/tmp' + module.add_cleanup_file = mocker.Mock(side_effect=AttributeError('raised intentionally')) + + mock_NamedTemporaryFile = mocker.patch('ansible.module_utils.urls.tempfile.NamedTemporaryFile', + return_value=FakeTemporaryFile(os.path.join(module.tmpdir, expected))) - with pytest.raises(RuntimeError): - fetch_file(module, 'http://ansible.com/foo.tar.gz?foo=%s' % ('bar' * 100)) + with pytest.raises(AttributeError, match='raised intentionally'): + fetch_file(module, url) - tempfile.NamedTemporaryFile.assert_called_once_with(dir='/tmp', prefix='foo.tar', suffix='.gz', delete=False) + mock_NamedTemporaryFile.assert_called_with(dir=module.tmpdir, prefix=prefix, suffix=suffix, delete=False) diff --git a/test/units/module_utils/urls/test_split.py b/test/units/module_utils/urls/test_split.py new file mode 100644 index 0000000000..7fd5fc13cf --- /dev/null +++ b/test/units/module_utils/urls/test_split.py @@ -0,0 +1,77 @@ +# -*- coding: utf-8 -*- +# Copyright: Contributors to the Ansible project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +import pytest + +from ansible.module_utils.urls import _split_multiext + + +@pytest.mark.parametrize( + 'name, expected', + ( + ('', ('', '')), + ('a', ('a', '')), + ('file.tar', ('file', '.tar')), + ('file.tar.', ('file.tar.', '')), + ('file.hidden', ('file.hidden', '')), + ('file.tar.gz', ('file', '.tar.gz')), + ('yaml-0.2.5.tar.gz', ('yaml-0.2.5', '.tar.gz')), + ('yaml-0.2.5.zip', ('yaml-0.2.5', '.zip')), + ('yaml-0.2.5.zip.hidden', ('yaml-0.2.5.zip.hidden', '')), + ('geckodriver-v0.26.0-linux64.tar', ('geckodriver-v0.26.0-linux64', '.tar')), + ('/var/lib/geckodriver-v0.26.0-linux64.tar', ('/var/lib/geckodriver-v0.26.0-linux64', '.tar')), + ('https://acme.com/drivers/geckodriver-v0.26.0-linux64.tar', ('https://acme.com/drivers/geckodriver-v0.26.0-linux64', '.tar')), + ('https://acme.com/drivers/geckodriver-v0.26.0-linux64.tar.bz', ('https://acme.com/drivers/geckodriver-v0.26.0-linux64', '.tar.bz')), + ) +) +def test__split_multiext(name, expected): + assert expected == _split_multiext(name) + + +@pytest.mark.parametrize( + 'args, expected', + ( + (('base-v0.26.0-linux64.tar.gz', 4, 4), ('base-v0.26.0-linux64.tar.gz', '')), + (('base-v0.26.0.hidden', 1, 7), ('base-v0.26', '.0.hidden')), + (('base-v0.26.0.hidden', 3, 4), ('base-v0.26.0.hidden', '')), + (('base-v0.26.0.hidden.tar', 1, 7), ('base-v0.26.0', '.hidden.tar')), + (('base-v0.26.0.hidden.tar.gz', 1, 7), ('base-v0.26.0.hidden', '.tar.gz')), + (('base-v0.26.0.hidden.tar.gz', 4, 7), ('base-v0.26.0.hidden.tar.gz', '')), + ) +) +def test__split_multiext_min_max(args, expected): + assert expected == _split_multiext(*args) + + +@pytest.mark.parametrize( + 'kwargs, expected', ( + (({'name': 'base-v0.25.0.tar.gz', 'count': 1}), ('base-v0.25.0.tar', '.gz')), + (({'name': 'base-v0.25.0.tar.gz', 'count': 2}), ('base-v0.25.0', '.tar.gz')), + (({'name': 'base-v0.25.0.tar.gz', 'count': 3}), ('base-v0.25.0', '.tar.gz')), + (({'name': 'base-v0.25.0.tar.gz', 'count': 4}), ('base-v0.25.0', '.tar.gz')), + (({'name': 'base-v0.25.foo.tar.gz', 'count': 3}), ('base-v0.25', '.foo.tar.gz')), + (({'name': 'base-v0.25.foo.tar.gz', 'count': 4}), ('base-v0', '.25.foo.tar.gz')), + ) +) +def test__split_multiext_count(kwargs, expected): + assert expected == _split_multiext(**kwargs) + + +@pytest.mark.parametrize( + 'name', + ( + list(), + tuple(), + dict(), + set(), + 1.729879, + 247, + ) +) +def test__split_multiext_invalid(name): + with pytest.raises((TypeError, AttributeError)): + _split_multiext(name) |