Merge "Change logic of wait_for_ping"

This commit is contained in:
Jenkins
2015-06-01 12:39:05 +00:00
committed by Gerrit Code Review
4 changed files with 43 additions and 22 deletions

View File

@@ -41,11 +41,12 @@ def get_status(resource):
class resource_is(object):
def __init__(self, desired_status):
def __init__(self, desired_status, status_getter=None):
self.desired_status = desired_status
self.status_getter = status_getter or get_status
def __call__(self, resource):
return get_status(resource) == self.desired_status.upper()
return self.status_getter(resource) == self.desired_status.upper()
def __str__(self):
return str(self.desired_status)

View File

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

View File

@@ -46,10 +46,18 @@ class BenchmarkUtilsTestCase(test.TestCase):
def test_resource_is(self):
is_active = utils.resource_is("ACTIVE")
self.assertEqual(is_active.status_getter, utils.get_status)
self.assertTrue(is_active(fakes.FakeResource(status="active")))
self.assertTrue(is_active(fakes.FakeResource(status="aCtIvE")))
self.assertFalse(is_active(fakes.FakeResource(status="ERROR")))
def test_resource_is_with_fake_status_getter(self):
fake_getter = mock.MagicMock(return_value="LGTM")
fake_res = mock.MagicMock()
is_lgtm = utils.resource_is("LGTM", fake_getter)
self.assertTrue(is_lgtm(fake_res))
fake_getter.assert_called_once_with(fake_res)
def test_infinite_run_args_generator(self):
args = lambda x: (x, "a", "b", 123)
for i, real_args in enumerate(utils.infinite_run_args_generator(args)):

View File

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