Exemple #1
0
def test_send_request_with_api_key():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        api_key=FAKE_API_KEY,
        ca_file=None
    )

    # Fake response from the server
    fake_response = MagicMock()
    fake_response.raise_for_status = MagicMock(return_value=None)
    fake_response.headers = {
        "Content-Type": "application/json",
        "Server": "HttpFs"
    }
    fake_response.json = MagicMock(return_value={
        "error_no": HttpFsResponse.ERR_NONE,
        "response_data": {}
    })

    # Fake POST request method
    def fake_post(server_url, **kwargs):
        assert server_url == client._server_url
        assert "Authorization" in kwargs["headers"] and kwargs["headers"]["Authorization"] == FAKE_API_KEY
        assert kwargs["json"] == FAKE_POST_REQ
        return fake_response

    # Fake requests.session
    fake_session = MagicMock()
    fake_session.post = fake_post

    client._http_keepalive_session = fake_session
    client._send_request(FAKE_REQ_TYPE, **FAKE_REQ_ARGS)
Exemple #2
0
def test_write():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_path = "/some/path"
    fake_data = b"This is some bytes"
    fake_offset = 1024
    fake_fd = 0

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        for arg in ["uid", "gid"]:
            assert arg in kwargs.keys()
        assert request_type == HttpFsRequest.OP_WRITE
        assert kwargs["file_descriptor"] == fake_fd
        assert kwargs["data"] == base64.standard_b64encode(fake_data).decode("utf-8")
        assert kwargs["offset"] == fake_offset
        resp = HttpFsResponse()
        resp._response_data["bytes_written"] = 10
        return resp

    client._send_request = fake_send_request
    client.write(fake_path, fake_data, fake_offset, fh=fake_fd)
Exemple #3
0
def test_read():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_path = "/some/path"
    fake_offset = 1024
    fake_size = 512
    fake_fd = 0

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        for arg in ["uid", "gid"]:
            assert arg in kwargs.keys()
        assert request_type == HttpFsRequest.OP_READ
        assert kwargs["file_descriptor"] == fake_fd
        assert kwargs["size"] == fake_size
        assert kwargs["offset"] == fake_offset
        resp = HttpFsResponse()
        resp._response_data["bytes_read"] = bytes()
        return resp

    client._send_request = fake_send_request
    client.read(fake_path, fake_size, fake_offset, fh=fake_fd)
Exemple #4
0
def test_statfs():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_path = "/some/path"

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        assert request_type == HttpFsRequest.OP_STAT_FS
        assert kwargs["path"] == fake_path
        return HttpFsResponse()

    client._send_request = fake_send_request
    client.statfs(fake_path)
Exemple #5
0
def test_release():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_path = "/"
    fake_fd = 999

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        assert request_type == HttpFsRequest.OP_RELEASE
        assert kwargs["file_descriptor"] == fake_fd
        return HttpFsResponse()

    client._send_request = fake_send_request
    client.release(fake_path, fh=fake_fd)
Exemple #6
0
def test_flush():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_path = "/some/path"
    fake_fd = 1234

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        assert request_type == HttpFsRequest.OP_FLUSH
        assert kwargs["file_descriptor"] == fake_fd
        return HttpFsResponse()

    client._send_request = fake_send_request
    client.flush(fake_path, fake_fd)
Exemple #7
0
def test_mkdir():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_path = "/some/path"
    fake_mode = 0o644

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        assert request_type == HttpFsRequest.OP_MKDIR
        assert kwargs["path"] == fake_path
        assert kwargs["mode"] == fake_mode
        return HttpFsResponse()

    client._send_request = fake_send_request
    client.mkdir(fake_path, fake_mode)
Exemple #8
0
def test_chown():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_path = "/some/path"

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        assert request_type == HttpFsRequest.OP_CHOWN
        assert kwargs["path"] == fake_path
        assert kwargs["uid"] == 2222
        assert kwargs["gid"] == 0
        return HttpFsResponse()

    client._send_request = fake_send_request
    client.chown(fake_path, 2222, 0)
Exemple #9
0
def test_readlink():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_path = "/some/path"

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        assert request_type == HttpFsRequest.OP_READLINK
        assert kwargs["link_path"] == fake_path
        resp = HttpFsResponse()
        resp._response_data["target"] = ""
        return resp

    client._send_request = fake_send_request
    client.readlink(fake_path)
Exemple #10
0
def test_unlink():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_path = "/some/path"

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        for arg in ["uid", "gid"]:
            assert arg in kwargs.keys()
        assert request_type == HttpFsRequest.OP_UNLINK
        assert kwargs["path"] == fake_path
        return HttpFsResponse()

    client._send_request = fake_send_request
    client.unlink(fake_path)
