summaryrefslogtreecommitdiff
path: root/smoketest/scripts/cli/test_service_dhcpv6-relay.py
blob: 0c46e14078b368baa76c047fa4f05673698583a0 (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
#!/usr/bin/env python3
#
# Copyright (C) 2020-2023 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.template import address_from_cidr
from vyos.util import process_named_running
from vyos.util import read_file

PROCESS_NAME = 'dhcrelay'
RELAY_CONF = '/run/dhcp-relay/dhcrelay6.conf'
base_path = ['service', 'dhcpv6-relay']

upstream_if = 'eth0'
upstream_if_addr = '2001:db8::1/64'
listen_addr = '2001:db8:ffff::1/64'
interfaces = []

class TestServiceDHCPv6Relay(VyOSUnitTestSHIM.TestCase):
    @classmethod
    def setUpClass(cls):
        super(TestServiceDHCPv6Relay, 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)

        for tmp in Section.interfaces('ethernet', vlan=False):
            interfaces.append(tmp)
            listen = listen_addr
            if tmp == upstream_if:
                listen = upstream_if_addr
            cls.cli_set(['interfaces', 'ethernet', tmp, 'address', listen])

    @classmethod
    def tearDownClass(cls):
        for tmp in interfaces:
            listen = listen_addr
            if tmp == upstream_if:
                listen = upstream_if_addr
            cld.cli_delete(['interfaces', 'ethernet', tmp, 'address', listen])

        super(TestServiceDHCPv6Relay, cls).tearDownClass()

    def test_relay_default(self):
        dhcpv6_server = '2001:db8::ffff'
        hop_count = '20'

        self.cli_set(base_path + ['use-interface-id-option'])
        self.cli_set(base_path + ['max-hop-count', hop_count])

        # check validate() - Must set at least one listen and upstream
        # interface addresses.
        with self.assertRaises(ConfigSessionError):
            self.cli_commit()
        self.cli_set(base_path + ['upstream-interface', upstream_if, 'address', dhcpv6_server])

        # check validate() - Must set at least one listen and upstream
        # interface addresses.
        with self.assertRaises(ConfigSessionError):
            self.cli_commit()

        # add listener on all ethernet interfaces except the upstream interface
        for tmp in interfaces:
            if tmp == upstream_if:
                continue
            self.cli_set(base_path + ['listen-interface', tmp, 'address', listen_addr.split('/')[0]])

        # commit changes
        self.cli_commit()

        # Check configured port
        config = read_file(RELAY_CONF)

        # Test configured upstream interfaces
        self.assertIn(f'-u {dhcpv6_server}%{upstream_if}', config)

        # Check listener on all ethernet interfaces
        for tmp in interfaces:
            if tmp == upstream_if:
                continue
            addr = listen_addr.split('/')[0]
            self.assertIn(f'-l {addr}%{tmp}', config)

        # Check hop count
        self.assertIn(f'-c {hop_count}', config)
        # Check Interface ID option
        self.assertIn('-I', config)

        # Check for running process
        self.assertTrue(process_named_running(PROCESS_NAME))

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