Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 7 | from subprocess import Popen, PIPE |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 8 | from time import sleep |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 9 | |
| 10 | from oeqa.runtime.case import OERuntimeTestCase |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 11 | from oeqa.core.decorator.oetimeout import OETimeout |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 12 | from oeqa.core.exception import OEQATimeoutError |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 13 | |
| 14 | class PingTest(OERuntimeTestCase): |
| 15 | |
| 16 | @OETimeout(30) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 17 | def test_ping(self): |
| 18 | output = '' |
| 19 | count = 0 |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 20 | self.assertNotEqual(len(self.target.ip), 0, msg="No target IP address set") |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 21 | try: |
| 22 | while count < 5: |
| 23 | cmd = 'ping -c 1 %s' % self.target.ip |
| 24 | proc = Popen(cmd, shell=True, stdout=PIPE) |
| 25 | output += proc.communicate()[0].decode('utf-8') |
| 26 | if proc.poll() == 0: |
| 27 | count += 1 |
| 28 | else: |
| 29 | count = 0 |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 30 | sleep(1) |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 31 | except OEQATimeoutError: |
| 32 | self.fail("Ping timeout error for address %s, count %s, output: %s" % (self.target.ip, count, output)) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 33 | msg = ('Expected 5 consecutive, got %d.\n' |
| 34 | 'ping output is:\n%s' % (count,output)) |
| 35 | self.assertEqual(count, 5, msg = msg) |