Skip to content
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

Run dependency managers only when config present #699

Merged
merged 1 commit into from Jan 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions molecule/config.py
Expand Up @@ -26,6 +26,7 @@
from molecule import scenario
from molecule import state
from molecule.dependency import ansible_galaxy
from molecule.dependency import gilt
from molecule.driver import docker
from molecule.lint import ansible_lint
from molecule.verifier import testinfra
Expand Down Expand Up @@ -64,6 +65,8 @@ def ephemeral_directory(self):
def dependency(self):
if self.config['dependency']['name'] == 'galaxy':
return ansible_galaxy.AnsibleGalaxy(self)
elif self.config['dependency']['name'] == 'gilt':
return gilt.Gilt(self)

@property
def driver(self):
Expand Down
8 changes: 8 additions & 0 deletions molecule/dependency/ansible_galaxy.py
Expand Up @@ -96,6 +96,9 @@ def execute(self):
if not self.enabled:
return

if not self._has_requirements_file():
return

if self._ansible_galaxy_command is None:
self.bake()

Expand All @@ -117,3 +120,8 @@ def _setup(self):
self.options['roles-path'])
if not os.path.isdir(role_directory):
os.makedirs(role_directory)

def _has_requirements_file(self):
role_file = self.options.get('role-file')

return role_file and os.path.isfile(role_file)
8 changes: 8 additions & 0 deletions molecule/dependency/gilt.py
Expand Up @@ -92,6 +92,9 @@ def execute(self):
if not self.enabled:
return

if not self._has_requirements_file():
return

if self._gilt_command is None:
self.bake()

Expand All @@ -100,3 +103,8 @@ def execute(self):
self._gilt_command, debug=self._config.args.get('debug'))
except sh.ErrorReturnCode as e:
util.sysexit(e.exit_code)

def _has_requirements_file(self):
config_file = self.options.get('config')

return config_file and os.path.isfile(config_file)
38 changes: 38 additions & 0 deletions test/unit/dependency/conftest.py
@@ -0,0 +1,38 @@
# Copyright (c) 2015-2017 Cisco Systems, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

import pytest


@pytest.fixture
def patched_ansible_galaxy_has_requirements_file(mocker):
m = mocker.patch('molecule.dependency.ansible_galaxy.'
'AnsibleGalaxy._has_requirements_file')
m.return_value = True

return m


@pytest.fixture
def patched_gilt_has_requirements_file(mocker):
m = mocker.patch('molecule.dependency.gilt.Gilt._has_requirements_file')
m.return_value = True

return m
26 changes: 21 additions & 5 deletions test/unit/dependency/test_ansible_galaxy.py
Expand Up @@ -108,7 +108,9 @@ def test_bake(ansible_galaxy_instance, role_file, roles_path):
assert x == ansible_galaxy_instance._ansible_galaxy_command


def test_execute(patched_run_command, ansible_galaxy_instance):
def test_execute(patched_run_command,
patched_ansible_galaxy_has_requirements_file,
ansible_galaxy_instance):
ansible_galaxy_instance._ansible_galaxy_command = 'patched-command'
ansible_galaxy_instance.execute()

Expand All @@ -120,14 +122,23 @@ def test_execute(patched_run_command, ansible_galaxy_instance):
patched_run_command.assert_called_once_with('patched-command', debug=None)


def test_execute_does_not_execute(patched_run_command,
ansible_galaxy_instance):
def test_execute_does_not_execute_when_disabled(patched_run_command,
ansible_galaxy_instance):
ansible_galaxy_instance._config.config['dependency']['enabled'] = False
ansible_galaxy_instance.execute()

assert not patched_run_command.called


def test_execute_does_not_execute_when_no_requirements_file(
patched_run_command, patched_ansible_galaxy_has_requirements_file,
ansible_galaxy_instance):
patched_ansible_galaxy_has_requirements_file.return_value = False
ansible_galaxy_instance.execute()

