Skip to content

Commit

Permalink
Fix query rules 2 (#77)
Browse files Browse the repository at this point in the history
* fix query rule

* prepare integration test to run against proxysql < 2 and >= 2

* dafuq

* Update tests/integration/targets/test_proxysql_query_rules/tasks/setup_test_query_rule.yml

Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>

* add changelog fragment

Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>
  • Loading branch information
markuman and Andersson007 committed Sep 28, 2021
1 parent 922c412 commit 7b7c5eb
Show file tree
Hide file tree
Showing 13 changed files with 444 additions and 182 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/77-fix-query-rules.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- proxysql_query_rules - fix backwards compatibility. Proxysql > 2 does not support parameter ``cache_empty_result`` (https://github.com/ansible-collections/community.proxysql/pull/77).
8 changes: 5 additions & 3 deletions plugins/modules/proxysql_query_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def load_config_to_runtime(cursor):

class ProxyQueryRule(object):

def __init__(self, module):
def __init__(self, module, version):
self.state = module.params["state"]
self.force_delete = module.params["force_delete"]
self.save_to_disk = module.params["save_to_disk"]
Expand All @@ -403,7 +403,6 @@ def __init__(self, module):
"replace_pattern",
"destination_hostgroup",
"cache_ttl",
"cache_empty_result",
"multiplex",
"timeout",
"retries",
Expand All @@ -418,6 +417,9 @@ def __init__(self, module):
"apply",
"comment"]

if version.get('major') >= 2:
config_data_keys.append("cache_empty_result")

self.config_data = dict((k, module.params[k])
for k in config_data_keys)

Expand Down Expand Up @@ -681,7 +683,7 @@ def main():
msg="unable to connect to ProxySQL Admin Module.. %s" % to_native(e)
)

proxysql_query_rule = ProxyQueryRule(module)
proxysql_query_rule = ProxyQueryRule(module, version)
result = {}

result['state'] = proxysql_query_rule.state
Expand Down
152 changes: 107 additions & 45 deletions tests/integration/targets/test_proxysql_query_rules/tasks/base_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,120 @@
include_tasks: "{{ test_delete|ternary('setup_test_query_rule', 'cleanup_test_query_rules') }}.yml"
when: not test_proxysql_query_rules_check_idempotence

### when

- name: "{{ role_name }} | {{ current_test }} | {{ test_delete|ternary('delete','create') }} test query rule"
proxysql_query_rules:
login_user: admin
login_password: admin
username: '{{ test_user }}'
match_pattern: '{{ test_match_pattern }}'
destination_hostgroup: '{{ test_destination_hostgroup }}'
active: '{{ test_active }}'
retries: '{{ test_retries }}'
cache_ttl: '{{ test_cache_ttl }}'
cache_empty_result: '{{ test_cache_empty_result }}'
multiplex: '{{ test_multiplex }}'
next_query_flagIN: '{{ test_next_query_flagin }}'
state: "{{ test_delete|ternary('absent', 'present') }}"
save_to_disk: "{{ not test_proxysql_query_rules_in_memory_only }}"
load_to_runtime: "{{ not test_proxysql_query_rules_in_memory_only }}"
check_mode: "{{ test_proxysql_query_rules_check_mode }}"
register: status

- name: "{{ role_name }} | {{ current_test }} | persist the changes to disk, and load to runtime"
block:
### when proxysql >= 2

- name: "{{ role_name }} | {{ current_test }} | save the query rules config from memory to disk"
proxysql_manage_config:
- name: test against proxysql >= 2
when: PROXYSQL2
block:
- name: "{{ role_name }} | {{ current_test }} | {{ test_delete|ternary('delete','create') }} test query rule"
proxysql_query_rules:
login_user: admin
login_password: admin
action: SAVE
config_settings: MYSQL QUERY RULES
direction: TO
config_layer: DISK
username: '{{ test_user }}'
match_pattern: '{{ test_match_pattern }}'
destination_hostgroup: '{{ test_destination_hostgroup }}'
active: '{{ test_active }}'
retries: '{{ test_retries }}'
cache_ttl: '{{ test_cache_ttl }}'
cache_empty_result: '{{ test_cache_empty_result }}'
multiplex: '{{ test_multiplex }}'
next_query_flagIN: '{{ test_next_query_flagin }}'
state: "{{ test_delete|ternary('absent', 'present') }}"
save_to_disk: "{{ not test_proxysql_query_rules_in_memory_only }}"
load_to_runtime: "{{ not test_proxysql_query_rules_in_memory_only }}"
check_mode: "{{ test_proxysql_query_rules_check_mode }}"
register: status

