Adds coverage for net strategies

This commit is contained in:
Matt Dietz
2013-07-26 23:50:37 +00:00
parent 0ca887dfa7
commit 00b3a96f9b
2 changed files with 100 additions and 2 deletions

View File

@@ -1,3 +1,18 @@
# Copyright 2013 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.
import json
from neutron.common import exceptions
@@ -15,10 +30,13 @@ CONF.register_opts(quark_opts, "QUARK")
class JSONStrategy(object):
def __init__(self):
def __init__(self, strategy=None):
self.reverse_strategy = {}
self.strategy = {}
self._compile_strategy(CONF.QUARK.default_net_strategy)
if not strategy:
self._compile_strategy(CONF.QUARK.default_net_strategy)
else:
self._compile_strategy(strategy)
def _compile_strategy(self, strategy):
strategy = json.loads(strategy)

View File

@@ -0,0 +1,80 @@
# Copyright 2013 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.
import json
from neutron.common import exceptions
from oslo.config import cfg
from quark import network_strategy
from quark.tests import test_base
class TestJSONStrategy(test_base.TestBase):
def setUp(self):
self.context = None
self.strategy = {"public_network":
{"required": True,
"children": {"nova": "child_net"}}}
strategy_json = json.dumps(self.strategy)
cfg.CONF.set_override("default_net_strategy", strategy_json, "QUARK")
def test_get_assignable_networks_default_strategy(self):
json_strategy = network_strategy.JSONStrategy()
net_ids = json_strategy.get_assignable_networks(self.context)
self.assertEqual("public_network", net_ids[0])
def test_get_assignable_networks_custom_strategy(self):
custom = {"private_network": self.strategy["public_network"]}
json_strategy = network_strategy.JSONStrategy(json.dumps(custom))
net_ids = json_strategy.get_assignable_networks(self.context)
self.assertEqual("private_network", net_ids[0])
def test_split_network_ids(self):
json_strategy = network_strategy.JSONStrategy()
net_ids = ["foo_net", "public_network"]
tenant, assignable = json_strategy.split_network_ids(self.context,
net_ids)
self.assertTrue("foo_net" in tenant)
self.assertTrue("foo_net" not in assignable)
self.assertTrue("public_network" not in tenant)
self.assertTrue("public_network" in assignable)
def test_get_parent_network(self):
json_strategy = network_strategy.JSONStrategy(None)
parent_net = json_strategy.get_parent_network("child_net")
self.assertEqual(parent_net, "public_network")
def test_get_parent_network_no_parent(self):
json_strategy = network_strategy.JSONStrategy(None)
parent_net = json_strategy.get_parent_network("bar_network")
self.assertEqual(parent_net, "bar_network")
def test_best_match_network_id(self):
json_strategy = network_strategy.JSONStrategy(None)
net = json_strategy.best_match_network_id(self.context,
"public_network", "nova")
self.assertEqual(net, "child_net")
def test_best_match_network_net_not_in_strategy(self):
json_strategy = network_strategy.JSONStrategy(None)
net = json_strategy.best_match_network_id(self.context,
"foo_net", "nova")
self.assertEqual(net, "foo_net")
def test_best_match_network_no_valid_child(self):
json_strategy = network_strategy.JSONStrategy(None)
with self.assertRaises(exceptions.NetworkNotFound):
json_strategy.best_match_network_id(self.context,
"public_network", "derpa")