forked from ansible/molecule
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The ability to call `molecule check` to perform a "Dry Run". This is currently not incorporated into the default test sequence. Fixes: ansible#128
- Loading branch information
Showing
13 changed files
with
202 additions
and
29 deletions.
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
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,63 @@ | ||
# Copyright (c) 2015-2016 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. | ||
|
||
from molecule import ansible_playbook | ||
from molecule import util | ||
from molecule.command import base | ||
|
||
LOG = util.get_logger(__name__) | ||
|
||
|
||
class Check(base.Base): | ||
""" | ||
Performs a check ("Dry Run") on the current role. | ||
Usage: | ||
check | ||
""" | ||
|
||
def execute(self, exit=True): | ||
""" | ||
Execute the actions necessary to perform a `molecule check` and | ||
return a tuple. | ||
:param exit: (Unused) Provided to complete method signature. | ||
:return: Return a tuple provided by :meth:`.AnsiblePlaybook.execute`. | ||
""" | ||
if not self.molecule.state.created: | ||
msg = ('Instance(s) not created, `check` should be run ' | ||
'against created instance(s)') | ||
LOG.error('ERROR: {}'.format(msg)) | ||
util.sysexit() | ||
|
||
if self.molecule.state.created and not self.molecule.state.converged: | ||
msg = ('Instance(s) already converged, `check` should be run ' | ||
'against unconverged instance(s)') | ||
LOG.error('ERROR: {}'.format(msg)) | ||
util.sysexit() | ||
|
||
ansible = ansible_playbook.AnsiblePlaybook(self.molecule.config.config[ | ||
'ansible']) | ||
ansible.add_cli_arg('check', True) | ||
for k, v in self.molecule.driver.ansible_connection_params.items(): | ||
ansible.add_cli_arg(k, v) | ||
|
||
util.print_info('Performing a "Dry Run" of playbook ...') | ||
return ansible.execute(hide_errors=True) |
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
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
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,61 @@ | ||
# Copyright (c) 2015-2016 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 os | ||
import shutil | ||
|
||
import pytest | ||
import yaml | ||
|
||
from molecule import config | ||
from molecule import core | ||
from molecule import state | ||
|
||
|
||
@pytest.fixture() | ||
def molecule_file(tmpdir, request, molecule_vagrant_config): | ||
d = tmpdir.mkdir('molecule') | ||
c = d.join(os.extsep.join(('molecule', 'yml'))) | ||
data = molecule_vagrant_config | ||
c.write(data) | ||
|
||
pbook = d.join(os.extsep.join(('playbook', 'yml'))) | ||
data = [{'hosts': 'all', 'tasks': [{'command': 'echo'}]}] | ||
pbook.write(yaml.safe_dump(data)) | ||
|
||
os.chdir(d.strpath) | ||
|
||
def cleanup(): | ||
os.remove(c.strpath) | ||
shutil.rmtree(d.strpath) | ||
|
||
request.addfinalizer(cleanup) | ||
|
||
return c.strpath | ||
|
||
|
||
@pytest.fixture() | ||
def molecule_instance(temp_files, state_path): | ||
c = temp_files(fixtures=['molecule_vagrant_config']) | ||
m = core.Molecule(dict()) | ||
m.config = config.Config(configs=c) | ||
m.state = state.State(state_file=state_path) | ||
|
||
return m |
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,61 @@ | ||
# Copyright (c) 2015-2016 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 | ||
|
||
from molecule.command import check | ||
|
||
|
||
@pytest.fixture | ||
def mocked_main(mocker): | ||
return mocker.patch('molecule.command.check.Check.main') | ||
|
||
|
||
def test_raises_when_instance_not_created(mocked_main, molecule_instance): | ||
c = check.Check([], dict(), molecule_instance) | ||
|
||
with pytest.raises(SystemExit) as e: | ||
c.execute() | ||
assert ('ERROR: Instance(s) not created, `check` ' | ||
'should be run against created instance(s)') in e | ||
|
||
|
||
def test_raises_when_instance_created_and_not_converged(mocked_main, | ||
molecule_instance): | ||
molecule_instance.state.change_state('created', True) | ||
molecule_instance.state.change_state('converged', False) | ||
c = check.Check([], dict(), molecule_instance) | ||
|
||
with pytest.raises(SystemExit) as e: | ||
c.execute() | ||
assert ('ERROR: Instance(s) already converged, `check` ' | ||
'should be run against unconverged instance(s)') in e | ||
|
||
|
||
def test_execute(mocker, mocked_main, molecule_instance): | ||
molecule_instance.state.change_state('created', True) | ||
molecule_instance.state.change_state('converged', True) | ||
molecule_instance._driver = mocker.Mock(ansible_connection_params={}) | ||
mocked = mocker.patch('molecule.ansible_playbook.AnsiblePlaybook.execute') | ||
|
||
c = check.Check([], dict(), molecule_instance) | ||
c.execute() | ||
|
||
assert mocked.called_once_with(hide_errors=True) |
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