From 18c880aac40f58e6a73fd4f49bd454d680f0cfd8 Mon Sep 17 00:00:00 2001 From: Emilien Macchi Date: Fri, 25 Sep 2020 01:12:56 -0400 Subject: [PATCH] Check mode support for 2 modules Add check mode support for both tripleo_container_config_scripts and tripleo_container_configs modules, and test them in check mode via molecule. Change-Id: I9667c581d9437ca6f210cb9ded9e189474d6641f --- .../modules/tripleo_container_config_scripts.py | 17 +++++++++-------- .../modules/tripleo_container_configs.py | 11 ++++++----- .../converge.yml | 6 ++++++ .../molecule.yml | 1 + .../tripleo_container_configs/converge.yml | 4 ++++ .../tripleo_container_configs/molecule.yml | 1 + 6 files changed, 27 insertions(+), 13 deletions(-) diff --git a/tripleo_ansible/ansible_plugins/modules/tripleo_container_config_scripts.py b/tripleo_ansible/ansible_plugins/modules/tripleo_container_config_scripts.py index d1a8934f7..22bf9e247 100644 --- a/tripleo_ansible/ansible_plugins/modules/tripleo_container_config_scripts.py +++ b/tripleo_ansible/ansible_plugins/modules/tripleo_container_config_scripts.py @@ -72,14 +72,15 @@ def main(): config_data = args['config_data'] config_dir = args['config_dir'] - for path, config in config_data.items(): - # this is specific to how the files are written in config-download - mode = config.get('mode', '0600') - config_path = os.path.join(config_dir, path) - with open(config_path, "w") as config_file: - config_file.write(config['content']) - os.chmod(config_path, int(mode, 8)) - results['changed'] = True + if not module.check_mode: + for path, config in config_data.items(): + # this is specific to how the files are written in config-download + mode = config.get('mode', '0600') + config_path = os.path.join(config_dir, path) + with open(config_path, "w") as config_file: + config_file.write(config['content']) + os.chmod(config_path, int(mode, 8)) + results['changed'] = True module.exit_json(**results) diff --git a/tripleo_ansible/ansible_plugins/modules/tripleo_container_configs.py b/tripleo_ansible/ansible_plugins/modules/tripleo_container_configs.py index 79b5ee3ec..abd932624 100644 --- a/tripleo_ansible/ansible_plugins/modules/tripleo_container_configs.py +++ b/tripleo_ansible/ansible_plugins/modules/tripleo_container_configs.py @@ -79,11 +79,12 @@ def main(): # Set parameters config_data = args['config_data'] - for path, config in config_data.items(): - with open(path, "wb") as config_file: - config_file.write(json.dumps(config, indent=2).encode('utf-8')) - os.chmod(path, 0o600) - results['changed'] = True + if not module.check_mode: + for path, config in config_data.items(): + with open(path, "wb") as config_file: + config_file.write(json.dumps(config, indent=2).encode('utf-8')) + os.chmod(path, 0o600) + results['changed'] = True module.exit_json(**results) diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_config_scripts/converge.yml b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_config_scripts/converge.yml index 5b1fa30bc..7ff4cad2f 100644 --- a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_config_scripts/converge.yml +++ b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_config_scripts/converge.yml @@ -14,6 +14,8 @@ content: "#!/bin/bash\npuppet apply" mode: "0700" config_dir: "{{ container_config_scripts_tmp_dir.path }}" + when: + - not ansible_check_mode|bool - name: Check that container_puppet_apply.sh file was created with right permissions stat: path: "{{ container_config_scripts_tmp_dir.path }}/container_puppet_apply.sh" @@ -21,9 +23,13 @@ failed_when: - not st_config.stat.exists - not (st_config.stat.mode == '0700') + when: + - not ansible_check_mode|bool - name: Check that container_puppet_apply.sh script is correct slurp: src: "{{ container_config_scripts_tmp_dir.path }}/container_puppet_apply.sh" register: slurp_config failed_when: - ('puppet apply' not in slurp_config['content']|b64decode) + when: + - not ansible_check_mode|bool diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_config_scripts/molecule.yml b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_config_scripts/molecule.yml index ff0274cc6..4e43995a0 100644 --- a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_config_scripts/molecule.yml +++ b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_config_scripts/molecule.yml @@ -45,6 +45,7 @@ scenario: test_sequence: - prepare - converge + - check verifier: name: testinfra diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/converge.yml b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/converge.yml index f8a94dd97..5b22439b7 100644 --- a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/converge.yml +++ b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/converge.yml @@ -25,9 +25,13 @@ register: st_config failed_when: - not st_config.stat.exists + when: + - not ansible_check_mode|bool - name: Check that container_config1.json configuration is correct slurp: src: "/tmp/container_config1.json" register: slurp_config failed_when: - ('ceilometer-polling' not in slurp_config['content']|b64decode) + when: + - not ansible_check_mode|bool diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/molecule.yml b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/molecule.yml index 631011e96..726b9475b 100644 --- a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/molecule.yml +++ b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/molecule.yml @@ -45,6 +45,7 @@ scenario: test_sequence: - prepare - converge + - check verifier: name: testinfra