From 8fd4bcb72d3be2dd837006008b0d7d33d728d104 Mon Sep 17 00:00:00 2001 From: Yair Fried Date: Thu, 10 Sep 2015 16:26:58 +0300 Subject: [PATCH] [VMTask] Avoid Floating IP leakage Moves 'wait_for_ping' from boot_server logic to try-except Change-Id: I0624c0b523a0cb87720715cb0a22514c2a37b330 Closes-bug: #1492980 --- rally/plugins/openstack/scenarios/vm/utils.py | 8 ++------ rally/plugins/openstack/scenarios/vm/vmtasks.py | 5 +++++ .../unit/plugins/openstack/scenarios/vm/test_utils.py | 6 ++---- .../plugins/openstack/scenarios/vm/test_vmtasks.py | 10 ++++++---- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/rally/plugins/openstack/scenarios/vm/utils.py b/rally/plugins/openstack/scenarios/vm/utils.py index 93f6dcf9..0fdd63f2 100644 --- a/rally/plugins/openstack/scenarios/vm/utils.py +++ b/rally/plugins/openstack/scenarios/vm/utils.py @@ -100,9 +100,8 @@ class VMScenario(nova_utils.NovaScenario, cinder_utils.CinderScenario): return ssh.execute(cmd, stdin=stdin) - def _boot_server_with_fip(self, image, flavor, - use_floating_ip=True, floating_network=None, - wait_for_ping=True, **kwargs): + def _boot_server_with_fip(self, image, flavor, use_floating_ip=True, + floating_network=None, **kwargs): """Boot server prepared for SSH actions.""" kwargs["auto_assign_nic"] = True server = self._boot_server(image, flavor, **kwargs) @@ -120,9 +119,6 @@ class VMScenario(nova_utils.NovaScenario, cinder_utils.CinderScenario): internal_network = list(server.networks)[0] fip = {"ip": server.addresses[internal_network][0]["addr"]} - if wait_for_ping: - self._wait_for_ping(fip["ip"]) - return server, {"ip": fip.get("ip"), "id": fip.get("id"), "is_floating": use_floating_ip} diff --git a/rally/plugins/openstack/scenarios/vm/vmtasks.py b/rally/plugins/openstack/scenarios/vm/vmtasks.py index f36ca678..0c50e412 100644 --- a/rally/plugins/openstack/scenarios/vm/vmtasks.py +++ b/rally/plugins/openstack/scenarios/vm/vmtasks.py @@ -55,6 +55,7 @@ class VMTasks(vm_utils.VMScenario): port=22, use_floating_ip=True, force_delete=False, + wait_for_ping=True, **kwargs): """Boot a server, run a script that outputs JSON, delete the server. @@ -140,6 +141,7 @@ class VMTasks(vm_utils.VMScenario): :param port: ssh port for SSH connection :param use_floating_ip: bool, floating or fixed IP for SSH connection :param force_delete: whether to use force_delete for servers + :param wait_for_ping: whether to check connectivity on server creation :param **kwargs: extra arguments for booting the server :returns: dictionary with keys `data' and `errors': data: dict, JSON output from the script @@ -159,6 +161,9 @@ class VMTasks(vm_utils.VMScenario): key_name=self.context["user"]["keypair"]["name"], **kwargs) try: + if wait_for_ping: + self._wait_for_ping(fip["ip"]) + code, out, err = self._run_command( fip["ip"], port, username, password, command=command) if code: diff --git a/tests/unit/plugins/openstack/scenarios/vm/test_utils.py b/tests/unit/plugins/openstack/scenarios/vm/test_utils.py index 3a59cd7a..4d2cf024 100644 --- a/tests/unit/plugins/openstack/scenarios/vm/test_utils.py +++ b/tests/unit/plugins/openstack/scenarios/vm/test_utils.py @@ -227,7 +227,7 @@ class VMScenarioTestCase(test.ScenarioTestCase): scenario._attach_floating_ip = mock.Mock() server, ip = scenario._boot_server_with_fip( "foo_image", "foo_flavor", floating_network="ext_network", - use_floating_ip=False, wait_for_ping=True, foo_arg="foo_value") + use_floating_ip=False, foo_arg="foo_value") self.assertEqual(ip, {"ip": "foo_ip", "id": None, "is_floating": False}) @@ -235,7 +235,6 @@ class VMScenarioTestCase(test.ScenarioTestCase): "foo_image", "foo_flavor", auto_assign_nic=True, foo_arg="foo_value") self.assertEqual(scenario._attach_floating_ip.mock_calls, []) - scenario._wait_for_ping.assert_called_once_with("foo_ip") def test__boot_server_with_fip(self): scenario, server = self.get_scenario() @@ -243,7 +242,7 @@ class VMScenarioTestCase(test.ScenarioTestCase): return_value={"id": "foo_id", "ip": "foo_ip"}) server, ip = scenario._boot_server_with_fip( "foo_image", "foo_flavor", floating_network="ext_network", - use_floating_ip=True, wait_for_ping=True, foo_arg="foo_value") + use_floating_ip=True, foo_arg="foo_value") self.assertEqual(ip, {"ip": "foo_ip", "id": "foo_id", "is_floating": True}) @@ -252,7 +251,6 @@ class VMScenarioTestCase(test.ScenarioTestCase): auto_assign_nic=True, foo_arg="foo_value") scenario._attach_floating_ip.assert_called_once_with( server, "ext_network") - scenario._wait_for_ping.assert_called_once_with("foo_ip") def test__delete_server_with_fixed_ip(self): ip = {"ip": "foo_ip", "id": None, "is_floating": False} diff --git a/tests/unit/plugins/openstack/scenarios/vm/test_vmtasks.py b/tests/unit/plugins/openstack/scenarios/vm/test_vmtasks.py index ce2dac52..4ce684e6 100644 --- a/tests/unit/plugins/openstack/scenarios/vm/test_vmtasks.py +++ b/tests/unit/plugins/openstack/scenarios/vm/test_vmtasks.py @@ -33,6 +33,7 @@ class VMTasksTestCase(test.TestCase): self.ip = {"id": "foo_id", "ip": "foo_ip", "is_floating": True} self.scenario._boot_server_with_fip = mock.Mock( return_value=("foo_server", self.ip)) + self.scenario._wait_for_ping = mock.Mock() self.scenario._delete_server_with_fip = mock.Mock() self.scenario._create_volume = mock.Mock( return_value=mock.Mock(id="foo_volume")) @@ -59,11 +60,12 @@ class VMTasksTestCase(test.TestCase): self.scenario._create_volume.assert_called_once_with( 16, imageRef=None) self.scenario._boot_server_with_fip.assert_called_once_with( - "foo_image", "foo_flavor", use_floating_ip="use_fip", - floating_network="ext_network", key_name="keypair_name", + "foo_image", "foo_flavor", key_name="keypair_name", + use_floating_ip="use_fip", floating_network="ext_network", block_device_mapping={"vdrally": "foo_volume:::1"}, foo_arg="foo_value") + self.scenario._wait_for_ping.assert_called_once_with("foo_ip") self.scenario._run_command.assert_called_once_with( "foo_ip", 22, "foo_username", "foo_password", command={"script_file": "foo_script", @@ -86,8 +88,8 @@ class VMTasksTestCase(test.TestCase): self.scenario._create_volume.assert_called_once_with( 16, imageRef=None) self.scenario._boot_server_with_fip.assert_called_once_with( - "foo_image", "foo_flavor", use_floating_ip="use_fip", - floating_network="ext_network", key_name="keypair_name", + "foo_image", "foo_flavor", key_name="keypair_name", + use_floating_ip="use_fip", floating_network="ext_network", block_device_mapping={"vdrally": "foo_volume:::1"}, foo_arg="foo_value")