Change in vdsm[master]: VmStatsThread: make _getNicStats testable

danken at redhat.com danken at redhat.com
Fri Mar 21 12:50:05 UTC 2014


Dan Kenigsberg has uploaded a new change for review.

Change subject: VmStatsThread: make _getNicStats testable
......................................................................

VmStatsThread: make _getNicStats testable

With this test, the shame of change Ie356d5f96c73b6 should not repeat.

Change-Id: Ibd6ffa12341b814775bea96a47365645b571f3a8
Signed-off-by: Dan Kenigsberg <danken at redhat.com>
---
M tests/vmTests.py
M vdsm/vm.py
2 files changed, 50 insertions(+), 27 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/82/25982/1

diff --git a/tests/vmTests.py b/tests/vmTests.py
index e9efb55..96c903e 100644
--- a/tests/vmTests.py
+++ b/tests/vmTests.py
@@ -755,3 +755,21 @@
             stats = fake.getStats()
             self.assertEqual(stats['exitReason'], exitReason)
             self.assertEqual(stats['exitMessage'], msg)
+
+
+class TestVmStatsThread(TestCaseBase):
+    def testGetNicStats(self):
+        GBPS = 10 ** 9 / 8
+        with FakeVM() as fake:
+            mock_stats_thread = vm.VmStatsThread(fake)
+            res = mock_stats_thread._getNicStats(
+                'vnettest', 'virtio', '00:11',
+                (2 ** 64 - 15 * GBPS, 1, 2, 3, 0, 4, 5, 6),
+                (0, 7, 8, 9, 5 * GBPS, 10, 11, 12),
+                15.0)
+            self.assertEqual(res, {
+                'rxErrors': '8', 'rxDropped': '9',
+                'txErrors': '11', 'txDropped': '12',
+                'macAddr': '00:11', 'name': 'vnettest',
+                'speed': '1000', 'state': 'unknown',
+                'rxRate': '100.0', 'txRate': '33.3'})
diff --git a/vdsm/vm.py b/vdsm/vm.py
index b81653d..bd424b2 100644
--- a/vdsm/vm.py
+++ b/vdsm/vm.py
@@ -585,6 +585,35 @@
             stats['cpuUser'] = 0.0
             stats['cpuSys'] = 0.0
 
+    def _getNicStats(self, name, model, mac, sInfo, eInfo, sampleInterval):
+        ifSpeed = [100, 1000][model in ('e1000', 'virtio')]
+
+        ifStats = {'macAddr': mac,
+                   'name': name,
+                   'speed': str(ifSpeed),
+                   'state': 'unknown'}
+
+        ifStats['rxErrors'] = str(eInfo[2])
+        ifStats['rxDropped'] = str(eInfo[3])
+        ifStats['txErrors'] = str(eInfo[6])
+        ifStats['txDropped'] = str(eInfo[7])
+
+        try:
+            ifRxBytes = (100.0 *
+                         ((eInfo[0] - sInfo[0]) % 2 ** 32) /
+                         sampleInterval / ifSpeed / self.MBPS_TO_BPS)
+            ifTxBytes = (100.0 *
+                         ((eInfo[4] - sInfo[4]) % 2 ** 32) /
+                         sampleInterval / ifSpeed / self.MBPS_TO_BPS)
+
+            logging.error('%s %s %s', eInfo[4], sInfo[4], ifTxBytes)
+            ifStats['rxRate'] = '%.1f' % ifRxBytes
+            ifStats['txRate'] = '%.1f' % ifTxBytes
+        except (KeyError, TypeError, ZeroDivisionError):
+            self._log.debug("Network stats not available")
+
+        return ifStats
+
     def _getNetworkStats(self, stats):
         stats['network'] = {}
         sInfo, eInfo, sampleInterval = self.sampleNet.getStats()
@@ -592,34 +621,10 @@
         for nic in self._vm._devices[NIC_DEVICES]:
             if nic.name.startswith('hostdev'):
                 continue
-            ifSpeed = [100, 1000][nic.nicModel in ('e1000', 'virtio')]
 
-            ifStats = {'macAddr': nic.macAddr,
-                       'name': nic.name,
-                       'speed': str(ifSpeed),
-                       'state': 'unknown'}
-
-            try:
-                ifStats['rxErrors'] = str(eInfo[nic.name][2])
-                ifStats['rxDropped'] = str(eInfo[nic.name][3])
-                ifStats['txErrors'] = str(eInfo[nic.name][6])
-                ifStats['txDropped'] = str(eInfo[nic.name][7])
-
-                ifRxBytes = (100.0 *
-                             ((eInfo[nic.name][0] - sInfo[nic.name][0]) %
-                              2 ** 32) /
-                             sampleInterval / ifSpeed / self.MBPS_TO_BPS)
-                ifTxBytes = (100.0 *
-                             ((eInfo[nic.name][4] - sInfo[nic.name][4]) %
-                              2 ** 32) /
-                             sampleInterval / ifSpeed / self.MBPS_TO_BPS)
-
-                ifStats['rxRate'] = '%.1f' % ifRxBytes
-                ifStats['txRate'] = '%.1f' % ifTxBytes
-            except (KeyError, TypeError, ZeroDivisionError):
-                self._log.debug("Network stats not available")
-
-            stats['network'][nic.name] = ifStats
+            stats['network'][nic.name] = self._getNicStats(
+                nic.name, nic.model, nic.macAddr,
+                sInfo[nic.name], eInfo[nic.name], sampleInterval)
 
     def _getDiskStats(self, stats):
         sInfo, eInfo, sampleInterval = self.sampleDisk.getStats()


-- 
To view, visit http://gerrit.ovirt.org/25982
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibd6ffa12341b814775bea96a47365645b571f3a8
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Dan Kenigsberg <danken at redhat.com>


More information about the vdsm-patches mailing list