~shakhat/os-faults/master

« back to all changes in this revision

Viewing changes to os_faults/tests/unit/drivers/cloud/test_fuel_management.py

  • Committer: Gerrit Code Review
  • Author(s): Zuul
  • Date: 2018-11-27 10:04:18 UTC
  • mfrom: (177.1.2)
  • Revision ID: git-v1:62b96dd2a4fcee9ca5410b447fd34bd900b93b6e
Merge "Remove old drivers"

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Licensed under the Apache License, Version 2.0 (the "License");
2
 
# you may not use this file except in compliance with the License.
3
 
# You may obtain a copy of the License at
4
 
#
5
 
#   http://www.apache.org/licenses/LICENSE-2.0
6
 
#
7
 
# Unless required by applicable law or agreed to in writing, software
8
 
# distributed under the License is distributed on an "AS IS" BASIS,
9
 
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10
 
# implied.
11
 
# See the License for the specific language governing permissions and
12
 
# limitations under the License.
13
 
 
14
 
import ddt
15
 
import mock
16
 
 
17
 
from os_faults.ansible import executor
18
 
from os_faults.api import error
19
 
from os_faults.api import node_collection
20
 
from os_faults.drivers.cloud import fuel
21
 
from os_faults.tests.unit import fakes
22
 
from os_faults.tests.unit import test
23
 
 
24
 
 
25
 
@ddt.ddt
26
 
class FuelManagementTestCase(test.TestCase):
27
 
 
28
 
    def setUp(self):
29
 
        super(FuelManagementTestCase, self).setUp()
30
 
        self.conf = {
31
 
            'address': 'fuel.local',
32
 
            'username': 'root',
33
 
        }
34
 
 
35
 
        self.fake_ansible_result = fakes.FakeAnsibleResult(
36
 
            payload={
37
 
                'stdout': '[{"ip": "10.0.0.2", "mac": "02", "fqdn": "node-2"},'
38
 
                          ' {"ip": "10.0.0.3", "mac": "03", "fqdn": "node-3"}]'
39
 
            })
40
 
 
41
 
        self.master_host = node_collection.Host('fuel.local')
42
 
        self.hosts = [
43
 
            node_collection.Host(ip='10.0.0.2', mac='02', fqdn='node-2'),
44
 
            node_collection.Host(ip='10.0.0.3', mac='03', fqdn='node-3'),
45
 
        ]
46
 
 
47
 
    @mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
48
 
    @ddt.data((
49
 
        dict(address='fuel.local', username='root'),
50
 
        (mock.call(private_key_file=None, remote_user='root'),
51
 
         mock.call(private_key_file=None, remote_user='root',
52
 
                   jump_host='fuel.local', serial=None))
53
 
    ), (
54
 
        dict(address='fuel.local', username='root', slave_direct_ssh=True,
55
 
             serial=42),
56
 
        (mock.call(private_key_file=None, remote_user='root'),
57
 
         mock.call(private_key_file=None, remote_user='root',
58
 
                   jump_host=None, serial=42))
59
 
    ))
60
 
    @ddt.unpack
61
 
    def test_init(self, config, expected_runner_calls, mock_ansible_runner):
62
 
        ansible_runner_inst = mock_ansible_runner.return_value
63
 
 
64
 
        fuel_managment = fuel.FuelManagement(config)
65
 
 
66
 
        mock_ansible_runner.assert_has_calls(expected_runner_calls)
67
 
        self.assertIs(fuel_managment.master_node_executor, ansible_runner_inst)
68
 
        self.assertIs(fuel_managment.cloud_executor, ansible_runner_inst)
69
 
 
70
 
    @mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
71
 
    def test_verify(self, mock_ansible_runner):
72
 
        ansible_runner_inst = mock_ansible_runner.return_value
73
 
        ansible_runner_inst.execute.side_effect = [
74
 
            [self.fake_ansible_result],
75
 
            [fakes.FakeAnsibleResult(payload={'stdout': ''}),
76
 
             fakes.FakeAnsibleResult(payload={'stdout': ''})],
77
 
        ]
78
 
        fuel_managment = fuel.FuelManagement(self.conf)
79
 
        fuel_managment.verify()
80
 
 
81
 
        ansible_runner_inst.execute.assert_has_calls([
82
 
            mock.call([self.master_host], {'command': 'fuel node --json'}),
83
 
            mock.call(self.hosts, {'command': 'hostname'}),
84
 
        ])
85
 
 
86
 
    @mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
87
 
    def test_get_nodes(self, mock_ansible_runner):
88
 
        ansible_runner_inst = mock_ansible_runner.return_value
89
 
        ansible_runner_inst.execute.side_effect = [[self.fake_ansible_result]]
90
 
        fuel_managment = fuel.FuelManagement(self.conf)
91
 
        nodes = fuel_managment.get_nodes()
