Merge "vnics: don't presume existence of filterref/filter"

This commit is contained in:
Jenkins 2013-05-22 15:08:01 +00:00 committed by Gerrit Code Review
commit a031f7b779
4 changed files with 71 additions and 13 deletions

View File

@ -212,6 +212,12 @@ class NetPollster(plugin.ComputePollster):
resource_metadata['instance_type'] = \ resource_metadata['instance_type'] = \
instance.flavor['id'] if instance.flavor else None instance.flavor['id'] if instance.flavor else None
if vnic_data.fref is not None:
rid = vnic_data.fref
else:
instance_name = _instance_name(instance)
rid = "%s-%s-%s" % (instance_name, instance.id, vnic_data.name)
return counter.Counter( return counter.Counter(
name=name, name=name,
type=type, type=type,
@ -219,7 +225,7 @@ class NetPollster(plugin.ComputePollster):
volume=volume, volume=volume,
user_id=instance.user_id, user_id=instance.user_id,
project_id=instance.tenant_id, project_id=instance.tenant_id,
resource_id=vnic_data.fref, resource_id=rid,
timestamp=timeutils.isotime(), timestamp=timeutils.isotime(),
resource_metadata=resource_metadata resource_metadata=resource_metadata
) )

View File

@ -110,7 +110,10 @@ class LibvirtInspector(virt_inspector.Inspector):
for iface in tree.findall('devices/interface'): for iface in tree.findall('devices/interface'):
name = iface.find('target').get('dev') name = iface.find('target').get('dev')
mac = iface.find('mac').get('address') mac = iface.find('mac').get('address')
fref = iface.find('filterref').get('filter') fref = iface.find('filterref')
if fref is not None:
fref = fref.get('filter')
params = dict((p.get('name').lower(), p.get('value')) params = dict((p.get('name').lower(), p.get('value'))
for p in iface.findall('filterref/parameter')) for p in iface.findall('filterref/parameter'))
interface = virt_inspector.Interface(name=name, mac=mac, interface = virt_inspector.Interface(name=name, mac=mac,

View File

@ -124,7 +124,18 @@ class TestNetPollster(TestPollsterBase):
dhcp_server='10.0.0.2')) dhcp_server='10.0.0.2'))
stats1 = virt_inspector.InterfaceStats(rx_bytes=5L, rx_packets=6L, stats1 = virt_inspector.InterfaceStats(rx_bytes=5L, rx_packets=6L,
tx_bytes=7L, tx_packets=8L) tx_bytes=7L, tx_packets=8L)
vnics = [(vnic0, stats0), (vnic1, stats1)] vnic2 = virt_inspector.Interface(
name='vnet2',
fref=None,
mac='fa:18:4e:72:fc:7e',
parameters=dict(ip='192.168.0.4',
projmask='255.255.255.0',
projnet='proj3',
dhcp_server='10.0.0.3'))
stats2 = virt_inspector.InterfaceStats(rx_bytes=9L, rx_packets=10L,
tx_bytes=11L, tx_packets=12L)
vnics = [(vnic0, stats0), (vnic1, stats1), (vnic2, stats2)]
self.inspector.inspect_vnics(self.instance.name).AndReturn(vnics) self.inspector.inspect_vnics(self.instance.name).AndReturn(vnics)
self.mox.ReplayAll() self.mox.ReplayAll()
@ -136,21 +147,39 @@ class TestNetPollster(TestPollsterBase):
self.assertEqual(set([c.name for c in counters]), self.assertEqual(set([c.name for c in counters]),
set(pollster.get_counter_names())) set(pollster.get_counter_names()))
def _verify_vnic_metering(name, ip, expected_volume): def _verify_vnic_metering(name, ip, expected_volume, expected_rid):
match = [c for c in counters if c.name == name and match = [c for c in counters if c.name == name and
c.resource_metadata['parameters']['ip'] == ip] c.resource_metadata['parameters']['ip'] == ip]
self.assertEquals(len(match), 1, 'missing counter %s' % name) self.assertEquals(len(match), 1, 'missing counter %s' % name)
self.assertEquals(match[0].volume, expected_volume) self.assertEquals(match[0].volume, expected_volume)
self.assertEquals(match[0].type, 'cumulative') self.assertEquals(match[0].type, 'cumulative')
self.assertEquals(match[0].resource_id, expected_rid)
_verify_vnic_metering('network.incoming.bytes', '10.0.0.2', 1L) instance_name_id = "%s-%s" % (self.instance.name, self.instance.id)
_verify_vnic_metering('network.incoming.bytes', '192.168.0.3', 5L) _verify_vnic_metering('network.incoming.bytes', '10.0.0.2', 1L,
_verify_vnic_metering('network.outgoing.bytes', '10.0.0.2', 3L) vnic0.fref)
_verify_vnic_metering('network.outgoing.bytes', '192.168.0.3', 7L) _verify_vnic_metering('network.incoming.bytes', '192.168.0.3', 5L,
_verify_vnic_metering('network.incoming.packets', '10.0.0.2', 2L) vnic1.fref)
_verify_vnic_metering('network.incoming.packets', '192.168.0.3', 6L) _verify_vnic_metering('network.incoming.bytes', '192.168.0.4', 9L,
_verify_vnic_metering('network.outgoing.packets', '10.0.0.2', 4L) "%s-%s" % (instance_name_id, vnic2.name))
_verify_vnic_metering('network.outgoing.packets', '192.168.0.3', 8L) _verify_vnic_metering('network.outgoing.bytes', '10.0.0.2', 3L,
vnic0.fref)
_verify_vnic_metering('network.outgoing.bytes', '192.168.0.3', 7L,
vnic1.fref)
_verify_vnic_metering('network.outgoing.bytes', '192.168.0.4', 11L,
"%s-%s" % (instance_name_id, vnic2.name))
_verify_vnic_metering('network.incoming.packets', '10.0.0.2', 2L,
vnic0.fref)
_verify_vnic_metering('network.incoming.packets', '192.168.0.3', 6L,
vnic1.fref)
_verify_vnic_metering('network.incoming.packets', '192.168.0.4', 10L,
"%s-%s" % (instance_name_id, vnic2.name))
_verify_vnic_metering('network.outgoing.packets', '10.0.0.2', 4L,
vnic0.fref)
_verify_vnic_metering('network.outgoing.packets', '192.168.0.3', 8L,
vnic1.fref)
_verify_vnic_metering('network.outgoing.packets', '192.168.0.4', 12L,
"%s-%s" % (instance_name_id, vnic2.name))
class TestCPUPollster(TestPollsterBase): class TestCPUPollster(TestPollsterBase):