- name: "{{ role_name }} | {{ current_test }} | persist the changes to disk, and load to runtime"
block:

- name: "{{ role_name }} | {{ current_test }} | save the query rules config from memory to disk"
proxysql_manage_config:
login_user: admin
login_password: admin
action: SAVE
config_settings: MYSQL QUERY RULES
direction: TO
config_layer: DISK

- name: "{{ role_name }} | {{ current_test }} | load the query rules config from memory to runtime"
proxysql_manage_config:
login_user: admin
login_password: admin
action: LOAD
config_settings: MYSQL QUERY RULES
direction: TO
config_layer: RUNTIME

when: test_proxysql_query_rules_with_delayed_persist

- name: "{{ role_name }} | {{ current_test }} | check if test query rule exists in memory"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT username || ',' || match_pattern || ',' || destination_hostgroup || ',' || active || ',' || retries || ',' || cache_ttl || ',' || cache_empty_result || ',' || multiplex || ',' || next_query_flagIN FROM mysql_query_rules where username = '{{ test_user }}' and match_pattern = '{{ test_match_pattern }}' and destination_hostgroup and '{{ test_destination_hostgroup }}' and active = '{{ test_active }}' and retries = '{{ test_retries }}' and cache_ttl = '{{ test_cache_ttl }}' and cache_empty_result = '{{ test_cache_empty_result }}' and multiplex = '{{ test_multiplex }}' and next_query_flagIN = '{{ test_next_query_flagin }}'"
register: memory_result

- name: "{{ role_name }} | {{ current_test }} | check if test query rule exists on disk"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT username || ',' || match_pattern || ',' || destination_hostgroup || ',' || active || ',' || retries || ',' || cache_ttl || ',' || cache_empty_result || ',' || multiplex || ',' || next_query_flagIN FROM disk.mysql_query_rules where username = '{{ test_user }}' and match_pattern = '{{ test_match_pattern }}' and destination_hostgroup and '{{ test_destination_hostgroup }}' and active = '{{ test_active }}' and retries = '{{ test_retries }}' and cache_ttl = '{{ test_cache_ttl }}' and cache_empty_result = '{{ test_cache_empty_result }}' and multiplex = '{{ test_multiplex }}' and next_query_flagIN = '{{ test_next_query_flagin }}'"
register: disk_result

- name: "{{ role_name }} | {{ current_test }} | load the query rules config from memory to runtime"
proxysql_manage_config:
- name: "{{ role_name }} | {{ current_test }} | check if test query rule exists in runtime"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT username || ',' || match_pattern || ',' || destination_hostgroup || ',' || active || ',' || retries || ',' || cache_ttl || ',' || cache_empty_result || ',' || multiplex || ',' || next_query_flagIN FROM runtime_mysql_query_rules where username = '{{ test_user }}' and match_pattern = '{{ test_match_pattern }}' and destination_hostgroup and '{{ test_destination_hostgroup }}' and active = '{{ test_active }}' and retries = '{{ test_retries }}' and cache_ttl = '{{ test_cache_ttl }}' and cache_empty_result = '{{ test_cache_empty_result }}' and multiplex = '{{ test_multiplex }}' and next_query_flagIN = '{{ test_next_query_flagin }}'"
register: runtime_result


### when proxysql < 2

