Add tests for undefined variable detection

Tests `test_playbook_undefined_varsX_fail` check if ansible detects
undefined variables when `error_on_undefined_vars` is enabled. These
tests fail without "Improve behavior with error_on_undefined_vars
enabled" patch.

Tests `test_playbook_undefined_varsX_ignore` check if ansible ignores
undefined variables when `error_on_undefined_vars` is disabled.

Also modify PlayBook._run_task_internal() so error_on_undefined_vars is
testable.
This commit is contained in:
Petr Svoboda 2013-07-22 16:23:19 +02:00 committed by James Cammarata
parent fff4f1da33
commit e3adfbf5f8
5 changed files with 67 additions and 1 deletions

View file

@ -312,7 +312,8 @@ class PlayBook(object):
conditional=task.only_if, callbacks=self.runner_callbacks,
sudo=task.sudo, sudo_user=task.sudo_user,
transport=task.transport, sudo_pass=task.sudo_pass, is_playbook=True,
check=self.check, diff=self.diff, environment=task.environment, complex_args=task.args
check=self.check, diff=self.diff, environment=task.environment, complex_args=task.args,
error_on_undefined_vars=C.DEFAULT_UNDEFINED_VAR_BEHAVIOR
)
if task.async_seconds == 0:

View file

@ -266,6 +266,52 @@ class TestPlaybook(unittest.TestCase):
)
playbook.run()
def _test_playbook_undefined_vars(self, playbook, fail_on_undefined):
# save DEFAULT_UNDEFINED_VAR_BEHAVIOR so we can restore it in the end of the test
saved_undefined_var_behavior = C.DEFAULT_UNDEFINED_VAR_BEHAVIOR
C.DEFAULT_UNDEFINED_VAR_BEHAVIOR = fail_on_undefined
test_callbacks = TestCallbacks()
playbook = ansible.playbook.PlayBook(
playbook=os.path.join(self.test_dir, 'test_playbook_undefined_vars', playbook),
host_list='test/test_playbook_undefined_vars/hosts',
stats=ans_callbacks.AggregateStats(),
callbacks=test_callbacks,
runner_callbacks=test_callbacks
)
actual = playbook.run()
C.DEFAULT_UNDEFINED_VAR_BEHAVIOR = saved_undefined_var_behavior
# if different, this will output to screen
print "**ACTUAL**"
print utils.jsonify(actual, format=True)
expected = {
"localhost": {
"changed": 0,
"failures": 0,
"ok": int(not fail_on_undefined) + 1,
"skipped": 0,
"unreachable": int(fail_on_undefined)
}
}
print "**EXPECTED**"
print utils.jsonify(expected, format=True)
assert utils.jsonify(expected, format=True) == utils.jsonify(actual, format=True)
def test_playbook_undefined_vars1_ignore(self):
self._test_playbook_undefined_vars('playbook1.yml', False)
def test_playbook_undefined_vars1_fail(self):
self._test_playbook_undefined_vars('playbook1.yml', True)
def test_playbook_undefined_vars2_ignore(self):
self._test_playbook_undefined_vars('playbook2.yml', False)
def test_playbook_undefined_vars2_fail(self):
self._test_playbook_undefined_vars('playbook2.yml', True)
def test_yaml_hosts_list(self):
# Make sure playbooks support hosts: [host1, host2]
# TODO: Actually run the play on more than one host

View file

@ -0,0 +1,5 @@
localhost
[all:vars]
inventory_var_good="{{ playbook_var_good }}"
inventory_var_bad="{{ playbook_var_bad }}"

View file

@ -0,0 +1,7 @@
- hosts: all
vars:
playbook_var_good: "ok"
playbook_var_bad: "{{ undefined_var }}"
tasks:
- debug: msg="{{ playbook_var_good }}"
- debug: msg="{{ playbook_var_bad }}"

View file

@ -0,0 +1,7 @@
- hosts: all
vars:
playbook_var_good: "ok"
playbook_var_bad: "{{ undefined_var }}"
tasks:
- debug: msg="{{ inventory_var_good }}"
- debug: msg="{{ inventory_var_bad }}"