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
5
# http://www.apache.org/licenses/LICENSE-2.0
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
11
# See the License for the specific language governing permissions and
12
# limitations under the License.
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
26
class FuelManagementTestCase(test.TestCase):
29
super(FuelManagementTestCase, self).setUp()
31
'address': 'fuel.local',
35
self.fake_ansible_result = fakes.FakeAnsibleResult(
37
'stdout': '[{"ip": "10.0.0.2", "mac": "02", "fqdn": "node-2"},'
38
' {"ip": "10.0.0.3", "mac": "03", "fqdn": "node-3"}]'
41
self.master_host = node_collection.Host('fuel.local')
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'),
47
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
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))
54
dict(address='fuel.local', username='root', slave_direct_ssh=True,
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))
61
def test_init(self, config, expected_runner_calls, mock_ansible_runner):
62
ansible_runner_inst = mock_ansible_runner.return_value
64
fuel_managment = fuel.FuelManagement(config)
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)
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': ''})],
78
fuel_managment = fuel.FuelManagement(self.conf)
79
fuel_managment.verify()
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'}),
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()
93
ansible_runner_inst.execute.assert_has_calls([
94
mock.call([self.master_host], {'command': 'fuel node --json'}),
97
self.assertEqual(nodes.hosts, self.hosts)
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
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'),
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()
114
self.assertFalse(ansible_runner_inst.execute.called)
115
self.assertEqual(hosts, nodes.hosts)
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': ''})]
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)
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'}, []),
135
self.assertEqual(result,
136
[fakes.FakeAnsibleResult(payload={'stdout': ''}),
137
fakes.FakeAnsibleResult(payload={'stdout': ''})])
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'])
147
node_collection.Host(ip='10.0.0.3', mac='03', fqdn='node-3'),
149
self.assertEqual(nodes.hosts, hosts)
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,
160
fakes.FakeAnsibleResult(payload={'stdout': ''},
164
fuel_managment = fuel.FuelManagement(self.conf)
166
service = fuel_managment.get_service(service_name)
168
nodes = service.get_nodes()
169
cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
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}, []),
176
self.assertEqual(nodes.hosts, [self.hosts[1]])
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')
183
def test_validate_services(self):
184
fuel_managment = fuel.FuelManagement(self.conf)
185
fuel_managment.validate_services()