summaryrefslogtreecommitdiff
path: root/smoketest/scripts/cli/test_system_option.py
blob: 4daa812c0eb7f5d3a2dca87b9ddacb59f91fa75e (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
#
# Copyright (C) 2024-2025 VyOS maintainers and contributors
#
# 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 os
import unittest

from base_vyostest_shim import VyOSUnitTestSHIM

from vyos.configsession import ConfigSessionError
from vyos.utils.cpu import get_cpus
from vyos.utils.file import read_file
from vyos.utils.process import is_systemd_service_active
from vyos.utils.system import sysctl_read
from vyos.system import image

base_path = ['system', 'option']

class TestSystemOption(VyOSUnitTestSHIM.TestCase):
    def tearDown(self):
        self.cli_delete(base_path)
        self.cli_commit()

    def test_ctrl_alt_delete(self):
        self.cli_set(base_path + ['ctrl-alt-delete', 'reboot'])
        self.cli_commit()

        tmp = os.readlink('/lib/systemd/system/ctrl-alt-del.target')
        self.assertEqual(tmp, '/lib/systemd/system/reboot.target')

        self.cli_set(base_path + ['ctrl-alt-delete', 'poweroff'])
        self.cli_commit()

        tmp = os.readlink('/lib/systemd/system/ctrl-alt-del.target')
        self.assertEqual(tmp, '/lib/systemd/system/poweroff.target')

        self.cli_delete(base_path + ['ctrl-alt-delete', 'poweroff'])
        self.cli_commit()
        self.assertFalse(os.path.exists('/lib/systemd/system/ctrl-alt-del.target'))

    def test_reboot_on_panic(self):
        panic_file = '/proc/sys/kernel/panic'

        tmp = read_file(panic_file)
        self.assertEqual(tmp, '0')

        self.cli_set(base_path + ['reboot-on-panic'])
        self.cli_commit()

        tmp = read_file(panic_file)
        self.assertEqual(tmp, '60')

    def test_performance(self):
        tuned_service = 'tuned.service'
        path = ['system', 'sysctl', 'parameter']

        self.assertFalse(is_systemd_service_active(tuned_service))

        # T3204 sysctl options must not be overwritten by tuned
        gc_thresh1 = '131072'
        gc_thresh2 = '262000'
        gc_thresh3 = '524000'

        self.cli_set(path + ['net.ipv4.neigh.default.gc_thresh1', 'value', gc_thresh1])
        self.cli_set(path + ['net.ipv4.neigh.default.gc_thresh2', 'value', gc_thresh2])
        self.cli_set(path + ['net.ipv4.neigh.default.gc_thresh3', 'value', gc_thresh3])

        self.cli_set(base_path + ['performance', 'network-throughput'])
        self.cli_commit()

        self.assertTrue(is_systemd_service_active(tuned_service))

        self.assertEqual(sysctl_read('net.ipv4.neigh.default.gc_thresh1'), gc_thresh1)
        self.assertEqual(sysctl_read('net.ipv4.neigh.default.gc_thresh2'), gc_thresh2)
        self.assertEqual(sysctl_read('net.ipv4.neigh.default.gc_thresh3'), gc_thresh3)

    def test_ssh_client_options(self):
        loopback = 'lo'
        ssh_client_opt_file = '/etc/ssh/ssh_config.d/91-vyos-ssh-client-options.conf'

        self.cli_set(['system', 'option', 'ssh-client', 'source-interface', loopback])
        self.cli_commit()

        tmp = read_file(ssh_client_opt_file)
        self.assertEqual(tmp, f'BindInterface {loopback}')

        self.cli_delete(['system', 'option'])
        self.cli_commit()
        self.assertFalse(os.path.exists(ssh_client_opt_file))

    def test_kernel_options(self):
        amd_pstate_mode = 'active'

        self.cli_set(['system', 'option', 'kernel', 'disable-mitigations'])
        self.cli_set(['system', 'option', 'kernel', 'disable-power-saving'])
        self.cli_set(['system', 'option', 'kernel', 'quiet'])

        self.cli_set(['system', 'option', 'kernel', 'amd-pstate-driver', amd_pstate_mode])
        cpu_vendor = get_cpus()[0]['vendor_id']
        if cpu_vendor != 'AuthenticAMD':
            with self.assertRaises(ConfigSessionError):
                self.cli_commit()
            self.cli_delete(['system', 'option', 'kernel', 'amd-pstate-driver'])

        self.cli_commit()

        # Read GRUB config file for current running image
        tmp = read_file(f'{image.grub.GRUB_DIR_VYOS_VERS}/{image.get_running_image()}.cfg')
        self.assertIn(' mitigations=off', tmp)
        self.assertIn(' intel_idle.max_cstate=0 processor.max_cstate=1', tmp)
        self.assertIn(' quiet', tmp)

        if cpu_vendor == 'AuthenticAMD':
            self.assertIn(f' initcall_blacklist=acpi_cpufreq_init amd_pstate={amd_pstate_mode}', tmp)

if __name__ == '__main__':
    unittest.main(verbosity=2)