xref: /OK3568_Linux_fs/yocto/poky/meta/lib/oeqa/runtime/cases/ping.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1#
2# SPDX-License-Identifier: MIT
3#
4
5from subprocess import Popen, PIPE
6
7from oeqa.runtime.case import OERuntimeTestCase
8from oeqa.core.decorator.oetimeout import OETimeout
9from oeqa.core.exception import OEQATimeoutError
10
11class PingTest(OERuntimeTestCase):
12
13    @OETimeout(30)
14    def test_ping(self):
15        output = ''
16        count = 0
17        try:
18            while count < 5:
19                cmd = 'ping -c 1 %s' % self.target.ip
20                proc = Popen(cmd, shell=True, stdout=PIPE)
21                output += proc.communicate()[0].decode('utf-8')
22                if proc.poll() == 0:
23                    count += 1
24                else:
25                    count = 0
26        except OEQATimeoutError:
27            self.fail("Ping timeout error for address %s, count %s, output: %s" % (self.target.ip, count, output))
28        msg = ('Expected 5 consecutive, got %d.\n'
29               'ping output is:\n%s' % (count,output))
30        self.assertEqual(count, 5, msg = msg)
31