Exemple #11
0
def test_symlink():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_source = "/some/source"
    fake_target = "/some/target"

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        assert request_type == HttpFsRequest.OP_SYMLINK
        assert kwargs["target"] == fake_target
        assert kwargs["source"] == fake_source
        return HttpFsResponse()

    client._send_request = fake_send_request
    client.symlink(fake_target, fake_source)
Exemple #12
0
def test_truncate():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_path = "/some/path"
    fake_len = 16

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        assert request_type == HttpFsRequest.OP_TRUNCATE
        assert kwargs["path"] == fake_path
        assert kwargs["length"] == fake_len
        return HttpFsResponse()

    client._send_request = fake_send_request
    client.truncate(fake_path, fake_len)
Exemple #13
0
def test_fsync():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_path = "/some/path"
    fake_fd = 1234
    fake_ds = True

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        assert request_type == HttpFsRequest.OP_FSYNC
        assert kwargs["file_descriptor"] == fake_fd
        assert kwargs["datasync"] == fake_ds
        return HttpFsResponse()

    client._send_request = fake_send_request
    client.fsync(fake_path, fh=fake_fd, datasync=fake_ds)
Exemple #14
0
def test_utimens():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_path = "/some/path"
    fake_times = (0, 0)

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        for arg in ["uid", "gid"]:
            assert arg in kwargs.keys()
        assert request_type == HttpFsRequest.OP_UTIMENS
        assert kwargs["path"] == fake_path
        assert kwargs["times"] == fake_times
        return HttpFsResponse()

    client._send_request = fake_send_request
    client.utimens(fake_path, times=fake_times)
Exemple #15
0
def test_mknod():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_path = "/some/path"
    fake_mode = 0o644
    fake_dev = False

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        assert request_type == HttpFsRequest.OP_MKNOD
        assert kwargs["path"] == fake_path
        assert kwargs["mode"] == fake_mode
        assert kwargs["dev"] == fake_dev
        return HttpFsResponse()

    client._send_request = fake_send_request
    client.mknod(fake_path, fake_mode, fake_dev)
Exemple #16
0
def test_rename():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_old_path = "/old-path"
    fake_new_path = "/new-path"

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        for arg in ["uid", "gid"]:
            assert arg in kwargs.keys()
        assert request_type == HttpFsRequest.OP_RENAME
        assert kwargs["old_path"] == fake_old_path
        assert kwargs["new_path"] == fake_new_path
        return HttpFsResponse()

    client._send_request = fake_send_request
    client.rename(fake_old_path, fake_new_path)
Exemple #17
0
def test_open():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_path = "/some/path"
    fake_flags = 0 | 1 | 2 | 666

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        for arg in ["uid", "gid"]:
            assert arg in kwargs.keys()
        assert request_type == HttpFsRequest.OP_OPEN
        assert kwargs["path"] == fake_path
        assert kwargs["flags"] == fake_flags
        return HttpFsResponse()

    client._send_request = fake_send_request
    client.open(fake_path, fake_flags)
Exemple #18
0
def test_chmod():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_path = "/some/path"
    fake_mode = 0o600

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        assert request_type == HttpFsRequest.OP_CHMOD
        for required_key in ["uid", "gid"]:
            assert required_key in kwargs.keys()
        assert kwargs["path"] == fake_path
        assert kwargs["mode"] == fake_mode
        return HttpFsResponse()

    client._send_request = fake_send_request
    client.chmod(fake_path, fake_mode)
Exemple #19
0
def test_readdir():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_path = "/some/path"

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        for arg in ["uid", "gid"]:
            assert arg in kwargs.keys()
        assert request_type == HttpFsRequest.OP_READDIR
        assert kwargs["path"] == fake_path
        resp = HttpFsResponse()
        resp._response_data["dir_listing"] = list()
        return resp

    client._send_request = fake_send_request
    client.readdir(fake_path)
Exemple #20
0
def test_create():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    fake_path = "/some/path"
    fake_mode = 0o600

    # Fake _send_request
    def fake_send_request(request_type, **kwargs):
        assert request_type == HttpFsRequest.OP_CREATE
        for required_key in ["uid", "gid"]:
            assert required_key in kwargs.keys()
        assert kwargs["path"] == fake_path
        assert kwargs["mode"] == fake_mode
        resp = HttpFsResponse()
        resp._response_data["file_descriptor"] = 0
        return resp

    client._send_request = fake_send_request
    client.create(fake_path, fake_mode)