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
This commit is contained in:
Kun Huang 2015-08-19 16:45:04 +08:00 committed by Sergey Skripnick
parent ba1db0c4c4
commit 5a0cc7428f
2 changed files with 37 additions and 10 deletions

View File

@ -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)

View File

@ -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):