Esempio n. 1
0
 def test__calls_methods(self):
     patch_call_and_check = self.patch(iproute_module, "call_and_check")
     patch_call_and_check.return_value = sentinel.ip_route_cmd
     patch_parse_ip_route = self.patch(iproute_module, "parse_ip_route")
     patch_parse_ip_route.return_value = sentinel.output
     self.assertEquals(sentinel.output, get_ip_route())
     self.assertThat(
         patch_call_and_check,
         MockCalledOnceWith(["ip", "route", "list", "scope", "global"]))
     self.assertThat(patch_parse_ip_route,
                     MockCalledOnceWith(sentinel.ip_route_cmd))
Esempio n. 2
0
 def test_parse_routes(self):
     routes = [
         {
             "dst": "default",
             "gateway": "10.10.10.1",
             "dev": "eth0",
             "protocol": "dhcp",
             "metric": 600,
             "flags": [],
         },
         {
             "dst": "192.168.1.0/24",
             "gateway": "192.168.1.1",
             "dev": "eth1",
             "protocol": "static",
             "metric": 100,
             "flags": [],
         },
     ]
     patch_call_and_check = self.patch(iproute_module, "call_and_check")
     patch_call_and_check.return_value = json.dumps(routes)
     self.assertEqual(
         get_ip_route(),
         {
             "default": {
                 "gateway": "10.10.10.1",
                 "dev": "eth0",
                 "protocol": "dhcp",
                 "metric": 600,
                 "flags": [],
             },
             "192.168.1.0/24": {
                 "gateway": "192.168.1.1",
                 "dev": "eth1",
                 "protocol": "static",
                 "metric": 100,
                 "flags": [],
             },
         },
     )
     patch_call_and_check.assert_called_once_with(
         ["ip", "-json", "route", "list", "scope", "global"]
     )
Esempio n. 3
0
def get_all_interfaces_definition(
        annotate_with_monitored: bool = True) -> dict:
    """Return interfaces definition by parsing "ip addr" and the running
    "dhclient" processes on the machine.

    The interfaces definition is defined as a contract between the region and
    the rack controller. The region controller processes this resulting
    dictionary to update the interfaces model for the rack controller.

    :param annotate_with_monitored: If True, annotates the given interfaces
        with whether or not they should be monitored. (Default: True)
    """
    interfaces = {}
    dhclient_info = get_dhclient_info()
    iproute_info = get_ip_route()
    exclude_types = ["loopback", "ipip"]
    if not running_in_container():
        exclude_types.append("ethernet")
    ipaddr_info = {
        name: ipaddr
        for name, ipaddr in get_ip_addr().items()
        if (ipaddr["type"] not in exclude_types
            and not ipaddr["type"].startswith("unknown-"))
    }
    for name, ipaddr in ipaddr_info.items():
        iface_type = "physical"
        parents = []
        mac_address = None
        vid = None
        if ipaddr["type"] == "ethernet.bond":
            iface_type = "bond"
            mac_address = ipaddr["mac"]
            for bond_nic in ipaddr["bonded_interfaces"]:
                if bond_nic in interfaces or bond_nic in ipaddr_info:
                    parents.append(bond_nic)
        elif ipaddr["type"] == "ethernet.vlan":
            iface_type = "vlan"
            parents.append(ipaddr['parent'])
            vid = ipaddr["vid"]
        elif ipaddr["type"] == "ethernet.bridge":
            iface_type = "bridge"
            mac_address = ipaddr["mac"]
            for bridge_nic in ipaddr["bridged_interfaces"]:
                if bridge_nic in interfaces or bridge_nic in ipaddr_info:
                    parents.append(bridge_nic)
        else:
            mac_address = ipaddr["mac"]

        # Create the interface definition will links for both IPv4 and IPv6.
        interface = {
            "type": iface_type,
            "index": ipaddr['index'],
            "links": [],
            "enabled": True if 'UP' in ipaddr['flags'] else False,
            "parents": parents,
            "source": "ipaddr",
        }
        if mac_address is not None:
            interface["mac_address"] = mac_address
        if vid is not None:
            interface["vid"] = vid
        # Add the static and dynamic IP addresses assigned to the interface.
        dhcp_address = dhclient_info.get(name, None)
        for address in ipaddr.get("inet", []) + ipaddr.get("inet6", []):
            if str(IPNetwork(address).ip) == dhcp_address:
                interface["links"].append({
                    "mode": "dhcp",
                    "address": address,
                })
            else:
                interface["links"].append({
                    "mode": "static",
                    "address": address,
                })
        fix_link_addresses(interface["links"])
        fix_link_gateways(interface["links"], iproute_info)
        interfaces[name] = interface

        if annotate_with_monitored:
            annotate_with_default_monitored_interfaces(interfaces)

    return interfaces