| 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 MagicMock |
| 22 |
|
| 23 |
from ansible.executor.playbook_executor import PlaybookExecutor |
| 24 |
from ansible.playbook import Playbook |
| 25 |
from ansible._internal._templating._engine import TemplateEngine |
| 26 |
from ansible.utils import context_objects as co |
| 27 |
|
| 28 |
from units.mock.loader import DictDataLoader |
| 29 |
|
| 30 |
|
| 31 |
class TestPlaybookExecutor(unittest.TestCase): |
| 32 |
|
| 33 |
def setUp(self): |
| 34 |
# Reset command line args for every test |
| 35 |
co.GlobalCLIArgs._Singleton__instance = None |
| 36 |
|
| 37 |
def tearDown(self): |
| 38 |
# And cleanup after ourselves too |
| 39 |
co.GlobalCLIArgs._Singleton__instance = None |
| 40 |
|
| 41 |
def test_get_serialized_batches(self): |
| 42 |
fake_loader = DictDataLoader({ |
| 43 |
'no_serial.yml': """ |
| 44 |
- hosts: all |
| 45 |
gather_facts: no |
| 46 |
tasks: |
| 47 |
- debug: var=inventory_hostname |
| 48 |
""", |
| 49 |
'serial_int.yml': """ |
| 50 |
- hosts: all |
| 51 |
gather_facts: no |
| 52 |
serial: 2 |
| 53 |
tasks: |
| 54 |
- debug: var=inventory_hostname |
| 55 |
""", |
| 56 |
'serial_pct.yml': """ |
| 57 |
- hosts: all |
| 58 |
gather_facts: no |
| 59 |
serial: 20% |
| 60 |
tasks: |
| 61 |
- debug: var=inventory_hostname |
| 62 |
""", |
| 63 |
'serial_list.yml': """ |
| 64 |
- hosts: all |
| 65 |
gather_facts: no |
| 66 |
serial: [1, 2, 3] |
| 67 |
tasks: |
| 68 |
- debug: var=inventory_hostname |
| 69 |
""", |
| 70 |
'serial_list_mixed.yml': """ |
| 71 |
- hosts: all |
| 72 |
gather_facts: no |
| 73 |
serial: [1, "20%", -1] |
| 74 |
tasks: |
| 75 |
- debug: var=inventory_hostname |
| 76 |
""", |
| 77 |
}) |
| 78 |
|
| 79 |
mock_inventory = MagicMock() |
| 80 |
mock_var_manager = MagicMock() |
| 81 |
|
| 82 |
templar = TemplateEngine(loader=fake_loader) |
| 83 |
|
| 84 |
pbe = PlaybookExecutor( |
| 85 |
playbooks=['no_serial.yml', 'serial_int.yml', 'serial_pct.yml', 'serial_list.yml', 'serial_list_mixed.yml'], |
| 86 |
inventory=mock_inventory, |
| 87 |
variable_manager=mock_var_manager, |
| 88 |
loader=fake_loader, |
| 89 |
passwords=[], |
| 90 |
) |
| 91 |
|
| 92 |
playbook = Playbook.load(pbe._playbooks[0], variable_manager=mock_var_manager, loader=fake_loader) |
| 93 |
play = playbook.get_plays()[0] |
| 94 |
play.post_validate(templar) |
| 95 |
mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9'] |
| 96 |
self.assertEqual(pbe._get_serialized_batches(play), [['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9']]) |
| 97 |
|
| 98 |
playbook = Playbook.load(pbe._playbooks[1], variable_manager=mock_var_manager, loader=fake_loader) |
| 99 |
play = playbook.get_plays()[0] |
| 100 |
play.post_validate(templar) |
| 101 |
mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9'] |
| 102 |
self.assertEqual( |
| 103 |
pbe._get_serialized_batches(play), |
| 104 |
[['host0', 'host1'], ['host2', 'host3'], ['host4', 'host5'], ['host6', 'host7'], ['host8', 'host9']] |
| 105 |
) |
| 106 |
|
| 107 |
playbook = Playbook.load(pbe._playbooks[2], variable_manager=mock_var_manager, loader=fake_loader) |
| 108 |
play = playbook.get_plays()[0] |
| 109 |
play.post_validate(templar) |
| 110 |
mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9'] |
| 111 |
self.assertEqual( |
| 112 |
pbe._get_serialized_batches(play), |
| 113 |
[['host0', 'host1'], ['host2', 'host3'], ['host4', 'host5'], ['host6', 'host7'], ['host8', 'host9']] |
| 114 |
) |
| 115 |
|
| 116 |
playbook = Playbook.load(pbe._playbooks[3], variable_manager=mock_var_manager, loader=fake_loader) |
| 117 |
play = playbook.get_plays()[0] |
| 118 |
play.post_validate(templar) |
| 119 |
mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9'] |
| 120 |
self.assertEqual( |
| 121 |
pbe._get_serialized_batches(play), |
| 122 |
[['host0'], ['host1', 'host2'], ['host3', 'host4', 'host5'], ['host6', 'host7', 'host8'], ['host9']] |
| 123 |
) |
| 124 |
|
| 125 |
playbook = Playbook.load(pbe._playbooks[4], variable_manager=mock_var_manager, loader=fake_loader) |
| 126 |
play = playbook.get_plays()[0] |
| 127 |
play.post_validate(templar) |
| 128 |
mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9'] |
| 129 |
self.assertEqual(pbe._get_serialized_batches(play), [['host0'], ['host1', 'host2'], ['host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9']]) |
| 130 |
|
| 131 |
# Test when serial percent is under 1.0 |
| 132 |
playbook = Playbook.load(pbe._playbooks[2], variable_manager=mock_var_manager, loader=fake_loader) |
| 133 |
play = playbook.get_plays()[0] |
| 134 |
play.post_validate(templar) |
| 135 |
mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2'] |
| 136 |
self.assertEqual(pbe._get_serialized_batches(play), [['host0'], ['host1'], ['host2']]) |
| 137 |
|
| 138 |
# Test when there is a remainder for serial as a percent |
| 139 |
playbook = Playbook.load(pbe._playbooks[2], variable_manager=mock_var_manager, loader=fake_loader) |
| 140 |
play = playbook.get_plays()[0] |
| 141 |
play.post_validate(templar) |
| 142 |
mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9', 'host10'] |
| 143 |
self.assertEqual( |
| 144 |
pbe._get_serialized_batches(play), |
| 145 |
[['host0', 'host1'], ['host2', 'host3'], ['host4', 'host5'], ['host6', 'host7'], ['host8', 'host9'], ['host10']] |
| 146 |
) |