1#!/usr/bin/env python3
2#
3# Functional test that boots a Linux kernel and checks the console
4#
5# Copyright (c) 2020 Red Hat, Inc.
6#
7# Author:
8#  Thomas Huth <thuth@redhat.com>
9#
10# This work is licensed under the terms of the GNU GPL, version 2 or
11# later.  See the COPYING file in the top-level directory.
12#
13# SPDX-License-Identifier: GPL-2.0-or-later
14
15import os
16import logging
17
18from qemu_test import QemuSystemTest, Asset
19from qemu_test import wait_for_console_pattern
20from unittest import skipUnless
21
22
23NUMPY_AVAILABLE = True
24try:
25    import numpy as np
26except ImportError:
27    NUMPY_AVAILABLE = False
28
29CV2_AVAILABLE = True
30try:
31    import cv2
32except ImportError:
33    CV2_AVAILABLE = False
34
35
36class IntegratorMachine(QemuSystemTest):
37
38    timeout = 90
39
40    ASSET_KERNEL = Asset(
41        ('https://github.com/zayac/qemu-arm/raw/master/'
42         'arm-test/kernel/zImage.integrator'),
43        '26e7c7e8f943de785d95bd3c74d66451604a9b6a7a3d25dceb279e7548fd8e78')
44
45    ASSET_INITRD = Asset(
46        ('https://github.com/zayac/qemu-arm/raw/master/'
47         'arm-test/kernel/arm_root.img'),
48        'e187c27fb342ad148c7f33475fbed124933e0b3f4be8c74bc4f3426a4793373a')
49
50    ASSET_TUXLOGO = Asset(
51        ('https://github.com/torvalds/linux/raw/v2.6.12/'
52         'drivers/video/logo/logo_linux_vga16.ppm'),
53        'b762f0d91ec018887ad1b334543c2fdf9be9fdfc87672b409211efaa3ea0ef79')
54
55    def boot_integratorcp(self):
56        kernel_path = self.ASSET_KERNEL.fetch()
57        initrd_path = self.ASSET_INITRD.fetch()
58
59        self.set_machine('integratorcp')
60        self.vm.set_console()
61        self.vm.add_args('-kernel', kernel_path,
62                         '-initrd', initrd_path,
63                         '-append', 'printk.time=0 console=ttyAMA0')
64        self.vm.launch()
65
66    @skipUnless(os.getenv('QEMU_TEST_ALLOW_UNTRUSTED_CODE'), 'untrusted code')
67    def test_integratorcp_console(self):
68        """
69        Boots the Linux kernel and checks that the console is operational
70        """
71        self.boot_integratorcp()
72        wait_for_console_pattern(self, 'Log in as root')
73
74    @skipUnless(NUMPY_AVAILABLE, 'Python NumPy not installed')
75    @skipUnless(CV2_AVAILABLE, 'Python OpenCV not installed')
76    @skipUnless(os.getenv('QEMU_TEST_ALLOW_UNTRUSTED_CODE'), 'untrusted code')
77    def test_framebuffer_tux_logo(self):
78        """
79        Boot Linux and verify the Tux logo is displayed on the framebuffer.
80        """
81        screendump_path = os.path.join(self.workdir, "screendump.pbm")
82        tuxlogo_path = self.ASSET_TUXLOGO.fetch()
83
84        self.boot_integratorcp()
85        framebuffer_ready = 'Console: switching to colour frame buffer device'
86        wait_for_console_pattern(self, framebuffer_ready)
87        self.vm.cmd('human-monitor-command', command_line='stop')
88        self.vm.cmd('human-monitor-command',
89                    command_line='screendump %s' % screendump_path)
90        logger = logging.getLogger('framebuffer')
91
92        cpu_count = 1
93        match_threshold = 0.92
94        screendump_bgr = cv2.imread(screendump_path)
95        screendump_gray = cv2.cvtColor(screendump_bgr, cv2.COLOR_BGR2GRAY)
96        result = cv2.matchTemplate(screendump_gray, cv2.imread(tuxlogo_path, 0),
97                                   cv2.TM_CCOEFF_NORMED)
98        loc = np.where(result >= match_threshold)
99        tux_count = 0
100        for tux_count, pt in enumerate(zip(*loc[::-1]), start=1):
101            logger.debug('found Tux at position [x, y] = %s', pt)
102        self.assertGreaterEqual(tux_count, cpu_count)
103
104if __name__ == '__main__':
105    QemuSystemTest.main()
106