Ejemplo n.º 1
0
def test_unlock_host_slot(slot_content, expected_log, expected_result, caplog):
    host = RemoteHost(hostname="remote-host-001", username="******",
                      ssh_keyfile="/path/to/key", slots=3, socket_path=SOCKET_PATH)

    def mocked_command(cmd, *args, **kwargs):
        if cmd == "touch /home/builder/osbs_slots/slot_2 && cat /home/builder/osbs_slots/slot_2":
            return make_ssh_result(stdout=slot_content)

        if cmd == ("flock --conflict-exit-code 42 --nonblocking "
                   "/home/builder/osbs_slots/slot_2.lock cat"):
            return make_flock_ssh_result(stdout="verify lock")

        write_patt = re.compile(r"truncate -s 0 /home/builder/osbs_slots/slot_2")
        if write_patt.match(cmd):
            return make_ssh_result()

        assert False, f"Unexpected command: {cmd}"

    (
        flexmock(SSHRetrySession)
        .should_receive("exec_command")
        .replace_with(mocked_command)
    )
    unlocked = host.unlock(2, "pr123")
    assert unlocked is expected_result
    assert expected_log in caplog.text
Ejemplo n.º 2
0
def test_lock_slot_with_flock_cat_error(caplog):
    host = RemoteHost(hostname="remote-host-001", username="******",
                      ssh_keyfile="/path/to/key", slots=3, socket_path=SOCKET_PATH)

    def mocked_command(cmd, *args, **kwargs):
        if cmd == "touch /home/builder/osbs_slots/slot_2 && cat /home/builder/osbs_slots/slot_2":
            return make_ssh_result()

        if cmd == ("flock --conflict-exit-code 42 --nonblocking "
                   "/home/builder/osbs_slots/slot_2.lock cat"):
            return make_flock_ssh_result(
                code=66,
                stdin_write_callback=lambda x: (_ for _ in ()).throw(OSError("socket is closed"))
            )

        assert False, f"Unexpected command: {cmd}"

    (
        flexmock(SSHRetrySession)
        .should_receive("exec_command")
        .replace_with(mocked_command)
    )
    locked = host.lock(2, "pr123")
    assert not locked
    assert "failed to acquire lock on slot 2: socket is closed" in caplog.text
Ejemplo n.º 3
0
def test_check_slot_is_free_with_invalid_id(caplog):
    host = RemoteHost(hostname="remote-host-001", username="******",
                      ssh_keyfile="/path/to/key", slots=3, socket_path=SOCKET_PATH)
    # slot id starts from 0
    free = host.is_free(3)
    assert "remote-host-001: invalid slot id 3, should be in" in caplog.text
    assert not free
Ejemplo n.º 4
0
def test_check_slot_is_free(cat_stdout, cat_stderr, cat_code, expected_result):
    host = RemoteHost(hostname="remote-host-001", username="******",
                      ssh_keyfile="/path/to/key", slots=3, socket_path=SOCKET_PATH)

    def mocked_command(cmd, *args, **kwargs):
        if cmd == "touch /home/builder/osbs_slots/slot_2 && cat /home/builder/osbs_slots/slot_2":
            return make_ssh_result(cat_stdout, cat_stderr, cat_code)

        assert False, f"Unexpected command: {cmd}"

    (
        flexmock(SSHRetrySession)
        .should_receive("exec_command")
        .replace_with(mocked_command)
    )
    free = host.is_free(2)
    assert free is expected_result
Ejemplo n.º 5
0
def test_lock_slot_with_other_locking_on_it(caplog):
    host = RemoteHost(hostname="remote-host-001", username="******",
                      ssh_keyfile="/path/to/key", slots=3, socket_path=SOCKET_PATH)

    def mocked_command(cmd, *args, **kwargs):
        if cmd == "touch /home/builder/osbs_slots/slot_2 && cat /home/builder/osbs_slots/slot_2":
            return make_ssh_result()

        if cmd == ("flock --conflict-exit-code 42 --nonblocking "
                   "/home/builder/osbs_slots/slot_2.lock cat"):
            return make_flock_ssh_result(code=42)

        assert False, f"Unexpected command: {cmd}"

    (
        flexmock(SSHRetrySession)
        .should_receive("exec_command")
        .replace_with(mocked_command)
    )
    locked = host.lock(2, "pr123")
    assert not locked
    assert "failed to acquire lock on slot 2: slot is locked by others" in caplog.text
Ejemplo n.º 6
0
def test_lock_an_occupied_slot(caplog):
    host = RemoteHost(hostname="remote-host-001", username="******",
                      ssh_keyfile="/path/to/key", slots=3, socket_path=SOCKET_PATH)

    def mocked_command(cmd, *args, **kwargs):
        if cmd == "touch /home/builder/osbs_slots/slot_2 && cat /home/builder/osbs_slots/slot_2":
            return make_ssh_result(stdout="123@2022-02-15T10:12:13.780426")

        if cmd == ("flock --conflict-exit-code 42 --nonblocking "
                   "/home/builder/osbs_slots/slot_2.lock cat"):
            return make_flock_ssh_result(stdout="")

        assert False, f"Unexpected command: {cmd}"

    (
        flexmock(SSHRetrySession)
        .should_receive("exec_command")
        .replace_with(mocked_command)
    )
    locked = host.lock(2, "pr234")
    assert not locked
    assert "remote-host-001: failed to lock slot 2 for pipelinerun pr234" in caplog.text
Ejemplo n.º 7
0
def test_lock_an_invalid_slot(caplog):
    host = RemoteHost(hostname="remote-host-001", username="******",
                      ssh_keyfile="/path/to/key", slots=3, socket_path=SOCKET_PATH)
    # Need to return different content for the same read slot commands,
    # which is not easy in a single mocked_command, so set it one by one
    read_slot = "touch /home/builder/osbs_slots/slot_2 && cat /home/builder/osbs_slots/slot_2"
    cmd_kwargs = {"timeout": int}
    (
        flexmock(SSHRetrySession)
        .should_receive("exec_command")
        .with_args(read_slot, **cmd_kwargs)
        .and_return(make_ssh_result())  # return empty slot for the first call
        .and_return(make_ssh_result(stdout="invalid_slot_content"))  # return invalid content
    )
    flock = "flock --conflict-exit-code 42 --nonblocking /home/builder/osbs_slots/slot_2.lock cat"
    (
        flexmock(SSHRetrySession)
        .should_receive("exec_command")
        .with_args(flock)
        .and_return(make_flock_ssh_result(stdout="verify lock"))
    )
    locked = host.lock(2, "pr123")
    assert not locked
    assert "slot 2 contains invalid content, it's probably corrupted" in caplog.text
Ejemplo n.º 8
0
def test_using_non_default_slots_dir():
    slots_dir = "/var/tmp/osbs/slots/"
    host = RemoteHost(hostname="remote-host-001", username="******",
                      ssh_keyfile="/path/to/key", slots=3, socket_path=SOCKET_PATH,
                      slots_dir=slots_dir)

    flexmock(SSHRetrySession).should_receive("connect")

    def mocked_command(cmd, *args, **kwargs):
        if cmd == f"mkdir -p {slots_dir}":
            return make_ssh_result()

        assert False, f"Unexpected command: {cmd}"

    (
        flexmock(SSHRetrySession)
        .should_receive("exec_command")
        .replace_with(mocked_command)
    )
    assert host.is_operational