Create background ping helpers

Keep the tests short and readable.

Change-Id: I0e15fedd255e8fdb702df822a7f68721527255f4
This commit is contained in:
abregman 2018-10-15 10:19:18 +03:00
parent 639f89215f
commit 9c676f745b
3 changed files with 55 additions and 26 deletions

View File

@ -1,7 +1,7 @@
keystoneauth1>=3.4.0
keystoneauth1>=2.0.0
oslo.config>=5.2.0
oslo.log>=3.36.0
python-heatclient>=1.10.0
python-heatclient>=1.5.0
testscenarios>=0.4
testtools>=2.2.0
tempest>=17.1.0

View File

@ -0,0 +1,49 @@
# Copyright 2018 Red Hat
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import re
import signal
import subprocess
def run_background_ping(server_fip):
"""Starts background ping process."""
ping_log = open("/tmp/ping_%s_output" % server_fip, 'ab')
p = subprocess.Popen(['ping -q %s' % server_fip],
stdout=ping_log, shell=True)
with open("/tmp/ping_%s_pid" % server_fip, 'ab') as pf:
pf.write(str(p.pid))
def get_packet_loss(server_fip):
"""Returns packet loss."""
# Kill Process
with open("/tmp/ping_%s_pid" % server_fip) as f:
pid = f.read()
os.kill(int(pid), signal.SIGINT)
# Packet loss pattern
p = re.compile("(\d{1,3})% packet loss")
# Get ping package loss
with open("/tmp/ping_%s_output" % server_fip) as f:
m = p.search(f.read())
packet_loss = m.group(1)
# Remove files created by pre test
os.remove("/tmp/ping_%s_output" % server_fip)
os.remove("/tmp/ping_%s_pid" % server_fip)
return packet_loss

View File

@ -12,12 +12,8 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import re
import signal
import subprocess
from tobiko.tests.scenario import base
import tobiko.common.net_utils as net_utils
class ContinuousPingTest(base.ScenarioTestsBase):
@ -34,10 +30,8 @@ class ContinuousPingTest(base.ScenarioTestsBase):
if not self.ping_ip_address(server_fip):
self.fail("IP address is not reachable: %s" % server_fip)
ping_log = open("/tmp/ping_%s_output" % server_fip, 'ab')
p = subprocess.Popen(['ping -q 8.8.8.8'], stdout=ping_log, shell=True)
with open("/tmp/ping_%s_pid" % server_fip, 'ab') as pf:
pf.write(str(p.pid))
# Start ping process
net_utils.run_background_ping(server_fip)
def test_post_continuous_ping(self):
"""Validates the ping test was successful."""
@ -46,20 +40,6 @@ class ContinuousPingTest(base.ScenarioTestsBase):
stack = self.stackManager.get_stack(stack_name="scenario")
server_fip = stack.outputs[0]['output_value']
# Kill Process
with open("/tmp/ping_%s_pid" % server_fip) as f:
pid = f.read()
os.kill(int(pid), signal.SIGINT)
packet_loss = net_utils.get_packet_loss(server_fip)
# Packet loss pattern
p = re.compile("(\d{1,3})% packet loss")
# Check ping package loss
with open("/tmp/ping_%s_output" % server_fip) as f:
m = p.search(f.read())
packet_loss = m.group(1)
self.assertLessEqual(int(packet_loss), 5)
# Remove files created by pre test
os.remove("/tmp/ping_%s_output" % server_fip)
os.remove("/tmp/ping_%s_pid" % server_fip)