3 # Copyright (c) 2021 Orange and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 """Implement a Xtesting driver to run any Ansible playbook."""
19 from xtesting.core import testcase
22 class Ansible(testcase.TestCase):
23 """Class designed to run any Ansible playbook via ansible-runner."""
25 __logger = logging.getLogger(__name__)
27 def check_requirements(self):
28 """Check if ansible-playbook is in $PATH"""
29 self.is_skipped = not shutil.which("ansible-playbook")
31 self.__logger.warning("ansible-playbook is missing")
33 def run(self, **kwargs):
34 """ Wrap ansible_runner.interface.run()
36 It calls ansible_runner.interface.run() by converting the testcase
37 description data to kwargs. It only overrides quiet and artifact_dir to
38 implement the Xtesting behavior.
40 Following the playbook logic, criteria is considered as boolean
41 whatever the value set in testcases.yaml.
44 kwargs: Arbitrary keyword arguments.
47 EX_OK if the playbook ran well.
48 EX_RUN_ERROR otherwise.
50 status = self.EX_RUN_ERROR
51 self.start_time = time.time()
52 if ("private_data_dir" in kwargs and
53 os.path.isdir(kwargs['private_data_dir'])):
55 if not os.path.exists(self.res_dir):
56 os.makedirs(self.res_dir)
57 if "quiet" not in kwargs:
58 kwargs["quiet"] = True
59 kwargs["artifact_dir"] = self.res_dir
60 runner = ansible_runner.run(**kwargs)
61 self.details = runner.stats
65 except Exception: # pylint: # pylint: disable=broad-except
66 self.__logger.exception("Cannot execute the playbook")
69 "Please set a relevant private_data_dir in testcases.yaml")
70 self.stop_time = time.time()