2015-10-12 17:27:37 +02:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2018-10-13 05:01:14 +02:00
|
|
|
from units.compat import unittest
|
|
|
|
from units.compat.mock import MagicMock
|
2018-12-18 03:10:59 +01:00
|
|
|
|
|
|
|
from ansible import arguments
|
2015-10-12 17:27:37 +02:00
|
|
|
from ansible.executor.playbook_executor import PlaybookExecutor
|
|
|
|
from ansible.playbook import Playbook
|
2016-08-04 07:05:30 +02:00
|
|
|
from ansible.template import Templar
|
2015-10-12 17:27:37 +02:00
|
|
|
|
|
|
|
from units.mock.loader import DictDataLoader
|
|
|
|
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2016-08-04 07:05:30 +02:00
|
|
|
class TestPlaybookExecutor(unittest.TestCase):
|
2015-10-12 17:27:37 +02:00
|
|
|
|
|
|
|
def setUp(self):
|
2018-12-18 03:10:59 +01:00
|
|
|
# Reset command line args for every test
|
|
|
|
arguments.CLIArgs._Singleton__instance = None
|
2015-10-12 17:27:37 +02:00
|
|
|
|
|
|
|
def tearDown(self):
|
2018-12-18 03:10:59 +01:00
|
|
|
# And cleanup after ourselves too
|
|
|
|
arguments.CLIArgs._Singleton__instance = None
|
2015-10-12 17:27:37 +02:00
|
|
|
|
2016-06-14 14:45:15 +02:00
|
|
|
def test_get_serialized_batches(self):
|
2015-10-12 17:27:37 +02:00
|
|
|
fake_loader = DictDataLoader({
|
|
|
|
'no_serial.yml': '''
|
|
|
|
- hosts: all
|
|
|
|
gather_facts: no
|
|
|
|
tasks:
|
|
|
|
- debug: var=inventory_hostname
|
|
|
|
''',
|
|
|
|
'serial_int.yml': '''
|
|
|
|
- hosts: all
|
|
|
|
gather_facts: no
|
|
|
|
serial: 2
|
|
|
|
tasks:
|
|
|
|
- debug: var=inventory_hostname
|
|
|
|
''',
|
|
|
|
'serial_pct.yml': '''
|
|
|
|
- hosts: all
|
|
|
|
gather_facts: no
|
|
|
|
serial: 20%
|
|
|
|
tasks:
|
|
|
|
- debug: var=inventory_hostname
|
|
|
|
''',
|
2016-08-04 07:05:30 +02:00
|
|
|
'serial_list.yml': '''
|
|
|
|
- hosts: all
|
|
|
|
gather_facts: no
|
|
|
|
serial: [1, 2, 3]
|
|
|
|
tasks:
|
|
|
|
- debug: var=inventory_hostname
|
|
|
|
''',
|
|
|
|
'serial_list_mixed.yml': '''
|
|
|
|
- hosts: all
|
|
|
|
gather_facts: no
|
|
|
|
serial: [1, "20%", -1]
|
|
|
|
tasks:
|
|
|
|
- debug: var=inventory_hostname
|
|
|
|
''',
|
2015-10-12 17:27:37 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
mock_inventory = MagicMock()
|
|
|
|
mock_var_manager = MagicMock()
|
|
|
|
|
2016-08-04 07:05:30 +02:00
|
|
|
templar = Templar(loader=fake_loader)
|
|
|
|
|
2015-10-12 17:27:37 +02:00
|
|
|
pbe = PlaybookExecutor(
|
2016-08-04 07:05:30 +02:00
|
|
|
playbooks=['no_serial.yml', 'serial_int.yml', 'serial_pct.yml', 'serial_list.yml', 'serial_list_mixed.yml'],
|
2015-10-12 17:27:37 +02:00
|
|
|
inventory=mock_inventory,
|
|
|
|
variable_manager=mock_var_manager,
|
|
|
|
loader=fake_loader,
|
|
|
|
passwords=[],
|
|
|
|
)
|
|
|
|
|
|
|
|
playbook = Playbook.load(pbe._playbooks[0], variable_manager=mock_var_manager, loader=fake_loader)
|
|
|
|
play = playbook.get_plays()[0]
|
2016-08-04 07:05:30 +02:00
|
|
|
play.post_validate(templar)
|
2017-05-30 19:05:19 +02:00
|
|
|
mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9']
|
|
|
|
self.assertEqual(pbe._get_serialized_batches(play), [['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9']])
|
2015-10-12 17:27:37 +02:00
|
|
|
|
|
|
|
playbook = Playbook.load(pbe._playbooks[1], variable_manager=mock_var_manager, loader=fake_loader)
|
|
|
|
play = playbook.get_plays()[0]
|
2016-08-04 07:05:30 +02:00
|
|
|
play.post_validate(templar)
|
2017-05-30 19:05:19 +02:00
|
|
|
mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9']
|
|
|
|
self.assertEqual(
|
|
|
|
pbe._get_serialized_batches(play),
|
|
|
|
[['host0', 'host1'], ['host2', 'host3'], ['host4', 'host5'], ['host6', 'host7'], ['host8', 'host9']]
|
|
|
|
)
|
2015-10-12 17:27:37 +02:00
|
|
|
|
|
|
|
playbook = Playbook.load(pbe._playbooks[2], variable_manager=mock_var_manager, loader=fake_loader)
|
|
|
|
play = playbook.get_plays()[0]
|
2016-08-04 07:05:30 +02:00
|
|
|
play.post_validate(templar)
|
2017-05-30 19:05:19 +02:00
|
|
|
mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9']
|
|
|
|
self.assertEqual(
|
|
|
|
pbe._get_serialized_batches(play),
|
|
|
|
[['host0', 'host1'], ['host2', 'host3'], ['host4', 'host5'], ['host6', 'host7'], ['host8', 'host9']]
|
|
|
|
)
|
2015-10-12 17:27:37 +02:00
|
|
|
|
2016-08-04 07:05:30 +02:00
|
|
|
playbook = Playbook.load(pbe._playbooks[3], variable_manager=mock_var_manager, loader=fake_loader)
|
|
|
|
play = playbook.get_plays()[0]
|
|
|
|
play.post_validate(templar)
|
2017-05-30 19:05:19 +02:00
|
|
|
mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9']
|
|
|
|
self.assertEqual(
|
|
|
|
pbe._get_serialized_batches(play),
|
|
|
|
[['host0'], ['host1', 'host2'], ['host3', 'host4', 'host5'], ['host6', 'host7', 'host8'], ['host9']]
|
|
|
|
)
|
2016-08-04 07:05:30 +02:00
|
|
|
|
|
|
|
playbook = Playbook.load(pbe._playbooks[4], variable_manager=mock_var_manager, loader=fake_loader)
|
|
|
|
play = playbook.get_plays()[0]
|
|
|
|
play.post_validate(templar)
|
2017-05-30 19:05:19 +02:00
|
|
|
mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9']
|
|
|
|
self.assertEqual(pbe._get_serialized_batches(play), [['host0'], ['host1', 'host2'], ['host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9']])
|
2016-08-04 07:05:30 +02:00
|
|
|
|
2016-04-13 16:49:38 +02:00
|
|
|
# Test when serial percent is under 1.0
|
|
|
|
playbook = Playbook.load(pbe._playbooks[2], variable_manager=mock_var_manager, loader=fake_loader)
|
|
|
|
play = playbook.get_plays()[0]
|
2016-08-04 07:05:30 +02:00
|
|
|
play.post_validate(templar)
|
2017-05-30 19:05:19 +02:00
|
|
|
mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2']
|
|
|
|
self.assertEqual(pbe._get_serialized_batches(play), [['host0'], ['host1'], ['host2']])
|
2016-04-13 16:49:38 +02:00
|
|
|
|
|
|
|
# Test when there is a remainder for serial as a percent
|
|
|
|
playbook = Playbook.load(pbe._playbooks[2], variable_manager=mock_var_manager, loader=fake_loader)
|
|
|
|
play = playbook.get_plays()[0]
|
2016-08-04 07:05:30 +02:00
|
|
|
play.post_validate(templar)
|
2017-05-30 19:05:19 +02:00
|
|
|
mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9', 'host10']
|
2017-02-10 18:33:48 +01:00
|
|
|
self.assertEqual(
|
|
|
|
pbe._get_serialized_batches(play),
|
2017-05-30 19:05:19 +02:00
|
|
|
[['host0', 'host1'], ['host2', 'host3'], ['host4', 'host5'], ['host6', 'host7'], ['host8', 'host9'], ['host10']]
|
2017-02-10 18:33:48 +01:00
|
|
|
)
|