From 7a3258934b2c53f7c29c212c695805da1893ba3f Mon Sep 17 00:00:00 2001 From: Patrick Vacek Date: Thu, 16 Nov 2017 16:04:36 +0100 Subject: Move oe-selftest qemu logic to its own function. This means we can call it from any number of tests and run arbitrary commands. I finally figured out how to get commands with arguments working, too. --- lib/oeqa/selftest/updater.py | 91 +++++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 31 deletions(-) (limited to 'lib/oeqa/selftest') diff --git a/lib/oeqa/selftest/updater.py b/lib/oeqa/selftest/updater.py index 2723b4a..8ee3c69 100644 --- a/lib/oeqa/selftest/updater.py +++ b/lib/oeqa/selftest/updater.py @@ -113,35 +113,64 @@ class GeneralTests(oeSelfTest): self.assertNotEqual(path1, path2, "Image paths are identical; image was not rebuilt.") self.assertNotEqual(size1, size2, "Image sizes are identical; image was not rebuilt.") - def test_qemu(self): - print('') - # Create empty object. - args = type('', (), {})() - args.imagename = 'core-image-minimal' - args.mac = None - # Could use DEPLOY_DIR_IMAGE here but it's already in the machine - # subdirectory. - args.dir = 'tmp/deploy/images' - args.efi = False - args.machine = None - args.kvm = None # Autodetect - args.no_gui = True - args.gdb = False - args.pcap = None - args.overlay = None - args.dry_run = False - - qemu_command = QemuCommand(args) - cmdline = qemu_command.command_line() - print('Booting image with run-qemu-ota...') - s = subprocess.Popen(cmdline) - time.sleep(10) - print('Machine name (hostname) of device is:') - ssh_cmd = ['ssh', '-q', '-o', 'UserKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=no', 'root@localhost', '-p', str(qemu_command.ssh_port), 'hostname'] - s2 = subprocess.Popen(ssh_cmd) - time.sleep(5) - try: - s.terminate() - except KeyboardInterrupt: - pass + +class QemuTests(oeSelfTest): + + @classmethod + def setUpClass(cls): + logger = logging.getLogger("selftest") + logger.info('Running bitbake to build core-image-minimal') + bitbake('core-image-minimal') + + def test_hostname(self): + value, err = run_test_qemu('hostname', False, 'Checking machine name (hostname) of device:') + machine = get_bb_var('MACHINE', 'core-image-minimal') + self.assertEqual(err, b'', 'Error: ' + err.decode()) + # Strip off line ending. + value_str = value.decode()[:-1] + self.assertEqual(value_str, machine, 'MACHINE does not match hostname: ' + machine + ', ' + value_str) + print('hostname: ' + value_str) + + def test_var_sota(self): + value, err = run_test_qemu('ls /var/sota', True, 'Checking contents of /var/sota:') + self.assertEqual(err, b'', 'Error: ' + err.decode()) + print(value.decode()) + + +def run_test_qemu(command, use_shell, message): + print('') + # Create empty object. + args = type('', (), {})() + args.imagename = 'core-image-minimal' + args.mac = None + # Could use DEPLOY_DIR_IMAGE here but it's already in the machine + # subdirectory. + args.dir = 'tmp/deploy/images' + args.efi = False + args.machine = None + args.kvm = None # Autodetect + args.no_gui = True + args.gdb = False + args.pcap = None + args.overlay = None + args.dry_run = False + + qemu_command = QemuCommand(args) + cmdline = qemu_command.command_line() + print('Booting image with run-qemu-ota...') + s = subprocess.Popen(cmdline) + time.sleep(10) + print(message) + if not use_shell: + command = ['ssh', '-q', '-o', 'UserKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=no', 'root@localhost', '-p', str(qemu_command.ssh_port), '"' + command + '"'] + else: + command = 'ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@localhost -p ' + str(qemu_command.ssh_port) + ' "' + command + '"' + s2 = subprocess.Popen(command, shell=use_shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + value, err = s2.communicate() + time.sleep(5) + try: + s.terminate() + except KeyboardInterrupt: + pass + return value, err -- cgit v1.2.3-54-g00ecf From c687237422dc7f22b85d9718d112fe0b674007c3 Mon Sep 17 00:00:00 2001 From: Patrick Vacek Date: Thu, 16 Nov 2017 16:57:57 +0100 Subject: Make launching qemu part of setUpClass. And terminating part of tearDownClass. This cleans things up nicely and means we only have to boot once for multiple tests. If we do want to boot multiple times (e.g. with different configs), we can move most of the qemu code to non-class functions and use multiple classes that each call those functions in setUpClass and tearDownClass. --- lib/oeqa/selftest/updater.py | 85 +++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 41 deletions(-) (limited to 'lib/oeqa/selftest') diff --git a/lib/oeqa/selftest/updater.py b/lib/oeqa/selftest/updater.py index 8ee3c69..ad99964 100644 --- a/lib/oeqa/selftest/updater.py +++ b/lib/oeqa/selftest/updater.py @@ -121,56 +121,59 @@ class QemuTests(oeSelfTest): logger = logging.getLogger("selftest") logger.info('Running bitbake to build core-image-minimal') bitbake('core-image-minimal') + # Create empty object. + args = type('', (), {})() + args.imagename = 'core-image-minimal' + args.mac = None + # Could use DEPLOY_DIR_IMAGE here but it's already in the machine + # subdirectory. + args.dir = 'tmp/deploy/images' + args.efi = False + args.machine = None + args.kvm = None # Autodetect + args.no_gui = True + args.gdb = False + args.pcap = None + args.overlay = None + args.dry_run = False + + cls.qemu = QemuCommand(args) + cmdline = cls.qemu.command_line() + print('Booting image with run-qemu-ota...') + cls.s = subprocess.Popen(cmdline) + time.sleep(10) + + @classmethod + def tearDownClass(cls): + try: + cls.s.terminate() + except KeyboardInterrupt: + pass + + def run_test_qemu(self, command): + command = ['ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@localhost -p ' + + str(self.qemu.ssh_port) + ' "' + command + '"'] + s2 = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + value, err = s2.communicate() + return value, err def test_hostname(self): - value, err = run_test_qemu('hostname', False, 'Checking machine name (hostname) of device:') + print('') + print('Checking machine name (hostname) of device:') + value, err = self.run_test_qemu('hostname') machine = get_bb_var('MACHINE', 'core-image-minimal') self.assertEqual(err, b'', 'Error: ' + err.decode()) # Strip off line ending. value_str = value.decode()[:-1] - self.assertEqual(value_str, machine, 'MACHINE does not match hostname: ' + machine + ', ' + value_str) - print('hostname: ' + value_str) + self.assertEqual(value_str, machine, + 'MACHINE does not match hostname: ' + machine + ', ' + value_str) + print(value_str) def test_var_sota(self): - value, err = run_test_qemu('ls /var/sota', True, 'Checking contents of /var/sota:') + print('') + print('Checking contents of /var/sota:') + value, err = self.run_test_qemu('ls /var/sota') self.assertEqual(err, b'', 'Error: ' + err.decode()) print(value.decode()) -def run_test_qemu(command, use_shell, message): - print('') - # Create empty object. - args = type('', (), {})() - args.imagename = 'core-image-minimal' - args.mac = None - # Could use DEPLOY_DIR_IMAGE here but it's already in the machine - # subdirectory. - args.dir = 'tmp/deploy/images' - args.efi = False - args.machine = None - args.kvm = None # Autodetect - args.no_gui = True - args.gdb = False - args.pcap = None - args.overlay = None - args.dry_run = False - - qemu_command = QemuCommand(args) - cmdline = qemu_command.command_line() - print('Booting image with run-qemu-ota...') - s = subprocess.Popen(cmdline) - time.sleep(10) - print(message) - if not use_shell: - command = ['ssh', '-q', '-o', 'UserKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=no', 'root@localhost', '-p', str(qemu_command.ssh_port), '"' + command + '"'] - else: - command = 'ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@localhost -p ' + str(qemu_command.ssh_port) + ' "' + command + '"' - s2 = subprocess.Popen(command, shell=use_shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - value, err = s2.communicate() - time.sleep(5) - try: - s.terminate() - except KeyboardInterrupt: - pass - return value, err - -- cgit v1.2.3-54-g00ecf