Move modules and module_utils unit tests to correct place (#81)

* Move modules and module_utils unit tests to correct place.

* Update ignore.txt

* Fix imports.

* Fix typos.

* Fix more typos.
This commit is contained in:
Felix Fontein 2020-03-31 10:42:38 +02:00 committed by GitHub
commit be191cce6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
1170 changed files with 732 additions and 751 deletions

View file

@ -0,0 +1,139 @@
# Copyright (c) 2018 Antoine Catton
# MIT License (see licenses/MIT-license.txt or https://opensource.org/licenses/MIT)
import copy
import pytest
from ansible_collections.community.general.plugins.modules.packaging.language import gem
from ansible_collections.community.general.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args
def get_command(run_command):
"""Generate the command line string from the patched run_command"""
args = run_command.call_args[0]
command = args[0]
return ' '.join(command)
class TestGem(ModuleTestCase):
def setUp(self):
super(TestGem, self).setUp()
self.rubygems_path = ['/usr/bin/gem']
self.mocker.patch(
'ansible_collections.community.general.plugins.modules.packaging.language.gem.get_rubygems_path',
lambda module: copy.deepcopy(self.rubygems_path),
)
@pytest.fixture(autouse=True)
def _mocker(self, mocker):
self.mocker = mocker
def patch_installed_versions(self, versions):
"""Mocks the versions of the installed package"""
target = 'ansible_collections.community.general.plugins.modules.packaging.language.gem.get_installed_versions'
def new(module, remote=False):
return versions
return self.mocker.patch(target, new)
def patch_rubygems_version(self, version=None):
target = 'ansible_collections.community.general.plugins.modules.packaging.language.gem.get_rubygems_version'
def new(module):
return version
return self.mocker.patch(target, new)
def patch_run_command(self):
target = 'ansible.module_utils.basic.AnsibleModule.run_command'
return self.mocker.patch(target)
def test_fails_when_user_install_and_install_dir_are_combined(self):
set_module_args({
'name': 'dummy',
'user_install': True,
'install_dir': '/opt/dummy',
})
with pytest.raises(AnsibleFailJson) as exc:
gem.main()
result = exc.value.args[0]
assert result['failed']
assert result['msg'] == "install_dir requires user_install=false"
def test_passes_install_dir_to_gem(self):
# XXX: This test is extremely fragile, and makes assuptions about the module code, and how
# functions are run.
# If you start modifying the code of the module, you might need to modify what this
# test mocks. The only thing that matters is the assertion that this 'gem install' is
# invoked with '--install-dir'.
set_module_args({
'name': 'dummy',
'user_install': False,
'install_dir': '/opt/dummy',
})
self.patch_rubygems_version()
self.patch_installed_versions([])
run_command = self.patch_run_command()
with pytest.raises(AnsibleExitJson) as exc:
gem.main()
result = exc.value.args[0]
assert result['changed']
assert run_command.called
assert '--install-dir /opt/dummy' in get_command(run_command)
def test_passes_install_dir_and_gem_home_when_uninstall_gem(self):
# XXX: This test is also extremely fragile because of mocking.
# If this breaks, the only that matters is to check whether '--install-dir' is
# in the run command, and that GEM_HOME is passed to the command.
set_module_args({
'name': 'dummy',
'user_install': False,
'install_dir': '/opt/dummy',
'state': 'absent',
})
self.patch_rubygems_version()
self.patch_installed_versions(['1.0.0'])
run_command = self.patch_run_command()
with pytest.raises(AnsibleExitJson) as exc:
gem.main()
result = exc.value.args[0]
assert result['changed']
assert run_command.called
assert '--install-dir /opt/dummy' in get_command(run_command)
update_environ = run_command.call_args[1].get('environ_update', {})
assert update_environ.get('GEM_HOME') == '/opt/dummy'
def test_passes_add_force_option(self):
set_module_args({
'name': 'dummy',
'force': True,
})
self.patch_rubygems_version()
self.patch_installed_versions([])
run_command = self.patch_run_command()
with pytest.raises(AnsibleExitJson) as exc:
gem.main()
result = exc.value.args[0]
assert result['changed']
assert run_command.called
assert '--force' in get_command(run_command)

View file

@ -0,0 +1,70 @@
# Copyright (c) 2017 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_collections.community.general.plugins.modules.packaging.language import maven_artifact
from ansible.module_utils import basic
pytestmark = pytest.mark.usefixtures('patch_ansible_module')
maven_metadata_example = b"""<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<versioning>
<latest>4.13-beta-2</latest>
<release>4.13-beta-2</release>
<versions>
<version>3.7</version>
<version>3.8</version>
<version>3.8.1</version>
<version>3.8.2</version>
<version>4.0</version>
<version>4.1</version>
<version>4.2</version>
<version>4.3</version>
<version>4.3.1</version>
<version>4.4</version>
<version>4.5</version>
<version>4.6</version>
<version>4.7</version>
<version>4.8</version>
<version>4.8.1</version>
<version>4.8.2</version>
<version>4.9</version>
<version>4.10</version>
<version>4.11-beta-1</version>
<version>4.11</version>
<version>4.12-beta-1</version>
<version>4.12-beta-2</version>
<version>4.12-beta-3</version>
<version>4.12</version>
<version>4.13-beta-1</version>
<version>4.13-beta-2</version>
</versions>
<lastUpdated>20190202141051</lastUpdated>
</versioning>
</metadata>
"""
@pytest.mark.parametrize('patch_ansible_module, version_by_spec, version_choosed', [
(None, "(,3.9]", "3.8.2"),
(None, "3.0", "3.8.2"),
(None, "[3.7]", "3.7"),
(None, "[4.10, 4.12]", "4.12"),
(None, "[4.10, 4.12)", "4.11"),
(None, "[2.0,)", "4.13-beta-2"),
])
def test_find_version_by_spec(mocker, version_by_spec, version_choosed):
_getContent = mocker.patch('ansible_collections.community.general.plugins.modules.packaging.language.maven_artifact.MavenDownloader._getContent')
_getContent.return_value = maven_metadata_example
artifact = maven_artifact.Artifact("junit", "junit", None, version_by_spec, "jar")
mvn_downloader = maven_artifact.MavenDownloader(basic.AnsibleModule, "https://repo1.maven.org/maven2")
assert mvn_downloader.find_version_by_spec(artifact) == version_choosed