From 58322b5f44bbcf7441ef94b8e5fd1a239caac70d Mon Sep 17 00:00:00 2001 From: Andrey Kurilin Date: Wed, 13 May 2015 14:33:52 +0300 Subject: [PATCH] Change logic of wait_for_ping If host is not pingable, TimeoutException will be raisen with unreadable message. This patch fixes such situation. Closes-Bug: #1454381 Change-Id: If10e27a77f79b59c162b7e6148eecf234c0160a7 --- rally/plugins/openstack/scenarios/vm/utils.py | 22 +++++++++----- .../openstack/scenarios/vm/test_utils.py | 30 +++++++++++-------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/rally/plugins/openstack/scenarios/vm/utils.py b/rally/plugins/openstack/scenarios/vm/utils.py index 0cb969ea..7e47e062 100644 --- a/rally/plugins/openstack/scenarios/vm/utils.py +++ b/rally/plugins/openstack/scenarios/vm/utils.py @@ -29,6 +29,9 @@ from rally import exceptions LOG = logging.getLogger(__name__) +ICMP_UP_STATUS = "ICMP UP" +ICMP_DOWN_STATUS = "ICMP DOWN" + class VMScenario(base.Scenario): """Base class for VM scenarios with basic atomic actions. @@ -124,9 +127,11 @@ class VMScenario(base.Scenario): @base.atomic_action_timer("vm.wait_for_ping") def _wait_for_ping(self, server_ip): + server_ip = netaddr.IPAddress(server_ip) bench_utils.wait_for( server_ip, - is_ready=self._ping_ip_address, + is_ready=bench_utils.resource_is(ICMP_UP_STATUS, + self._ping_ip_address), timeout=120 ) @@ -156,13 +161,16 @@ class VMScenario(base.Scenario): script, is_file) @staticmethod - def _ping_ip_address(host, should_succeed=True): - ip = netaddr.IPAddress(host) - ping = "ping" if ip.version == 4 else "ping6" + def _ping_ip_address(host): + """Check ip address that it is pingable. + + :param host: instance of `netaddr.IPAddress` + """ + ping = "ping" if host.version == 4 else "ping6" if sys.platform.startswith("linux"): - cmd = [ping, "-c1", "-w1", host] + cmd = [ping, "-c1", "-w1", str(host)] else: - cmd = [ping, "-c1", host] + cmd = [ping, "-c1", str(host)] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, @@ -170,4 +178,4 @@ class VMScenario(base.Scenario): proc.wait() LOG.debug("Host %s is ICMP %s" % (host, proc.returncode and "down" or "up")) - return (proc.returncode == 0) == should_succeed + return ICMP_UP_STATUS if (proc.returncode == 0) else ICMP_DOWN_STATUS diff --git a/tests/unit/plugins/openstack/scenarios/vm/test_utils.py b/tests/unit/plugins/openstack/scenarios/vm/test_utils.py index 9572ac0c..6ac6cddb 100644 --- a/tests/unit/plugins/openstack/scenarios/vm/test_utils.py +++ b/tests/unit/plugins/openstack/scenarios/vm/test_utils.py @@ -17,6 +17,7 @@ import subprocess import mock +import netaddr from oslotest import mockpatch import six @@ -64,14 +65,17 @@ class VMScenarioTestCase(test.TestCase): vm_scenario._wait_for_ssh(ssh) ssh.wait.assert_called_once_with() + @mock.patch(VMTASKS_UTILS + ".bench_utils.resource_is") @mock.patch(VMTASKS_UTILS + ".VMScenario._ping_ip_address", return_value=True) - def test__wait_for_ping(self, mock__ping): + def test__wait_for_ping(self, mock__ping, mock_resource_is): vm_scenario = utils.VMScenario() - vm_scenario._wait_for_ping("1.2.3.4") - self.wait_for.mock.assert_called_once_with("1.2.3.4", - is_ready=mock__ping, - timeout=120) + vm_scenario._wait_for_ping(netaddr.IPAddress("1.2.3.4")) + self.wait_for.mock.assert_called_once_with( + netaddr.IPAddress("1.2.3.4"), + is_ready=mock_resource_is.return_value, + timeout=120) + mock_resource_is.assert_called_once_with("ICMP UP", mock__ping) @mock.patch(VMTASKS_UTILS + ".VMScenario._run_command_over_ssh") @mock.patch("rally.common.sshutils.SSH") @@ -120,11 +124,11 @@ class VMScenarioTestCase(test.TestCase): mock_sys.platform = "linux2" vm_scenario = utils.VMScenario() - host_ip = "1.2.3.4" + host_ip = netaddr.IPAddress("1.2.3.4") self.assertTrue(vm_scenario._ping_ip_address(host_ip)) mock_subprocess.assert_called_once_with( - ["ping", "-c1", "-w1", host_ip], + ["ping", "-c1", "-w1", str(host_ip)], stderr=subprocess.PIPE, stdout=subprocess.PIPE) ping_process.wait.assert_called_once_with() @@ -137,11 +141,11 @@ class VMScenarioTestCase(test.TestCase): mock_sys.platform = "linux2" vm_scenario = utils.VMScenario() - host_ip = "1ce:c01d:bee2:15:a5:900d:a5:11fe" + host_ip = netaddr.IPAddress("1ce:c01d:bee2:15:a5:900d:a5:11fe") self.assertTrue(vm_scenario._ping_ip_address(host_ip)) mock_subprocess.assert_called_once_with( - ["ping6", "-c1", "-w1", host_ip], + ["ping6", "-c1", "-w1", str(host_ip)], stderr=subprocess.PIPE, stdout=subprocess.PIPE) ping_process.wait.assert_called_once_with() @@ -154,11 +158,11 @@ class VMScenarioTestCase(test.TestCase): mock_sys.platform = "freebsd10" vm_scenario = utils.VMScenario() - host_ip = "1.2.3.4" + host_ip = netaddr.IPAddress("1.2.3.4") self.assertTrue(vm_scenario._ping_ip_address(host_ip)) mock_subprocess.assert_called_once_with( - ["ping", "-c1", host_ip], + ["ping", "-c1", str(host_ip)], stderr=subprocess.PIPE, stdout=subprocess.PIPE) ping_process.wait.assert_called_once_with() @@ -171,11 +175,11 @@ class VMScenarioTestCase(test.TestCase): mock_sys.platform = "freebsd10" vm_scenario = utils.VMScenario() - host_ip = "1ce:c01d:bee2:15:a5:900d:a5:11fe" + host_ip = netaddr.IPAddress("1ce:c01d:bee2:15:a5:900d:a5:11fe") self.assertTrue(vm_scenario._ping_ip_address(host_ip)) mock_subprocess.assert_called_once_with( - ["ping6", "-c1", host_ip], + ["ping6", "-c1", str(host_ip)], stderr=subprocess.PIPE, stdout=subprocess.PIPE) ping_process.wait.assert_called_once_with()