Fix usage of BandwidthUsage.create()

The _poll_bandwidth_usage periodic task was attempting to use
BandwidthUsage as a classmethod even though it is not defined as a
classmethod.  It has been updated to be used properly and a test was
added to ensure there is no error.

Change-Id: I96709675a952857da5121da29acd1ab2e6726fd0
Closes-Bug: #1392423
This commit is contained in:
Andrew Laski
2014-11-13 13:03:04 -05:00
parent 4c45902548
commit 31f05239d2
2 changed files with 34 additions and 6 deletions

View File

@@ -5565,9 +5565,6 @@ class ComputeManager(manager.Manager):
curr_time - self._last_bw_usage_cell_update > curr_time - self._last_bw_usage_cell_update >
cells_update_interval): cells_update_interval):
self._last_bw_usage_cell_update = curr_time self._last_bw_usage_cell_update = curr_time
update_cells = True
else:
update_cells = False
instances = objects.InstanceList.get_by_host(context, instances = objects.InstanceList.get_by_host(context,
self.host, self.host,
@@ -5624,7 +5621,7 @@ class ComputeManager(manager.Manager):
else: else:
bw_out += (bw_ctr['bw_out'] - last_ctr_out) bw_out += (bw_ctr['bw_out'] - last_ctr_out)
objects.BandwidthUsage.create(context, objects.BandwidthUsage(context=context).create(
bw_ctr['uuid'], bw_ctr['uuid'],
bw_ctr['mac_address'], bw_ctr['mac_address'],
bw_in, bw_in,
@@ -5632,8 +5629,7 @@ class ComputeManager(manager.Manager):
bw_ctr['bw_in'], bw_ctr['bw_in'],
bw_ctr['bw_out'], bw_ctr['bw_out'],
start_period=start_time, start_period=start_time,
last_refreshed=refreshed, last_refreshed=refreshed)
update_cells=update_cells)
def _get_host_volume_bdms(self, context, use_slave=False): def _get_host_volume_bdms(self, context, use_slave=False):
"""Return all block device mappings on a compute host.""" """Return all block device mappings on a compute host."""

View File

@@ -22,6 +22,7 @@ import mox
from oslo.config import cfg from oslo.config import cfg
from oslo import messaging from oslo import messaging
from oslo.utils import importutils from oslo.utils import importutils
from oslo.utils import timeutils
from nova.compute import manager from nova.compute import manager
from nova.compute import power_state from nova.compute import power_state
@@ -2028,6 +2029,37 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
self.assertTrue(mock_save.called) self.assertTrue(mock_save.called)
self.assertTrue(mock_spawn.called) self.assertTrue(mock_spawn.called)
@mock.patch.object(utils, 'last_completed_audit_period',
return_value=(0, 0))
@mock.patch.object(time, 'time', side_effect=[10, 20, 21])
@mock.patch.object(objects.InstanceList, 'get_by_host', return_value=[])
@mock.patch.object(objects.BandwidthUsage, 'get_by_instance_uuid_and_mac')
@mock.patch.object(db, 'bw_usage_update')
def test_poll_bandwidth_usage(self, bw_usage_update, get_by_uuid_mac,
get_by_host, time, last_completed_audit):
bw_counters = [{'uuid': 'fake-uuid', 'mac_address': 'fake-mac',
'bw_in': 1, 'bw_out': 2}]
usage = objects.BandwidthUsage()
usage.bw_in = 3
usage.bw_out = 4
usage.last_ctr_in = 0
usage.last_ctr_out = 0
self.flags(bandwidth_poll_interval=1)
get_by_uuid_mac.return_value = usage
_time = timeutils.utcnow()
bw_usage_update.return_value = {'instance_uuid': '', 'mac': '',
'start_period': _time, 'last_refreshed': _time, 'bw_in': 0,
'bw_out': 0, 'last_ctr_in': 0, 'last_ctr_out': 0, 'deleted': 0,
'created_at': _time, 'updated_at': _time, 'deleted_at': _time}
with mock.patch.object(self.compute.driver,
'get_all_bw_counters', return_value=bw_counters):
self.compute._poll_bandwidth_usage(self.context)
get_by_uuid_mac.assert_called_once_with(self.context, 'fake-uuid',
'fake-mac', start_period=0, use_slave=True)
bw_usage_update.assert_called_once_with(self.context, 'fake-uuid',
'fake-mac', 0, 4, 6, 1, 2,
last_refreshed=timeutils.isotime(_time))
class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase): class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
def setUp(self): def setUp(self):