From b7f9940769618616d37267a50afe495bb8608d27 Mon Sep 17 00:00:00 2001 From: Yaguang Tang Date: Mon, 6 May 2013 15:54:54 +0800 Subject: Add unitests for VMware vif, and fix code logical error. Fix code logical error that cause variable referenced before assignment. Add unittests for vmwareapi vif. Fix bug #1177635 Change-Id: I6656e8b77417061eca2a56d50820f5385c0ae987 --- nova/tests/test_vmwareapi_vif.py | 87 ++++++++++++++++++++++++++++++++++++++++ nova/virt/vmwareapi/fake.py | 6 +++ nova/virt/vmwareapi/vif.py | 34 ++++++++-------- 3 files changed, 109 insertions(+), 18 deletions(-) create mode 100644 nova/tests/test_vmwareapi_vif.py diff --git a/nova/tests/test_vmwareapi_vif.py b/nova/tests/test_vmwareapi_vif.py new file mode 100644 index 000000000..043885543 --- /dev/null +++ b/nova/tests/test_vmwareapi_vif.py @@ -0,0 +1,87 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 +# +# Copyright 2013 Canonical Corp. +# 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.network import model as network_model +from nova import test +from nova.virt.vmwareapi import network_util +from nova.virt.vmwareapi import vif + + +class VMwareVifTestCase(test.TestCase): + def setUp(self): + super(VMwareVifTestCase, self).setUp() + self.flags(vmwareapi_vlan_interface='vmnet0') + network = network_model.Network(id=0, + bridge='fa0', + label='fake', + vlan=3, + bridge_interface='eth0', + injected=True) + + self.vif = network_model.NetworkInfo([ + network_model.VIF(id=None, + address='DE:AD:BE:EF:00:00', + network=network, + type=None, + devname=None, + ovs_interfaceid=None, + rxtx_cap=3) + ])[0] + self.session = "fake" + self.cluster = None + + def tearDown(self): + super(VMwareVifTestCase, self).tearDown() + + def test_ensure_vlan_bridge(self): + self.mox.StubOutWithMock(network_util, 'get_network_with_the_name') + self.mox.StubOutWithMock(network_util, + 'get_vswitch_for_vlan_interface') + self.mox.StubOutWithMock(network_util, + 'check_if_vlan_interface_exists') + self.mox.StubOutWithMock(network_util, 'create_port_group') + network_util.get_network_with_the_name(self.session, 'fa0', + self.cluster).AndReturn(None) + network_util.get_vswitch_for_vlan_interface(self.session, 'vmnet0', + self.cluster).AndReturn('vmnet0') + network_util.check_if_vlan_interface_exists(self.session, 'vmnet0', + self.cluster).AndReturn(True) + network_util.create_port_group(self.session, 'fa0', 'vmnet0', 3, + self.cluster) + + self.mox.ReplayAll() + vif.ensure_vlan_bridge(self.session, self.vif, create_vlan=True) + + # FlatDHCP network mode without vlan + def test_ensure_vlan_bridge_without_vlan(self): + self.mox.StubOutWithMock(network_util, 'get_network_with_the_name') + self.mox.StubOutWithMock(network_util, + 'get_vswitch_for_vlan_interface') + self.mox.StubOutWithMock(network_util, + 'check_if_vlan_interface_exists') + self.mox.StubOutWithMock(network_util, 'create_port_group') + + network_util.get_network_with_the_name(self.session, 'fa0', + self.cluster).AndReturn(None) + network_util.get_vswitch_for_vlan_interface(self.session, 'vmnet0', + self.cluster).AndReturn('vmnet0') + network_util.check_if_vlan_interface_exists(self.session, 'vmnet0', + self.cluster).AndReturn(True) + network_util.create_port_group(self.session, 'fa0', 'vmnet0', 0, + self.cluster) + self.mox.ReplayAll() + vif.ensure_vlan_bridge(self.session, self.vif, create_vlan=False) diff --git a/nova/virt/vmwareapi/fake.py b/nova/virt/vmwareapi/fake.py index ab4bf10e0..86b6e5fc2 100644 --- a/nova/virt/vmwareapi/fake.py +++ b/nova/virt/vmwareapi/fake.py @@ -308,7 +308,13 @@ class HostSystem(ManagedObject): config.product = product summary.config = config + pnic_do = DataObject() + pnic_do.device = "vmnic0" + net_info_pnic = DataObject() + net_info_pnic.PhysicalNic = [pnic_do] + self.set("summary", summary) + self.set("config.network.pnic", net_info_pnic) if _db_content.get("Network", None) is None: create_network() diff --git a/nova/virt/vmwareapi/vif.py b/nova/virt/vmwareapi/vif.py index cb8ce52c0..c98f619e1 100644 --- a/nova/virt/vmwareapi/vif.py +++ b/nova/virt/vmwareapi/vif.py @@ -45,17 +45,15 @@ def ensure_vlan_bridge(session, vif, cluster=None, create_vlan=True): # Get the vSwitch associated with the Physical Adapter vswitch_associated = network_util.get_vswitch_for_vlan_interface( session, vlan_interface, cluster) + if vswitch_associated is None: + raise exception.SwitchNotFoundForNetworkAdapter( + adapter=vlan_interface) + # Check if the vlan_interface physical network adapter exists on the + # host. + if not network_util.check_if_vlan_interface_exists(session, + vlan_interface, cluster): + raise exception.NetworkAdapterNotFound(adapter=vlan_interface) if create_vlan: - # Check if the vlan_interface physical network adapter exists on the - # host. - if not network_util.check_if_vlan_interface_exists(session, - vlan_interface, - cluster): - raise exception.NetworkAdapterNotFound(adapter=vlan_interface) - - if vswitch_associated is None: - raise exception.SwitchNotFoundForNetworkAdapter( - adapter=vlan_interface) if network_ref is None: # Create a port group on the vSwitch associated with the @@ -69,15 +67,15 @@ def ensure_vlan_bridge(session, vif, cluster=None, create_vlan=True): _get_pg_info = network_util.get_vlanid_and_vswitch_for_portgroup pg_vlanid, pg_vswitch = _get_pg_info(session, bridge, cluster) - # Check if the vswitch associated is proper - if pg_vswitch != vswitch_associated: - raise exception.InvalidVLANPortGroup( - bridge=bridge, expected=vswitch_associated, - actual=pg_vswitch) + # Check if the vswitch associated is proper + if pg_vswitch != vswitch_associated: + raise exception.InvalidVLANPortGroup( + bridge=bridge, expected=vswitch_associated, + actual=pg_vswitch) - # Check if the vlan id is proper for the port group - if pg_vlanid != vlan_num: - raise exception.InvalidVLANTag(bridge=bridge, tag=vlan_num, + # Check if the vlan id is proper for the port group + if pg_vlanid != vlan_num: + raise exception.InvalidVLANTag(bridge=bridge, tag=vlan_num, pgroup=pg_vlanid) else: if network_ref is None: -- cgit