From 2dcbd7846f9a279d56b8b7b44c484b3e6a602dc5 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Fri, 6 Nov 2020 13:12:07 +0300 Subject: [PATCH] mysql_query, mysql_user: simple refactoring of type checks (#58) * mysql_query: simple refactoring of query type check * do the same for mysql_user * Improve integration test coverage --- .../fragments/58-mysql_query_refactoring.yml | 3 +++ plugins/modules/mysql_query.py | 2 +- plugins/modules/mysql_user.py | 2 +- .../targets/test_mysql_user/tasks/main.yml | 19 +++++++++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/58-mysql_query_refactoring.yml diff --git a/changelogs/fragments/58-mysql_query_refactoring.yml b/changelogs/fragments/58-mysql_query_refactoring.yml new file mode 100644 index 0000000..1060b58 --- /dev/null +++ b/changelogs/fragments/58-mysql_query_refactoring.yml @@ -0,0 +1,3 @@ +minor_changes: +- mysql_query - simple refactoring of query type check (https://github.com/ansible-collections/community.mysql/pull/58). +- mysql_user - simple refactoring of priv type check (https://github.com/ansible-collections/community.mysql/pull/58). diff --git a/plugins/modules/mysql_query.py b/plugins/modules/mysql_query.py index 5bb6a01..ed3ace4 100644 --- a/plugins/modules/mysql_query.py +++ b/plugins/modules/mysql_query.py @@ -149,7 +149,7 @@ def main(): config_file = module.params['config_file'] query = module.params["query"] - if not isinstance(query, str) and not isinstance(query, list): + if not isinstance(query, (str, list)): module.fail_json(msg="the query option value must be a string or list, passed %s" % type(query)) if isinstance(query, str): diff --git a/plugins/modules/mysql_user.py b/plugins/modules/mysql_user.py index c5b5b09..1df0c26 100644 --- a/plugins/modules/mysql_user.py +++ b/plugins/modules/mysql_user.py @@ -1044,7 +1044,7 @@ def main(): plugin_hash_string = module.params["plugin_hash_string"] plugin_auth_string = module.params["plugin_auth_string"] resource_limits = module.params["resource_limits"] - if priv and not (isinstance(priv, str) or isinstance(priv, dict)): + if priv and not isinstance(priv, (str, dict)): module.fail_json(msg="priv parameter must be str or dict but %s was passed" % type(priv)) if priv and isinstance(priv, dict): diff --git a/tests/integration/targets/test_mysql_user/tasks/main.yml b/tests/integration/targets/test_mysql_user/tasks/main.yml index 3e50608..6755a35 100644 --- a/tests/integration/targets/test_mysql_user/tasks/main.yml +++ b/tests/integration/targets/test_mysql_user/tasks/main.yml @@ -192,6 +192,25 @@ - "'%db' in result.stdout" - "'SELECT' in result.stdout" + - name: test priv type check, must fail + mysql_user: + <<: *mysql_params + name: '{{ user_name_1 }}' + priv: + - unsuitable + - type + append_privs: yes + host_all: yes + password: '{{ user_password_1 }}' + register: result + ignore_errors: yes + + - name: check fail message + assert: + that: + - result is failed + - result.msg is search('priv parameter must be str or dict') + - name: change user access to database via wildcard mysql_user: <<: *mysql_params