| 1 |
# (c) 2012-2014, Michael DeHaan <[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 patch, MagicMock |
| 22 |
|
| 23 |
from ansible.executor.play_iterator import HostState, PlayIterator, IteratingStates, FailedStates |
| 24 |
from ansible.playbook import Playbook |
| 25 |
from ansible.playbook.play_context import PlayContext |
| 26 |
from ansible.plugins.loader import init_plugin_loader |
| 27 |
|
| 28 |
from units.mock.loader import DictDataLoader |
| 29 |
from units.mock.path import mock_unfrackpath_noop |
| 30 |
|
| 31 |
|
| 32 |
class TestPlayIterator(unittest.TestCase): |
| 33 |
|
| 34 |
def test_host_state(self): |
| 35 |
hs = HostState(blocks=list(range(0, 10))) |
| 36 |
hs.tasks_child_state = HostState(blocks=[0]) |
| 37 |
hs.rescue_child_state = HostState(blocks=[1]) |
| 38 |
hs.always_child_state = HostState(blocks=[2]) |
| 39 |
repr(hs) |
| 40 |
hs.run_state = 100 |
| 41 |
repr(hs) |
| 42 |
hs.fail_state = 15 |
| 43 |
repr(hs) |
| 44 |
|
| 45 |
for i in range(0, 10): |
| 46 |
hs.cur_block = i |
| 47 |
self.assertEqual(hs.get_current_block(), i) |
| 48 |
|
| 49 |
new_hs = hs.copy() |
| 50 |
|
| 51 |
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop) |
| 52 |
def test_play_iterator(self): |
| 53 |
fake_loader = DictDataLoader({ |
| 54 |
"test_play.yml": """ |
| 55 |
- hosts: all |
| 56 |
gather_facts: false |
| 57 |
roles: |
| 58 |
- test_role |
| 59 |
pre_tasks: |
| 60 |
- debug: msg="this is a pre_task" |
| 61 |
tasks: |
| 62 |
- debug: msg="this is a regular task" |
| 63 |
- block: |
| 64 |
- debug: msg="this is a block task" |
| 65 |
- block: |
| 66 |
- debug: msg="this is a sub-block in a block" |
| 67 |
rescue: |
| 68 |
- debug: msg="this is a rescue task" |
| 69 |
- block: |
| 70 |
- debug: msg="this is a sub-block in a rescue" |
| 71 |
always: |
| 72 |
- debug: msg="this is an always task" |
| 73 |
- block: |
| 74 |
- debug: msg="this is a sub-block in an always" |
| 75 |
post_tasks: |
| 76 |
- debug: msg="this is a post_task" |
| 77 |
""", |
| 78 |
'/etc/ansible/roles/test_role/tasks/main.yml': """ |
| 79 |
- name: role task |
| 80 |
debug: msg="this is a role task" |
| 81 |
- block: |
| 82 |
- name: role block task |
| 83 |
debug: msg="inside block in role" |
| 84 |
always: |
| 85 |
- name: role always task |
| 86 |
debug: msg="always task in block in role" |
| 87 |
- name: role include_tasks |
| 88 |
include_tasks: foo.yml |
| 89 |
- name: role task after include |
| 90 |
debug: msg="after include in role" |
| 91 |
- block: |
| 92 |
- name: starting role nested block 1 |
| 93 |
debug: |
| 94 |
- block: |
| 95 |
- name: role nested block 1 task 1 |
| 96 |
debug: |
| 97 |
- name: role nested block 1 task 2 |
| 98 |
debug: |
| 99 |
- name: role nested block 1 task 3 |
| 100 |
debug: |
| 101 |
- name: end of role nested block 1 |
| 102 |
debug: |
| 103 |
- name: starting role nested block 2 |
| 104 |
debug: |
| 105 |
- block: |
| 106 |
- name: role nested block 2 task 1 |
| 107 |
debug: |
| 108 |
- name: role nested block 2 task 2 |
| 109 |
debug: |
| 110 |
- name: role nested block 2 task 3 |
| 111 |
debug: |
| 112 |
- name: end of role nested block 2 |
| 113 |
debug: |
| 114 |
""", |
| 115 |
'/etc/ansible/roles/test_role/tasks/foo.yml': """ |
| 116 |
- name: role included task |
| 117 |
debug: msg="this is task in an include from a role" |
| 118 |
""" |
| 119 |
}) |
| 120 |
|
| 121 |
mock_var_manager = MagicMock() |
| 122 |
mock_var_manager._fact_cache = dict() |
| 123 |
mock_var_manager.get_vars.return_value = dict() |
| 124 |
|
| 125 |
p = Playbook.load('test_play.yml', loader=fake_loader, variable_manager=mock_var_manager) |
| 126 |
|
| 127 |
hosts = [] |
| 128 |
for i in range(0, 10): |
| 129 |
host = MagicMock() |
| 130 |
host.name = host.get_name.return_value = 'host%02d' % i |
| 131 |
hosts.append(host) |
| 132 |
|
| 133 |
mock_var_manager._fact_cache['host00'] = dict() |
| 134 |
|
| 135 |
inventory = MagicMock() |
| 136 |
inventory.get_hosts.return_value = hosts |
| 137 |
inventory.filter_hosts.return_value = hosts |
| 138 |
|
| 139 |
play_context = PlayContext(play=p._entries[0]) |
| 140 |
|
| 141 |
itr = PlayIterator( |
| 142 |
inventory=inventory, |
| 143 |
play=p._entries[0], |
| 144 |
play_context=play_context, |
| 145 |
variable_manager=mock_var_manager, |
| 146 |
all_vars=dict(), |
| 147 |
) |
| 148 |
|
| 149 |
# pre task |
| 150 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 151 |
self.assertIsNotNone(task) |
| 152 |
self.assertEqual(task.action, 'debug') |
| 153 |
# role task |
| 154 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 155 |
self.assertIsNotNone(task) |
| 156 |
self.assertEqual(task.action, 'debug') |
| 157 |
self.assertEqual(task.name, "role task") |
| 158 |
self.assertIsNotNone(task._role) |
| 159 |
# role block task |
| 160 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 161 |
self.assertIsNotNone(task) |
| 162 |
self.assertEqual(task.name, "role block task") |
| 163 |
self.assertIsNotNone(task._role) |
| 164 |
# role block always task |
| 165 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 166 |
self.assertIsNotNone(task) |
| 167 |
self.assertEqual(task.name, "role always task") |
| 168 |
self.assertIsNotNone(task._role) |
| 169 |
# role include_tasks |
| 170 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 171 |
self.assertIsNotNone(task) |
| 172 |
self.assertEqual(task.action, 'include_tasks') |
| 173 |
self.assertEqual(task.name, "role include_tasks") |
| 174 |
self.assertIsNotNone(task._role) |
| 175 |
# role task after include |
| 176 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 177 |
self.assertIsNotNone(task) |
| 178 |
self.assertEqual(task.name, "role task after include") |
| 179 |
self.assertIsNotNone(task._role) |
| 180 |
# role nested block tasks |
| 181 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 182 |
self.assertIsNotNone(task) |
| 183 |
self.assertEqual(task.name, "starting role nested block 1") |
| 184 |
self.assertIsNotNone(task._role) |
| 185 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 186 |
self.assertIsNotNone(task) |
| 187 |
self.assertEqual(task.name, "role nested block 1 task 1") |
| 188 |
self.assertIsNotNone(task._role) |
| 189 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 190 |
self.assertIsNotNone(task) |
| 191 |
self.assertEqual(task.name, "role nested block 1 task 2") |
| 192 |
self.assertIsNotNone(task._role) |
| 193 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 194 |
self.assertIsNotNone(task) |
| 195 |
self.assertEqual(task.name, "role nested block 1 task 3") |
| 196 |
self.assertIsNotNone(task._role) |
| 197 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 198 |
self.assertIsNotNone(task) |
| 199 |
self.assertEqual(task.name, "end of role nested block 1") |
| 200 |
self.assertIsNotNone(task._role) |
| 201 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 202 |
self.assertIsNotNone(task) |
| 203 |
self.assertEqual(task.name, "starting role nested block 2") |
| 204 |
self.assertIsNotNone(task._role) |
| 205 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 206 |
self.assertIsNotNone(task) |
| 207 |
self.assertEqual(task.name, "role nested block 2 task 1") |
| 208 |
self.assertIsNotNone(task._role) |
| 209 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 210 |
self.assertIsNotNone(task) |
| 211 |
self.assertEqual(task.name, "role nested block 2 task 2") |
| 212 |
self.assertIsNotNone(task._role) |
| 213 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 214 |
self.assertIsNotNone(task) |
| 215 |
self.assertEqual(task.name, "role nested block 2 task 3") |
| 216 |
self.assertIsNotNone(task._role) |
| 217 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 218 |
self.assertIsNotNone(task) |
| 219 |
self.assertEqual(task.name, "end of role nested block 2") |
| 220 |
self.assertIsNotNone(task._role) |
| 221 |
# implicit meta: role_complete |
| 222 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 223 |
self.assertIsNotNone(task) |
| 224 |
self.assertEqual(task.action, 'meta') |
| 225 |
self.assertIsNotNone(task._role) |
| 226 |
# regular play task |
| 227 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 228 |
self.assertIsNotNone(task) |
| 229 |
self.assertEqual(task.action, 'debug') |
| 230 |
self.assertIsNone(task._role) |
| 231 |
# block task |
| 232 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 233 |
self.assertIsNotNone(task) |
| 234 |
self.assertEqual(task.action, 'debug') |
| 235 |
self.assertEqual(task.args, dict(msg="this is a block task")) |
| 236 |
# sub-block task |
| 237 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 238 |
self.assertIsNotNone(task) |
| 239 |
self.assertEqual(task.action, 'debug') |
| 240 |
self.assertEqual(task.args, dict(msg="this is a sub-block in a block")) |
| 241 |
# mark the host failed |
| 242 |
itr.mark_host_failed(hosts[0]) |
| 243 |
# block rescue task |
| 244 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 245 |
self.assertIsNotNone(task) |
| 246 |
self.assertEqual(task.action, 'debug') |
| 247 |
self.assertEqual(task.args, dict(msg="this is a rescue task")) |
| 248 |
# sub-block rescue task |
| 249 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 250 |
self.assertIsNotNone(task) |
| 251 |
self.assertEqual(task.action, 'debug') |
| 252 |
self.assertEqual(task.args, dict(msg="this is a sub-block in a rescue")) |
| 253 |
# block always task |
| 254 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 255 |
self.assertIsNotNone(task) |
| 256 |
self.assertEqual(task.action, 'debug') |
| 257 |
self.assertEqual(task.args, dict(msg="this is an always task")) |
| 258 |
# sub-block always task |
| 259 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 260 |
self.assertIsNotNone(task) |
| 261 |
self.assertEqual(task.action, 'debug') |
| 262 |
self.assertEqual(task.args, dict(msg="this is a sub-block in an always")) |
| 263 |
# post task |
| 264 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 265 |
self.assertIsNotNone(task) |
| 266 |
self.assertEqual(task.action, 'debug') |
| 267 |
# end of iteration |
| 268 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 269 |
self.assertIsNone(task) |
| 270 |
|
| 271 |
# host 0 shouldn't be in the failed hosts, as the error |
| 272 |
# was handled by a rescue block |
| 273 |
failed_hosts = itr.get_failed_hosts() |
| 274 |
self.assertNotIn(hosts[0], failed_hosts) |
| 275 |
|
| 276 |
def test_play_iterator_nested_blocks(self): |
| 277 |
init_plugin_loader() |
| 278 |
fake_loader = DictDataLoader({ |
| 279 |
"test_play.yml": """ |
| 280 |
- hosts: all |
| 281 |
gather_facts: false |
| 282 |
tasks: |
| 283 |
- block: |
| 284 |
- block: |
| 285 |
- block: |
| 286 |
- block: |
| 287 |
- block: |
| 288 |
- debug: msg="this is the first task" |
| 289 |
- ping: |
| 290 |
rescue: |
| 291 |
- block: |
| 292 |
- block: |
| 293 |
- block: |
| 294 |
- block: |
| 295 |
- debug: msg="this is the rescue task" |
| 296 |
always: |
| 297 |
- block: |
| 298 |
- block: |
| 299 |
- block: |
| 300 |
- block: |
| 301 |
- debug: msg="this is the always task" |
| 302 |
""", |
| 303 |
}) |
| 304 |
|
| 305 |
mock_var_manager = MagicMock() |
| 306 |
mock_var_manager._fact_cache = dict() |
| 307 |
mock_var_manager.get_vars.return_value = dict() |
| 308 |
|
| 309 |
p = Playbook.load('test_play.yml', loader=fake_loader, variable_manager=mock_var_manager) |
| 310 |
|
| 311 |
hosts = [] |
| 312 |
for i in range(0, 10): |
| 313 |
host = MagicMock() |
| 314 |
host.name = host.get_name.return_value = 'host%02d' % i |
| 315 |
hosts.append(host) |
| 316 |
|
| 317 |
inventory = MagicMock() |
| 318 |
inventory.get_hosts.return_value = hosts |
| 319 |
inventory.filter_hosts.return_value = hosts |
| 320 |
|
| 321 |
play_context = PlayContext(play=p._entries[0]) |
| 322 |
|
| 323 |
itr = PlayIterator( |
| 324 |
inventory=inventory, |
| 325 |
play=p._entries[0], |
| 326 |
play_context=play_context, |
| 327 |
variable_manager=mock_var_manager, |
| 328 |
all_vars=dict(), |
| 329 |
) |
| 330 |
|
| 331 |
# get the first task |
| 332 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 333 |
self.assertIsNotNone(task) |
| 334 |
self.assertEqual(task.action, 'debug') |
| 335 |
self.assertEqual(task.args, dict(msg='this is the first task')) |
| 336 |
# fail the host |
| 337 |
itr.mark_host_failed(hosts[0]) |
| 338 |
# get the rescue task |
| 339 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 340 |
self.assertIsNotNone(task) |
| 341 |
self.assertEqual(task.action, 'debug') |
| 342 |
self.assertEqual(task.args, dict(msg='this is the rescue task')) |
| 343 |
# get the always task |
| 344 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 345 |
self.assertIsNotNone(task) |
| 346 |
self.assertEqual(task.action, 'debug') |
| 347 |
self.assertEqual(task.args, dict(msg='this is the always task')) |
| 348 |
# end of iteration |
| 349 |
(host_state, task) = itr.get_next_task_for_host(hosts[0]) |
| 350 |
self.assertIsNone(task) |
| 351 |
|
| 352 |
def test_play_iterator_add_tasks(self): |
| 353 |
fake_loader = DictDataLoader({ |
| 354 |
'test_play.yml': """ |
| 355 |
- hosts: all |
| 356 |
gather_facts: no |
| 357 |
tasks: |
| 358 |
- debug: msg="dummy task" |
| 359 |
""", |
| 360 |
}) |
| 361 |
|
| 362 |
mock_var_manager = MagicMock() |
| 363 |
mock_var_manager._fact_cache = dict() |
| 364 |
mock_var_manager.get_vars.return_value = dict() |
| 365 |
|
| 366 |
p = Playbook.load('test_play.yml', loader=fake_loader, variable_manager=mock_var_manager) |
| 367 |
|
| 368 |
hosts = [] |
| 369 |
for i in range(0, 10): |
| 370 |
host = MagicMock() |
| 371 |
host.name = host.get_name.return_value = 'host%02d' % i |
| 372 |
hosts.append(host) |
| 373 |
|
| 374 |
inventory = MagicMock() |
| 375 |
inventory.get_hosts.return_value = hosts |
| 376 |
inventory.filter_hosts.return_value = hosts |
| 377 |
|
| 378 |
play_context = PlayContext(play=p._entries[0]) |
| 379 |
|
| 380 |
itr = PlayIterator( |
| 381 |
inventory=inventory, |
| 382 |
play=p._entries[0], |
| 383 |
play_context=play_context, |
| 384 |
variable_manager=mock_var_manager, |
| 385 |
all_vars=dict(), |
| 386 |
) |
| 387 |
|
| 388 |
# test the high-level add_tasks() method |
| 389 |
s = HostState(blocks=[0, 1, 2]) |
| 390 |
itr._insert_tasks_into_state = MagicMock(return_value=s) |
| 391 |
itr.add_tasks(hosts[0], [MagicMock(), MagicMock(), MagicMock()]) |
| 392 |
self.assertEqual(itr._host_states[hosts[0].name], s) |
| 393 |
|
| 394 |
# now actually test the lower-level method that does the work |
| 395 |
itr = PlayIterator( |
| 396 |
inventory=inventory, |
| 397 |
play=p._entries[0], |
| 398 |
play_context=play_context, |
| 399 |
variable_manager=mock_var_manager, |
| 400 |
all_vars=dict(), |
| 401 |
) |
| 402 |
|
| 403 |
# iterate past first task |
| 404 |
dummy, task = itr.get_next_task_for_host(hosts[0]) |
| 405 |
while (task and task.action != 'debug'): |
| 406 |
dummy, task = itr.get_next_task_for_host(hosts[0]) |
| 407 |
|
| 408 |
self.assertIsNotNone(task, 'iterated past end of play while looking for place to insert tasks') |
| 409 |
|
| 410 |
# get the current host state and copy it so we can mutate it |
| 411 |
s = itr.get_host_state(hosts[0]) |
| 412 |
s_copy = s.copy() |
| 413 |
|
| 414 |
# assert with an empty task list, or if we're in a failed state, we simply return the state as-is |
| 415 |
res_state = itr._insert_tasks_into_state(s_copy, task_list=[]) |
| 416 |
self.assertEqual(res_state, s_copy) |
| 417 |
|
| 418 |
s_copy.fail_state = FailedStates.TASKS |
| 419 |
res_state = itr._insert_tasks_into_state(s_copy, task_list=[MagicMock()]) |
| 420 |
self.assertEqual(res_state, s_copy) |
| 421 |
|
| 422 |
# but if we've failed with a rescue/always block |
| 423 |
mock_task = MagicMock() |
| 424 |
s_copy.run_state = IteratingStates.RESCUE |
| 425 |
res_state = itr._insert_tasks_into_state(s_copy, task_list=[mock_task]) |
| 426 |
self.assertEqual(res_state, s_copy) |
| 427 |
self.assertIn(mock_task, res_state._blocks[res_state.cur_block].rescue) |
| 428 |
itr.set_state_for_host(hosts[0].name, res_state) |
| 429 |
(next_state, next_task) = itr.get_next_task_for_host(hosts[0], peek=True) |
| 430 |
self.assertEqual(next_task, mock_task) |
| 431 |
itr.set_state_for_host(hosts[0].name, s) |
| 432 |
|
| 433 |
# test a regular insertion |
| 434 |
s_copy = s.copy() |
| 435 |
res_state = itr._insert_tasks_into_state(s_copy, task_list=[MagicMock()]) |