From 54d5335edddd4c75dd3c8b6dc344761cb95abd9e Mon Sep 17 00:00:00 2001 From: Vladyslav Drok Date: Fri, 7 Jul 2017 13:25:17 +0300 Subject: [PATCH] Mock random generator for BackoffLoopingCall in IPMI unittests Set power state methods now use BackoffLoopingCall to wait for the desired power state. It uses random.SystemRandom gauss distribution to determine the amount of time to wait for the next check. This change adds a method to mock the result of the pseudo-random sleep interval generation, so that the value to wait between power state checks is 1. Closes-Bug: 1702859 Change-Id: I9270a187fa0f413ba8a8a14f859b0fd65c439b6e --- .../unit/drivers/modules/test_ipmitool.py | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/ironic/tests/unit/drivers/modules/test_ipmitool.py b/ironic/tests/unit/drivers/modules/test_ipmitool.py index fdb8706847..f5a8c22eaf 100644 --- a/ironic/tests/unit/drivers/modules/test_ipmitool.py +++ b/ironic/tests/unit/drivers/modules/test_ipmitool.py @@ -22,6 +22,7 @@ import contextlib import os +import random import stat import subprocess import tempfile @@ -350,6 +351,19 @@ class IPMIToolPrivateMethodTestCase(db_base.DbTestCase): driver_info=INFO_DICT) self.info = ipmi._parse_driver_info(self.node) + # power actions use oslo_service.BackoffLoopingCall, + # mock random.SystemRandom gauss distribution + self._mock_system_random_distribution() + + def _mock_system_random_distribution(self): + # random.SystemRandom with gauss distribution is used by oslo_service's + # BackoffLoopingCall, it multiplies default interval (equals to 1) by + # 2 * return_value, so if you want BackoffLoopingCall to "sleep" for + # 1 second, return_value should be 0.5. + m = mock.patch.object(random.SystemRandom, 'gauss', return_value=0.5) + m.start() + self.addCleanup(m.stop) + def _test__make_password_file(self, mock_sleep, input_password, exception_to_raise=None): pw_file = None @@ -1259,7 +1273,7 @@ class IPMIToolPrivateMethodTestCase(db_base.DbTestCase): self.assertRaises(exception.PowerStateFailure, ipmi._power_on, task, self.info, timeout=2) - self.assertEqual(mock_exec.call_args_list, expected) + self.assertEqual(expected, mock_exec.call_args_list) @mock.patch.object(ipmi, '_exec_ipmitool', autospec=True) @mock.patch('eventlet.greenthread.sleep', autospec=True) @@ -1279,8 +1293,8 @@ class IPMIToolPrivateMethodTestCase(db_base.DbTestCase): with task_manager.acquire(self.context, self.node.uuid) as task: state = ipmi._soft_power_off(task, self.info) - self.assertEqual(mock_exec.call_args_list, expected) - self.assertEqual(state, states.POWER_OFF) + self.assertEqual(expected, mock_exec.call_args_list) + self.assertEqual(states.POWER_OFF, state) @mock.patch.object(ipmi, '_exec_ipmitool', autospec=True) @mock.patch('eventlet.greenthread.sleep', autospec=True) @@ -1302,7 +1316,7 @@ class IPMIToolPrivateMethodTestCase(db_base.DbTestCase): self.assertRaises(exception.PowerStateFailure, ipmi._soft_power_off, task, self.info, timeout=2) - self.assertEqual(mock_exec.call_args_list, expected) + self.assertEqual(expected, mock_exec.call_args_list) @mock.patch.object(ipmi, '_power_status', autospec=True) @mock.patch.object(ipmi, '_exec_ipmitool', autospec=True)