From 5a0cc7428fcc4daee0510335faab846b6492d371 Mon Sep 17 00:00:00 2001 From: Kun Huang Date: Wed, 19 Aug 2015 16:45:04 +0800 Subject: [PATCH] Move auto_assign_nic to class method Moving logic of auto_assign_nic to class function is to help re-use it in _boot_servers (it is used in _boot_server only now). Change-Id: I22e80757b2394fcec63a55da6bee291d3b8fd573 Partial-Bug: #1477583 --- .../plugins/openstack/scenarios/nova/utils.py | 26 ++++++++++++------- .../openstack/scenarios/nova/test_utils.py | 21 +++++++++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/rally/plugins/openstack/scenarios/nova/utils.py b/rally/plugins/openstack/scenarios/nova/utils.py index 68f40bdd..21e579ea 100644 --- a/rally/plugins/openstack/scenarios/nova/utils.py +++ b/rally/plugins/openstack/scenarios/nova/utils.py @@ -96,6 +96,19 @@ class NovaScenario(scenario.OpenStackScenario): """Returns user servers list.""" return self.clients("nova").servers.list(detailed) + def _pick_random_nic(self): + """Choose one network from existing ones.""" + ctxt = self.context + nets = [net["id"] + for net in ctxt.get("tenant", {}).get("networks", [])] + if nets: + # NOTE(amaretskiy): Balance servers among networks: + # divmod(iteration % tenants_num, nets_num)[1] + net_idx = divmod( + (ctxt["iteration"] % ctxt["config"]["users"]["tenants"]), + len(nets))[1] + return [{"net-id": nets[net_idx]}] + @atomic.action_timer("nova.boot_server") def _boot_server(self, image_id, flavor_id, auto_assign_nic=False, name=None, **kwargs): @@ -122,16 +135,9 @@ class NovaScenario(scenario.OpenStackScenario): kwargs["security_groups"].append(secgroup["name"]) if auto_assign_nic and not kwargs.get("nics", False): - nets = [net["id"] for net in - self.context.get("tenant", {}).get("networks", [])] - if nets: - # NOTE(amaretskiy): Balance servers among networks: - # divmod(iteration % tenants_num, nets_num)[1] - net_idx = divmod( - (self.context["iteration"] - % self.context["config"]["users"]["tenants"]), - len(nets))[1] - kwargs["nics"] = [{"net-id": nets[net_idx]}] + nic = self._pick_random_nic() + if nic: + kwargs["nics"] = nic server = self.clients("nova").servers.create( server_name, image_id, flavor_id, **kwargs) diff --git a/tests/unit/plugins/openstack/scenarios/nova/test_utils.py b/tests/unit/plugins/openstack/scenarios/nova/test_utils.py index 2a164ea4..e7f2f440 100644 --- a/tests/unit/plugins/openstack/scenarios/nova/test_utils.py +++ b/tests/unit/plugins/openstack/scenarios/nova/test_utils.py @@ -46,6 +46,27 @@ class NovaScenarioTestCase(test.ScenarioTestCase): self._test_atomic_action_timer(nova_scenario.atomic_actions(), "nova.list_servers") + def test__pick_random_nic(self): + context = {"tenant": {"networks": [{"id": "net_id_1"}, + {"id": "net_id_2"}]}, + "iteration": 0, + "config": {"users": {"tenants": 2}}} + nova_scenario = utils.NovaScenario(context=context) + nic1 = nova_scenario._pick_random_nic() + self.assertEqual(nic1, [{"net-id": "net_id_1"}]) + + context["iteration"] = 1 + nova_scenario = utils.NovaScenario(context=context) + nic2 = nova_scenario._pick_random_nic() + # balance to net 2 + self.assertEqual(nic2, [{"net-id": "net_id_2"}]) + + context["iteration"] = 2 + nova_scenario = utils.NovaScenario(context=context) + nic3 = nova_scenario._pick_random_nic() + # balance again, get net 1 + self.assertEqual(nic3, [{"net-id": "net_id_1"}]) + @mock.patch(NOVA_UTILS + ".NovaScenario._generate_random_name", return_value="foo_server_name") def test__boot_server(self, mock__generate_random_name):