- name: test against proxysql < 2
when: not PROXYSQL2
block:
- name: "{{ role_name }} | {{ current_test }} | {{ test_delete|ternary('delete','create') }} test query rule"
proxysql_query_rules:
login_user: admin
login_password: admin
action: LOAD
config_settings: MYSQL QUERY RULES
direction: TO
config_layer: RUNTIME
username: '{{ test_user }}'
match_pattern: '{{ test_match_pattern }}'
destination_hostgroup: '{{ test_destination_hostgroup }}'
active: '{{ test_active }}'
retries: '{{ test_retries }}'
cache_ttl: '{{ test_cache_ttl }}'
multiplex: '{{ test_multiplex }}'
next_query_flagIN: '{{ test_next_query_flagin }}'
state: "{{ test_delete|ternary('absent', 'present') }}"
save_to_disk: "{{ not test_proxysql_query_rules_in_memory_only }}"
load_to_runtime: "{{ not test_proxysql_query_rules_in_memory_only }}"
check_mode: "{{ test_proxysql_query_rules_check_mode }}"
register: status1

- name: "{{ role_name }} | {{ current_test }} | persist the changes to disk, and load to runtime"
block:

- name: "{{ role_name }} | {{ current_test }} | save the query rules config from memory to disk"
proxysql_manage_config:
login_user: admin
login_password: admin
action: SAVE
config_settings: MYSQL QUERY RULES
direction: TO
config_layer: DISK

- name: "{{ role_name }} | {{ current_test }} | load the query rules config from memory to runtime"
proxysql_manage_config:
login_user: admin
login_password: admin
action: LOAD
config_settings: MYSQL QUERY RULES
direction: TO
config_layer: RUNTIME

when: test_proxysql_query_rules_with_delayed_persist
when: test_proxysql_query_rules_with_delayed_persist

- name: "{{ role_name }} | {{ current_test }} | check if test query rule exists in memory"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT username || ',' || match_pattern || ',' || destination_hostgroup || ',' || active || ',' || retries || ',' || cache_ttl || ',' || cache_empty_result || ',' || multiplex || ',' || next_query_flagIN FROM mysql_query_rules where username = '{{ test_user }}' and match_pattern = '{{ test_match_pattern }}' and destination_hostgroup and '{{ test_destination_hostgroup }}' and active = '{{ test_active }}' and retries = '{{ test_retries }}' and cache_ttl = '{{ test_cache_ttl }}' and cache_empty_result = '{{ test_cache_empty_result }}' and multiplex = '{{ test_multiplex }}' and next_query_flagIN = '{{ test_next_query_flagin }}'"
register: memory_result
- name: "{{ role_name }} | {{ current_test }} | check if test query rule exists in memory"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT username || ',' || match_pattern || ',' || destination_hostgroup || ',' || active || ',' || retries || ',' || cache_ttl || ',' || multiplex || ',' || next_query_flagIN FROM mysql_query_rules where username = '{{ test_user }}' and match_pattern = '{{ test_match_pattern }}' and destination_hostgroup and '{{ test_destination_hostgroup }}' and active = '{{ test_active }}' and retries = '{{ test_retries }}' and cache_ttl = '{{ test_cache_ttl }}' and multiplex = '{{ test_multiplex }}' and next_query_flagIN = '{{ test_next_query_flagin }}'"
register: memory_result1

- name: "{{ role_name }} | {{ current_test }} | check if test query rule exists on disk"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT username || ',' || match_pattern || ',' || destination_hostgroup || ',' || active || ',' || retries || ',' || cache_ttl || ',' || cache_empty_result || ',' || multiplex || ',' || next_query_flagIN FROM disk.mysql_query_rules where username = '{{ test_user }}' and match_pattern = '{{ test_match_pattern }}' and destination_hostgroup and '{{ test_destination_hostgroup }}' and active = '{{ test_active }}' and retries = '{{ test_retries }}' and cache_ttl = '{{ test_cache_ttl }}' and cache_empty_result = '{{ test_cache_empty_result }}' and multiplex = '{{ test_multiplex }}' and next_query_flagIN = '{{ test_next_query_flagin }}'"
register: disk_result
- name: "{{ role_name }} | {{ current_test }} | check if test query rule exists on disk"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT username || ',' || match_pattern || ',' || destination_hostgroup || ',' || active || ',' || retries || ',' || cache_ttl || ',' || multiplex || ',' || next_query_flagIN FROM disk.mysql_query_rules where username = '{{ test_user }}' and match_pattern = '{{ test_match_pattern }}' and destination_hostgroup and '{{ test_destination_hostgroup }}' and active = '{{ test_active }}' and retries = '{{ test_retries }}' and cache_ttl = '{{ test_cache_ttl }}' and multiplex = '{{ test_multiplex }}' and next_query_flagIN = '{{ test_next_query_flagin }}'"
register: disk_result1

