test_block.py 2.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# (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

from ansible.playbook.block import Block
from ansible.playbook.task import Task
24
from ansible.compat.tests import unittest
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

class TestBlock(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_construct_empty_block(self):
        b = Block()

    def test_construct_block_with_role(self):
        pass

    def test_load_block_simple(self):
        ds = dict(
James Cammarata committed
42
           block = [],
43
           rescue = [],
James Cammarata committed
44 45
           always = [],
           #otherwise = [],
46 47
        )
        b = Block.load(ds)
James Cammarata committed
48
        self.assertEqual(b.block, [])
49
        self.assertEqual(b.rescue, [])
James Cammarata committed
50 51 52
        self.assertEqual(b.always, [])
        # not currently used
        #self.assertEqual(b.otherwise, [])
53 54 55

    def test_load_block_with_tasks(self):
        ds = dict(
James Cammarata committed
56
           block = [dict(action='block')],
57
           rescue = [dict(action='rescue')],
James Cammarata committed
58 59
           always = [dict(action='always')],
           #otherwise = [dict(action='otherwise')],
60 61
        )
        b = Block.load(ds)
James Cammarata committed
62 63
        self.assertEqual(len(b.block), 1)
        assert isinstance(b.block[0], Task)
64 65
        self.assertEqual(len(b.rescue), 1)
        assert isinstance(b.rescue[0], Task)
James Cammarata committed
66 67 68 69 70 71 72 73 74 75 76
        self.assertEqual(len(b.always), 1)
        assert isinstance(b.always[0], Task)
        # not currently used
        #self.assertEqual(len(b.otherwise), 1)
        #assert isinstance(b.otherwise[0], Task)

    def test_load_implicit_block(self):
        ds = [dict(action='foo')]
        b = Block.load(ds)
        self.assertEqual(len(b.block), 1)
        assert isinstance(b.block[0], Task)
77

78 79 80 81 82 83
    def test_block_compile(self):
        ds = [dict(action='foo')]
        b = Block.load(ds)
        tasks = b.compile()
        self.assertEqual(len(tasks), 1)
        self.assertIsInstance(tasks[0], Task)