Beispiel #1
0
def test_manage_nodes_refresh_fails(tmpdir, monkeypatch):
    nodes_json = tmpdir / "nodes.json"
    real_path = tmpdir / "nodes"
    monkeypatch.setattr(sambacc.samba_cmds, "_GLOBAL_PREFIX", ["false"])

    def once():
        raise _Stop()

    # node needs to be added
    with open(nodes_json, "w") as fh:
        fh.write("""
            {"nodes": [
                {"node": "10.0.0.10", "pnn": 0, "state": "ready"},
                {"node": "10.0.0.11", "pnn": 1, "state": "new"}
            ]}
        """)
    with open(real_path, "w") as fh:
        fh.write("10.0.0.10\n")
    with pytest.raises(Exception):
        ctdb.manage_nodes(0,
                          nodes_json=nodes_json,
                          real_path=real_path,
                          pause_func=once)
    with open(real_path, "r") as fh:
        lines = [x.strip() for x in fh.readlines()]
    assert "10.0.0.10" in lines
    assert "10.0.0.11" in lines
    with open(nodes_json, "r") as fh:
        jdata = json.load(fh)
    assert jdata["nodes"][1]["node"] == "10.0.0.11"
    assert jdata["nodes"][1]["state"] == "new"
Beispiel #2
0
def ctdb_manage_nodes(ctx: Context) -> None:
    """Run a long lived procees to monitor the node state file for new nodes.
    When a new node is found, if the current node is in the correct state, this
    node will add it to CTDB.
    """
    _ctdb_ok()
    np = NodeParams(ctx)
    expected_pnn = np.node_number or 0
    waiter = best_waiter(np.nodes_json)

    errors = 0
    while True:
        try:
            ctdb.manage_nodes(
                expected_pnn,
                nodes_json=np.nodes_json,
                real_path=np.persistent_path,
                pause_func=waiter.wait,
            )
            errors = 0
        except KeyboardInterrupt:
            raise
        except Exception as err:
            _logger.error(
                f"error during manage_nodes: {err}, count={errors}",
                exc_info=True,
            )
            errors += 1
            if errors > 10:
                _logger.error(f"too many retries ({errors}). giving up")
                raise
            waiter.wait()
Beispiel #3
0
def test_manage_nodes_invalid_state(tmpdir):
    nodes_json = tmpdir / "nodes.json"
    real_path = tmpdir / "nodes"

    def once():
        raise _Stop()

    # node is ready but missing from nodes file
    with open(nodes_json, "w") as fh:
        fh.write("""
            {"nodes": [
                {"node": "10.0.0.10", "pnn": 0, "state": "ready"},
                {"node": "10.0.0.11", "pnn": 1, "state": "ready"}
            ]}
        """)
    with open(real_path, "w") as fh:
        fh.write("10.0.0.10\n")
    with pytest.raises(ValueError):
        ctdb.manage_nodes(0,
                          nodes_json=nodes_json,
                          real_path=real_path,
                          pause_func=once)
