kumquat-buildroot/support/testing/tests/download/test_ssh.py
Thomas Preston fd548e16fb support/testing: Add download tests for SCP/SFTP
Add download test infrastructure which starts an OpenSSH server using
the sshd binary installed on the Buildroot host. This server can then be
used to test the expected usage of the SCP and SFTP download methods.
The test creates new SSH keys for the server and client, so that the
server can be run as a non-root user.

A new test module has been added called `tests.download.sshd` which
contains helper methods to create the SSH keys and a class called
`OpenSSHDaemon` which handles the sshd server component.

The tests download example packages in the br2-external project `ssh`.
They check the following conditions for both SCP and SFTP download
methods:
- Correct hash.
- Incorrect hash.
- No hash file.

The SSH download test infrastructure is based on test_git.py.

Signed-off-by: Thomas Preston <thomas.preston@codethink.co.uk>
[Arnout:
 - remove spurious end-of-line backslash;
 - remove unnecessary executable bit;
 - skip test instead of failing if sshd, ssh-keygen, scp or sftp are not
   found;
 - decode the output of subprocess;
 - use subprocess.check_output instead of subprocess.get_output;
 - use subprocess.check_call instead of manually checking return code;
 - don't set always-overridden SSHD_PORT_NUMBER in .mk file;
 - explicitly set sshd options on commandline instead of relying on host
   /etc/sshd/sshd_config;
 - let sshd listen only on localhost;
 - user internal sftp server;
 - disable BACKUP_SITE, no network is supposed to be accessed;
 - remove the -bad and -nohash versions;
 - rename {sftp,scp}-good to plain {sftp,scp};
 - move the sftp and scp packages into a single "ssh" external.
]
Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
2022-01-06 09:34:05 +01:00

63 lines
1.9 KiB
Python

import os
import shutil
import tests.download.sshd
import infra
class SSHTestBase(infra.basetest.BRConfigTest):
config = infra.basetest.MINIMAL_CONFIG + '''
BR2_BACKUP_SITE=""
'''
sshd_test_dir = infra.filepath("tests/download/sshd")
sshd = None
def setUp(self):
super(SSHTestBase, self).setUp()
self.show_msg("Generating keys")
tests.download.sshd.generate_keys(self.builddir, self.logtofile)
self.show_msg("Starting sshd")
self.sshd = tests.download.sshd.OpenSSHDaemon(self.builddir,
self.logtofile)
def tearDown(self):
self.show_msg("Stopping sshd")
if self.sshd:
self.sshd.stop()
super(SSHTestBase, self).tearDown()
def download_package(self, package):
self.show_msg("Downloading {}".format(package))
# store downloaded tarball inside the output dir so the test infra
# cleans it up at the end
dl_dir = os.path.join(self.builddir, "dl")
ssh_identity = os.path.join(self.builddir,
tests.download.sshd.SSH_CLIENT_KEY)
# enforce that we test the download
if os.path.exists(dl_dir):
shutil.rmtree(dl_dir)
env = {"BR2_DL_DIR": dl_dir,
"SSHD_PORT_NUMBER": str(self.sshd.port),
"SSHD_TEST_DIR": self.sshd_test_dir,
"SSH_IDENTITY": ssh_identity}
self.b.build(["{}-dirclean".format(package),
"{}-source".format(package)],
env)
class TestSCP(SSHTestBase):
br2_external = [infra.filepath("tests/download/br2-external/ssh")]
def test_run(self):
self.download_package("scp")
class TestSFTP(SSHTestBase):
br2_external = [infra.filepath("tests/download/br2-external/ssh")]
def test_run(self):
self.download_package("sftp")