- name: "{{ role_name }} | {{ current_test }} | check if test query rule exists in runtime"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT username || ',' || match_pattern || ',' || destination_hostgroup || ',' || active || ',' || retries || ',' || cache_ttl || ',' || cache_empty_result || ',' || multiplex || ',' || next_query_flagIN FROM runtime_mysql_query_rules where username = '{{ test_user }}' and match_pattern = '{{ test_match_pattern }}' and destination_hostgroup and '{{ test_destination_hostgroup }}' and active = '{{ test_active }}' and retries = '{{ test_retries }}' and cache_ttl = '{{ test_cache_ttl }}' and cache_empty_result = '{{ test_cache_empty_result }}' and multiplex = '{{ test_multiplex }}' and next_query_flagIN = '{{ test_next_query_flagin }}'"
register: runtime_result
- name: "{{ role_name }} | {{ current_test }} | check if test query rule exists in runtime"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT username || ',' || match_pattern || ',' || destination_hostgroup || ',' || active || ',' || retries || ',' || cache_ttl || ',' || multiplex || ',' || next_query_flagIN FROM runtime_mysql_query_rules where username = '{{ test_user }}' and match_pattern = '{{ test_match_pattern }}' and destination_hostgroup and '{{ test_destination_hostgroup }}' and active = '{{ test_active }}' and retries = '{{ test_retries }}' and cache_ttl = '{{ test_cache_ttl }}' and multiplex = '{{ test_multiplex }}' and next_query_flagIN = '{{ test_next_query_flagin }}'"
register: runtime_result1
10 changes: 10 additions & 0 deletions tests/integration/targets/test_proxysql_query_rules/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
####################################################################

### tests
- name: "{{ role_name }} | proxysql_info"
check_mode: yes
community.proxysql.proxysql_info:
login_user: admin
login_password: admin
register: proxysql_information

- name: set proxysql version fact
set_fact:
PROXYSQL2: "{{ proxysql_information.version.major >= 2 }}"

- name: "{{ role_name }} | test_create_using_check_mode | test create query rule using check mode"
import_tasks: test_create_using_check_mode.yml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
- name: "{{ role_name }} | {{ current_test }} | ensure test query rule is created when we start"
block:

- name: "{{ role_name }} | {{ current_test }} | ensure test query rule is created in memory"
- name: "{{ role_name }} | {{ current_test }} | ensure test query rule is created in memory proxysql >= 2"
when: PROXYSQL2
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"INSERT OR REPLACE INTO mysql_query_rules (username, match_pattern, destination_hostgroup, active, retries, cache_ttl, cache_empty_result, multiplex, next_query_flagIN) VALUES ('{{ test_user }}', '{{ test_match_pattern}}', '{{ test_destination_hostgroup }}', '{{ test_active }}', '{{ test_retries }}', '{{ test_cache_ttl }}', '{{ test_cache_empty_result }}', '{{ test_multiplex }}', '{{ test_next_query_flagin }}')"

- name: "{{ role_name }} | {{ current_test }} | ensure test query rule is created in memory proxysql < 2"
when: not PROXYSQL2
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"INSERT OR REPLACE INTO mysql_query_rules (username, match_pattern, destination_hostgroup, active, retries, cache_ttl, multiplex, next_query_flagIN) VALUES ('{{ test_user }}', '{{ test_match_pattern}}', '{{ test_destination_hostgroup }}', '{{ test_active }}', '{{ test_retries }}', '{{ test_cache_ttl }}', '{{ test_multiplex }}', '{{ test_next_query_flagin }}')"

- name: "{{ role_name }} | {{ current_test }} | ensure test query rule is created on disk"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SAVE MYSQL QUERY RULES TO DISK"

Expand Down
Loading

0 comments on commit 7b7c5eb

Please sign in to comment.