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

« back to all changes in this revision

Viewing changes to ironic/tests/drivers/test_ipmitool.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:
16
16
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
17
#    License for the specific language governing permissions and limitations
18
18
#    under the License.
 
19
#
19
20
 
20
21
"""Test class for IPMITool driver module."""
21
22
 
27
28
import mock
28
29
from oslo_concurrency import processutils
29
30
from oslo_config import cfg
 
31
from oslo_utils import uuidutils
30
32
 
31
33
from ironic.common import boot_devices
32
34
from ironic.common import driver_factory
221
223
                driver_info=INFO_DICT)
222
224
        self.info = ipmi._parse_driver_info(self.node)
223
225
 
224
 
    def test__make_password_file(self, mock_sleep):
225
 
        with ipmi._make_password_file(self.info.get('password')) as pw_file:
226
 
            del_chk_pw_file = pw_file
227
 
            self.assertTrue(os.path.isfile(pw_file))
228
 
            self.assertEqual(0o600, os.stat(pw_file)[stat.ST_MODE] & 0o777)
229
 
            with open(pw_file, "r") as f:
230
 
                password = f.read()
231
 
            self.assertEqual(self.info.get('password'), password)
232
 
        self.assertFalse(os.path.isfile(del_chk_pw_file))
 
226
    def _test__make_password_file(self, mock_sleep, input_password,
 
227
                                  exception_to_raise=None):
 
228
        pw_file = None
 
229
        try:
 
230
            with ipmi._make_password_file(input_password) as pw_file:
 
231
                if exception_to_raise is not None:
 
232
                    raise exception_to_raise
 
233
                self.assertTrue(os.path.isfile(pw_file))
 
234
                self.assertEqual(0o600, os.stat(pw_file)[stat.ST_MODE] & 0o777)
 
235
                with open(pw_file, "r") as f:
 
236
                    password = f.read()
 
237
                self.assertEqual(str(input_password), password)
 
238
        finally:
 
239
            if pw_file is not None:
 
240
                self.assertFalse(os.path.isfile(pw_file))
 
241
 
 
242
    def test__make_password_file_str_password(self, mock_sleep):
 
243
        self._test__make_password_file(mock_sleep, self.info.get('password'))
 
244
 
 
245
    def test__make_password_file_with_numeric_password(self, mock_sleep):
 
246
        self._test__make_password_file(mock_sleep, 12345)
 
247
 
 
248
    def test__make_password_file_caller_exception(self, mock_sleep):
 
249
        # Test caller raising exception
 
250
        result = self.assertRaises(
 
251
            ValueError,
 
252
            self._test__make_password_file,
 
253
            mock_sleep, 12345, ValueError('we should fail'))
 
254
        self.assertEqual(result.message, 'we should fail')
 
255
 
 
256
    @mock.patch.object(tempfile, 'NamedTemporaryFile',
 
257
                       new=mock.MagicMock(side_effect=OSError('Test Error')))
 
258
    def test__make_password_file_tempfile_known_exception(self, mock_sleep):
 
259
        # Test OSError exception in _make_password_file for
 
260
        # tempfile.NamedTemporaryFile
 
261
        self.assertRaises(
 
262
            exception.PasswordFileFailedToCreate,
 
263
            self._test__make_password_file, mock_sleep, 12345)
 
264
 
 
265
    @mock.patch.object(
 
266
        tempfile, 'NamedTemporaryFile',
 
267
        new=mock.MagicMock(side_effect=OverflowError('Test Error')))
 
268
    def test__make_password_file_tempfile_unknown_exception(self, mock_sleep):
 
269
        # Test exception in _make_password_file for tempfile.NamedTemporaryFile
 
270
        result = self.assertRaises(
 
271
            OverflowError,
 
272
            self._test__make_password_file, mock_sleep, 12345)
 
273
        self.assertEqual(result.message, 'Test Error')
 
274
 
 
275
    def test__make_password_file_write_exception(self, mock_sleep):
 
276
        # Test exception in _make_password_file for write()
 
277
        mock_namedtemp = mock.mock_open(mock.MagicMock(name='JLV'))
 
278
        with mock.patch('tempfile.NamedTemporaryFile', mock_namedtemp):
 
279
            mock_filehandle = mock_namedtemp.return_value
 
280
            mock_write = mock_filehandle.write
 
281
            mock_write.side_effect = OSError('Test 2 Error')
 
282
            self.assertRaises(
 
283
                exception.PasswordFileFailedToCreate,
 
284
                self._test__make_password_file, mock_sleep, 12345)
233
285
 
234
286
    def test__parse_driver_info(self, mock_sleep):
235
287
        # make sure we get back the expected things
477
529
            '-U', self.info['username'],
478
530
            '-f', file_handle1,
479
531
            'A', 'B', 'C',
480
 
        ],
481
 
        [
 
532
        ], [
482
533
            'ipmitool',
483
534
            '-I', 'lanplus',
484
535
            '-H', self.info['address'],
522
573
            '-U', self.info['username'],
523
574
            '-f', file_handle1,
524
575
            'A', 'B', 'C',
525
 
        ],
526
 
        [
 
576
        ], [
527
577
            'ipmitool',
528
578
            '-I', 'lanplus',
529
579
            '-H', self.info['address'],
569
619
            '-U', self.info['username'],
570
620
            '-f', file_handle1,
571
621
            'A', 'B', 'C',
572
 
        ],
573
 
        [
 
622
        ], [
574
623
            'ipmitool',
575
624
            '-I', 'lanplus',
576
625
            '-H', '127.127.127.127',
955
1004
                    "fake state")
956
1005
 
957
1006
    @mock.patch.object(ipmi, '_exec_ipmitool', autospec=True)
958
 
    def test__send_raw_bytes_ok(self, mock_exec):
 
1007
    def test_send_raw_bytes_ok(self, mock_exec):
959
1008
        mock_exec.return_value = [None, None]
960
1009
 
961
1010
        with task_manager.acquire(self.context,
966
1015
        mock_exec.assert_called_once_with(self.info, 'raw 0x00 0x01')
967
1016
 
968
1017
    @mock.patch.object(ipmi, '_exec_ipmitool', autospec=True)
969
 
    def test__send_raw_bytes_fail(self, mock_exec):
 
1018
    def test_send_raw_bytes_fail(self, mock_exec):
970
1019
        mock_exec.side_effect = exception.PasswordFileFailedToCreate('error')
971
1020
 
972
1021
        with task_manager.acquire(self.context,
1313
1362
    def test_management_interface_validate_fail(self):
1314
1363
        # Missing IPMI driver_info information
1315
1364
        node = obj_utils.create_test_node(self.context,
1316
 
                                          uuid=utils.generate_uuid(),
 
1365
                                          uuid=uuidutils.generate_uuid(),
1317
1366
                                          driver='fake_ipmitool')
1318
1367
        with task_manager.acquire(self.context, node.uuid) as task:
1319
1368
            self.assertRaises(exception.MissingParameterValue,
1488
1537
                                     'Negative Hysteresis': '375.000',
1489
1538
                                     'Sensor ID': 'FAN MOD 1A RPM (0x30)',
1490
1539
                                     'Nominal Reading': '5325.000'
1491
 
                                  }
 
1540
                                 }
1492
1541
                             },
1493
1542
                             'Temperature': {
1494
1543
                                 'Temp (0x2)': {