From be9db4572e413d420da554496dd264f27a309bcf Mon Sep 17 00:00:00 2001 From: Bogdan Dobrelya Date: Tue, 23 Aug 2022 18:07:25 +0200 Subject: [PATCH] Control molecule tags with test molecule script Make test_molecule.py script reading scenario test_vars.yml and react based on existing test_tags, molecule_tags_enforce, test_skip_tags, and molecule_skip_tags_enforce variables of it: * (skip_)tags configured, not enforced - molecule ignores it, leaving it for the converge playbook control or not; * (skip_)tags configured, and enforced - molecule applies it, even if the converge playbook controls it as well (likely making it wrong); This is required because converge may not always control skipping tags. For example, include_tasks can only apply tags, but not skip it. Also, molecule tags, if included, might conflict with tags management of converge playbooks. So provided enfore-or-not it options help to tweak that behavior case by case. Change-Id: I0e3193c5f64457e55209891105df990c01dd2657 Signed-off-by: Bogdan Dobrelya --- tests/test_molecule.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/test_molecule.py b/tests/test_molecule.py index 8ef8c17d1..a9561edd3 100644 --- a/tests/test_molecule.py +++ b/tests/test_molecule.py @@ -34,6 +34,35 @@ def set_proper_molecule_config(role_path, scenario='default'): return mol_config +def set_molecule_tags(role_path, scenario='default'): + mol_tags = [] + if os.path.exists(os.path.join(role_path, 'molecule', + f'{scenario}/test_vars.yml')): + test_vars_path = os.path.join(role_path, 'molecule', + f'{scenario}/test_vars.yml') + with open(test_vars_path) as content: + data = yaml.safe_load(content) + + if not data: + return [] + if ('test_skip_tags' in data.keys() and data['test_skip_tags'] + and data.get('molecule_skip_tags_enforce', True)): + mol_tags.append('--skip-tags') + if type(data['test_skip_tags']) == str: + mol_tags.append(data['test_skip_tags']) + elif type(data['test_skip_tags']) == list: + mol_tags.append(",".join(data['test_skip_tags'])) + + if ('test_tags' in data.keys() and data['test_tags'] + and data.get('molecule_tags_enforce', True)): + mol_tags.append('--tags') + if type(data['test_tags']) == str: + mol_tags.append(data['test_tags']) + elif type(data['test_tags']) == list: + mol_tags.append(",".join(data['test_tags'])) + return mol_tags + + def run_molecule(pytestconfig, scenario=None): cmd = ['python', '-m', 'molecule'] if not scenario: @@ -55,6 +84,12 @@ def run_molecule(pytestconfig, scenario=None): else: cmd.append('--all') + alltags = set_molecule_tags(os.getcwd(), scenario) + if alltags: + if '--' not in cmd: + cmd.append('--') + cmd.extend(alltags) + try: assert subprocess.call(cmd) == 0 finally: