summaryrefslogtreecommitdiff
path: root/tests/unittests/config/test_cc_refresh_rmc_and_interface.py
blob: e038f81424c9f92431a85de48d07d4a1ad7f7e8b (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import logging
from textwrap import dedent

from cloudinit import util
from cloudinit.config import cc_refresh_rmc_and_interface as ccrmci
from tests.unittests import helpers as t_help
from tests.unittests.helpers import mock

LOG = logging.getLogger(__name__)
MPATH = "cloudinit.config.cc_refresh_rmc_and_interface"
NET_INFO = {
    "lo": {
        "ipv4": [
            {
                "ip": "127.0.0.1",
                "bcast": "",
                "mask": "255.0.0.0",
                "scope": "host",
            }
        ],
        "ipv6": [{"ip": "::1/128", "scope6": "host"}],
        "hwaddr": "",
        "up": "True",
    },
    "env2": {
        "ipv4": [
            {
                "ip": "8.0.0.19",
                "bcast": "8.0.0.255",
                "mask": "255.255.255.0",
                "scope": "global",
            }
        ],
        "ipv6": [{"ip": "fe80::f896:c2ff:fe81:8220/64", "scope6": "link"}],
        "hwaddr": "fa:96:c2:81:82:20",
        "up": "True",
    },
    "env3": {
        "ipv4": [
            {
                "ip": "90.0.0.14",
                "bcast": "90.0.0.255",
                "mask": "255.255.255.0",
                "scope": "global",
            }
        ],
        "ipv6": [{"ip": "fe80::f896:c2ff:fe81:8221/64", "scope6": "link"}],
        "hwaddr": "fa:96:c2:81:82:21",
        "up": "True",
    },
    "env4": {
        "ipv4": [
            {
                "ip": "9.114.23.7",
                "bcast": "9.114.23.255",
                "mask": "255.255.255.0",
                "scope": "global",
            }
        ],
        "ipv6": [{"ip": "fe80::f896:c2ff:fe81:8222/64", "scope6": "link"}],
        "hwaddr": "fa:96:c2:81:82:22",
        "up": "True",
    },
    "env5": {
        "ipv4": [],
        "ipv6": [{"ip": "fe80::9c26:c3ff:fea4:62c8/64", "scope6": "link"}],
        "hwaddr": "42:20:86:df:fa:4c",
        "up": "True",
    },
}


class TestRsctNodeFile(t_help.CiTestCase):
    def test_disable_ipv6_interface(self):
        """test parsing of iface files."""
        fname = self.tmp_path("iface-eth5")
        util.write_file(
            fname,
            dedent(
                """\
            BOOTPROTO=static
            DEVICE=eth5
            HWADDR=42:20:86:df:fa:4c
            IPV6INIT=yes
            IPADDR6=fe80::9c26:c3ff:fea4:62c8/64
            IPV6ADDR=fe80::9c26:c3ff:fea4:62c8/64
            NM_CONTROLLED=yes
            ONBOOT=yes
            STARTMODE=auto
            TYPE=Ethernet
            USERCTL=no
            """
            ),
        )

        ccrmci.disable_ipv6(fname)
        self.assertEqual(
            dedent(
                """\
            BOOTPROTO=static
            DEVICE=eth5
            HWADDR=42:20:86:df:fa:4c
            ONBOOT=yes
            STARTMODE=auto
            TYPE=Ethernet
            USERCTL=no
            NM_CONTROLLED=no
            """
            ),
            util.load_file(fname),
        )

    @mock.patch(MPATH + ".refresh_rmc")
    @mock.patch(MPATH + ".restart_network_manager")
    @mock.patch(MPATH + ".disable_ipv6")
    @mock.patch(MPATH + ".refresh_ipv6")
    @mock.patch(MPATH + ".netinfo.netdev_info")
    @mock.patch(MPATH + ".subp.which")
    def test_handle(
        self,
        m_refresh_rmc,
        m_netdev_info,
        m_refresh_ipv6,
        m_disable_ipv6,
        m_restart_nm,
        m_which,
    ):
        """Basic test of handle."""
        m_netdev_info.return_value = NET_INFO
        m_which.return_value = "/opt/rsct/bin/rmcctrl"
        ccrmci.handle("refresh_rmc_and_interface", None, None, None, None)
        self.assertEqual(1, m_netdev_info.call_count)
        m_refresh_ipv6.assert_called_with("env5")
        m_disable_ipv6.assert_called_with(
            "/etc/sysconfig/network-scripts/ifcfg-env5"
        )
        self.assertEqual(1, m_restart_nm.call_count)
        self.assertEqual(1, m_refresh_rmc.call_count)

    @mock.patch(MPATH + ".netinfo.netdev_info")
    def test_find_ipv6(self, m_netdev_info):
        """find_ipv6_ifaces parses netdev_info returning those with ipv6"""
        m_netdev_info.return_value = NET_INFO
        found = ccrmci.find_ipv6_ifaces()
        self.assertEqual(["env5"], found)

    @mock.patch(MPATH + ".subp.subp")
    def test_refresh_ipv6(self, m_subp):
        """refresh_ipv6 should ip down and up the interface."""
        iface = "myeth0"
        ccrmci.refresh_ipv6(iface)
        m_subp.assert_has_calls(
            [
                mock.call(["ip", "link", "set", iface, "down"]),
                mock.call(["ip", "link", "set", iface, "up"]),
            ]
        )