test_ansible_manager.py 3.09 KB
Newer Older
1
from fokia.ansible_manager import Manager
2
3
4
from mock import patch, MagicMock

from fokia.ansible_manager_minimal import Manager as MiniManager
5

Ioannis Tsafaras's avatar
Ioannis Tsafaras committed
6
test_provisioner_response = {
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    u'ips':    [{
        u'floating_network_id': u'2186', u'floating_ip_address': u'83.212.116.49',
        u'id':                  u'688160'
    }],
    u'nodes':  {
        u'master': {
            'internal_ip': u'192.168.0.2', u'adminPass': u'0igc3vbnSx', u'id': 666976,
            u'name':       u'test_vm'
        },
        u'slaves': [{'internal_ip': u'192.168.0.3', u'id': 666977, u'name': u'lambda-node1'}]
    },
    u'vpn':    {u'type': u'MAC_FILTERED', u'id': u'143713'},
    'pk':      'Dummy pk',
    u'subnet': {u'cidr': u'192.168.0.0/24', u'gateway_ip': u'192.168.0.1', u'id': u'142761'}
}


def test_playbook_run_minimal_manager():
    with patch('fokia.ansible_manager_minimal.PlayBook') as pb, \
            patch('fokia.ansible_manager_minimal.ansible') as mock_ansible, \
            patch('fokia.ansible_manager_minimal.callbacks') as cb, \
            patch('fokia.ansible_manager_minimal.os'), \
            patch('fokia.ansible_manager_minimal.utils'):
        # Initialize mocks with some test values
        mock_ansible.inventory.Inventory.return_value = MagicMock()
        cb.PlaybookCallbacks.return_value = "a"
        cb.PlaybookRunnerCallbacks.return_value = "b"
        cb.AggregateStats.return_value = "c"

        # Initialize the ansible manager and run a playbook
        manager = MiniManager("test_host", "test_group", "/path/to/private/keys")
        manager.run_playbook(playbook_file="/path/to/playbook.yml",
39
                             only_tags=["touch"], skip_tags=["touch-2"])
40
41

        # Check playbook was called with the correct arguements
42
43
        pb.assert_called_with(only_tags=["touch"], skip_tags=["touch-2"],
                              stats='c', callbacks='a', runner_callbacks='b',
44
45
                              playbook='/path/to/playbook.yml',
                              inventory=mock_ansible.inventory.Inventory.return_value)
46
47
48


def test_playbook_run():
49
50
    with patch('fokia.ansible_manager.PlayBook') as pb, \
            patch('fokia.ansible_manager.callbacks') as cb, \
51
52
            patch('fokia.ansible_manager.utils'):
        # Initialize mocks with some test values
53
54
55
        cb.PlaybookCallbacks.return_value = "a"
        cb.PlaybookRunnerCallbacks.return_value = "b"
        cb.AggregateStats.return_value = "c"
56
57

        # Initialize a manager, create an inventory and run a playbook
58
59
        manager = Manager(test_provisioner_response)
        manager.create_inventory()
60
        manager.run_playbook(playbook_file="/path/to/playbook.yml",
Avraam Tsantekidis's avatar
Avraam Tsantekidis committed
61
                             only_tags=["touch"], skip_tags=["touch-2"])
62

63
        # Check that playbook was called with the correct argumenets
Avraam Tsantekidis's avatar
Avraam Tsantekidis committed
64
        pb.asset_called_with(stats='c', only_tags=['touch'], skip_tags=["touch-2"], callbacks='a',
65
                             playbook='/path/to/playbook.yml', runner_callbacks='b',
66
                             inventory=manager.inventory)
67
68
69


if __name__ == "__main__":
70
    test_playbook_run_minimal_manager()
71
    test_playbook_run()