diff options
author | Ajay Kumar <ajay4.kumar@orange.com> | 2021-04-05 16:08:01 +0530 |
---|---|---|
committer | Cédric Ollivier <cedric.ollivier@orange.com> | 2021-04-27 17:23:38 +0200 |
commit | 95611ad4a2943831f710c32867d8636f03021346 (patch) | |
tree | 68a85ed47cdb7e7cab64f06e6436deeca836fa4a /xtesting/core | |
parent | 49504223bc2ca476e63484e98d8b4cdbe299a09d (diff) |
Add an Ansible driver
It calls ansible_runner.interface.run() by converting the testcase
description data to kwargs. It only overrides quiet and artifact_dir to
implement the Xtesting behavior.
Co-Authored-By: Cédric Ollivier <cedric.ollivier@orange.com>
Change-Id: Ifd09810400babc0f2b81f2c33edf55a3ed88807b
Signed-off-by: Ajay kumar <ajay4.kumar@orange.com>
Signed-off-by: Cédric Ollivier <cedric.ollivier@orange.com>
Diffstat (limited to 'xtesting/core')
-rw-r--r-- | xtesting/core/ansible.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/xtesting/core/ansible.py b/xtesting/core/ansible.py new file mode 100644 index 00000000..21148a18 --- /dev/null +++ b/xtesting/core/ansible.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python + +# Copyright (c) 2021 Orange and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +"""Implement a Xtesting driver to run any Ansible playbook.""" + +import logging +import os +import shutil +import time + +import ansible_runner + +from xtesting.core import testcase + + +class Ansible(testcase.TestCase): + """Class designed to run any Ansible playbook via ansible-runner.""" + + __logger = logging.getLogger(__name__) + + def check_requirements(self): + """Check if ansible-playbook is in $PATH""" + self.is_skipped = not shutil.which("ansible-playbook") + if self.is_skipped: + self.__logger.warning("ansible-playbook is missing") + + def run(self, **kwargs): + """ Wrap ansible_runner.interface.run() + + It calls ansible_runner.interface.run() by converting the testcase + description data to kwargs. It only overrides quiet and artifact_dir to + implement the Xtesting behavior. + + Following the playbook logic, criteria is considered as boolean + whatever the value set in testcases.yaml. + + Args: + kwargs: Arbitrary keyword arguments. + + Returns: + EX_OK if the playbook ran well. + EX_RUN_ERROR otherwise. + """ + status = self.EX_RUN_ERROR + self.start_time = time.time() + if ("private_data_dir" in kwargs and + os.path.isdir(kwargs['private_data_dir'])): + try: + if not os.path.exists(self.res_dir): + os.makedirs(self.res_dir) + kwargs["quiet"] = True + kwargs["artifact_dir"] = self.res_dir + runner = ansible_runner.run(**kwargs) + self.details = runner.stats + if runner.rc == 0: + self.result = 100 + status = self.EX_OK + except Exception: # pylint: # pylint: disable=broad-except + self.__logger.exception("Cannot execute the playbook") + else: + self.__logger.error( + "Please set a relevant private_data_dir in testcases.yaml") + self.stop_time = time.time() + return status |