-
Notifications
You must be signed in to change notification settings - Fork 23.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the ability to specify an install_dir to the gem module #38195
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f5c5609
Add the ability to specify an install_dir to the gem module
acatton 3740cad
Add GEM_HOME when installing a non-global gem
acatton 21a963a
Add tests for custom gem path
samdoran f7b0399
Fix sanity tests
samdoran 388608d
Add changelog entry
samdoran 6fe61ed
Rebase and add tests for incorrect options
samdoran File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
new_features: | ||
- gem - add ability to specify a custom directory for installing gems (https://github.com/ansible/ansible/pull/38195) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# Copyright (c) 2018 Antoine Catton | ||
# MIT License (see licenses/MIT-license.txt or https://opensource.org/licenses/MIT) | ||
import copy | ||
import json | ||
|
||
import pytest | ||
|
||
from ansible.modules.packaging.language import gem | ||
from units.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.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.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.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' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than adding an explicit check, you could just set
user_install
andinstall_dir
as mutually exclusive.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tell me if I'm wrong, but I'm not sure this is possible.
In order for this feature to work, the user MUST specify:
The default of
user_install
beingTrue
.Unless I'm mistaking,
mutually_exclusive
means that "the user CAN specifyone, but MUST NOT specify the other." However, here, the user MUST specify both.
If I'm right, there are two solutions:
user_install
tono
, thus loosing backward compatibility.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're correct. After more careful reading, I see what you're trying to accomplish. Changing the default would not be a good idea.