summaryrefslogtreecommitdiff
path: root/smoketest/scripts/cli/test_system_console.py
blob: 15172a8ac549c3ae0e3749cda68df8cb905895da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
#
# Copyright (C) VyOS Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import unittest

from base_vyostest_shim import VyOSUnitTestSHIM

from vyos.configsession import ConfigSessionError
from vyos.system import disk
from vyos.system.grub import CFG_VYOS_VARS
from vyos.system.grub import vars_read
from vyos.utils.cpu import get_cpu_arch
from vyos.xml_ref import default_value

base_path = ['system', 'console']
# The serial console device is platform dependent. On amd64 this is the 8250
# UART at ttyS0, while arm64 systems use the PL011 UART exposed as ttyAMA0.
# A device that does not exist is not a TTY and thus never becomes the Kernel
# boot console - so we must test with the device of the running platform.
serial_console = 'ttyAMA0' if get_cpu_arch() == 'arm64' else 'ttyS0'
console_type = serial_console[:-1]
default_speed = default_value(base_path + ['device', serial_console, 'speed'])

def get_grub_vars() -> dict:
    root_dir = disk.find_persistence()
    vars_file: str = f'{root_dir}/{CFG_VYOS_VARS}'
    vars_current: dict[str, str] = vars_read(vars_file)
    return vars_current

class TestSystemConsole(VyOSUnitTestSHIM.TestCase):
    @classmethod
    def setUpClass(cls):
        super(TestSystemConsole, cls).setUpClass()

        # ensure we can also run this test on a live system - so lets clean
        # out the current configuration :)
        cls.cli_delete(cls, base_path)
        cls.cli_commit(cls)

    def tearDown(self):
        self.cli_delete(base_path)
        self.cli_commit()
        # always forward to base class
        super().tearDown()

    def test_multiple_kernel_consoles(self):
        self.cli_set(base_path + ['device', f'{console_type}1', 'kernel'])
        self.cli_set(base_path + ['device', f'{console_type}2', 'kernel'])

        # Only one console can have 'kernel'
        with self.assertRaises(ConfigSessionError):
            self.cli_commit()

    def test_fbcon_and_serial_con_switch(self):
        if not self.running_in_smoketest_harness():
            self.skipTest('Not running under VyOS CI/CD QEMU environment!')

        grub_vars = get_grub_vars()
        # we have deleted the CLI config in tearDown() so the default is now
        # the framebuffer console at tty0
        self.assertEqual(grub_vars['console_type'], 'tty')

        self.cli_set(base_path + ['device', serial_console, 'kernel'])
        self.cli_commit()

        grub_vars = get_grub_vars()
        # We moved the Kernel boot console to the platform serial device
        self.assertEqual(grub_vars['console_type'], console_type)
        self.assertEqual(grub_vars['console_num'], serial_console[-1])
        self.assertEqual(grub_vars['console_speed'], default_speed)

        self.cli_delete(base_path)
        self.cli_commit()

        # We moved back to tty as Kernel boot console
        grub_vars = get_grub_vars()
        self.assertEqual(grub_vars['console_type'], 'tty')

if __name__ == '__main__':
    unittest.main(verbosity=2, failfast=VyOSUnitTestSHIM.TestCase.debug_on())