Debian Codegraph @

test/units/plugins/action/test_raw.py

raw
1 # (c) 2016, Saran Ahluwalia <[email protected]>
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import annotations
19
20 import unittest
21 from unittest.mock import MagicMock, Mock
22 from ansible.plugins.action.raw import ActionModule
23 from ansible.playbook.task import Task
24 from ansible.plugins.loader import connection_loader
25
26
27 class TestCopyResultExclude(unittest.TestCase):
28
29 def setUp(self):
30 self.play_context = Mock()
31 self.play_context.shell = 'sh'
32 self.connection = connection_loader.get('local', self.play_context)
33
34 def tearDown(self):
35 pass
36
37 def _build_task(self):
38
39 task = MagicMock(Task)
40 task.async_val = False
41 task.diff = False
42 task.check_mode = False
43 task.environment = None
44 task.args = {'_raw_params': 'Args1'}
45
46 return task
47 # The current behavior of the raw action in regards to executable is currently in question;
48 # the test_raw_executable_is_not_empty_string verifies the current behavior (whether it is desired or not).
49 # Please refer to the following for context:
50 # Issue: https://github.com/ansible/ansible/issues/16054
51 # PR: https://github.com/ansible/ansible/pull/16085
52
53 def test_raw_executable_is_not_empty_string(self):
54
55 task = self._build_task()
56
57 self.mock_am = ActionModule(task, self.connection, self.play_context, loader=None, templar=None, shared_loader_obj=None)
58 self.mock_am._low_level_execute_command = Mock(return_value={})
59 self.mock_am.display = Mock()
60 self.mock_am._admin_users = ['root', 'toor']
61
62 self.mock_am.run()
63 self.mock_am._low_level_execute_command.assert_called_with('Args1', executable=False)
64
65 def test_raw_check_mode_is_True(self):
66
67 task = self._build_task()
68 task.check_mode = True
69
70 self.mock_am = ActionModule(task, self.connection, self.play_context, loader=None, templar=None, shared_loader_obj=None)
71
72 def test_raw_test_environment_is_None(self):
73
74 task = self._build_task()
75
76 self.mock_am = ActionModule(task, self.connection, self.play_context, loader=None, templar=None, shared_loader_obj=None)
77 self.mock_am._low_level_execute_command = Mock(return_value={})
78 self.mock_am.display = Mock()
79
80 self.assertEqual(task.environment, None)
81
82 def test_raw_task_vars_is_not_None(self):
83
84 task = self._build_task()
85
86 self.mock_am = ActionModule(task, self.connection, self.play_context, loader=None, templar=None, shared_loader_obj=None)
87 self.mock_am._low_level_execute_command = Mock(return_value={})
88 self.mock_am.display = Mock()
89
90 self.mock_am.run(task_vars={'a': 'b'})
91 self.assertEqual(task.environment, None)