Integration tests for import/include (#33418)

* First pass at include_role tests

* Reorganize test structure

Do all import and include tests in a single target.

* Build out more tests and test with linear and free strategy for each type

* import_role tests

* Set target host for play test

* Basic import_playbook tests

* Basic import/include_tasks tests

* Add recursion test

* import_playbook tests

* Add import_playbook group_vars test

Issue #33177

* Additional group_var tests for import_playbook

* Enable recursion test

* More work on import_tasks

* Run all tests via run.sh, improve conditional tests, add never tag

Add never tag to certain failining tests and explicitly skip it for now until #34104 is merged. Some tasks need to remain commented out because they cause the entire play to fail and aren't skippable by tags because they are syntax and/or variable checks.

Improve test for whether or not a role was run based on a conditional. Since the [import|include]_role does not register a variable, use a variable that is set inside a role as a canary test for whether or not the role was run.

* Use a fail task rather than trigger a failure via bogus command

This should resolve the "unstable" test results.

* Import tag tests

Fix bug it tests where validate tasks weren't run on tag tests.
Add tests for task import/include with tags.

* Remove test for playbook group_var inheritance
This commit is contained in:
Sam Doran 2018-01-19 11:23:48 -05:00 committed by Toshio Kuratomi
parent 2697c0e237
commit 10a8c6bc25
62 changed files with 650 additions and 3 deletions

View file

@ -1,5 +1,5 @@
[local]
testhost ansible_ssh_host=127.0.0.1 ansible_connection=local
testhost ansible_ssh_host=127.0.0.1 ansible_connection=local host_var_role_name=role3
testhost2 ansible_ssh_host=127.0.0.1 ansible_connection=local
# For testing delegate_to
testhost3 ansible_ssh_host=127.0.0.3

View file

@ -0,0 +1 @@
posix/ci/group3

View file

@ -0,0 +1 @@
group_var1: set in group_vars/all.yml

View file

@ -0,0 +1,9 @@
- name: Playbook 1
hosts: testhost2
tasks:
- name: Set fact in playbook 1
set_fact:
canary_var1: playbook1 imported
tags:
- canary1

View file

@ -0,0 +1,9 @@
- name: Playbook 2
hosts: testhost2
tasks:
- name: Set fact in playbook 2
set_fact:
canary_var2: playbook2 imported
tags:
- canary2

View file

@ -0,0 +1,10 @@
- name: Playbook 3
hosts: testhost2
tasks:
- name: Set fact in playbook 3
set_fact:
canary_var3: playbook3 imported
include_next_playbook: yes
tags:
- canary3

View file

@ -0,0 +1,9 @@
- name: Playbook 4
hosts: testhost2
tasks:
- name: Set fact in playbook 4
set_fact:
canary_var4: playbook4 imported
tags:
- canary4

View file

@ -0,0 +1,16 @@
# Test and validate playbook import
- import_playbook: playbook1.yml
- import_playbook: validate1.yml
# Test and validate conditional import
- import_playbook: playbook2.yml
when: no
- import_playbook: validate2.yml
- import_playbook: playbook3.yml
- import_playbook: playbook4.yml
when: include_next_playbook
- import_playbook: validate34.yml

View file

@ -0,0 +1,10 @@
- import_playbook: playbook1.yml # Test tag in tasks in included play
- import_playbook: playbook2.yml # Test tag added to import_playbook
tags:
- canary22
- import_playbook: playbook3.yml # Test skipping tags added to import_playbook
tags:
- skipme
- import_playbook: validate_tags.yml # Validate

View file

@ -0,0 +1,10 @@
- hosts: testhost2
tasks:
- name: Assert that variable was set in playbook1.yml
assert:
that:
- canary_var1 == 'playbook1 imported'
tags:
- validate
- validate1

View file

@ -0,0 +1,10 @@
- hosts: testhost2
tasks:
- name: Assert that playbook2.yml was skipeed
assert:
that:
- canary_var2 is not defined
tags:
- validate
- validate2

View file

@ -0,0 +1,11 @@
- hosts: testhost2
tasks:
- name: Assert that playbook3.yml and playbook4.yml were imported
assert:
that:
- canary_var3 == 'playbook3 imported'
- canary_var4 == 'playbook4 imported'
tags:
- validate
- validate34

View file

@ -0,0 +1,11 @@
- hosts: testhost2
tasks:
- name: Assert that only tasks with tags were run
assert:
that:
- canary_var1 == 'playbook1 imported'
- canary_var2 == 'playbook2 imported'
- canary_var3 is not defined
tags:
- validate

View file

@ -0,0 +1,138 @@
- name: Test import_role
hosts: testhost
vars:
run_role: yes
do_not_run_role: no
role_name: role1
test_var: templating test in playbook
role_vars:
where_am_i_defined: in the playbook
entire_task:
include_role:
name: role1
tasks:
- name: Test basic role import
import_role:
name: role1
- name: Assert that basic include works
assert:
that:
- _role1_result.msg == 'In role1'
- name: Test conditional role include
import_role:
name: role1
tasks_from: canary1.yml
when: run_role
- name: Assert that role ran
assert:
that:
- role1_canary1 == 'r1c1'
- name: Test conditional role import that should be skipped
import_role:
name: role1
tasks_from: canary2.yml
when: do_not_run_role
- name: Assert that role did not run
assert:
that:
- role1_canary2 is not defined
# FIXME We expect this to fail, but I'm not sure how best to test for
# syntax level failures.
#
# - name: Test role import with a loop
# import_role:
# name: "{{ item }}"
# register: loop_test
# with_items:
# - role1
# - role3
# - role2
- name: Test importing a task file from a role
import_role:
name: role1
tasks_from: tasks.yml
- name: Test importing vars file and tasks file from a role
import_role:
name: role3
tasks_from: vartest.yml
vars_from: role3vars.yml
private: no
# FIXME Setting private: no in previous task does not make the variables
# available to the play
#
- name: Assert that variables defined in previous task are available to play
assert:
that:
- role3_default == 'defined in role3/defaults/main.yml'
- role3_main == 'defined in role3/vars/main.yml'
- role3_var == 'defined in role3/vars/role3vars.yml'
ignore_errors: yes
- name: Test using a play variable for role name
import_role:
name: "{{ role_name }}"
# FIXME Trying to use a host_var here causes play execution to fail because
# the variable is undefined.
#
# - name: Test using a host variable for role name
# import_role:
# name: "{{ host_var_role_name }}"
- name: Pass variable to role
import_role:
name: role1
tasks_from: vartest.yml
vars:
where_am_i_defined: in the task
## FIXME Currently failing
## ERROR! Vars in a IncludeRole must be specified as a dictionary, or a list of dictionaries
# - name: Pass all variables in a variable to role
# import_role:
# name: role1
# tasks_from: vartest.yml
# vars: "{{ role_vars }}"
- name: Pass templated variable to a role
import_role:
name: role1
tasks_from: vartest.yml
vars:
where_am_i_defined: "{{ test_var }}"
# FIXME This fails with the following error:
# The module {u'import_role': {u'name': u'role1'}} was not found in configured module paths.
#
- name: Include an entire task
action:
module: "{{ entire_task }}"
tags:
- never
- block:
- name: Include a role that will fail
import_role:
name: role1
tasks_from: fail.yml
rescue:
- name: Include a role inside rescue
import_role:
name: role2
always:
- name: Include role inside always
import_role:
name: role3

View file

@ -0,0 +1,128 @@
- name: Test include_role
hosts: testhost
vars:
run_role: yes
do_not_run_role: no
role_name: role1
test_var: templating test in playbook
role_vars:
where_am_i_defined: in the playbook
entire_task:
include_role:
name: role1
tasks:
- name: Test basic role include
include_role:
name: role1
- name: Assert that basic include works
assert:
that:
- _role1_result.msg == 'In role1'
- name: Test conditional role include
include_role:
name: role1
tasks_from: canary1.yml
when: run_role
- name: Assert that role ran
assert:
that:
- role1_canary1 == 'r1c1'
- name: Test conditional role include that should be skipped
include_role:
name: role1
tasks_from: canary2.yml
when: do_not_run_role
- name: Assert that role did not run
assert:
that:
- role1_canary2 is not defined
- name: Test role include with a loop
include_role:
name: "{{ item }}"
register: loop_test
with_items:
- role1
- role3
- role2
- name: Test including a task file from a role
include_role:
name: role1
tasks_from: tasks.yml
- name: Test including vars file and tasks file from a role
include_role:
name: role3
tasks_from: vartest.yml
vars_from: role3vars.yml
private: no
# FIXME Setting private: no in previous task does not make the variables
# available to the play
- name: Assert that variables defined in previous task are available to play
assert:
that:
- role3_default == 'defined in role3/defaults/main.yml'
- role3_main == 'defined in role3/vars/main.yml'
- role3_var == 'defined in role3/vars/role3vars.yml'
ignore_errors: yes
- name: Test using a play variable for role name
include_role:
name: "{{ role_name }}"
- name: Test using a host variable for role name
include_role:
name: "{{ host_var_role_name }}"
- name: Pass variable to role
include_role:
name: role1
tasks_from: vartest.yml
vars:
where_am_i_defined: in the task
## FIXME Currently failing with
## ERROR! Vars in a IncludeRole must be specified as a dictionary, or a list of dictionaries
# - name: Pass all variables in a variable to role
# include_role:
# name: role1
# tasks_from: vartest.yml
# vars: "{{ role_vars }}"
- name: Pass templated variable to a role
include_role:
name: role1
tasks_from: vartest.yml
vars:
where_am_i_defined: "{{ test_var }}"
## FIXME This fails with the following error:
## The module {u'include_role': {u'name': u'role1'}} was not found in configured module paths.
# - name: Include an entire task
# action:
# module: "{{ entire_task }}"
- block:
- name: Include a role that will fail
include_role:
name: role1
tasks_from: fail.yml
rescue:
- name: Include a role inside rescue
include_role:
name: role2
always:
- name: Include role inside always
include_role:
name: role3

View file

@ -0,0 +1,2 @@
- set_fact:
role1_canary1: r1c1

View file

@ -0,0 +1,2 @@
- set_fact:
role1_canary2: r1c2

View file

@ -0,0 +1,3 @@
- name: EXPECTED FAILURE
fail:
msg: This command should always fail

View file

@ -0,0 +1,3 @@
- debug:
msg: In role1
register: _role1_result

View file

@ -0,0 +1 @@
- import_tasks: r1t02.yml

View file

@ -0,0 +1 @@
- import_tasks: r1t03.yml

View file

@ -0,0 +1 @@
- import_tasks: r1t04.yml

View file

@ -0,0 +1 @@
- import_tasks: r1t05.yml

View file

@ -0,0 +1 @@
- import_tasks: r1t06.yml

View file

@ -0,0 +1 @@
- import_tasks: r1t07.yml

View file

@ -0,0 +1 @@
- import_tasks: r1t08.yml

View file

@ -0,0 +1 @@
- import_tasks: r1t09.yml

View file

@ -0,0 +1 @@
- import_tasks: r1t10.yml

View file

@ -0,0 +1 @@
- import_tasks: r1t11.yml

View file

@ -0,0 +1 @@
- import_tasks: r1t12.yml

View file

@ -0,0 +1,2 @@
- debug:
msg: r1t12

View file

@ -0,0 +1,2 @@
- debug:
msg: Tasks file inside role1

View file

@ -0,0 +1,2 @@
- debug:
var: where_am_i_defined

View file

@ -0,0 +1 @@
where_am_i_defined: role1 vars/main.yml

View file

@ -0,0 +1 @@
where_am_i_defined: role1 vars/main.yml

View file

@ -0,0 +1,2 @@
- debug:
msg: In role2

View file

@ -0,0 +1,2 @@
where_am_i_defined: defaults in role3
role3_default: defined in role3/defaults/main.yml

View file

@ -0,0 +1,3 @@
- name: runme
debug:
msg: role3 handler

View file

@ -0,0 +1,2 @@
- debug:
msg: In role3

View file

@ -0,0 +1,2 @@
- debug:
msg: Tasks file inside role3

View file

@ -0,0 +1,2 @@
- debug:
var: role3_var

View file

@ -0,0 +1 @@
role3_main: defined in role3/vars/main.yml

View file

@ -0,0 +1,2 @@
where_am_i_defined: role3vars.yml
role3_var: defined in role3/vars/role3vars.yml

View file

@ -0,0 +1,3 @@
dependencies:
- role1
- role2

View file

@ -0,0 +1,2 @@
- debug:
msg: In role_with_deps

View file

@ -0,0 +1,38 @@
#!/usr/bin/env bash
set -eux
export ANSIBLE_ROLES_PATH=./roles
## Import (static)
# Playbook
ANSIBLE_STRATEGY='linear' ansible-playbook playbook/test_import_playbook.yml -i ../../inventory "$@" --skip-tags never
ANSIBLE_STRATEGY='free' ansible-playbook playbook/test_import_playbook.yml -i ../../inventory "$@" --skip-tags never
ANSIBLE_STRATEGY='linear' ansible-playbook playbook/test_import_playbook_tags.yml -i ../../inventory "$@" --tags canary1,canary22,validate --skip-tags skipme,never
# Tasks
ANSIBLE_STRATEGY='linear' ansible-playbook tasks/test_import_tasks.yml -i ../../inventory "$@" --skip-tags never
ANSIBLE_STRATEGY='free' ansible-playbook tasks/test_import_tasks.yml -i ../../inventory "$@" --skip-tags never
ANSIBLE_STRATEGY='free' ansible-playbook tasks/test_import_tasks_tags.yml -i ../../inventory "$@" --tags tasks1,canary1,validate --skip-tags never
# Role
ANSIBLE_STRATEGY='linear' ansible-playbook role/test_import_role.yml -i ../../inventory "$@" --skip-tags never
ANSIBLE_STRATEGY='free' ansible-playbook role/test_import_role.yml -i ../../inventory "$@" --skip-tags never
## Include (dynamic)
# Tasks
ANSIBLE_STRATEGY='linear' ansible-playbook tasks/test_include_tasks.yml -i ../../inventory "$@" --skip-tags never
ANSIBLE_STRATEGY='free' ansible-playbook tasks/test_include_tasks.yml -i ../../inventory "$@" --skip-tags never
ANSIBLE_STRATEGY='free' ansible-playbook tasks/test_include_tasks_tags.yml -i ../../inventory "$@" --tags tasks1,canary1,validate --skip-tags never
# Role
ANSIBLE_STRATEGY='linear' ansible-playbook role/test_include_role.yml -i ../../inventory "$@" --skip-tags never
ANSIBLE_STRATEGY='free' ansible-playbook role/test_include_role.yml -i ../../inventory "$@" --skip-tags never
## Recursion
# https://github.com/ansible/ansible/issues/23609
ANSIBLE_STRATEGY='linear' ansible-playbook test_recursion.yml -i ../../inventory "$@" --skip-tags never

View file

@ -0,0 +1,5 @@
- name: Set variable inside tasks1.yml
set_fact:
set_in_tasks1: yes
tags:
- tasks1

View file

@ -0,0 +1,5 @@
- name: Set variable inside tasks2.yml
set_fact:
set_in_tasks2: yes
tags:
- tasks2

View file

@ -0,0 +1,5 @@
- name: Set variable inside tasks3.yml
set_fact:
set_in_tasks3: yes
tags:
- tasks3

View file

@ -0,0 +1,5 @@
- name: Set variable inside tasks4.yml
set_fact:
set_in_tasks4: yes
tags:
- tasks4

View file

@ -0,0 +1,6 @@
- name: Set variable inside tasks5.yml
set_fact:
set_in_tasks5: yes
tags:
- tasks5
- canary1

View file

@ -0,0 +1,5 @@
- name: Set variable inside tasks6.yml
set_fact:
set_in_tasks6: yes
tags:
- tasks6

View file

@ -0,0 +1,41 @@
- name: Test import_tasks
hosts: testhost
tasks:
- name: Test basic task import
import_tasks: tasks1.yml
- name: Assert that fact was set in import
assert:
that:
- set_in_tasks1
- name: Test conditional task import
import_tasks: tasks2.yml
when: no
- name: Assert that tasks were skipped
assert:
that:
- set_in_tasks2 is not defined
- block:
- name: Import tasks inside a block
import_tasks: tasks3.yml
- name: Assert that task3 was included
assert:
that:
- set_in_tasks3
always:
- name: Import task inside always
import_tasks: tasks4.yml
- name: Validate that variables set in previously improted tasks are passed down.
import_tasks: validate3.yml
- name: Assert that tasks4 was included
assert:
that:
- set_in_tasks4

View file

@ -0,0 +1,23 @@
- name: Test import_tasks using tags
hosts: testhost
tasks:
- name: Import tasks1.yml
import_tasks: tasks1.yml
- name: Import tasks4.yml using tag on import task
import_tasks: tasks4.yml
tags:
- canary1
- name: Import tasks2.yml
import_tasks: tasks2.yml
- name: Assert that appropriate tasks were run
assert:
that:
- set_in_tasks1
- set_in_tasks4
- set_in_tasks2 is not defined
tags:
- validate

View file

@ -0,0 +1,41 @@
- name: Test include_tasks
hosts: testhost
tasks:
- name: Test basic task include
include_tasks: tasks1.yml
- name: Assert that fact was set in include
assert:
that:
- set_in_tasks1
- name: Test conditional task include
include_tasks: tasks2.yml
when: no
- name: Assert that tasks were skipped
assert:
that:
- set_in_tasks2 is not defined
- block:
- name: Include tasks inside a block
include_tasks: tasks3.yml
- name: Assert that task3 was included
assert:
that:
- set_in_tasks3
always:
- name: Include task inside always
include_tasks: tasks4.yml
- name: Validate that variables set in previously improted tasks are passed down
include_tasks: validate3.yml
- name: Assert that tasks4 was included
assert:
that:
- set_in_tasks4

View file

@ -0,0 +1,25 @@
- name: Test include_tasks using tags
hosts: testhost
tasks:
# This should not be included
- name: Include tasks1.yml
include_tasks: tasks1.yml
# This should be included but tasks inside should not run because they do not have
# the canary1 tag and tasks2 is not in the list of tags for the ansible-playbook command
- name: Include tasks2.yml
include_tasks: tasks2.yml
tags:
- canary1
# This should be included and tasks inside should be run
- name: Include tasks5.yml using tag on include task
include_tasks: tasks5.yml
tags:
- canary1
- name: Include validate_tags.yml
include_tasks: validate_tags.yml
tags:
- validate

View file

@ -0,0 +1,6 @@
- hosts: testhost
tasks:
- include_role:
name: role
tasks_from: r1t1.yml

View file

@ -0,0 +1,4 @@
- name: Assert than variable set in previously included task is defined
assert:
that:
- set_in_tasks3

View file

@ -0,0 +1,8 @@
- name: Assert that appropriate tasks were run
assert:
that:
- set_in_tasks1 is undefined
- set_in_tasks2 is undefined
- set_in_tasks5
tags:
- validate

View file

@ -0,0 +1,7 @@
- name: Test max recursion depth
hosts: testhost
tasks:
- import_role:
name: role1
tasks_from: r1t01.yml

View file

@ -10,7 +10,8 @@
hosts: testhost
gather_facts: True
roles:
- { role: test_includes, tags: test_includes }
- role: test_includes
tags: test_includes
tasks:
- include: roles/test_includes/tasks/not_a_role_task.yml
- include: roles/test_includes/tasks/empty.yml

View file

@ -1,4 +1,3 @@
---
- hosts: localhost
tasks:
- include: test_includes4.yml