From b58c64e3b13e1e3ed9cfcf1f5dbdecb0d08d3289 Mon Sep 17 00:00:00 2001 From: Pilou Date: Wed, 10 Jul 2019 19:15:19 +0000 Subject: [PATCH] consul_session: ensure empty result is handled (#58694) --- ...session_ensure_empty_result_is_handled.yml | 2 ++ .../modules/clustering/consul_session.py | 2 +- .../targets/consul/tasks/consul_session.yml | 24 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/58694-consul_session_ensure_empty_result_is_handled.yml diff --git a/changelogs/fragments/58694-consul_session_ensure_empty_result_is_handled.yml b/changelogs/fragments/58694-consul_session_ensure_empty_result_is_handled.yml new file mode 100644 index 00000000000..d7f8e8fc63e --- /dev/null +++ b/changelogs/fragments/58694-consul_session_ensure_empty_result_is_handled.yml @@ -0,0 +1,2 @@ +bugfixes: + - consul_session - ``sessions`` returned value is a list even though no sessions were found diff --git a/lib/ansible/modules/clustering/consul_session.py b/lib/ansible/modules/clustering/consul_session.py index c6911a4596a..f7f0f7a9749 100644 --- a/lib/ansible/modules/clustering/consul_session.py +++ b/lib/ansible/modules/clustering/consul_session.py @@ -162,7 +162,7 @@ def lookup_sessions(module): if state == 'list': sessions_list = consul_client.session.list(dc=datacenter) # Ditch the index, this can be grabbed from the results - if sessions_list and sessions_list[1]: + if sessions_list and len(sessions_list) >= 2: sessions_list = sessions_list[1] module.exit_json(changed=True, sessions=sessions_list) diff --git a/test/integration/targets/consul/tasks/consul_session.yml b/test/integration/targets/consul/tasks/consul_session.yml index f50fe0f7af6..f1006642249 100644 --- a/test/integration/targets/consul/tasks/consul_session.yml +++ b/test/integration/targets/consul/tasks/consul_session.yml @@ -91,3 +91,27 @@ - assert: that: - result is changed + +- name: list sessions after deletion + consul_session: + state: list + register: result + +- assert: + that: + - result is changed + # selectattr and equalto not available on Jinja 2.2 provided by CentOS 6 + # hence the two following tasks (command/assert) are used + # - (result['sessions'] | selectattr('ID', 'equalto', session_id) | list | length) == 0 + +- name: search deleted session + command: echo 'session found' + loop: "{{ result['sessions'] }}" + when: "item.get('ID') == session_id and item.get('Name') == 'testsession'" + register: search_deleted + +- name: ensure session was deleted + assert: + that: + - search_deleted is success + - search_deleted is not changed