Debian Codegraph @

test/units/plugins/strategy/test_linear.py

raw
1 # Copyright (c) 2018 Ansible Project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 from __future__ import annotations
5
6
7 import unittest
8 from unittest.mock import patch, MagicMock
9
10 from ansible.executor.play_iterator import PlayIterator
11 from ansible.playbook import Playbook
12 from ansible.playbook.play_context import PlayContext
13 from ansible.plugins.strategy.linear import StrategyModule
14 from ansible.executor.task_queue_manager import TaskQueueManager
15
16 from units.mock.loader import DictDataLoader
17 from units.mock.path import mock_unfrackpath_noop
18
19
20 class TestStrategyLinear(unittest.TestCase):
21
22 @patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
23 def test_noop(self):
24 fake_loader = DictDataLoader({
25 "test_play.yml": """
26 - hosts: all
27 gather_facts: no
28 tasks:
29 - block:
30 - block:
31 - name: task1
32 debug: msg='task1'
33 failed_when: inventory_hostname == 'host01'
34
35 - name: task2
36 debug: msg='task2'
37
38 rescue:
39 - name: rescue1
40 debug: msg='rescue1'
41
42 - name: rescue2
43 debug: msg='rescue2'
44 """,
45 })
46
47 mock_var_manager = MagicMock()
48 mock_var_manager._fact_cache = dict()
49 mock_var_manager.get_vars.return_value = dict()
50
51 p = Playbook.load('test_play.yml', loader=fake_loader, variable_manager=mock_var_manager)
52
53 inventory = MagicMock()
54 inventory.hosts = {}
55 hosts = []
56 for i in range(0, 2):
57 host = MagicMock()
58 host.name = host.get_name.return_value = 'host%02d' % i
59 hosts.append(host)
60 inventory.hosts[host.name] = host
61 inventory.get_hosts.return_value = hosts
62 inventory.filter_hosts.return_value = hosts
63
64 mock_var_manager._fact_cache['host00'] = dict()
65
66 play_context = PlayContext(play=p._entries[0])
67
68 itr = PlayIterator(
69 inventory=inventory,
70 play=p._entries[0],
71 play_context=play_context,
72 variable_manager=mock_var_manager,
73 all_vars=dict(),
74 )
75
76 tqm = TaskQueueManager(
77 inventory=inventory,
78 variable_manager=mock_var_manager,
79 loader=fake_loader,
80 passwords=None,
81 forks=5,
82 )
83 tqm._initialize_processes(3)
84 strategy = StrategyModule(tqm)
85 strategy._hosts_cache = [h.name for h in hosts]
86 strategy._hosts_cache_all = [h.name for h in hosts]
87
88 # debug: task1, debug: task1
89 hosts_left = strategy.get_hosts_left(itr)
90 hosts_tasks = strategy._get_next_task_lockstep(hosts_left, itr)
91 host1_task = hosts_tasks[0][1]
92 host2_task = hosts_tasks[1][1]
93 self.assertIsNotNone(host1_task)
94 self.assertIsNotNone(host2_task)
95 self.assertEqual(host1_task.action, 'debug')
96 self.assertEqual(host2_task.action, 'debug')
97 self.assertEqual(host1_task.name, 'task1')
98 self.assertEqual(host2_task.name, 'task1')
99
100 # mark the second host failed
101 itr.mark_host_failed(hosts[1])
102
103 # debug: task2, noop
104 hosts_left = strategy.get_hosts_left(itr)
105 hosts_tasks = strategy._get_next_task_lockstep(hosts_left, itr)
106 self.assertEqual(len(hosts_tasks), 1)
107 host, task = hosts_tasks[0]
108 self.assertEqual(host.name, 'host00')
109 self.assertEqual(task.action, 'debug')
110 self.assertEqual(task.name, 'task2')
111
112 # noop, debug: rescue1
113 hosts_left = strategy.get_hosts_left(itr)
114 hosts_tasks = strategy._get_next_task_lockstep(hosts_left, itr)
115 self.assertEqual(len(hosts_tasks), 1)
116 host, task = hosts_tasks[0]
117 self.assertEqual(host.name, 'host01')
118 self.assertEqual(task.action, 'debug')
119 self.assertEqual(task.name, 'rescue1')
120
121 # noop, debug: rescue2
122 hosts_left = strategy.get_hosts_left(itr)
123 hosts_tasks = strategy._get_next_task_lockstep(hosts_left, itr)
124 self.assertEqual(len(hosts_tasks), 1)
125 host, task = hosts_tasks[0]
126 self.assertEqual(host.name, 'host01')
127 self.assertEqual(task.action, 'debug')
128 self.assertEqual(task.name, 'rescue2')
129
130 # end of iteration
131 assert not strategy._get_next_task_lockstep(strategy.get_hosts_left(itr), itr)
132
133 def test_noop_64999(self):
134 fake_loader = DictDataLoader({
135 "test_play.yml": """
136 - hosts: all
137 gather_facts: no
138 tasks:
139 - name: block1
140 block:
141 - name: block2
142 block:
143 - name: block3
144 block:
145 - name: task1
146 debug:
147 failed_when: inventory_hostname == 'host01'
148 rescue:
149 - name: rescue1
150 debug:
151 msg: "rescue"
152 - name: after_rescue1
153 debug:
154 msg: "after_rescue1"
155 """,
156 })
157
158 mock_var_manager = MagicMock()
159 mock_var_manager._fact_cache = dict()
160 mock_var_manager.get_vars.return_value = dict()
161
162 p = Playbook.load('test_play.yml', loader=fake_loader, variable_manager=mock_var_manager)
163
164 inventory = MagicMock()
165 inventory.hosts = {}
166 hosts = []
167 for i in range(0, 2):
168 host = MagicMock()
169 host.name = host.get_name.return_value = 'host%02d' % i
170 hosts.append(host)
171 inventory.hosts[host.name] = host
172 inventory.get_hosts.return_value = hosts
173 inventory.filter_hosts.return_value = hosts
174
175 mock_var_manager._fact_cache['host00'] = dict()
176
177 play_context = PlayContext(play=p._entries[0])
178
179 itr = PlayIterator(
180 inventory=inventory,
181 play=p._entries[0],
182 play_context=play_context,
183 variable_manager=mock_var_manager,
184 all_vars=dict(),
185 )
186
187 tqm = TaskQueueManager(
188 inventory=inventory,
189 variable_manager=mock_var_manager,
190 loader=fake_loader,
191 passwords=None,
192 forks=5,
193 )
194 tqm._initialize_processes(3)
195 strategy = StrategyModule(tqm)
196 strategy._hosts_cache = [h.name for h in hosts]
197 strategy._hosts_cache_all = [h.name for h in hosts]
198
199 # debug: task1, debug: task1
200 hosts_left = strategy.get_hosts_left(itr)
201 hosts_tasks = strategy._get_next_task_lockstep(hosts_left, itr)
202 host1_task = hosts_tasks[0][1]
203 host2_task = hosts_tasks[1][1]
204 self.assertIsNotNone(host1_task)
205 self.assertIsNotNone(host2_task)
206 self.assertEqual(host1_task.action, 'debug')
207 self.assertEqual(host2_task.action, 'debug')
208 self.assertEqual(host1_task.name, 'task1')
209 self.assertEqual(host2_task.name, 'task1')
210
211 # mark the second host failed
212 itr.mark_host_failed(hosts[1])
213
214 # noop, debug: rescue1
215 hosts_left = strategy.get_hosts_left(itr)
216 hosts_tasks = strategy._get_next_task_lockstep(hosts_left, itr)
217 self.assertEqual(len(hosts_tasks), 1)
218 host, task = hosts_tasks[0]
219 self.assertEqual(host.name, 'host01')
220 self.assertEqual(task.action, 'debug')
221 self.assertEqual(task.name, 'rescue1')
222
223 # debug: after_rescue1, debug: after_rescue1
224 hosts_left = strategy.get_hosts_left(itr)
225 hosts_tasks = strategy._get_next_task_lockstep(hosts_left, itr)
226 host1_task = hosts_tasks[0][1]
227 host2_task = hosts_tasks[1][1]
228 self.assertIsNotNone(host1_task)
229 self.assertIsNotNone(host2_task)
230 self.assertEqual(host1_task.action, 'debug')
231 self.assertEqual(host2_task.action, 'debug')
232 self.assertEqual(host1_task.name, 'after_rescue1')
233 self.assertEqual(host2_task.name, 'after_rescue1')
234
235 # end of iteration
236 assert not strategy._get_next_task_lockstep(strategy.get_hosts_left(itr), itr)