Example #1
0
def test_interface_set_state(setup_networking):
    result = get_interface_state("test-ns-1", "eth0")
    assert not result["interface"]["up"]

    set_interface_state("test-ns-1", "eth0", {"up": True})
    result = get_interface_state("test-ns-1", "eth0")
    assert result["interface"]["up"]

    set_interface_state("test-ns-1", "eth0", {"mtu": 1600})
    result = get_interface_state("test-ns-1", "eth0")
    assert result["interface"]["mtu"] == 1600

    set_interface_state(
        "test-ns-1", "eth0",
        {"address": [{
            "address": "192.0.2.15",
            "prefixlen": "24"
        }]})
    result = get_interface_state("test-ns-1", "eth0")
    assert result["interface"]["address"][0]["address"] == "192.0.2.15"

    set_interface_state(
        "test-ns-1", "eth0",
        {"address": [{
            "address": "192.0.3.15",
            "prefixlen": "24"
        }]})
    result = get_interface_state("test-ns-1", "eth0")
    assert len(result["interface"]["address"]) == 1
    assert result["interface"]["address"][0]["address"] == "192.0.3.15"

    set_interface_state("test-ns-1", "eth0", {"address": []})
    result = get_interface_state("test-ns-1", "eth0")
    assert len(result["interface"]["address"]) == 0
Example #2
0
def test_interface_get_state(setup_networking):
    result = get_interface_state("test-ns-4", "eth0")

    assert "interface" in result
    assert "up" in result["interface"]
    assert result["interface"]["up"]
    assert "mtu" in result["interface"]
    assert result["interface"]["mtu"] == 1500

    assert len(result["interface"]["address"]) == 1
    assert result["interface"]["address"][0]["address"] == "192.0.2.1"
    assert result["interface"]["address"][0]["family"] == "inet"
    assert result["interface"]["address"][0]["prefixlen"] == 24
Example #3
0
def set_interface_state(namespace, interface):
    namespace = util.process_namespace(namespace)

    if app.simulate:
        cfg = get_config()
        if namespace not in cfg.namespaces:
            abort(404)

        ns = cfg.namespaces[namespace]
        intf = ns.get_interface(interface)
        if intf is None:
            abort(404)

        intf.set_state(request.json)
        return jsonify({"interface": intf.get_state()})
    else:
        util.set_interface_state(namespace, interface, request.json)
        return jsonify(util.get_interface_state(namespace, interface))
Example #4
0
def create_sub_interface(namespace, interface):
    if namespace is None or len(namespace) == 0:
        raise exceptions.ServerError(f"Invalid namespace {namespace}")

    parts = interface.split(".")
    if len(parts) not in [2, 3]:
        raise exceptions.ServerError(
            f"Only single and double tagged interfaces are supported")

    if app.simulate:
        cfg = get_config()
        if namespace not in cfg.namespaces:
            abort(404)

        all_interfaces = [
            intf.name for intf in cfg.namespaces[namespace].interfaces
        ]
    else:
        all_interfaces = util.list_interfaces(namespace)

    if interface in all_interfaces:
        raise exceptions.ServerError(
            f"Interface {interface} already exists in {namespace}")

    base_interface = parts.pop(0)

    if base_interface not in all_interfaces:
        raise exceptions.ServerError(
            f"Base interface {base_interface} does not exist")

    outer = int(parts.pop(0))
    inner = 0
    if len(parts):
        inner = int(parts[0])

    if app.simulate:
        ns = cfg.namespaces[namespace]
        base_intf = ns.get_interface(base_interface)
        intf = config.Interface(name=interface, mac=base_intf.mac)
        ns.interfaces.append(intf)
        return jsonify({"interface": intf.get_state()})
    else:
        util.create_sub_interface(namespace, base_interface, outer, inner)
        return jsonify(util.get_interface_state(namespace, interface))
Example #5
0
def set_interface_state(namespace, interface):
    if namespace is None or len(namespace) == 0:
        raise exceptions.ServerError(f"Invalid namespace {namespace}")

    if app.simulate:
        cfg = get_config()
        if namespace not in cfg.namespaces:
            abort(404)

        ns = cfg.namespaces[namespace]
        intf = ns.get_interface(interface)
        if intf is None:
            abort(404)

        intf.set_state(request.json)
        return jsonify({"interface": intf.get_state()})
    else:
        util.set_interface_state(namespace, interface, request.json)
        return jsonify(util.get_interface_state(namespace, interface))
Example #6
0
def get_interface_state(namespace, interface):
    namespace = util.process_namespace(namespace, allow_none=True)

    if app.simulate:
        cfg = get_config()
        if namespace not in cfg.namespaces:
            abort(404)

        ns = cfg.namespaces[namespace]
        intf = ns.get_interface(interface)
        if intf is None:
            abort(404)

        return jsonify({"interface": intf.get_state()})
    else:
        try:
            return jsonify(util.get_interface_state(namespace, interface))
        except Exception:
            LOGGER.exception("Failed to get the interface state")
            abort(404)
Example #7
0
def get_interface_state(namespace, interface):
    if namespace is None or len(namespace) == 0:
        raise exceptions.ServerError(f"Invalid namespace {namespace}")

    if app.simulate:
        cfg = get_config()
        if namespace not in cfg.namespaces:
            abort(404)

        ns = cfg.namespaces[namespace]
        intf = ns.get_interface(interface)
        if intf is None:
            abort(404)

        return jsonify({"interface": intf.get_state()})
    else:
        try:
            return jsonify(util.get_interface_state(namespace, interface))
        except Exception:
            LOGGER.exception("Failed to get the interface state")
            abort(404)