From db592394195a2e5b95bf2818af64bbc34089390b Mon Sep 17 00:00:00 2001 From: FCO Date: Sun, 28 Mar 2021 13:59:53 +0200 Subject: [PATCH] Fix check mode for MQ modules and disable asserts in test playbooks for check mode --- plugins/modules/mq_broker.py | 19 +++++++----- plugins/modules/mq_broker_config.py | 29 ++++++++++++------- plugins/modules/mq_broker_info.py | 14 ++++++++- plugins/modules/mq_user_info.py | 9 +++++- .../targets/mq/tasks/test_mq_broker.yml | 12 ++++++++ .../mq/tasks/test_mq_broker_config.yml | 3 ++ .../targets/mq/tasks/test_mq_user.yml | 12 ++++++++ .../targets/mq/tasks/test_mq_user_info.yml | 21 +++----------- 8 files changed, 82 insertions(+), 37 deletions(-) diff --git a/plugins/modules/mq_broker.py b/plugins/modules/mq_broker.py index c32fb553fc0..f8922da520a 100644 --- a/plugins/modules/mq_broker.py +++ b/plugins/modules/mq_broker.py @@ -425,13 +425,7 @@ def create_broker(conn, module): module.fail_json_aws(RuntimeError, msg="At least one security group must be specified on broker creation") # changed = True - if not module.check_mode: - result = conn.create_broker(**kwargs) - else: - result = { - 'BrokerArn': 'fakeArn', - 'BrokerId': 'fakeId' - } + result = conn.create_broker(**kwargs) # return {'broker': result, 'changed': changed} @@ -463,7 +457,6 @@ def update_broker(conn, module, broker_id): return {'broker': result, 'changed': changed} - def ensure_absent(conn, module): result = { 'BrokerName': module.params['broker_name'], @@ -490,6 +483,12 @@ def ensure_absent(conn, module): def ensure_present(conn, module): + if module.check_mode: + return {'broker': { + 'BrokerArn': 'fakeArn', + 'BrokerId': 'fakeId' + }, 'changed': True} + # broker_id = get_broker_id(conn, module) if broker_id: return update_broker(conn, module, broker_id) @@ -542,6 +541,10 @@ def main(): module.exit_json(**compound_result) elif module.params['state'] == 'restarted': broker_id = get_broker_id(connection, module) + if module.check_mode: + module.exit_json(broker={ + 'BrokerId': broker_id if broker_id else 'fakeId' + }, changed=True) if not broker_id: module.fail_json_aws(RuntimeError, msg="Cannot find broker with name {0}.".format(module.params['broker_name'])) diff --git a/plugins/modules/mq_broker_config.py b/plugins/modules/mq_broker_config.py index bbd02951dae..8f87ea1255b 100644 --- a/plugins/modules/mq_broker_config.py +++ b/plugins/modules/mq_broker_config.py @@ -132,7 +132,12 @@ def get_broker_info(conn, module): try: return conn.describe_broker(BrokerId=module.params['broker_id']) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: - module.fail_json_aws(e, msg="Couldn't get broker details.") + if module.check_mode: + return { + 'BrokerId': module.params['broker_id'], + } + else: + module.fail_json_aws(e, msg="Couldn't get broker details.") def get_current_configuration(conn, module, cfg_id, cfg_revision): @@ -185,16 +190,20 @@ def ensure_config(conn, module): broker_id = module.params['broker_id'] broker_info = get_broker_info(conn, module) changed = False - current_cfg = broker_info['Configurations']['Current'] - if 'Pending' in broker_info['Configurations']: - current_cfg = broker_info['Configurations']['Pending'] - current_cfg_encoded = get_current_configuration(conn, module, - current_cfg['Id'], - current_cfg['Revision'])['Data'] - if IS_PYTHON3: - current_cfg_decoded = base64.b64decode(current_cfg_encoded.encode()).decode() + if module.check_mode and 'Configurations' not in broker_info: + # not result from get_broker_info(). use requeste config + current_cfg_decoded = module.params['config_xml'] else: - current_cfg_decoded = base64.b64decode(current_cfg_encoded) + current_cfg = broker_info['Configurations']['Current'] + if 'Pending' in broker_info['Configurations']: + current_cfg = broker_info['Configurations']['Pending'] + current_cfg_encoded = get_current_configuration(conn, module, + current_cfg['Id'], + current_cfg['Revision'])['Data'] + if IS_PYTHON3: + current_cfg_decoded = base64.b64decode(current_cfg_encoded.encode()).decode() + else: + current_cfg_decoded = base64.b64decode(current_cfg_encoded) if is_same_config(current_cfg_decoded, module.params['config_xml']): return { 'changed': changed, diff --git a/plugins/modules/mq_broker_info.py b/plugins/modules/mq_broker_info.py index d2ac3b3e48a..6f3f71734e4 100644 --- a/plugins/modules/mq_broker_info.py +++ b/plugins/modules/mq_broker_info.py @@ -89,7 +89,13 @@ def get_broker_info(conn, module, broker_id): try: return conn.describe_broker(BrokerId=broker_id) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: - module.fail_json_aws(e, msg="Couldn't get broker details.") + if module.check_mode: + module.exit_json(broker={ + 'BrokerId': broker_id, + 'BrokerName': 'fakeName' + }) + else: + module.fail_json_aws(e, msg="Couldn't get broker details.") def main(): @@ -109,6 +115,12 @@ def main(): try: if not broker_id: broker_id = get_broker_id(connection, module) + if not broker_id: + if module.check_mode: + module.exit_json(broker={ + 'BrokerId': 'fakeId', + 'BrokerName': broker_name if broker_name else 'fakeName' + }) result = get_broker_info(connection, module, broker_id) except botocore.exceptions.ClientError as e: module.fail_json_aws(e) diff --git a/plugins/modules/mq_user_info.py b/plugins/modules/mq_user_info.py index c3032899bcd..71a5b414df5 100644 --- a/plugins/modules/mq_user_info.py +++ b/plugins/modules/mq_user_info.py @@ -114,7 +114,14 @@ def get_user_info(conn, module): response = conn.list_users(BrokerId=module.params['broker_id'], MaxResults=module.params['max_results']) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: - module.fail_json_aws(e, msg='Failed to describe users') + if module.check_mode: + # return empty set for unknown broker in check mode + if DEFAULTS['as_dict']: + return {} + else: + return [] + else: + module.fail_json_aws(e, msg='Failed to describe users') # if not module.params['skip_pending_create'] and not module.params['skip_pending_delete']: # we can simply return the sub-object from the response diff --git a/tests/integration/targets/mq/tasks/test_mq_broker.yml b/tests/integration/targets/mq/tasks/test_mq_broker.yml index 21136116b98..f0b4d0f8291 100644 --- a/tests/integration/targets/mq/tasks/test_mq_broker.yml +++ b/tests/integration/targets/mq/tasks/test_mq_broker.yml @@ -49,6 +49,7 @@ - result_c1.broker['BrokerName'] == broker_name - result_c1.broker['BrokerState'] == 'CREATION_IN_PROGRESS' - ( result_c1.broker['StorageType'] | upper ) == 'EFS' + when: not ansible_check_mode - debug: msg: "Wait until broker {{ broker_name }} ({{ broker_id }}) enters running state. This may take several minutes" - name: wait for startup @@ -63,6 +64,7 @@ until: result.broker['BrokerState'] == 'RUNNING' retries: 15 delay: 60 + when: not ansible_check_mode - name: repeat creation # amazon.aws.mq_broker: mq_broker: @@ -95,6 +97,7 @@ - result_c2.broker['BrokerId'] == broker_id - result_c2.broker['BrokerName'] == broker_name - ( result_c2.broker['StorageType'] | upper ) == 'EFS' + when: not ansible_check_mode - name: update broker # amazon.aws.mq_broker: mq_broker: @@ -113,6 +116,7 @@ that: - ( result.changed | bool) - result.broker['BrokerId'] == broker_id + when: not ansible_check_mode - name: reboot broker to make pending changes active # amazon.aws.mq_broker: mq_broker: @@ -139,6 +143,7 @@ that: - result.changed | bool - result_r1.broker['BrokerState'] == 'REBOOT_IN_PROGRESS' + when: not ansible_check_mode - debug: msg: "Wait until reboot of broker {{ broker_name }} ({{ broker_id }}) is finished. This may take several minutes" - name: wait for reboot @@ -153,6 +158,7 @@ until: result.broker['BrokerState'] == 'RUNNING' retries: 15 delay: 60 + when: not ansible_check_mode - name: get details after update # amazon.aws.mq_broker_info: mq_broker_info: @@ -172,6 +178,7 @@ - not ( result_u1.broker['AutoMinorVersionUpgrade'] | bool ) # the next one checks that changes to create-only parameters are silently ignore - result_u1.broker['StorageType'] == result_c1.broker['StorageType'] + when: not ansible_check_mode - name: repeat update broker # amazon.aws.mq_broker: mq_broker: @@ -201,6 +208,7 @@ - result_u2.broker['BrokerId'] == result_u1.broker['BrokerId'] - result_u2.broker['StorageType'] == result_u1.broker['StorageType'] - result_u2.broker['EngineVersion'] == result_u1.broker['EngineVersion'] + when: not ansible_check_mode # here we can put in tests for mq_broker_config - name: delete broker # amazon.aws.mq_broker: @@ -218,6 +226,7 @@ fail_msg: broker delete failed that: - ( result.changed | bool) + when: not ansible_check_mode - name: get details after delete # amazon.aws.mq_broker_info: mq_broker_info: @@ -233,6 +242,7 @@ fail_msg: broker delete too fast? that: - result_d1.broker['BrokerState'] == 'DELETION_IN_PROGRESS' + when: not ansible_check_mode - name: repeat broker deletion # amazon.aws.mq_broker: mq_broker: @@ -249,6 +259,7 @@ fail_msg: didn't detect DELETION_IN_PROGRESS in progress that: - not ( result.changed | bool) + when: not ansible_check_mode - name: deletion unknown broker - simulates re-deletion of completely deleted broker # amazon.aws.mq_broker: mq_broker: @@ -265,4 +276,5 @@ fail_msg: deletion of unknown broker return unexpected result that: - not ( result.changed | bool) + when: not ansible_check_mode diff --git a/tests/integration/targets/mq/tasks/test_mq_broker_config.yml b/tests/integration/targets/mq/tasks/test_mq_broker_config.yml index 797ac3f099f..5e0ffab3077 100644 --- a/tests/integration/targets/mq/tasks/test_mq_broker_config.yml +++ b/tests/integration/targets/mq/tasks/test_mq_broker_config.yml @@ -42,6 +42,7 @@ - result.broker['BrokerId'] == broker_id - result.configuration['id'] == result.broker['Configurations']['Pending']['Id'] - result.configuration['revision'] == result.broker['Configurations']['Pending']['Revision'] + when: not ansible_check_mode - name: test 2 - send (almost) same config again # amazon.aws.mq_broker_info: mq_broker_config: @@ -58,6 +59,7 @@ fail_msg: test2 failed that: - not (result.changed | bool ) + when: not ansible_check_mode - name: test 3 - send new config with custom description and request reboot # amazon.aws.mq_broker_info: mq_broker_config: @@ -77,3 +79,4 @@ that: - result.changed | bool - result.broker['BrokerState'] == 'REBOOT_IN_PROGRESS' + when: not ansible_check_mode diff --git a/tests/integration/targets/mq/tasks/test_mq_user.yml b/tests/integration/targets/mq/tasks/test_mq_user.yml index 0d1c10211c8..342f6755d37 100644 --- a/tests/integration/targets/mq/tasks/test_mq_user.yml +++ b/tests/integration/targets/mq/tasks/test_mq_user.yml @@ -45,6 +45,7 @@ - result.user['Username'] == usernames[0] - not (result.user['Pending']['ConsoleAccess'] | bool) - result.user['Pending']['Groups'] | length == 0 + when: not ansible_check_mode - name: test2 - create user with console access and group list # amazon.aws.mq_user: mq_user: @@ -67,6 +68,7 @@ - result.user['Username'] == usernames[1] - result.user['Pending']['ConsoleAccess'] | bool - result.user['Pending']['Groups'] | length == 2 + when: not ansible_check_mode - name: test3 - create user with defined password # amazon.aws.mq_user: mq_user: @@ -87,6 +89,7 @@ - result.user['Username'] == usernames[2] - not (result.user['Pending']['ConsoleAccess'] | bool) - result.user['Pending']['Groups'] | length == 0 + when: not ansible_check_mode - name: test4 - update user password - ignore mode # amazon.aws.mq_user: mq_user: @@ -104,6 +107,7 @@ fail_msg: test4 failed that: - not (result.changed | bool) + when: not ansible_check_mode - name: test5 - update user password - force mode # amazon.aws.mq_user: mq_user: @@ -122,6 +126,7 @@ fail_msg: test5 failed that: - result.changed | bool + when: not ansible_check_mode - name: test6 - update console access - same value # amazon.aws.mq_user: mq_user: @@ -139,6 +144,7 @@ fail_msg: test6 failed that: - not (result.changed | bool) + when: not ansible_check_mode - name: test7 - update console access - new value # amazon.aws.mq_user: mq_user: @@ -158,6 +164,7 @@ - result.changed | bool - not( result.user['Pending']['ConsoleAccess'] | bool ) - result.user['Pending']['Groups'] | length == 2 + when: not ansible_check_mode - name: test8 - update group list - same list but different order # amazon.aws.mq_user: mq_user: @@ -175,6 +182,7 @@ fail_msg: test8 failed that: - not (result.changed | bool) + when: not ansible_check_mode - name: test9 - update group list - add element # amazon.aws.mq_user: mq_user: @@ -193,6 +201,7 @@ that: - result.changed | bool - result.user['Pending']['Groups'] | length == 3 + when: not ansible_check_mode - name: test10 - update group list - remove element # amazon.aws.mq_user: mq_user: @@ -211,6 +220,7 @@ that: - result.changed | bool - result.user['Pending']['Groups'] | length == 2 + when: not ansible_check_mode - name: test11 - update group list - set to empty list # amazon.aws.mq_user: mq_user: @@ -229,6 +239,7 @@ that: - result.changed | bool - result.user['Pending']['Groups'] | length == 0 + when: not ansible_check_mode - name: delete all users # amazon.aws.mq_user: mq_user: @@ -257,3 +268,4 @@ fail_msg: test13 failed that: - not(result.changed | bool) + when: not ansible_check_mode diff --git a/tests/integration/targets/mq/tasks/test_mq_user_info.yml b/tests/integration/targets/mq/tasks/test_mq_user_info.yml index 566195c1957..3eaeae8736f 100644 --- a/tests/integration/targets/mq/tasks/test_mq_user_info.yml +++ b/tests/integration/targets/mq/tasks/test_mq_user_info.yml @@ -53,23 +53,7 @@ aws_secret_key: "{{ aws_secret_access_key }}" security_token: "{{ aws_session_token }}" loop: "{{ delete_users | flatten(levels=1) }}" - - name: test1 - list all users with custom limit - # amazon.aws.mq_user_info: - mq_user_info: - broker_id: "{{ broker_id }}" - region: "{{ aws_region }}" - max_results: 5 - aws_access_key: "{{ aws_access_key_id }}" - aws_secret_key: "{{ aws_secret_access_key }}" - security_token: "{{ aws_session_token }}" - register: result - - name: test1 - verify - #ansible.builtin.assert: - assert: - fail_msg: test1 failed - that: - - (result.users | length) == 5 - - name: test2 - list all users as dict + - name: test2 - list all users # amazon.aws.mq_user_info: mq_user_info: broker_id: "{{ broker_id }}" @@ -86,6 +70,7 @@ - result.users['info_user1'] - result.users['info_user2'] - result.users['info_user3'] + when: not ansible_check_mode - name: test3 - list only user currently being active until next broker reboot # amazon.aws.mq_user_info: mq_user_info: @@ -106,6 +91,7 @@ - not ('info_user3' in result.users) - not ('info_user4' in result.users) - result.users['info_user5'] + when: not ansible_check_mode - name: test4 - list only user that will be active after next broker reboot # amazon.aws.mq_user_info: mq_user_info: @@ -126,3 +112,4 @@ - result.users['info_user3'] - result.users['info_user4'] - not ('info_user5' in result.users) + when: not ansible_check_mode