1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7import time
8import signal
9
10from oeqa.runtime.case import OERuntimeTestCase
11from oeqa.core.decorator.depends import OETestDepends
12from oeqa.runtime.decorator.package import OEHasPackage
13
14class SSHTest(OERuntimeTestCase):
15
16    @OETestDepends(['ping.PingTest.test_ping'])
17    @OEHasPackage(['dropbear', 'openssh-sshd'])
18    def test_ssh(self):
19        for i in range(5):
20          status, output = self.target.run("uname -a", timeout=30)
21          if status == 0:
22              break
23          elif status == 255 or status == -signal.SIGTERM:
24              # ssh returns 255 only if a ssh error occurs.  This could
25              # be an issue with "Connection refused" because the port
26              # isn't open yet, and this could check explicitly for that
27              # here.  However, let's keep it simple and just retry for
28              # all errors a limited amount of times with a sleep to
29              # give it time for the port to open.
30              # We sometimes see -15 (SIGTERM) on slow emulation machines too, likely
31              # from boot/init not being 100% complete, retry for these too.
32              time.sleep(5)
33              continue
34          else:
35              self.fail("uname failed with \"%s\" (exit code %s)" % (output, status))
36        if status != 0:
37            self.fail("ssh failed with \"%s\" (exit code %s)" % (output, status))
38