Beispiel #4
0
def test_manage_nodes(tmpdir, monkeypatch):
    nodes_json = tmpdir / "nodes.json"
    real_path = tmpdir / "nodes"
    monkeypatch.setattr(sambacc.samba_cmds, "_GLOBAL_PREFIX", ["true"])

    def once():
        raise _Stop()

    with pytest.raises(FileNotFoundError):
        ctdb.manage_nodes(0,
                          nodes_json=nodes_json,
                          real_path=real_path,
                          pause_func=once)

    # node not present - can not update
    with open(nodes_json, "w") as fh:
        fh.write("""
            {"nodes": [
            ]}
        """)
    with pytest.raises(_Stop):
        ctdb.manage_nodes(0,
                          nodes_json=nodes_json,
                          real_path=real_path,
                          pause_func=once)

    # node present, not in nodes - can not update
    with open(nodes_json, "w") as fh:
        fh.write("""
            {"nodes": [
                {"identity":"a", "node": "10.0.0.10", "pnn": 0, "state": "new"}
            ]}
        """)
    with pytest.raises(_Stop):
        ctdb.manage_nodes(0,
                          nodes_json=nodes_json,
                          real_path=real_path,
                          pause_func=once)

    # node present, in nodes - nothing to do
    with open(nodes_json, "w") as fh:
        fh.write("""
            {"nodes": [
                {"identity":"a", "node": "10.0.0.10", "pnn": 0,
                 "state": "ready"}
            ]}
        """)
    with open(real_path, "w") as fh:
        fh.write("10.0.0.10\n")
    with pytest.raises(_Stop):
        ctdb.manage_nodes(0,
                          nodes_json=nodes_json,
                          real_path=real_path,
                          pause_func=once)

    # node present, in nodes - new node in json
    with open(nodes_json, "w") as fh:
        fh.write("""
            {"nodes": [
                {"identity":"a", "node": "10.0.0.10", "pnn": 0,
                 "state": "ready"},
                {"identity":"b", "node": "10.0.0.11", "pnn": 1,
                 "state": "new"}
            ]}
        """)
    with open(real_path, "w") as fh:
        fh.write("10.0.0.10\n")
    with pytest.raises(_Stop):
        ctdb.manage_nodes(0,
                          nodes_json=nodes_json,
                          real_path=real_path,
                          pause_func=once)
    with open(real_path, "r") as fh:
        lines = [x.strip() for x in fh.readlines()]
    assert "10.0.0.10" in lines
    assert "10.0.0.11" in lines

    # invalid state - nodes file and nodes json out of whack
    with open(nodes_json, "w") as fh:
        fh.write("""
            {"nodes": [
                {"identity":"a", "node": "10.0.0.10", "pnn": 0,
                 "state": "ready"},
                {"identity":"b", "node": "10.0.0.11", "pnn": 1,
                 "state": "new"}
            ]}
        """)
    with open(real_path, "w") as fh:
        fh.write("10.0.0.10\n")
        fh.write("10.0.0.12\n")
        fh.write("10.0.0.13\n")
    with pytest.raises(ValueError):
        ctdb.manage_nodes(0,
                          nodes_json=nodes_json,
                          real_path=real_path,
                          pause_func=once)

    # node present but json file shows update incomplete
    with open(nodes_json, "w") as fh:
        fh.write("""
            {"nodes": [
                {"identity":"a", "node": "10.0.0.10", "pnn": 0,
                 "state": "ready"},
                {"identity":"b", "node": "10.0.0.11", "pnn": 1,
                 "state": "new"}
            ]}
        """)
    with open(real_path, "w") as fh:
        fh.write("10.0.0.10\n")
        fh.write("10.0.0.11\n")
    with pytest.raises(_Stop):
        ctdb.manage_nodes(0,
                          nodes_json=nodes_json,
                          real_path=real_path,
                          pause_func=once)
    with open(real_path, "r") as fh:
        lines = [x.strip() for x in fh.readlines()]
    assert "10.0.0.10" in lines
    assert "10.0.0.11" in lines
    with open(nodes_json, "r") as fh:
        jdata = json.load(fh)
    assert jdata["nodes"][1]["node"] == "10.0.0.11"
    assert jdata["nodes"][1]["state"] == "ready"

    with open(nodes_json, "w") as fh:
        fh.write("""
            {"nodes": [
                {"identity":"a", "node": "10.0.0.10", "pnn": 0,
                 "state": "ready"},
                {"identity":"b", "node": "10.0.1.11", "pnn": 1,
                 "state": "changed"}
            ]}
        """)
    with open(real_path, "w") as fh:
        fh.write("10.0.0.10\n")
        fh.write("10.0.0.11\n")
    with pytest.raises(_Stop):
        ctdb.manage_nodes(0,
                          nodes_json=nodes_json,
                          real_path=real_path,
                          pause_func=once)
    with open(real_path, "r") as fh:
        lines = [x.strip() for x in fh.readlines()]
    assert "10.0.0.10" in lines
    assert "#10.0.0.11" in lines
    with open(nodes_json, "r") as fh:
        jdata = json.load(fh)
    assert jdata["nodes"][1]["node"] == "10.0.1.11"
    assert jdata["nodes"][1]["state"] == "replaced"

    with pytest.raises(_Stop):
        ctdb.manage_nodes(0,
                          nodes_json=nodes_json,
                          real_path=real_path,
                          pause_func=once)
    with open(real_path, "r") as fh:
        lines = [x.strip() for x in fh.readlines()]
    assert "10.0.0.10" in lines
    assert "10.0.1.11" in lines
    with open(nodes_json, "r") as fh:
        jdata = json.load(fh)
    assert jdata["nodes"][1]["node"] == "10.0.1.11"
    assert jdata["nodes"][1]["state"] == "ready"