92
 
 
93
 
        ansible_runner_inst.execute.assert_has_calls([
94
 
            mock.call([self.master_host], {'command': 'fuel node --json'}),
95
 
        ])
96
 
 
97
 
        self.assertEqual(nodes.hosts, self.hosts)
98
 
 
99
 
    @mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
100
 
    def test_get_nodes_from_discover_driver(self, mock_ansible_runner):
101
 
        ansible_runner_inst = mock_ansible_runner.return_value
102
 
        hosts = [
103
 
            node_collection.Host(ip='10.0.2.2', mac='09:7b:74:90:63:c2',
104
 
                                 fqdn='mynode1.local'),
105
 
            node_collection.Host(ip='10.0.2.3', mac='09:7b:74:90:63:c3',
106
 
                                 fqdn='mynode2.local'),
107
 
        ]
108
 
        node_discover_driver = mock.Mock()
109
 
        node_discover_driver.discover_hosts.return_value = hosts
110
 
        fuel_managment = fuel.FuelManagement(self.conf)
111
 
        fuel_managment.set_node_discover(node_discover_driver)
112
 
        nodes = fuel_managment.get_nodes()
113
 
 
114
 
        self.assertFalse(ansible_runner_inst.execute.called)
115
 
        self.assertEqual(hosts, nodes.hosts)
116
 
 
117
 
    @mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
118
 
    def test_execute_on_cloud(self, mock_ansible_runner):
119
 
        ansible_runner_inst = mock_ansible_runner.return_value
120
 
        ansible_runner_inst.execute.side_effect = [
121
 
            [self.fake_ansible_result],
122
 
            [fakes.FakeAnsibleResult(payload={'stdout': ''}),
123
 
             fakes.FakeAnsibleResult(payload={'stdout': ''})]
124
 
        ]
125
 
        fuel_managment = fuel.FuelManagement(self.conf)
126
 
        nodes = fuel_managment.get_nodes()
127
 
        result = fuel_managment.execute_on_cloud(
128
 
            nodes.hosts, {'command': 'mycmd'}, raise_on_error=False)
129
 
 
130
 
        ansible_runner_inst.execute.assert_has_calls([
131
 
            mock.call([self.master_host], {'command': 'fuel node --json'}),
132
 
            mock.call(self.hosts, {'command': 'mycmd'}, []),
133
 
        ])
134
 
 
135
 
        self.assertEqual(result,
136
 
                         [fakes.FakeAnsibleResult(payload={'stdout': ''}),
137
 
                          fakes.FakeAnsibleResult(payload={'stdout': ''})])
138
 
 
139
 
    @mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
140
 
    def test_get_nodes_fqdns(self, mock_ansible_runner):
141
 
        ansible_runner_inst = mock_ansible_runner.return_value
142
 
        ansible_runner_inst.execute.side_effect = [[self.fake_ansible_result]]
143
 
        fuel_managment = fuel.FuelManagement(self.conf)
144
 
        nodes = fuel_managment.get_nodes(fqdns=['node-3'])
145
 
 
146
 
        hosts = [
147
 
            node_collection.Host(ip='10.0.0.3', mac='03', fqdn='node-3'),
148
 
        ]
149
 
        self.assertEqual(nodes.hosts, hosts)
150
 
 
151
 
    @mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
152
 
    @ddt.data(*fuel.FuelManagement.SERVICES.keys())
153
 
    def test_get_service_nodes(self, service_name, mock_ansible_runner):
154
 
        ansible_runner_inst = mock_ansible_runner.return_value
155
 
        ansible_runner_inst.execute.side_effect = [
156
 
            [self.fake_ansible_result],
157
 
            [fakes.FakeAnsibleResult(payload={'stdout': ''},
158
 
                                     status=executor.STATUS_FAILED,
159
 
                                     host='10.0.0.2'),
160
 
             fakes.FakeAnsibleResult(payload={'stdout': ''},
161
 
                                     host='10.0.0.3')]
162
 
        ]
163
 
 
164
 
        fuel_managment = fuel.FuelManagement(self.conf)
165
 
 
166
 
        service = fuel_managment.get_service(service_name)
167
 
 
168
 
        nodes = service.get_nodes()
169
 
        cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
170
 
            service.grep)
171
 
        ansible_runner_inst.execute.assert_has_calls([
172
 
            mock.call([self.master_host], {'command': 'fuel node --json'}),
173
 
            mock.call(self.hosts, {'command': cmd}, []),
174
 
        ])
175
 
 
176
 
        self.assertEqual(nodes.hosts, [self.hosts[1]])
177
 
 
178
 
    def test_get_unknown_service(self):
179
 
        fuel_managment = fuel.FuelManagement(self.conf)
180
 
        self.assertRaises(error.ServiceError,
181
 
                          fuel_managment.get_service, 'unknown')
182
 
 
183
 
    def test_validate_services(self):
184
 
        fuel_managment = fuel.FuelManagement(self.conf)
185
 
        fuel_managment.validate_services()