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