From f9c74d6e571405d942a96abfd383df84560aae26 Mon Sep 17 00:00:00 2001 From: Chris Church Date: Tue, 6 May 2014 22:30:37 -0400 Subject: [PATCH] Add integration tests for group_by module. Fixed bug introduced by ansible/ansible#7273 while also fixing issue described in ansible/ansible#6953 --- lib/ansible/runner/action_plugins/group_by.py | 13 +- test/integration/Makefile | 5 +- test/integration/inventory.group_by | 6 + test/integration/test_group_by.yml | 111 ++++++++++++++++++ 4 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 test/integration/inventory.group_by create mode 100644 test/integration/test_group_by.yml diff --git a/lib/ansible/runner/action_plugins/group_by.py b/lib/ansible/runner/action_plugins/group_by.py index 110f0eafe6e..63e72711aa6 100644 --- a/lib/ansible/runner/action_plugins/group_by.py +++ b/lib/ansible/runner/action_plugins/group_by.py @@ -38,6 +38,8 @@ class ActionModule(object): # the group_by module does not need to pay attention to check mode. # it always runs. + # module_args and complex_args have already been templated for the first host. + # Use them here only to check that a key argument is provided. args = {} if complex_args: args.update(complex_args) @@ -68,7 +70,16 @@ class ActionModule(object): break if next_host: continue - group_name = template.template(self.runner.basedir, args['key'], data) + + # Template original module_args and complex_args from runner for each host. + host_module_args = template.template(self.runner.basedir, self.runner.module_args, data) + host_complex_args = template.template(self.runner.basedir, self.runner.complex_args, data) + host_args = {} + if host_complex_args: + host_args.update(host_complex_args) + host_args.update(parse_kv(host_module_args)) + + group_name = host_args['key'] group_name = group_name.replace(' ','-') if group_name not in groups: groups[group_name] = [] diff --git a/test/integration/Makefile b/test/integration/Makefile index d53201d18b3..19e087e75be 100644 --- a/test/integration/Makefile +++ b/test/integration/Makefile @@ -14,7 +14,7 @@ else CREDENTIALS_ARG = endif -all: non_destructive destructive check_mode test_hash test_handlers +all: non_destructive destructive check_mode test_hash test_handlers test_group_by non_destructive: ansible-playbook non_destructive.yml -i $(INVENTORY) -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v $(TEST_FLAGS) @@ -25,6 +25,9 @@ destructive: check_mode: ansible-playbook check_mode.yml -i $(INVENTORY) -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v --check $(TEST_FLAGS) +test_group_by: + ansible-playbook test_group_by.yml -i inventory.group_by -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v $(TEST_FLAGS) + test_handlers: ansible-playbook test_handlers.yml -i inventory.handlers -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v $(TEST_FLAGS) diff --git a/test/integration/inventory.group_by b/test/integration/inventory.group_by new file mode 100644 index 00000000000..d8d285dee52 --- /dev/null +++ b/test/integration/inventory.group_by @@ -0,0 +1,6 @@ +[lamini] +alpaca genus=vicugna +llama genus=lama + +[lamini:vars] +ansible_connection=local diff --git a/test/integration/test_group_by.yml b/test/integration/test_group_by.yml new file mode 100644 index 00000000000..6385c1f5ad7 --- /dev/null +++ b/test/integration/test_group_by.yml @@ -0,0 +1,111 @@ +# test code for the group_by module +# (c) 2014, Chris Church + +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +- hosts: lamini + gather_facts: false + tasks: + - name: group by genus + group_by: key={{ genus }} + - name: group by first three letters of genus with key in quotes + group_by: key="{{ genus | truncate(3, true, '') }}" + - name: group by first two letters of genus with key not in quotes + group_by: key={{ genus | truncate(2, true, '') }} + - name: group by genus in uppercase using complex args + group_by: { key: "{{ genus | upper() }}" } + +- hosts: vicugna + gather_facts: false + tasks: + - name: verify that only the alpaca is in this group + assert: { that: "inventory_hostname == 'alpaca'" } + - name: set a fact to check that we ran this play + set_fact: genus_vicugna=true + +- hosts: lama + gather_facts: false + tasks: + - name: verify that only the llama is in this group + assert: { that: "inventory_hostname == 'llama'" } + - name: set a fact to check that we ran this play + set_fact: genus_lama=true + +- hosts: vic + gather_facts: false + tasks: + - name: verify that only the alpaca is in this group + assert: { that: "inventory_hostname == 'alpaca'" } + - name: set a fact to check that we ran this play + set_fact: genus_vic=true + +- hosts: lam + gather_facts: false + tasks: + - name: verify that only the llama is in this group + assert: { that: "inventory_hostname == 'llama'" } + - name: set a fact to check that we ran this play + set_fact: genus_lam=true + +- hosts: vi + gather_facts: false + tasks: + - name: verify that only the alpaca is in this group + assert: { that: "inventory_hostname == 'alpaca'" } + - name: set a fact to check that we ran this play + set_fact: genus_vi=true + +- hosts: la + gather_facts: false + tasks: + - name: verify that only the llama is in this group + assert: { that: "inventory_hostname == 'llama'" } + - name: set a fact to check that we ran this play + set_fact: genus_la=true + +- hosts: VICUGNA + gather_facts: false + tasks: + - name: verify that only the alpaca is in this group + assert: { that: "inventory_hostname == 'alpaca'" } + - name: set a fact to check that we ran this play + set_fact: genus_VICUGNA=true + +- hosts: LAMA + gather_facts: false + tasks: + - name: verify that only the llama is in this group + assert: { that: "inventory_hostname == 'llama'" } + - name: set a fact to check that we ran this play + set_fact: genus_LAMA=true + +- hosts: '{{genus' + gather_facts: false + tasks: + - name: no hosts should match this group + fail: msg="should never get here" + +- hosts: alpaca + gather_facts: false + tasks: + - name: check that alpaca matched all four groups + assert: { that: ["genus_vicugna", "genus_vic", "genus_vi", "genus_VICUGNA"] } + +- hosts: llama + gather_facts: false + tasks: + - name: check that llama matched all four groups + assert: { that: ["genus_lama", "genus_lam", "genus_la", "genus_LAMA"] }