summaryrefslogtreecommitdiff
path: root/smoketest/scripts/cli/test_service_lldp.py
blob: 9d72ef78f9dadfe6f88892dd22d35af3277b49f2 (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
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
#
# Copyright (C) 2022-2024 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 unittest

from base_vyostest_shim import VyOSUnitTestSHIM

from vyos.configsession import ConfigSessionError
from vyos.ifconfig import Section
from vyos.utils.process import process_named_running
from vyos.utils.file import read_file
from vyos.version import get_version_data

PROCESS_NAME = 'lldpd'
LLDPD_CONF = '/etc/lldpd.d/01-vyos.conf'
base_path = ['service', 'lldp']
mgmt_if = 'dum83513'
mgmt_addr = ['1.2.3.4', '1.2.3.5']

class TestServiceLLDP(VyOSUnitTestSHIM.TestCase):
    @classmethod
    def setUpClass(cls):
        # call base-classes classmethod
        super(TestServiceLLDP, cls).setUpClass()

        # create a test interfaces
        for addr in mgmt_addr:
            cls.cli_set(cls, ['interfaces', 'dummy', mgmt_if, 'address', addr + '/32'])

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

    @classmethod
    def tearDownClass(cls):
        cls.cli_delete(cls, ['interfaces', 'dummy', mgmt_if])
        super(TestServiceLLDP, cls).tearDownClass()

    def tearDown(self):
        # service must be running after it was configured
        self.assertTrue(process_named_running(PROCESS_NAME))

        # delete/stop LLDP service
        self.cli_delete(base_path)
        self.cli_commit()

        # service is no longer allowed to run after it was removed
        self.assertFalse(process_named_running(PROCESS_NAME))

    def test_01_lldp_basic(self):
        self.cli_set(base_path)
        self.cli_commit()

        config = read_file(LLDPD_CONF)
        version_data = get_version_data()
        version = version_data['version']
        self.assertIn(f'configure system platform VyOS', config)
        self.assertIn(f'configure system description "VyOS {version}"', config)

    def test_02_lldp_mgmt_address(self):
        for addr in mgmt_addr:
            self.cli_set(base_path + ['management-address', addr])
        self.cli_commit()

        config = read_file(LLDPD_CONF)
        self.assertIn(f'configure system ip management pattern {",".join(mgmt_addr)}', config)

    def test_03_lldp_interfaces(self):
        for interface in Section.interfaces('ethernet'):
            if not '.' in interface:
                self.cli_set(base_path + ['interface', interface])

        # commit changes
        self.cli_commit()

        # verify configuration
        config = read_file(LLDPD_CONF)

        interface_list = []
        for interface in Section.interfaces('ethernet'):
            if not '.' in interface:
                interface_list.append(interface)
        tmp = ','.join(interface_list)
        self.assertIn(f'configure system interface pattern "{tmp}"', config)

    def test_04_lldp_all_interfaces(self):
        self.cli_set(base_path + ['interface', 'all'])
        # commit changes
        self.cli_commit()

        # verify configuration
        config = read_file(LLDPD_CONF)
        self.assertIn(f'configure system interface pattern "*"', config)

    def test_05_lldp_location(self):
        interface = 'eth0'
        elin = '1234567890'
        self.cli_set(base_path + ['interface', interface, 'location', 'elin', elin])

        # commit changes
        self.cli_commit()

        # verify configuration
        config = read_file(LLDPD_CONF)

        self.assertIn(f'configure ports {interface} med location elin "{elin}"', config)
        self.assertIn(f'configure system interface pattern "{interface}"', config)

    def test_06_lldp_snmp(self):
        self.cli_set(base_path + ['snmp'])

        # verify - can not start lldp snmp without snmp beeing configured
        with self.assertRaises(ConfigSessionError):
            self.cli_commit()
        self.cli_set(['service', 'snmp'])
        self.cli_commit()

        # SNMP required process to be started with -x option
        tmp = read_file('/etc/default/lldpd')
        self.assertIn('-x', tmp)

        self.cli_delete(['service', 'snmp'])

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