From e252c303da7933c534fe9e841f5a8230dd7222cd Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Thu, 23 Jul 2015 15:28:53 -0500 Subject: [PATCH] Nova: make nova server scenarios easier with existing users nova.utils._boot_server() requires configuration for the "users" context, but if running with existing users that will not (necessarily) exist. This makes it handle existing users as well. This change also switches the Nova _boot_server tests to ddt. Change-Id: If9ca77fd73e881a0082757035cb67ffbc0914315 --- .../plugins/openstack/scenarios/nova/utils.py | 7 +- .../openstack/scenarios/nova/test_utils.py | 168 +++++------------- 2 files changed, 49 insertions(+), 126 deletions(-) diff --git a/rally/plugins/openstack/scenarios/nova/utils.py b/rally/plugins/openstack/scenarios/nova/utils.py index 21e579ea..758539a1 100644 --- a/rally/plugins/openstack/scenarios/nova/utils.py +++ b/rally/plugins/openstack/scenarios/nova/utils.py @@ -102,11 +102,8 @@ class NovaScenario(scenario.OpenStackScenario): 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] + # NOTE(amaretskiy): Balance servers among networks. + net_idx = self.context["iteration"] % len(nets) return [{"net-id": nets[net_idx]}] @atomic.action_timer("nova.boot_server") diff --git a/tests/unit/plugins/openstack/scenarios/nova/test_utils.py b/tests/unit/plugins/openstack/scenarios/nova/test_utils.py index e7f2f440..a4fb19c7 100644 --- a/tests/unit/plugins/openstack/scenarios/nova/test_utils.py +++ b/tests/unit/plugins/openstack/scenarios/nova/test_utils.py @@ -13,6 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. +import ddt import mock from oslo_config import cfg @@ -26,6 +27,7 @@ NOVA_UTILS = "rally.plugins.openstack.scenarios.nova.utils" CONF = cfg.CONF +@ddt.ddt class NovaScenarioTestCase(test.ScenarioTestCase): def setUp(self): @@ -49,8 +51,7 @@ class NovaScenarioTestCase(test.ScenarioTestCase): def test__pick_random_nic(self): context = {"tenant": {"networks": [{"id": "net_id_1"}, {"id": "net_id_2"}]}, - "iteration": 0, - "config": {"users": {"tenants": 2}}} + "iteration": 0} nova_scenario = utils.NovaScenario(context=context) nic1 = nova_scenario._pick_random_nic() self.assertEqual(nic1, [{"net-id": "net_id_1"}]) @@ -67,41 +68,33 @@ class NovaScenarioTestCase(test.ScenarioTestCase): # 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): + @ddt.data( + {}, + {"kwargs": {"auto_assign_nic": True}}, + {"kwargs": {"auto_assign_nic": True, "nics": [{"net-id": "baz_id"}]}}, + {"context": {"user": {"secgroup": {"name": "test"}}}}, + {"context": {"user": {"secgroup": {"name": "new"}}}, + "kwargs": {"security_groups": ["test"]}}, + {"context": {"user": {"secgroup": {"name": "test1"}}}, + "kwargs": {"security_groups": ["test1"]}}, + ) + @ddt.unpack + def test__boot_server(self, context=None, kwargs=None): self.clients("nova").servers.create.return_value = self.server - nova_scenario = utils.NovaScenario(context={}) - return_server = nova_scenario._boot_server("image_id", - "flavor_id") - self.mock_wait_for.mock.assert_called_once_with( - self.server, - is_ready=self.mock_resource_is.mock.return_value, - update_resource=self.mock_get_from_manager.mock.return_value, - check_interval=CONF.benchmark.nova_server_boot_poll_interval, - timeout=CONF.benchmark.nova_server_boot_timeout) - self.mock_resource_is.mock.assert_called_once_with("ACTIVE") - self.mock_get_from_manager.mock.assert_called_once_with() - self.assertEqual(self.mock_wait_for.mock.return_value, return_server) - self.clients("nova").servers.create.assert_called_once_with( - "foo_server_name", "image_id", "flavor_id") - self._test_atomic_action_timer(nova_scenario.atomic_actions(), - "nova.boot_server") - @mock.patch(NOVA_UTILS + ".NovaScenario._generate_random_name", - return_value="foo_server_name") - def test__boot_server_with_network(self, mock__generate_random_name): - self.clients("nova").servers.create.return_value = self.server - networks = [{"id": "foo_id", "external": False}, - {"id": "bar_id", "external": False}] - self.clients("nova").networks.list.return_value = networks - nova_scenario = utils.NovaScenario(context={ - "iteration": 3, - "config": {"users": {"tenants": 2}}, - "tenant": {"networks": networks}}) - return_server = nova_scenario._boot_server("image_id", - "flavor_id", - auto_assign_nic=True) + if context is None: + context = {} + context.setdefault("user", {}).setdefault("endpoint", mock.MagicMock()) + context.setdefault("config", {}) + + nova_scenario = utils.NovaScenario(context=context) + nova_scenario._generate_random_name = mock.Mock() + nova_scenario._pick_random_nic = mock.Mock() + if kwargs is None: + kwargs = {} + kwargs["fakearg"] = "fakearg" + return_server = nova_scenario._boot_server("image_id", "flavor_id", + **kwargs) self.mock_wait_for.mock.assert_called_once_with( self.server, is_ready=self.mock_resource_is.mock.return_value, @@ -110,10 +103,26 @@ class NovaScenarioTestCase(test.ScenarioTestCase): timeout=CONF.benchmark.nova_server_boot_timeout) self.mock_resource_is.mock.assert_called_once_with("ACTIVE") self.mock_get_from_manager.mock.assert_called_once_with() - self.clients("nova").servers.create.assert_called_once_with( - "foo_server_name", "image_id", "flavor_id", - nics=[{"net-id": "bar_id"}]) self.assertEqual(self.mock_wait_for.mock.return_value, return_server) + + expected_kwargs = {"fakearg": "fakearg"} + if "nics" in kwargs: + expected_kwargs["nics"] = kwargs["nics"] + elif "auto_assign_nic" in kwargs: + expected_kwargs["nics"] = (nova_scenario._pick_random_nic. + return_value) + + expected_secgroups = set() + if "security_groups" in kwargs: + expected_secgroups.update(kwargs["security_groups"]) + if "secgroup" in context["user"]: + expected_secgroups.add(context["user"]["secgroup"]["name"]) + if expected_secgroups: + expected_kwargs["security_groups"] = list(expected_secgroups) + + self.clients("nova").servers.create.assert_called_once_with( + nova_scenario._generate_random_name.return_value, + "image_id", "flavor_id", **expected_kwargs) self._test_atomic_action_timer(nova_scenario.atomic_actions(), "nova.boot_server") @@ -125,89 +134,6 @@ class NovaScenarioTestCase(test.ScenarioTestCase): "image_id", "flavor_id", auto_assign_nic=True) - @mock.patch(NOVA_UTILS + ".NovaScenario._generate_random_name", - return_value="foo_server_name") - def test__boot_server_with_ssh(self, mock__generate_random_name): - self.clients("nova").servers.create.return_value = self.server - nova_scenario = utils.NovaScenario(context={ - "user": { - "secgroup": {"name": "test"}, - "endpoint": mock.MagicMock() - }} - ) - return_server = nova_scenario._boot_server("image_id", "flavor_id") - self.mock_wait_for.mock.assert_called_once_with( - self.server, - is_ready=self.mock_resource_is.mock.return_value, - update_resource=self.mock_get_from_manager.mock.return_value, - check_interval=CONF.benchmark.nova_server_boot_poll_interval, - timeout=CONF.benchmark.nova_server_boot_timeout) - self.mock_resource_is.mock.assert_called_once_with("ACTIVE") - self.mock_get_from_manager.mock.assert_called_once_with() - self.assertEqual(self.mock_wait_for.mock.return_value, return_server) - self.clients("nova").servers.create.assert_called_once_with( - "foo_server_name", "image_id", "flavor_id", - security_groups=["test"]) - self._test_atomic_action_timer(nova_scenario.atomic_actions(), - "nova.boot_server") - - @mock.patch(NOVA_UTILS + ".NovaScenario._generate_random_name", - return_value="foo_server_name") - def test__boot_server_with_sec_group(self, mock__generate_random_name): - self.clients("nova").servers.create.return_value = self.server - nova_scenario = utils.NovaScenario(context={ - "user": { - "secgroup": {"name": "new"}, - "endpoint": mock.MagicMock() - } - }) - return_server = nova_scenario._boot_server( - "image_id", "flavor_id", - security_groups=["test"]) - self.mock_wait_for.mock.assert_called_once_with( - self.server, - is_ready=self.mock_resource_is.mock.return_value, - update_resource=self.mock_get_from_manager.mock.return_value, - check_interval=CONF.benchmark.nova_server_boot_poll_interval, - timeout=CONF.benchmark.nova_server_boot_timeout) - self.mock_resource_is.mock.assert_called_once_with("ACTIVE") - self.mock_get_from_manager.mock.assert_called_once_with() - self.assertEqual(self.mock_wait_for.mock.return_value, return_server) - self.clients("nova").servers.create.assert_called_once_with( - "foo_server_name", "image_id", "flavor_id", - security_groups=["test", "new"]) - self._test_atomic_action_timer(nova_scenario.atomic_actions(), - "nova.boot_server") - - @mock.patch(NOVA_UTILS + ".NovaScenario._generate_random_name", - return_value="foo_server_name") - def test__boot_server_with_similar_sec_group(self, - mock__generate_random_name): - self.clients("nova").servers.create.return_value = self.server - nova_scenario = utils.NovaScenario(context={ - "user": { - "secgroup": {"name": "test1"}, - "endpoint": mock.MagicMock() - }} - ) - return_server = nova_scenario._boot_server( - "image_id", "flavor_id", - security_groups=["test1"]) - self.mock_wait_for.mock.assert_called_once_with( - self.server, - is_ready=self.mock_resource_is.mock.return_value, - update_resource=self.mock_get_from_manager.mock.return_value, - check_interval=CONF.benchmark.nova_server_boot_poll_interval, - timeout=CONF.benchmark.nova_server_boot_timeout) - self.mock_resource_is.mock.assert_called_once_with("ACTIVE") - self.mock_get_from_manager.mock.assert_called_once_with() - self.assertEqual(self.mock_wait_for.mock.return_value, return_server) - self.clients("nova").servers.create.assert_called_once_with( - "foo_server_name", "image_id", "flavor_id", - security_groups=["test1"]) - self._test_atomic_action_timer(nova_scenario.atomic_actions(), - "nova.boot_server") - def test__suspend_server(self): nova_scenario = utils.NovaScenario() nova_scenario._suspend_server(self.server)