Exemple #1
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 #2
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 #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_api_key():
    if os.path.exists(CRED_STORE_FILE):
        os.remove(CRED_STORE_FILE)

    # Test that cli correctly generates an API key
    cli_result = sp.run(shlex.split(
        "bin/httpfs-cli add-api-key {}".format(CRED_STORE_FILE)),
                        stdout=sp.PIPE,
                        stderr=sp.PIPE,
                        env=os.environ)
    try:
        assert cli_result.returncode == 0
    except:
        raise AssertionError("[API Key Generation Failed] {}".format(
            cli_result.stderr.decode("utf-8").strip()))

    # Make sure cli correctly populates the new API key in the keystore
    cred_store = TextCredStore(file_path=CRED_STORE_FILE)

    api_key_str = cli_result.stdout.decode("utf-8").strip()
    assert cred_store.has_cred(api_key_str)

    client = HttpFsClient("localhost", 8080, api_key=api_key_str)

    # Mock sending a request
    FAKE_PATH = "/some/link"

    # 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": {
                "target": FAKE_PATH
            }
        })

    def fake_post(server_url, **kwargs):
        assert server_url == client._server_url
        assert "Authorization" in kwargs["headers"] and kwargs["headers"][
            "Authorization"] == api_key_str
        assert kwargs["json"]["type"] == HttpFsRequest.OP_READLINK
        assert kwargs["json"]["args"]["link_path"] == FAKE_PATH
        return fake_response

    client._http_keepalive_session.post = fake_post
    client.readlink("/some/link")

    os.remove(CRED_STORE_FILE)
Exemple #5
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 #6
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 #7
0
def test_constructor_with_ssl(mocker):
    client_w_ca = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file="test-file.crt"
    )

    assert client_w_ca.server_hostname == HOSTNAME
    assert client_w_ca._server_url.startswith("https")
Exemple #8
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 #9
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 #10
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 #11
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 #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_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 #14
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 #15
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 #16
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 #17
0
def test_constructor_without_ssl():
    client = HttpFsClient(
        HOSTNAME,
        PORT,
        ca_file=None
    )

    assert client.server_hostname == HOSTNAME
    assert client._server_url == EXPECTED_URL
    assert client._api_key == None

    client = HttpFsClient(
        HOSTNAME,
        PORT,
        api_key=FAKE_API_KEY,
        ca_file=None
    )

    assert client.server_hostname == HOSTNAME
    assert client._server_url == EXPECTED_URL
    assert client._api_key == FAKE_API_KEY
Exemple #18
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 #19
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 #20
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 #21
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 #22
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 #23
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)
Exemple #24
0
PARSER.add_argument("--verbose",
                    dest="verbose",
                    help="Be verbose",
                    action="store_true")
ARGS = PARSER.parse_args()

log_level = logging.WARN
if ARGS.verbose:
    log_level = logging.DEBUG

logging.basicConfig(level=log_level, format=LOG_FMT, datefmt=DATE_FMT)

try:
    # Create the mount directory
    if not os.path.exists(ARGS.mount):
        logging.error("Mount point '%s' does not exist", ARGS.mount)
        sys.exit(1)

    [HOSTNAME, PORT] = ARGS.server.rsplit(':', 1)

    # Mount the filesystem
    FUSE(HttpFsClient(HOSTNAME,
                      PORT,
                      api_key=ARGS.api_key,
                      ca_file=ARGS.ca_file),
         ARGS.mount,
         foreground=True,
         allow_other=True)
except Exception as exception:
    logging.error("ERROR: %s", exception)