~ubuntu-branches/ubuntu/vivid/ironic/vivid-updates

« back to all changes in this revision

Viewing changes to ironic/tests/drivers/irmc/test_management.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2015-03-30 11:14:57 UTC
  • mfrom: (1.2.6)
  • Revision ID: package-import@ubuntu.com-20150330111457-kr4ju3guf22m4vbz
Tags: 2015.1~b3-0ubuntu1
* New upstream release.
  + d/control: 
    - Align with upstream dependencies.
    - Add dh-python to build-dependencies.
    - Add psmisc as a dependency. (LP: #1358820)
  + d/p/fix-requirements.patch: Rediffed.
  + d/ironic-conductor.init.in: Fixed typos in LSB headers,
    thanks to JJ Asghar. (LP: #1429962)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Licensed under the Apache License, Version 2.0 (the "License"); you may
 
3
# not use this file except in compliance with the License. You may obtain
 
4
# a copy of the License at
 
5
#
 
6
#      http://www.apache.org/licenses/LICENSE-2.0
 
7
#
 
8
# Unless required by applicable law or agreed to in writing, software
 
9
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
10
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
11
# License for the specific language governing permissions and limitations
 
12
# under the License.
 
13
 
 
14
"""
 
15
Test class for iRMC Management Driver
 
16
"""
 
17
 
 
18
import os
 
19
import xml.etree.ElementTree as ET
 
20
 
 
21
import mock
 
22
 
 
23
from ironic.common import boot_devices
 
24
from ironic.common import driver_factory
 
25
from ironic.common import exception
 
26
from ironic.conductor import task_manager
 
27
from ironic.drivers.modules import ipmitool
 
28
from ironic.drivers.modules.irmc import common as irmc_common
 
29
from ironic.drivers.modules.irmc import management as irmc_management
 
30
from ironic.drivers import utils as driver_utils
 
31
from ironic.tests.conductor import utils as mgr_utils
 
32
from ironic.tests.db import base as db_base
 
33
from ironic.tests.db import utils as db_utils
 
34
from ironic.tests.objects import utils as obj_utils
 
35
 
 
36
INFO_DICT = db_utils.get_test_irmc_info()
 
37
 
 
38
 
 
39
class IRMCManagementTestCase(db_base.DbTestCase):
 
40
    def setUp(self):
 
41
        super(IRMCManagementTestCase, self).setUp()
 
42
        driver_info = INFO_DICT
 
43
 
 
44
        mgr_utils.mock_the_extension_manager(driver="fake_irmc")
 
45
        self.driver = driver_factory.get_driver("fake_irmc")
 
46
        self.node = obj_utils.create_test_node(self.context,
 
47
                                               driver='fake_irmc',
 
48
                                               driver_info=driver_info)
 
49
        self.info = irmc_common.parse_driver_info(self.node)
 
50
 
 
51
    def test_get_properties(self):
 
52
        expected = irmc_common.COMMON_PROPERTIES
 
53
        expected.update(ipmitool.COMMON_PROPERTIES)
 
54
        expected.update(ipmitool.CONSOLE_PROPERTIES)
 
55
        with task_manager.acquire(self.context, self.node.uuid,
 
56
                                  shared=True) as task:
 
57
            self.assertEqual(expected, task.driver.get_properties())
 
58
 
 
59
    @mock.patch.object(irmc_common, 'parse_driver_info')
 
60
    def test_validate(self, mock_drvinfo):
 
61
        with task_manager.acquire(self.context, self.node.uuid,
 
62
                                  shared=True) as task:
 
63
            task.driver.management.validate(task)
 
64
            mock_drvinfo.assert_called_once_with(task.node)
 
65
 
 
66
    @mock.patch.object(irmc_common, 'parse_driver_info')
 
67
    def test_validate_fail(self, mock_drvinfo):
 
68
        side_effect = exception.InvalidParameterValue("Invalid Input")
 
69
        mock_drvinfo.side_effect = side_effect
 
70
        with task_manager.acquire(self.context, self.node.uuid,
 
71
                                  shared=True) as task:
 
72
            self.assertRaises(exception.InvalidParameterValue,
 
73
                              task.driver.management.validate,
 
74
                              task)
 
75
 
 
76
    def test_management_interface_get_supported_boot_devices(self):
 
77
        with task_manager.acquire(self.context, self.node.uuid) as task:
 
78
            expected = [boot_devices.PXE, boot_devices.DISK,
 
79
                        boot_devices.CDROM, boot_devices.BIOS,
 
80
                        boot_devices.SAFE]
 
81
            self.assertEqual(sorted(expected), sorted(task.driver.management.
 
82
                             get_supported_boot_devices()))
 
83
 
 
84
    @mock.patch.object(ipmitool.IPMIManagement, 'set_boot_device')
 
85
    def test_management_interface_set_boot_device_no_mode_ok(
 
86
            self,
 
87
            set_boot_device_mock):
 
88
        """no boot mode specified."""
 
89
        with task_manager.acquire(self.context, self.node.uuid,
 
90
                                  shared=False) as task:
 
91
            task.driver.management.set_boot_device(task, boot_devices.PXE)
 
92
            set_boot_device_mock.assert_called_once_with(
 
93
                task,
 
94
                boot_devices.PXE,
 
95
                False)
 
96
 
 
97
    @mock.patch.object(ipmitool.IPMIManagement, 'set_boot_device')
 
98
    def test_management_interface_set_boot_device_bios_ok(
 
99
            self,
 
100
            set_boot_device_mock):
 
101
        """bios mode specified."""
 
102
        with task_manager.acquire(self.context, self.node.uuid) as task:
 
103
            driver_utils.add_node_capability(task, 'boot_mode', 'bios')
 
104
            task.driver.management.set_boot_device(task, boot_devices.PXE)
 
105
            set_boot_device_mock.assert_called_once_with(
 
106
                task,
 
107
                boot_devices.PXE,
 
108
                False)
 
109
 
 
110
    @mock.patch.object(irmc_management.ipmitool, "send_raw", autospec=True)
 
111
    def _test_management_interface_set_boot_device_uefi_ok(self, params,
 
112
                                                           expected_raw_code,
 
113
                                                           send_raw_mock):
 
114
        send_raw_mock.return_value = [None, None]
 
115
 
 
116
        with task_manager.acquire(self.context, self.node.uuid) as task:
 
117
            task.node.properties['capabilities'] = ''
 
118
            driver_utils.add_node_capability(task, 'boot_mode', 'uefi')
 
119
            self.driver.management.set_boot_device(task, **params)
 
120
            send_raw_mock.assert_has_calls([
 
121
                mock.call(task, "0x00 0x08 0x03 0x08"),
 
122
                mock.call(task, expected_raw_code)])
 
123
 
 
124
    def test_management_interface_set_boot_device_uefi_ok_pxe(self):
 
125
        params = {'device': boot_devices.PXE, 'persistent': False}
 
126
        self._test_management_interface_set_boot_device_uefi_ok(
 
127
            params,
 
128
            "0x00 0x08 0x05 0xa0 0x04 0x00 0x00 0x00")
 
129
 
 
130
        params['persistent'] = True
 
131
        self._test_management_interface_set_boot_device_uefi_ok(
 
132
            params,
 
133
            "0x00 0x08 0x05 0xe0 0x04 0x00 0x00 0x00")
 
134
 
 
135
    def test_management_interface_set_boot_device_uefi_ok_disk(self):
 
136
        params = {'device': boot_devices.DISK, 'persistent': False}
 
137
        self._test_management_interface_set_boot_device_uefi_ok(
 
138
            params,
 
139
            "0x00 0x08 0x05 0xa0 0x08 0x00 0x00 0x00")
 
140
 
 
141
        params['persistent'] = True
 
142
        self._test_management_interface_set_boot_device_uefi_ok(
 
143
            params,
 
144
            "0x00 0x08 0x05 0xe0 0x08 0x00 0x00 0x00")
 
145
 
 
146
    def test_management_interface_set_boot_device_uefi_ok_cdrom(self):
 
147
        params = {'device': boot_devices.CDROM, 'persistent': False}
 
148
        self._test_management_interface_set_boot_device_uefi_ok(
 
149
            params,
 
150
            "0x00 0x08 0x05 0xa0 0x14 0x00 0x00 0x00")
 
151
 
 
152
        params['persistent'] = True
 
153
        self._test_management_interface_set_boot_device_uefi_ok(
 
154
            params,
 
155
            "0x00 0x08 0x05 0xe0 0x14 0x00 0x00 0x00")
 
156
 
 
157
    def test_management_interface_set_boot_device_uefi_ok_bios(self):
 
158
        params = {'device': boot_devices.BIOS, 'persistent': False}
 
159
        self._test_management_interface_set_boot_device_uefi_ok(
 
160
            params,
 
161
            "0x00 0x08 0x05 0xa0 0x18 0x00 0x00 0x00")
 
162
 
 
163
        params['persistent'] = True
 
164
        self._test_management_interface_set_boot_device_uefi_ok(
 
165
            params,
 
166
            "0x00 0x08 0x05 0xe0 0x18 0x00 0x00 0x00")
 
167
 
 
168
    def test_management_interface_set_boot_device_uefi_ok_safe(self):
 
169
        params = {'device': boot_devices.SAFE, 'persistent': False}
 
170
        self._test_management_interface_set_boot_device_uefi_ok(
 
171
            params,
 
172
            "0x00 0x08 0x05 0xa0 0x0c 0x00 0x00 0x00")
 
173
 
 
174
        params['persistent'] = True
 
175
        self._test_management_interface_set_boot_device_uefi_ok(
 
176
            params,
 
177
            "0x00 0x08 0x05 0xe0 0x0c 0x00 0x00 0x00")
 
178
 
 
179
    @mock.patch.object(irmc_management.ipmitool, "send_raw", autospec=True)
 
180
    def test_management_interface_set_boot_device_uefi_ng(self,
 
181
                                                          send_raw_mock):
 
182
        """uefi mode, next boot only, unknown device."""
 
183
        send_raw_mock.return_value = [None, None]
 
184
 
 
185
        with task_manager.acquire(self.context, self.node.uuid) as task:
 
186
            driver_utils.add_node_capability(task, 'boot_mode', 'uefi')
 
187
            self.assertRaises(exception.InvalidParameterValue,
 
188
                              self.driver.management.set_boot_device,
 
189
                              task,
 
190
                              "unknown")
 
191
 
 
192
    @mock.patch.object(irmc_management, 'scci')
 
193
    @mock.patch.object(irmc_common, 'get_irmc_report')
 
194
    def test_management_interface_get_sensors_data_scci_ok(self,
 
195
                                                          mock_get_irmc_report,
 
196
                                                          mock_scci):
 
197
        """'irmc_sensor_method' = 'scci' specified and OK data."""
 
198
        with open(os.path.join(os.path.dirname(__file__),
 
199
                               'fake_sensors_data_ok.xml'), "r") as report:
 
200
            fake_txt = report.read()
 
201
        fake_xml = ET.fromstring(fake_txt)
 
202
 
 
203
        mock_get_irmc_report.return_value = fake_xml
 
204
        mock_scci.get_sensor_data.return_value = fake_xml.find(
 
205
            "./System/SensorDataRecords")
 
206
 
 
207
        with task_manager.acquire(self.context, self.node.uuid) as task:
 
208
            task.node.driver_info['irmc_sensor_method'] = 'scci'
 
209
            sensor_dict = self.driver.management.get_sensors_data(task)
 
210
 
 
211
        expected = {
 
212
            'Fan (4)': {
 
213
                'FAN1 SYS (29)': {
 
214
                    'Units': 'RPM',
 
215
                    'Sensor ID': 'FAN1 SYS (29)',
 
216
                    'Sensor Reading': '600 RPM'
 
217
                },
 
218
                'FAN2 SYS (29)': {
 
219
                    'Units': 'None',
 
220
                    'Sensor ID': 'FAN2 SYS (29)',
 
221
                    'Sensor Reading': 'None None'
 
222
                }
 
223
            },
 
224
            'Temperature (1)': {
 
225
                'Systemboard 1 (7)': {
 
226
                    'Units': 'degree C',
 
227
                    'Sensor ID': 'Systemboard 1 (7)',
 
228
                    'Sensor Reading': '80 degree C'
 
229
                },
 
230
                'Ambient (55)': {
 
231
                    'Units': 'degree C',
 
232
                    'Sensor ID': 'Ambient (55)',
 
233
                    'Sensor Reading': '42 degree C'
 
234
                }
 
235
            }
 
236
        }
 
237
        self.assertEqual(expected, sensor_dict)
 
238
 
 
239
    @mock.patch.object(irmc_management, 'scci')
 
240
    @mock.patch.object(irmc_common, 'get_irmc_report')
 
241
    def test_management_interface_get_sensors_data_scci_ng(self,
 
242
                                                          mock_get_irmc_report,
 
243
                                                          mock_scci):
 
244
        """'irmc_sensor_method' = 'scci' specified and NG data."""
 
245
        with open(os.path.join(os.path.dirname(__file__),
 
246
                               'fake_sensors_data_ng.xml'), "r") as report:
 
247
            fake_txt = report.read()
 
248
        fake_xml = ET.fromstring(fake_txt)
 
249
 
 
250
        mock_get_irmc_report.return_value = fake_xml
 
251
        mock_scci.get_sensor_data.return_value = fake_xml.find(
 
252
            "./System/SensorDataRecords")
 
253
 
 
254
        with task_manager.acquire(self.context, self.node.uuid) as task:
 
255
            task.node.driver_info['irmc_sensor_method'] = 'scci'
 
256
            sensor_dict = self.driver.management.get_sensors_data(task)
 
257
 
 
258
        self.assertEqual(len(sensor_dict), 0)
 
259
 
 
260
    @mock.patch.object(ipmitool.IPMIManagement, 'get_sensors_data')
 
261
    def test_management_interface_get_sensors_data_ipmitool_ok(
 
262
            self,
 
263
            get_sensors_data_mock):
 
264
        """'irmc_sensor_method' = 'ipmitool' specified."""
 
265
        with task_manager.acquire(self.context, self.node.uuid) as task:
 
266
            task.node.driver_info['irmc_sensor_method'] = 'ipmitool'
 
267
            task.driver.management.get_sensors_data(task)
 
268
            get_sensors_data_mock.assert_called_once_with(task)
 
269
 
 
270
    @mock.patch.object(irmc_common, 'get_irmc_report')
 
271
    def test_management_interface_get_sensors_data_exception1(
 
272
            self,
 
273
            get_irmc_report_mock):
 
274
        """'FailedToGetSensorData Exception."""
 
275
 
 
276
        get_irmc_report_mock.side_effect = Exception("Report Error")
 
277
 
 
278
        with task_manager.acquire(self.context, self.node.uuid) as task:
 
279
            task.node.driver_info['irmc_sensor_method'] = 'scci'
 
280
            e = self.assertRaises(exception.FailedToGetSensorData,
 
281
                                  self.driver.management.get_sensors_data,
 
282
                                  task)
 
283
        self.assertEqual("Failed to get sensor data for node 1be26c0b-" +
 
284
                         "03f2-4d2e-ae87-c02d7f33c123. Error: Report Error",
 
285
                         str(e))