[VMTask] Avoid Floating IP leakage

Moves 'wait_for_ping' from boot_server logic to try-except

Change-Id: I0624c0b523a0cb87720715cb0a22514c2a37b330
Closes-bug: #1492980
This commit is contained in:
Yair Fried 2015-09-10 16:26:58 +03:00
parent 29aa3eb358
commit 8fd4bcb72d
4 changed files with 15 additions and 14 deletions

View File

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

View File

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

View File

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

View File

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