assert not patched_run_command.called


@pytest.mark.skip(reason='baked command does not always return arguments in'
'the same order')
def test_execute_bakes(patched_run_command, ansible_galaxy_instance, role_file,
Expand All @@ -141,8 +152,9 @@ def test_execute_bakes(patched_run_command, ansible_galaxy_instance, role_file,
patched_run_command.assert_called_with(cmd, debug=None)


def test_executes_catches_and_exits_return_code(patched_run_command,
ansible_galaxy_instance):
def test_executes_catches_and_exits_return_code(
patched_run_command, patched_ansible_galaxy_has_requirements_file,
ansible_galaxy_instance):
patched_run_command.side_effect = sh.ErrorReturnCode_1(sh.ansible_galaxy,
None, None)
with pytest.raises(SystemExit) as e:
Expand All @@ -160,3 +172,7 @@ def test_setup(ansible_galaxy_instance):
ansible_galaxy_instance._setup()

assert os.path.isdir(role_directory)


def test_has_requirements_file(ansible_galaxy_instance):
assert not ansible_galaxy_instance._has_requirements_file()
24 changes: 20 additions & 4 deletions test/unit/dependency/test_gilt.py
Expand Up @@ -87,20 +87,31 @@ def test_bake(gilt_config, gilt_instance):
assert x == gilt_instance._gilt_command


def test_execute(patched_run_command, gilt_instance):
def test_execute(patched_run_command, patched_gilt_has_requirements_file,
gilt_instance):
gilt_instance._gilt_command = 'patched-command'
gilt_instance.execute()

patched_run_command.assert_called_once_with('patched-command', debug=None)


def test_execute_does_not_execute(patched_run_command, gilt_instance):
def test_execute_does_not_execute_when_disabled(patched_run_command,
gilt_instance):
gilt_instance._config.config['dependency']['enabled'] = False
gilt_instance.execute()

assert not patched_run_command.called


def test_execute_does_not_execute_when_no_requirements_file(
patched_run_command, patched_gilt_has_requirements_file,
gilt_instance):
patched_gilt_has_requirements_file.return_value = False
gilt_instance.execute()

assert not patched_run_command.called


@pytest.mark.skip(reason='baked command does not always return arguments in'
'the same order')
def test_execute_bakes(patched_run_command, gilt_config, gilt_instance):
Expand All @@ -112,11 +123,16 @@ def test_execute_bakes(patched_run_command, gilt_config, gilt_instance):
patched_run_command.assert_called_with(cmd, debug=None)


def test_executes_catches_and_exits_return_code(patched_run_command,
gilt_instance):
def test_executes_catches_and_exits_return_code(
patched_run_command, patched_gilt_has_requirements_file,
gilt_instance):
patched_run_command.side_effect = sh.ErrorReturnCode_1(sh.ansible_galaxy,
None, None)
with pytest.raises(SystemExit) as e:
gilt_instance.execute()

assert 1 == e.value.code


def test_has_requirements_file(gilt_instance):
assert not gilt_instance._has_requirements_file()
9 changes: 9 additions & 0 deletions test/unit/test_config.py
Expand Up @@ -27,6 +27,7 @@
from molecule import scenario
from molecule import state
from molecule.dependency import ansible_galaxy
from molecule.dependency import gilt
from molecule.driver import docker
from molecule.lint import ansible_lint
from molecule.verifier import testinfra
Expand Down Expand Up @@ -78,6 +79,14 @@ def test_dependency_property(config_instance):
assert isinstance(config_instance.dependency, ansible_galaxy.AnsibleGalaxy)


def test_dependency_property_is_gilt(config_instance, molecule_file):
gilt_data = {'dependency': {'name': 'gilt'}}
configs = [gilt_data]
c = config.Config(molecule_file, configs=configs)

assert isinstance(c.dependency, gilt.Gilt)


def test_driver_property(config_instance):
assert isinstance(config_instance.driver, docker.Docker)

Expand Down