Refactor network_utils to new call_xenapi pattern

Move network_utils to the new call_xenapi style, and add some unit
tests, and to make the unit reasonable, start raising NovaException.

Change-Id: I991965ffd57bf4b309cac8c6e9d4ef94f8d430f2
This commit is contained in:
John Garbutt
2014-02-09 09:56:46 -06:00
committed by Gerrit Code Review
parent 53f52b3000
commit f9d2af5d7c
2 changed files with 89 additions and 6 deletions

View File

@@ -0,0 +1,79 @@
# 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.
import mock
from nova import exception
from nova.tests.virt.xenapi import stubs
from nova.virt.xenapi import network_utils
class NetworkUtilsTestCase(stubs.XenAPITestBaseNoDB):
def setUp(self):
super(NetworkUtilsTestCase, self).setUp()
def test_find_network_with_name_label_works(self):
session = mock.Mock()
session.network.get_by_name_label.return_value = ["net"]
result = network_utils.find_network_with_name_label(session, "label")
self.assertEqual("net", result)
session.network.get_by_name_label.assert_called_once_with("label")
def test_find_network_with_name_returns_none(self):
session = mock.Mock()
session.network.get_by_name_label.return_value = []
result = network_utils.find_network_with_name_label(session, "label")
self.assertIsNone(result)
def test_find_network_with_name_label_raises(self):
session = mock.Mock()
session.network.get_by_name_label.return_value = ["net", "net2"]
self.assertRaises(exception.NovaException,
network_utils.find_network_with_name_label,
session, "label")
def test_find_network_with_bridge_works(self):
session = mock.Mock()
session.network.get_all_records_where.return_value = {"net": "asdf"}
result = network_utils.find_network_with_bridge(session, "bridge")
self.assertEqual(result, "net")
expr = 'field "name__label" = "bridge" or field "bridge" = "bridge"'
session.network.get_all_records_where.assert_called_once_with(expr)
def test_find_network_with_bridge_raises_too_many(self):
session = mock.Mock()
session.network.get_all_records_where.return_value = {
"net": "asdf",
"net2": "asdf2"
}
self.assertRaises(exception.NovaException,
network_utils.find_network_with_bridge,
session, "bridge")
def test_find_network_with_bridge_raises_no_networks(self):
session = mock.Mock()
session.network.get_all_records_where.return_value = {}
self.assertRaises(exception.NovaException,
network_utils.find_network_with_bridge,
session, "bridge")

View File

@@ -18,16 +18,18 @@ records and their attributes like bridges, PIFs, QoS, as well as
their lookup functions.
"""
from nova import exception
from nova.openstack.common.gettextutils import _
def find_network_with_name_label(session, name_label):
networks = session.call_xenapi('network.get_by_name_label', name_label)
networks = session.network.get_by_name_label(name_label)
if len(networks) == 1:
return networks[0]
elif len(networks) > 1:
raise Exception(_('Found non-unique network for name_label %s') %
name_label)
raise exception.NovaException(
_('Found non-unique network for name_label %s') %
name_label)
else:
return None
@@ -39,10 +41,12 @@ def find_network_with_bridge(session, bridge):
"""
expr = ('field "name__label" = "%s" or field "bridge" = "%s"' %
(bridge, bridge))
networks = session.call_xenapi('network.get_all_records_where', expr)
networks = session.network.get_all_records_where(expr)
if len(networks) == 1:
return networks.keys()[0]
elif len(networks) > 1:
raise Exception(_('Found non-unique network for bridge %s') % bridge)
raise exception.NovaException(
_('Found non-unique network for bridge %s') % bridge)
else:
raise Exception(_('Found no network for bridge %s') % bridge)
raise exception.NovaException(
_('Found no network for bridge %s') % bridge)