Esempio n. 1
0
 def test_no_use_sudo_in_snap(self):
     patch_call_and_check = self.patch(ipaddr_module, "call_and_check")
     patch_call_and_check.return_value = json.dumps(
         {"networks": SAMPLE_LXD_NETWORKS})
     self.patch(ipaddr_module, "running_in_snap").return_value = True
     get_ip_addr()
     patch_call_and_check.assert_called_once_with(
         [get_resources_bin_path()])
Esempio n. 2
0
 def test_get_ip_addr_calls_methods(self):
     self.patch(ipaddr_module, "running_in_snap").return_value = False
     patch_call_and_check = self.patch(ipaddr_module, "call_and_check")
     patch_call_and_check.return_value = json.dumps(
         {"networks": SAMPLE_LXD_NETWORKS})
     # all interfaces from binary output are included in result
     self.assertCountEqual(SAMPLE_LXD_NETWORKS, get_ip_addr())
     patch_call_and_check.assert_called_once_with(
         ["sudo", get_resources_bin_path()])
Esempio n. 3
0
 def test_get_ip_addr_calls_methods(self):
     patch_call_and_check = self.patch(ipaddr_module, "call_and_check")
     patch_call_and_check.return_value = json.dumps(
         {"networks": SAMPLE_LXD_NETWORKS})
     # all interfaces from binary output are included in result
     self.assertCountEqual(SAMPLE_LXD_NETWORKS, get_ip_addr())
     self.assertThat(
         patch_call_and_check,
         MockCalledOnceWith([get_resources_bin_path()]),
     )
Esempio n. 4
0
 def test_get_ip_addr_calls_methods(self):
     patch_call_and_check = self.patch(ipaddr_module, "call_and_check")
     patch_call_and_check.return_value = sentinel.ip_addr_cmd
     patch_parse_ip_addr = self.patch(ipaddr_module, "parse_ip_addr")
     patch_parse_ip_addr.return_value = sentinel.parse_result
     patch_annotate_with_driver_information = self.patch(
         ipaddr_module, "annotate_with_driver_information")
     patch_annotate_with_driver_information.return_value = sentinel.output
     self.assertEquals(sentinel.output, get_ip_addr())
     self.assertThat(patch_call_and_check,
                     MockCalledOnceWith(["ip", "addr"]))
     self.assertThat(patch_parse_ip_addr,
                     MockCalledOnceWith(sentinel.ip_addr_cmd))
     self.assertThat(patch_annotate_with_driver_information,
                     MockCalledOnceWith(sentinel.parse_result))
Esempio n. 5
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