summaryrefslogtreecommitdiff
path: root/smoketest/scripts/cli/test_interfaces_pseudo-ethernet.py
blob: c70d194748a58b5ab49a195707454ce6377d1480 (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
#!/usr/bin/env python3
#
# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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_interfaces_test import BasicInterfaceTest
from base_vyostest_shim import VyOSUnitTestSHIM
from vyos.configsession import ConfigSessionError
from vyos.utils.process import cmd

from vyos.ifconfig import Section

class PEthInterfaceTest(BasicInterfaceTest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls._base_path = ['interfaces', 'pseudo-ethernet']

        cls._options = {}
        # we need to filter out VLAN interfaces identified by a dot (.)
        # in their name - just in case!
        if 'TEST_ETH' in os.environ:
            for tmp in os.environ['TEST_ETH'].split():
                cls._options.update({f'p{tmp}' : [f'source-interface {tmp}']})

        else:
            for tmp in Section.interfaces('ethernet'):
                if '.' in tmp:
                    continue
                cls._options.update({f'p{tmp}' : [f'source-interface {tmp}']})

        cls._interfaces = list(cls._options)
        # call base-classes classmethod
        super(PEthInterfaceTest, cls).setUpClass()

    def test_anycast_gateway(self):
        # Create the underlying bridge and sub-interface in the test

        for i, peth in enumerate(self._interfaces):
            eth = peth[1:]  # Convert peth0 -> eth0
            br = f'br{i}'
            vlan = str(i + 100)

            # Format the MAC using index as a two-digit hexadecimal
            mac_address = f'00:aa:aa:aa:aa:{i:02x}'

            with self.subTest(peth=peth, eth=eth, mac_address=mac_address, i=i):
                base_bridge_path = ['interfaces', 'bridge', br]
                base_br_member_path = base_bridge_path + ['member', 'interface']

                self.cli_set(base_bridge_path + ['enable-vlan'])
                self.cli_set(base_br_member_path + [eth, 'native-vlan', vlan])
                self.cli_set(base_br_member_path + [f'vxlan{i}'])
                self.cli_set(base_bridge_path + ['vif', vlan])

                self.cli_set(
                    self._base_path + [peth, 'source-interface', f'{br}.{vlan}']
                )
                self.cli_set(self._base_path + [peth, 'anycast-gateway'])

                # Anycast gateway requires MAC
                with self.assertRaises(ConfigSessionError):
                    self.cli_commit()

                self.cli_set(self._base_path + [peth, 'mac', mac_address])
                self.cli_commit()

                # Verify FDB entry exists with flag
                fdb = cmd(f'bridge fdb show dev {br}')
                self.assertIn(f'{mac_address} master {br} permanent', fdb)
                self.assertIn(f'{mac_address} self permanent', fdb)

                # Then remove just the anycast-gateway flag
                self.cli_delete(self._base_path + [peth, 'anycast-gateway'])
                self.cli_commit()

                fdb = cmd(f'bridge fdb show dev {br}')
                self.assertNotIn(f'{mac_address} master {br} permanent', fdb)

                # Clean up temp bridge and peth
                self.cli_delete(self._base_path + [peth])
                self.cli_delete(base_bridge_path)
                self.cli_commit()


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