XenAPI: add unit test for plugin bandwidth

Add unit test for plugin bandwidth. And there is a bug in
the plugin which does not close the opened file. This change
will try to fix it. It also splits _read_proc_net() into two
functions, so that it's easier to make the UT.

Change-Id: Ie871b1d16cf180e9709e46bb0d67573053d14376
This commit is contained in:
Jianghua Wang
2016-03-01 08:38:12 +00:00
parent 97c57fb7b2
commit ab5e67f8ea
2 changed files with 60 additions and 3 deletions

View File

@@ -0,0 +1,49 @@
# Copyright (c) 2016 OpenStack Foundation
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from nova.tests.unit.virt.xenapi.plugins import plugin_test
class BandwidthTestCase(plugin_test.PluginTestBase):
def setUp(self):
super(BandwidthTestCase, self).setUp()
self.pluginlib = self.load_plugin("pluginlib_nova.py")
# Prevent any logging to syslog
self.mock_patch_object(self.pluginlib,
'configure_logging')
self.bandwidth = self.load_plugin("bandwidth")
def test_get_bandwitdth_from_proc(self):
fake_data = ['Inter-| Receive | Transmit',
'if|bw_in i1 i2 i3 i4 i5 i6 i7|bw_out o1 o2 o3 o4 o5 o6 o7',
'xenbr1: 1 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0',
'vif2.0: 2 0 0 0 0 0 0 0 12 0 0 0 0 0 0 0',
'vif2.1: 3 0 0 0 0 0 0 0 13 0 0 0 0 0 0 0\n']
expect_devmap = {'2': {'1': {'bw_in': 13, 'bw_out': 3},
'0': {'bw_in': 12, 'bw_out': 2}
}
}
mock_read_proc_net = self.mock_patch_object(
self.bandwidth,
'_read_proc_net',
return_val=fake_data)
devmap = self.bandwidth._get_bandwitdth_from_proc()
self.assertTrue(mock_read_proc_net.called)
self.assertEqual(devmap, expect_devmap)

View File

@@ -29,8 +29,16 @@ pluginlib_nova.configure_logging('bandwidth')
def _read_proc_net():
devs = [l.strip() for l in open('/proc/net/dev', 'r').readlines()]
# Ignore headers
f = open('/proc/net/dev', 'r')
try:
return f.readlines()
finally:
f.close()
def _get_bandwitdth_from_proc():
devs = [l.strip() for l in _read_proc_net()]
# ignore headers
devs = devs[2:]
dlist = [d.split(':', 1) for d in devs if d.startswith('vif')]
devmap = dict()
@@ -46,7 +54,7 @@ def _read_proc_net():
def fetch_all_bandwidth(session):
return _read_proc_net()
return _get_bandwitdth_from_proc()
if __name__ == '__main__':