Merge "Change endpoint initialization"

This commit is contained in:
Jenkins 2015-08-04 09:52:46 +00:00 committed by Gerrit Code Review
commit fb2c8fc6ce
2 changed files with 22 additions and 13 deletions

View File

@ -49,14 +49,15 @@ class Network(context.Context):
"networks_per_tenant": 1
}
def __init__(self, ctx):
super(Network, self).__init__(ctx)
self.net_wrapper = network_wrapper.wrap(
osclients.Clients(ctx["admin"]["endpoint"]),
self.config)
@utils.log_task_wrapper(LOG.info, _("Enter context: `network`"))
def setup(self):
# NOTE(rkiran): Some clients are not thread-safe. Thus during
# multithreading/multiprocessing, it is likely the
# sockets are left open. This problem is eliminated by
# creating a connection in setup and cleanup seperately.
net_wrapper = network_wrapper.wrap(
osclients.Clients(self.context["admin"]["endpoint"]),
self.config)
for user, tenant_id in (utils.iterate_per_tenants(
self.context.get("users", []))):
self.context["tenants"][tenant_id]["networks"] = []
@ -64,17 +65,20 @@ class Network(context.Context):
# NOTE(amaretskiy): add_router and subnets_num take effect
# for Neutron only.
# NOTE(amaretskiy): Do we need neutron subnets_num > 1 ?
network = self.net_wrapper.create_network(tenant_id,
add_router=True,
subnets_num=1)
network = net_wrapper.create_network(tenant_id,
add_router=True,
subnets_num=1)
self.context["tenants"][tenant_id]["networks"].append(network)
@utils.log_task_wrapper(LOG.info, _("Exit context: `network`"))
def cleanup(self):
net_wrapper = network_wrapper.wrap(
osclients.Clients(self.context["admin"]["endpoint"]),
self.config)
for tenant_id, tenant_ctx in six.iteritems(self.context["tenants"]):
for network in tenant_ctx.get("networks", []):
with logging.ExceptionLogger(
LOG,
_("Failed to delete network for tenant %s")
% tenant_id):
self.net_wrapper.delete_network(network)
net_wrapper.delete_network(network)

View File

@ -27,6 +27,8 @@ class NetworkTestCase(test.TestCase):
return {"task": {"uuid": "foo_task"},
"admin": {"endpoint": "foo_admin"},
"config": {"network": kwargs},
"users": [{"id": "foo_user", "tenant_id": "foo_tenant"},
{"id": "bar_user", "tenant_id": "bar_tenant"}],
"tenants": {"foo_tenant": {"networks": [{"id": "foo_net"}]},
"bar_tenant": {"networks": [{"id": "bar_net"}]}}}
@ -35,16 +37,17 @@ class NetworkTestCase(test.TestCase):
@mock.patch("rally.osclients.Clients")
@mock.patch(NET + "wrap", return_value="foo_service")
def test__init__(self, mock_wrap, mock_clients):
def test__init__default(self, mock_wrap, mock_clients):
context = network_context.Network(self.get_context())
self.assertEqual(context.net_wrapper, "foo_service")
self.assertEqual(context.config["networks_per_tenant"], 1)
self.assertEqual(context.config["start_cidr"],
network_context.Network.DEFAULT_CONFIG["start_cidr"])
@mock.patch("rally.osclients.Clients")
@mock.patch(NET + "wrap", return_value="foo_service")
def test__init__explicit(self, mock_wrap, mock_clients):
context = network_context.Network(
self.get_context(start_cidr="foo_cidr", networks_per_tenant=42))
self.assertEqual(context.net_wrapper, "foo_service")
self.assertEqual(context.config["networks_per_tenant"], 42)
self.assertEqual(context.config["start_cidr"], "foo_cidr")
@ -61,6 +64,8 @@ class NetworkTestCase(test.TestCase):
net_context = network_context.Network(
self.get_context(networks_per_tenant=nets_per_tenant))
net_context.setup()
mock_utils.iterate_per_tenants.assert_called_once_with(
net_context.context["users"])
expected_networks = [["bar_tenant-net"] * nets_per_tenant,
["foo_tenant-net"] * nets_per_tenant]
actual_networks = []