Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provision.localhost: Add prepare functionality
Adding 'ansible' and 'shell' steps to local provisioner. Adding a few tests for the provisioner along the way :) Signed-off-by: Miroslav Vadkerti <mvadkert@redhat.com>
- Loading branch information
Showing
5 changed files
with
159 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import mock | ||
import os | ||
import pytest | ||
import unittest | ||
import tempfile | ||
|
||
from tmt.steps.provision import Provision, localhost, vagrant | ||
from tmt.utils import GeneralError, SpecificationError | ||
|
||
|
||
class PlanMock(mock.MagicMock): | ||
workdir = tempfile.mkdtemp() | ||
|
||
def opt(self, *args, **kwargs): | ||
return {} | ||
|
||
def _level(self): | ||
return 0 | ||
|
||
|
||
def test_empty_plan(): | ||
provision = Provision({}, None) | ||
with pytest.raises(GeneralError): | ||
provision.wake() | ||
|
||
|
||
def test_defaults(): | ||
plan = mock.MagicMock() | ||
provision = Provision({}, PlanMock()) | ||
provision.wake() | ||
for provision_data in provision.data: | ||
assert provision_data['how'] == 'virtual' | ||
|
||
|
||
def test_unsupported_provisioner(): | ||
provision = Provision({'how': 'not-a-valid-provisioner'}, PlanMock()) | ||
with pytest.raises(SpecificationError): | ||
provision.wake() | ||
|
||
|
||
@pytest.mark.parametrize('how,provisioner', [ | ||
('libvirt', vagrant.ProvisionVagrant), | ||
('virtual', vagrant.ProvisionVagrant), | ||
('vagrant', vagrant.ProvisionVagrant), | ||
('local', localhost.ProvisionLocalhost), | ||
('localhost', localhost.ProvisionLocalhost) | ||
]) | ||
def test_pick_provision(how, provisioner): | ||
plan = PlanMock() | ||
provision = Provision({'how': how}, plan) | ||
provision.wake() | ||
for guest in provision.guests: | ||
assert isinstance(guest, provisioner) | ||
|
||
|
||
def test_localhost_execute(): | ||
plan = PlanMock() | ||
provision = Provision({'how': 'localhost'}, plan) | ||
provision.wake() | ||
|
||
with mock.patch('tmt.utils.Common.run') as run: | ||
provision.execute('a', 'b', 'c') | ||
run.assert_called_once_with('a b c') | ||
|
||
|
||
def test_localhost_execute(): | ||
plan = PlanMock() | ||
provision = Provision({'how': 'localhost'}, plan) | ||
provision.wake() | ||
|
||
with mock.patch('tmt.utils.Common.run') as run: | ||
provision.execute('a', 'b', 'c') | ||
run.assert_called_once_with('a b c') | ||
|
||
|
||
def test_localhost_prepare_ansible(): | ||
plan = PlanMock() | ||
provision = Provision({'how': 'localhost'}, plan) | ||
provision.wake() | ||
|
||
with mock.patch('tmt.utils.Common.run') as run: | ||
provision.prepare('ansible', 'playbook.yml') | ||
run.assert_called_once_with('ansible-playbook -c local -i localhost, playbook.yml', cwd=os.getcwd()) | ||
|
||
|
||
def test_localhost_prepare_shell(): | ||
plan = PlanMock() | ||
provision = Provision({'how': 'localhost'}, plan) | ||
provision.wake() | ||
|
||
with mock.patch('tmt.utils.Common.run') as run: | ||
provision.prepare('shell', 'a b c') | ||
run.assert_called_once_with('a b c', cwd=os.getcwd()) |
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 |
---|---|---|
@@ -1,7 +1,36 @@ | ||
from click import echo | ||
import os | ||
|
||
from click import echo | ||
from tmt.steps.provision.base import ProvisionBase | ||
from tmt.utils import SpecificationError | ||
|
||
|
||
class ProvisionLocalhost(ProvisionBase): | ||
""" Localhost provisioner """ | ||
|
||
def __init__(self, data, step): | ||
super(ProvisionLocalhost, self).__init__(data, step) | ||
self._prepare_map = { | ||
'ansible': self._prepare_ansible, | ||
'shell': self._prepare_shell, | ||
} | ||
|
||
def execute(self, *args, **kwargs): | ||
self.run(self.join(args)) | ||
|
||
def _prepare_ansible(self, what): | ||
""" Run ansible on localhost """ | ||
# note: we expect playbooks are placed relatively to the current directory | ||
self.run(f'ansible-playbook -c local -i localhost, {what}', cwd=os.getcwd()) | ||
|
||
def _prepare_shell(self, what): | ||
""" Run ansible on localhost """ | ||
# note: we expect playbooks are placed relatively to the current directory | ||
self.run(what, cwd=os.getcwd()) | ||
|
||
def prepare(self, how, what): | ||
""" Run prepare phase """ | ||
try: | ||
self._prepare_map[how](what) | ||
except AttributeError as e: | ||
raise SpecificationError(f"How '{how}' is not supported.") |