3.92KiB; Python | 2019-11-03 10:35:34+01 | SLOC 111
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
19
from __future__ import (absolute_import, division, print_function)
20
__metaclass__ = type
21
22
from units.compat import unittest
23
from units.compat.mock import patch, MagicMock
24
25
from ansible.errors import AnsibleParserError
26
from ansible.playbook.play import Play
27
28 1
from units.mock.loader import DictDataLoader
29
from units.mock.path import mock_unfrackpath_noop
30
31
32
class TestPlay(unittest.TestCase):
33
34
    def test_empty_play(self):
35
        p = Play.load(dict())
36
        self.assertEqual(str(p), '')
37
38 1
    def test_basic_play(self):
39 1
        p = Play.load(dict(
40
            name="test play",
41
            hosts=['foo'],
42
            gather_facts=False,
43
            connection='local',
44
            remote_user="root",
45
            become=True,
46
            become_user="testing",
47
        ))
48
49
    def test_play_with_user_conflict(self):
50
        p = Play.load(dict(
51
            name="test play",
52
            hosts=['foo'],
53
            user="testing",
54
            gather_facts=False,
55
        ))
56
        self.assertEqual(p.remote_user, "testing")
57
58 1
    def test_play_with_user_conflict(self):
59
        play_data = dict(
60
            name="test play",
61
            hosts=['foo'],
62
            user="testing",
63
            remote_user="testing",
64
        )
65
        self.assertRaises(AnsibleParserError, Play.load, play_data)
66
67 1
    def test_play_with_tasks(self):
68 1
        p = Play.load(dict(
69
            name="test play",
70
            hosts=['foo'],
71
            gather_facts=False,
72
            tasks=[dict(action='shell echo "hello world"')],
73
        ))
74
75 1
    def test_play_with_handlers(self):
76 1
        p = Play.load(dict(
77
            name="test play",
78
            hosts=['foo'],
79
            gather_facts=False,
80
            handlers=[dict(action='shell echo "hello world"')],
81
        ))
82
83 1
    def test_play_with_pre_tasks(self):
84 1
        p = Play.load(dict(
85
            name="test play",
86
            hosts=['foo'],
87
            gather_facts=False,
88
            pre_tasks=[dict(action='shell echo "hello world"')],
89
        ))
90
91 1
    def test_play_with_post_tasks(self):
92 1
        p = Play.load(dict(
93
            name="test play",
94
            hosts=['foo'],
95
            gather_facts=False,
96
            post_tasks=[dict(action='shell echo "hello world"')],
97
        ))
98
99
    @patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
100
    def test_play_with_roles(self):
101
        fake_loader = DictDataLoader({
102
            '/etc/ansible/roles/foo/tasks.yml': """
103
            - name: role task
104
              shell: echo "hello world"
105
            """,
106
        })
107
108
        mock_var_manager = MagicMock()
109
        mock_var_manager.get_vars.return_value = dict()
110
111
        p = Play.load(dict(
112
            name="test play",
113
            hosts=['foo'],
114
            gather_facts=False,
115
            roles=['foo'],
116
        ), loader=fake_loader, variable_manager=mock_var_manager)
117
118
        blocks = p.compile()
119
120
    def test_play_compile(self):
121
        p = Play.load(dict(
122
            name="test play",
123
            hosts=['foo'],
124
            gather_facts=False,
125
            tasks=[dict(action='shell echo "hello world"')],
126
        ))
127
128
        blocks = p.compile()
129
130
        # with a single block, there will still be three
131
        # implicit meta flush_handler blocks inserted
132
        self.assertEqual(len(blocks), 4)