From e3adfbf5f8b58279770811436cfb64956f04c5c6 Mon Sep 17 00:00:00 2001 From: Petr Svoboda Date: Mon, 22 Jul 2013 16:23:19 +0200 Subject: [PATCH] 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. --- lib/ansible/playbook/__init__.py | 3 +- test/TestPlayBook.py | 46 +++++++++++++++++++ test/test_playbook_undefined_vars/hosts | 5 ++ .../playbook1.yml | 7 +++ .../playbook2.yml | 7 +++ 5 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 test/test_playbook_undefined_vars/hosts create mode 100644 test/test_playbook_undefined_vars/playbook1.yml create mode 100644 test/test_playbook_undefined_vars/playbook2.yml diff --git a/lib/ansible/playbook/__init__.py b/lib/ansible/playbook/__init__.py index 6bb0921345..68e94d226b 100644 --- a/lib/ansible/playbook/__init__.py +++ b/lib/ansible/playbook/__init__.py @@ -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: diff --git a/test/TestPlayBook.py b/test/TestPlayBook.py index a34de0ae3c..ac65ce5efc 100644 --- a/test/TestPlayBook.py +++ b/test/TestPlayBook.py @@ -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 diff --git a/test/test_playbook_undefined_vars/hosts b/test/test_playbook_undefined_vars/hosts new file mode 100644 index 0000000000..3ca87c2c1e --- /dev/null +++ b/test/test_playbook_undefined_vars/hosts @@ -0,0 +1,5 @@ +localhost + +[all:vars] +inventory_var_good="{{ playbook_var_good }}" +inventory_var_bad="{{ playbook_var_bad }}" diff --git a/test/test_playbook_undefined_vars/playbook1.yml b/test/test_playbook_undefined_vars/playbook1.yml new file mode 100644 index 0000000000..7f46d704cc --- /dev/null +++ b/test/test_playbook_undefined_vars/playbook1.yml @@ -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 }}" diff --git a/test/test_playbook_undefined_vars/playbook2.yml b/test/test_playbook_undefined_vars/playbook2.yml new file mode 100644 index 0000000000..08c8c28e19 --- /dev/null +++ b/test/test_playbook_undefined_vars/playbook2.yml @@ -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 }}"