View File

@ -73,6 +73,14 @@ class TestLibvirtInspection(test_base.TestCase):
</filterref> </filterref>
<alias name='net1'/> <alias name='net1'/>
</interface> </interface>
<interface type='bridge'>
<mac address='fa:16:3e:96:33:f0'/>
<source bridge='qbr420008b3-7c'/>
<target dev='vnet2'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' \
slot='0x03' function='0x0'/>
</interface>
</devices> </devices>
</domain> </domain>
""" """
@ -82,11 +90,13 @@ class TestLibvirtInspection(test_base.TestCase):
3L, 4L, 0L, 0L)) 3L, 4L, 0L, 0L))
self.domain.interfaceStats('vnet1').AndReturn((5L, 6L, 0L, 0L, self.domain.interfaceStats('vnet1').AndReturn((5L, 6L, 0L, 0L,
7L, 8L, 0L, 0L)) 7L, 8L, 0L, 0L))
self.domain.interfaceStats('vnet2').AndReturn((9L, 10L, 0L, 0L,
11L, 12L, 0L, 0L))
self.mox.ReplayAll() self.mox.ReplayAll()
interfaces = list(self.inspector.inspect_vnics(self.instance_name)) interfaces = list(self.inspector.inspect_vnics(self.instance_name))
self.assertEquals(len(interfaces), 2) self.assertEquals(len(interfaces), 3)
vnic0, info0 = interfaces[0] vnic0, info0 = interfaces[0]
self.assertEqual(vnic0.name, 'vnet0') self.assertEqual(vnic0.name, 'vnet0')
self.assertEqual(vnic0.mac, 'fa:16:3e:71:ec:6d') self.assertEqual(vnic0.mac, 'fa:16:3e:71:ec:6d')
@ -115,6 +125,16 @@ class TestLibvirtInspection(test_base.TestCase):
self.assertEqual(info1.tx_bytes, 7L) self.assertEqual(info1.tx_bytes, 7L)
self.assertEqual(info1.tx_packets, 8L) self.assertEqual(info1.tx_packets, 8L)
vnic2, info2 = interfaces[2]
self.assertEqual(vnic2.name, 'vnet2')
self.assertEqual(vnic2.mac, 'fa:16:3e:96:33:f0')
self.assertEqual(vnic2.fref, None)
self.assertEqual(vnic2.parameters, dict())
self.assertEqual(info2.rx_bytes, 9L)
self.assertEqual(info2.rx_packets, 10L)
self.assertEqual(info2.tx_bytes, 11L)
self.assertEqual(info2.tx_packets, 12L)
def test_inspect_disks(self): def test_inspect_disks(self):
dom_xml = """ dom_xml = """
<domain type='kvm'> <domain type='kvm'>