| 1 |
# Copyright (c) 2018 Antoine Catton |
| 2 |
# MIT License (see LICENSES/MIT.txt or https://opensource.org/licenses/MIT) |
| 3 |
# SPDX-License-Identifier: MIT |
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import pytest |
| 7 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 8 |
AnsibleExitJson, |
| 9 |
AnsibleFailJson, |
| 10 |
ModuleTestCase, |
| 11 |
set_module_args, |
| 12 |
) |
| 13 |
|
| 14 |
from ansible_collections.community.general.plugins.modules import gem |
| 15 |
|
| 16 |
|
| 17 |
def get_command(run_command): |
| 18 |
"""Generate the command line string from the patched run_command""" |
| 19 |
args = run_command.call_args[0] |
| 20 |
command = args[0] |
| 21 |
return " ".join(command) |
| 22 |
|
| 23 |
|
| 24 |
class TestGem(ModuleTestCase): |
| 25 |
def setUp(self): |
| 26 |
super().setUp() |
| 27 |
self.mocker.patch( |
| 28 |
"ansible.module_utils.basic.AnsibleModule.get_bin_path", |
| 29 |
return_value="/usr/bin/gem", |
| 30 |
) |
| 31 |
|
| 32 |
@pytest.fixture(autouse=True) |
| 33 |
def _mocker(self, mocker): |
| 34 |
self.mocker = mocker |
| 35 |
|
| 36 |
def patch_installed_versions(self, versions): |
| 37 |
"""Mocks the versions of the installed package""" |
| 38 |
|
| 39 |
target = "ansible_collections.community.general.plugins.modules.gem.get_installed_versions" |
| 40 |
|
| 41 |
def new(runner, remote=False): |
| 42 |
return versions |
| 43 |
|
| 44 |
return self.mocker.patch(target, new) |
| 45 |
|
| 46 |
def patch_rubygems_version(self, version=None): |
| 47 |
target = "ansible_collections.community.general.plugins.modules.gem.get_rubygems_version" |
| 48 |
|
| 49 |
def new(module): |
| 50 |
return version |
| 51 |
|
| 52 |
return self.mocker.patch(target, new) |
| 53 |
|
| 54 |
def patch_run_command(self): |
| 55 |
target = "ansible.module_utils.basic.AnsibleModule.run_command" |
| 56 |
mock = self.mocker.patch(target) |
| 57 |
mock.return_value = (0, "", "") |
| 58 |
return mock |
| 59 |
|
| 60 |
def test_fails_when_user_install_and_install_dir_are_combined(self): |
| 61 |
with set_module_args( |
| 62 |
{ |
| 63 |
"name": "dummy", |
| 64 |
"user_install": True, |
| 65 |
"install_dir": "/opt/dummy", |
| 66 |
} |
| 67 |
): |
| 68 |
with pytest.raises(AnsibleFailJson) as exc: |
| 69 |
gem.main() |
| 70 |
|
| 71 |
result = exc.value.args[0] |
| 72 |
assert result["failed"] |
| 73 |
assert result["msg"] == "install_dir requires user_install=false" |
| 74 |
|
| 75 |
def test_passes_install_dir_to_gem(self): |
| 76 |
# XXX: This test is extremely fragile, and makes assumptions about the module code, and how |
| 77 |
# functions are run. |
| 78 |
# If you start modifying the code of the module, you might need to modify what this |
| 79 |
# test mocks. The only thing that matters is the assertion that this 'gem install' is |
| 80 |
# invoked with '--install-dir'. |
| 81 |
|
| 82 |
with set_module_args( |
| 83 |
{ |
| 84 |
"name": "dummy", |
| 85 |
"user_install": False, |
| 86 |
"install_dir": "/opt/dummy", |
| 87 |
} |
| 88 |
): |
| 89 |
self.patch_rubygems_version() |
| 90 |
self.patch_installed_versions([]) |
| 91 |
run_command = self.patch_run_command() |
| 92 |
|
| 93 |
with pytest.raises(AnsibleExitJson) as exc: |
| 94 |
gem.main() |
| 95 |
|
| 96 |
result = exc.value.args[0] |
| 97 |
assert result["changed"] |
| 98 |
assert run_command.called |
| 99 |
|
| 100 |
assert "--install-dir /opt/dummy" in get_command(run_command) |
| 101 |
|
| 102 |
def test_passes_install_dir_and_gem_home_when_uninstall_gem(self): |
| 103 |
# XXX: This test is also extremely fragile because of mocking. |
| 104 |
# If this breaks, the only that matters is to check whether '--install-dir' is |
| 105 |
# in the run command, and that GEM_HOME is passed to the command. |
| 106 |
with set_module_args( |
| 107 |
{ |
| 108 |
"name": "dummy", |
| 109 |
"user_install": False, |
| 110 |
"install_dir": "/opt/dummy", |
| 111 |
"state": "absent", |
| 112 |
} |
| 113 |
): |
| 114 |
self.patch_rubygems_version() |
| 115 |
self.patch_installed_versions(["1.0.0"]) |
| 116 |
|
| 117 |
run_command = self.patch_run_command() |
| 118 |
|
| 119 |
with pytest.raises(AnsibleFailJson) as exc: |
| 120 |
gem.main() |
| 121 |
|
| 122 |
result = exc.value.args[0] |
| 123 |
assert result["failed"] |
| 124 |
assert run_command.called |
| 125 |
|
| 126 |
assert "--install-dir /opt/dummy" in get_command(run_command) |
| 127 |
|
| 128 |
update_environ = run_command.call_args[1].get("environ_update", {}) |
| 129 |
assert update_environ.get("GEM_HOME") == "/opt/dummy" |
| 130 |
|
| 131 |
def test_passes_add_force_option(self): |
| 132 |
with set_module_args( |
| 133 |
{ |
| 134 |
"name": "dummy", |
| 135 |
"force": True, |
| 136 |
} |
| 137 |
): |
| 138 |
self.patch_rubygems_version() |
| 139 |
self.patch_installed_versions([]) |
| 140 |
run_command = self.patch_run_command() |
| 141 |
|
| 142 |
with pytest.raises(AnsibleExitJson) as exc: |
| 143 |
gem.main() |
| 144 |
|
| 145 |
result = exc.value.args[0] |
| 146 |
assert result["changed"] |
| 147 |
assert run_command.called |
| 148 |
|
| 149 |
assert "--force" in get_command(run_command) |