Ejemplo n.º 1
0
async def test_avro_publish(rest_async_client, registry_async_client, admin_client):
    # pylint: disable=W0612
    tn = new_topic(admin_client)
    other_tn = new_topic(admin_client)
    await wait_for_topics(rest_async_client, topic_names=[tn, other_tn], timeout=NEW_TOPIC_TIMEOUT, sleep=1)
    header = REST_HEADERS["avro"]
    # check succeeds with 1 record and brand new schema
    res = await registry_async_client.post(f"subjects/{other_tn}/versions", json={"schema": second_schema_json})
    assert res.ok
    new_schema_id = res.json()["id"]
    urls = [f"/topics/{tn}", f"/topics/{tn}/partitions/0"]
    for url in urls:
        partition_id = 0 if "partition" in url else None
        for pl_type in ["key", "value"]:
            correct_payload = {f"{pl_type}_schema": schema_avro_json, "records": [{pl_type: o} for o in test_objects_avro]}
            res = await rest_async_client.post(url, correct_payload, headers=header)
            check_successful_publish_response(res, test_objects_avro, partition_id)
            # check succeeds with prepublished schema
            pre_publish_payload = {f"{pl_type}_schema_id": new_schema_id, "records": [{pl_type: o} for o in second_obj]}
            res = await rest_async_client.post(f"/topics/{tn}", json=pre_publish_payload, headers=header)
            check_successful_publish_response(res, second_obj, partition_id)
            # unknown schema id
            unknown_payload = {f"{pl_type}_schema_id": 666, "records": [{pl_type: o} for o in second_obj]}
            res = await rest_async_client.post(url, json=unknown_payload, headers=header)
            assert res.status == 408
Ejemplo n.º 2
0
async def test_topics(rest_async_client, admin_client):
    tn = new_topic(admin_client)
    res = await rest_async_client.get("/topics")
    assert res.ok, "Status code is not 200: %r" % res.status_code
    data = res.json()
    assert {tn}.difference(
        set(data)) == set(), "Retrieved topic names do not match: %r" % data
    res = await rest_async_client.get(f"/topics/{tn}")
    assert res.ok, "Status code is not 200: %r" % res.status_code
    data = res.json()
    assert data[
        "name"] == tn, f"Topic name should be {tn} and is {data['name']}"
    assert "configs" in data, "'configs' key is missing : %r" % data
    assert data["configs"] != {}, "'configs' key should not be empty"
    assert "partitions" in data, "'partitions' key is missing"
    assert len(data["partitions"]) == 1, "should only have one partition"
    assert "replicas" in data["partitions"][0], "'replicas' key is missing"
    assert len(
        data["partitions"][0]["replicas"]) == 1, "should only have one replica"
    assert data["partitions"][0]["replicas"][0][
        "leader"], "Replica should be leader"
    assert data["partitions"][0]["replicas"][0][
        "in_sync"], "Replica should be in sync"
    res = await rest_async_client.get("/topics/foo")
    assert res.status_code == 404, "Topic should not exist"
    assert res.json()["error_code"] == 40401, "Error code does not match"
Ejemplo n.º 3
0
async def test_publish_consume_avro(rest_async_client, admin_client, trail, schema_type):
    header = REST_HEADERS[schema_type]
    group_name = "e2e_group"
    instance_id = await new_consumer(rest_async_client, group_name, fmt=schema_type, trail=trail)
    assign_path = f"/consumers/{group_name}/instances/{instance_id}/assignments{trail}"
    consume_path = f"/consumers/{group_name}/instances/{instance_id}/records{trail}?timeout=1000"
    tn = new_topic(admin_client)
    assign_payload = {"partitions": [{"topic": tn, "partition": 0}]}
    res = await rest_async_client.post(assign_path, json=assign_payload, headers=header)
    assert res.ok
    publish_payload = schema_data[schema_type][1]
    await repeat_until_successful_request(
        rest_async_client.post,
        f"topics/{tn}{trail}",
        json_data={
            "value_schema": schema_data[schema_type][0],
            "records": [{
                "value": o
            } for o in publish_payload]
        },
        headers=header,
        error_msg="Unexpected response status for offset commit",
        timeout=10,
        sleep=1,
    )
    resp = await rest_async_client.get(consume_path, headers=header)
    assert resp.ok, f"Expected a successful response: {resp}"
    data = resp.json()
    assert len(data) == len(publish_payload), f"Expected to read test_objects from fetch request but got {data}"
    data_values = [x["value"] for x in data]
    for expected, actual in zip(publish_payload, data_values):
        assert expected == actual, f"Expecting {actual} to be {expected}"
Ejemplo n.º 4
0
async def test_publish(rest_async_client, admin_client):
    topic = new_topic(admin_client)
    await wait_for_topics(rest_async_client,
                          topic_names=[topic],
                          timeout=NEW_TOPIC_TIMEOUT,
                          sleep=1)
    topic_url = f"/topics/{topic}"
    partition_url = f"/topics/{topic}/partitions/0"
    # Proper Json / Binary
    for url in [topic_url, partition_url]:
        for payload, h in [({
                "value": {
                    "foo": "bar"
                }
        }, "json"), ({
                "value": "Zm9vCg=="
        }, "binary")]:
            res = await rest_async_client.post(url,
                                               json={"records": [payload]},
                                               headers=REST_HEADERS[h])
            res_json = res.json()
            assert res.ok
            assert "offsets" in res_json
            if "partition" in url:
                for o in res_json["offsets"]:
                    assert "partition" in o
                    assert o["partition"] == 0
Ejemplo n.º 5
0
async def test_consume(rest_async_client, admin_client, producer, trail):
    # avro to be handled in a separate testcase ??
    values = {
        "json": [json.dumps({
            "foo": f"bar{i}"
        }).encode("utf-8") for i in range(3)],
        "binary": [f"val{i}".encode('utf-8') for i in range(3)]
    }
    deserializers = {"binary": base64.b64decode, "json": lambda x: json.dumps(x).encode("utf-8")}
    group_name = "consume_group"
    for fmt in ["binary", "json"]:
        header = copy.deepcopy(REST_HEADERS[fmt])
        instance_id = await new_consumer(rest_async_client, group_name, fmt=fmt, trail=trail)
        assign_path = f"/consumers/{group_name}/instances/{instance_id}/assignments{trail}"
        seek_path = f"/consumers/{group_name}/instances/{instance_id}/positions/beginning{trail}"
        consume_path = f"/consumers/{group_name}/instances/{instance_id}/records{trail}?timeout=1000"
        topic_name = new_topic(admin_client)
        assign_payload = {"partitions": [{"topic": topic_name, "partition": 0}]}
        res = await rest_async_client.post(assign_path, json=assign_payload, headers=header)
        assert res.ok
        for i in range(len(values[fmt])):
            producer.send(topic_name, value=values[fmt][i]).get()
        seek_payload = {"partitions": [{"topic": topic_name, "partition": 0}]}
        resp = await rest_async_client.post(seek_path, headers=header, json=seek_payload)
        assert resp.ok
        header["Accept"] = f"application/vnd.kafka.{fmt}.v2+json"
        resp = await rest_async_client.get(consume_path, headers=header)
        assert resp.ok, f"Expected a successful response: {resp}"
        data = resp.json()
        assert len(data) == len(values[fmt]), f"Expected {len(values[fmt])} element in response: {resp}"
        for i in range(len(values[fmt])):
            assert deserializers[fmt](data[i]["value"]) == values[fmt][i], \
                f"Extracted data {deserializers[fmt](data[i]['value'])}" \
                f" does not match {values[fmt][i]} for format {fmt}"
Ejemplo n.º 6
0
async def test_topics(rest_async_client, admin_client):
    topic_foo = "foo"
    tn = new_topic(admin_client)
    await wait_for_topics(rest_async_client,
                          topic_names=[tn],
                          timeout=NEW_TOPIC_TIMEOUT,
                          sleep=1)
    res = await rest_async_client.get(f"/topics/{tn}")
    assert res.ok, "Status code is not 200: %r" % res.status_code
    data = res.json()
    assert data[
        "name"] == tn, f"Topic name should be {tn} and is {data['name']}"
    assert "configs" in data, "'configs' key is missing : %r" % data
    assert data["configs"] != {}, "'configs' key should not be empty"
    assert "partitions" in data, "'partitions' key is missing"
    assert len(data["partitions"]) == 1, "should only have one partition"
    assert "replicas" in data["partitions"][0], "'replicas' key is missing"
    assert len(
        data["partitions"][0]["replicas"]) == 1, "should only have one replica"
    assert data["partitions"][0]["replicas"][0][
        "leader"], "Replica should be leader"
    assert data["partitions"][0]["replicas"][0][
        "in_sync"], "Replica should be in sync"
    res = await rest_async_client.get(f"/topics/{topic_foo}")
    assert res.status_code == 404, f"Topic {topic_foo} should not exist, status_code={res.status_code}"
    assert res.json()["error_code"] == 40403, "Error code does not match"
Ejemplo n.º 7
0
async def test_assignment(rest_async_client, admin_client, trail):
    header = REST_HEADERS["json"]
    instance_id = await new_consumer(rest_async_client,
                                     "assignment_group",
                                     fmt="json",
                                     trail=trail)
    assign_path = f"/consumers/assignment_group/instances/{instance_id}/assignments{trail}"
    res = await rest_async_client.get(assign_path, headers=header)
    assert res.ok, f"Expected status 200 but got {res.status}"
    assert "partitions" in res.json() and len(
        res.json()["partitions"]) == 0, "Assignment list should be empty"
    # assign one topic
    topic_name = new_topic(admin_client)
    assign_payload = {"partitions": [{"topic": topic_name, "partition": 0}]}
    res = await rest_async_client.post(assign_path,
                                       headers=header,
                                       json=assign_payload)
    assert res.ok
    assign_path = f"/consumers/assignment_group/instances/{instance_id}/assignments{trail}"
    res = await rest_async_client.get(assign_path, headers=header)
    assert res.ok, f"Expected status 200 but got {res.status}"
    data = res.json()
    assert "partitions" in data and len(
        data["partitions"]) == 1, "Should have one assignment"
    p = data["partitions"][0]
    assert p["topic"] == topic_name
    assert p["partition"] == 0
Ejemplo n.º 8
0
async def test_offsets(rest_async_client, admin_client, trail):
    group_name = "offset_group"
    fmt = "binary"
    header = REST_HEADERS[fmt]
    instance_id = await new_consumer(rest_async_client, group_name, fmt=fmt, trail=trail)
    topic_name = new_topic(admin_client)
    offsets_path = f"/consumers/{group_name}/instances/{instance_id}/offsets{trail}"
    assign_path = f"/consumers/{group_name}/instances/{instance_id}/assignments{trail}"
    res = await rest_async_client.post(
        assign_path, json={"partitions": [{
            "topic": topic_name,
            "partition": 0
        }]}, headers=header
    )
    assert res.ok, f"Unexpected response status for assignment {res}"

    res = await rest_async_client.post(
        offsets_path, json={"offsets": [{
            "topic": topic_name,
            "partition": 0,
            "offset": 0
        }]}, headers=header
    )
    assert res.ok, f"Unexpected response status for offset commit {res}"

    res = await rest_async_client.get(
        offsets_path, headers=header, json={"partitions": [{
            "topic": topic_name,
            "partition": 0
        }]}
    )
    assert res.ok, f"Unexpected response status for {res}"
    data = res.json()
    assert "offsets" in data and len(data["offsets"]) == 1, f"Unexpected offsets response {res}"
    data = data["offsets"][0]
    assert "topic" in data and data["topic"] == topic_name, f"Unexpected topic {data}"
    assert "offset" in data and data["offset"] == 1, f"Unexpected offset {data}"
    assert "partition" in data and data["partition"] == 0, f"Unexpected partition {data}"
    res = await rest_async_client.post(
        offsets_path, json={"offsets": [{
            "topic": topic_name,
            "partition": 0,
            "offset": 1
        }]}, headers=header
    )
    assert res.ok, f"Unexpected response status for offset commit {res}"

    res = await rest_async_client.get(
        offsets_path, headers=header, json={"partitions": [{
            "topic": topic_name,
            "partition": 0
        }]}
    )
    assert res.ok, f"Unexpected response status for {res}"
    data = res.json()
    assert "offsets" in data and len(data["offsets"]) == 1, f"Unexpected offsets response {res}"
    data = data["offsets"][0]
    assert "topic" in data and data["topic"] == topic_name, f"Unexpected topic {data}"
    assert "offset" in data and data["offset"] == 2, f"Unexpected offset {data}"
    assert "partition" in data and data["partition"] == 0, f"Unexpected partition {data}"
Ejemplo n.º 9
0
async def test_publish_malformed_requests(rest_async_client, admin_client):
    topic_name = new_topic(admin_client)
    await wait_for_topics(rest_async_client,
                          topic_names=[topic_name],
                          timeout=NEW_TOPIC_TIMEOUT,
                          sleep=1)
    for url in [f"/topics/{topic_name}", f"/topics/{topic_name}/partitions/0"]:
        # Malformed schema ++ empty records
        for js in [{
                "records": []
        }, {
                "foo": "bar"
        }, {
                "records": [{
                    "valur": {
                        "foo": "bar"
                    }
                }]
        }]:
            res = await rest_async_client.post(url,
                                               json=js,
                                               headers=REST_HEADERS["json"])
            assert res.status == 422
        res = await rest_async_client.post(
            url,
            json={"records": [{
                "value": {
                    "foo": "bar"
                }
            }]},
            headers=REST_HEADERS["avro"])
        res_json = res.json()
        assert res.status == 422
        assert res_json["error_code"] == 42202
        res = await rest_async_client.post(
            url,
            json={"records": [{
                "key": {
                    "foo": "bar"
                }
            }]},
            headers=REST_HEADERS["avro"])
        res_json = res.json()
        assert res.status == 422
        assert res_json["error_code"] == 42201
        res = await rest_async_client.post(
            url,
            json={"records": [{
                "value": "not base64"
            }]},
            headers=REST_HEADERS["binary"])
        res_json = res.json()
        assert res.status == 422
        assert res_json["error_code"] == 42205
Ejemplo n.º 10
0
async def test_publish_malformed_requests(rest_async_client, admin_client):
    topic_name = new_topic(admin_client)
    for url in [f"/topics/{topic_name}", f"/topics/{topic_name}/partitions/0"]:
        # Malformed schema ++ empty records
        for js in [{
                "records": []
        }, {
                "foo": "bar"
        }, {
                "records": [{
                    "valur": {
                        "foo": "bar"
                    }
                }]
        }]:
            res = await rest_async_client.post(url,
                                               json=js,
                                               headers=REST_HEADERS["json"])
            assert res.status == 422
        res = await rest_async_client.post(
            url,
            json={"records": [{
                "value": {
                    "foo": "bar"
                }
            }]},
            headers=REST_HEADERS["avro"])
        res_json = res.json()
        assert res.status == 422
        assert res_json["error_code"] == 42202
        res = await rest_async_client.post(
            url,
            json={"records": [{
                "key": {
                    "foo": "bar"
                }
            }]},
            headers=REST_HEADERS["avro"])
        res_json = res.json()
        assert res.status == 422
        assert res_json["error_code"] == 42201
        if "REST_URI" in os.environ and "REGISTRY_URI" in os.environ:
            pytest.skip("Skipping encoding tests for remote proxy")
        res = await rest_async_client.post(
            url,
            json={"records": [{
                "value": "not base64"
            }]},
            headers=REST_HEADERS["binary"])
        res_json = res.json()
        assert res.status == 422
        assert res_json["error_code"] == 42205
Ejemplo n.º 11
0
async def test_partitions(rest_async_client, admin_client, producer):
    # TODO -> This seems to be the only combination accepted by the offsets endpoint
    topic_name = new_topic(admin_client)
    await wait_for_topics(rest_async_client,
                          topic_names=[topic_name],
                          timeout=NEW_TOPIC_TIMEOUT,
                          sleep=1)
    header = {"Accept": "*/*", "Content-Type": "application/vnd.kafka.v2+json"}
    all_partitions_res = await rest_async_client.get(
        f"/topics/{topic_name}/partitions")
    assert all_partitions_res.ok, "Topic should exist"
    partitions = all_partitions_res.json()
    assert len(partitions) == 1, "Only one partition should exist"
    assert len(partitions[0]["replicas"]) == 1, "Only one replica should exist"
    partition = partitions[0]
    assert partition["replicas"][0]["leader"], "Replica should be leader"
    assert partition["replicas"][0]["in_sync"], "Replica should be in sync"
    first_partition_res = await rest_async_client.get(
        f"/topics/{topic_name}/partitions/0")
    assert first_partition_res.ok
    partition_data = first_partition_res.json()
    assert partition_data == partition, f"Unexpected partition data: {partition_data}"

    res = await rest_async_client.get("/topics/fooo/partitions")
    assert res.status_code == 404
    assert res.json()["error_code"] == 40401
    res = await rest_async_client.get("/topics/fooo/partitions/0")
    assert res.status_code == 404
    assert res.json()["error_code"] == 40401

    res = await rest_async_client.get(f"/topics/{topic_name}/partitions/10")
    assert res.status_code == 404
    assert res.json()["error_code"] == 40402
    for _ in range(5):
        producer.send(topic_name, value=b"foo_val").get()
    offset_res = await rest_async_client.get(
        f"/topics/{topic_name}/partitions/0/offsets", headers=header)
    assert offset_res.ok, "Status code %r is not expected: %r" % (
        offset_res.status_code, offset_res.json())
    data = offset_res.json()
    assert data == {
        "beginning_offset": 0,
        "end_offset": 5
    }, "Unexpected offsets for topic %r: %r" % (topic_name, data)
    res = await rest_async_client.get("/topics/fooo/partitions/0/offsets",
                                      headers=header)
    assert res.json() == {"beginning_offset": -1, "end_offset": -1}
    res = await rest_async_client.get(
        f"/topics/{topic_name}/partitions/foo/offsets", headers=header)
    assert res.status_code == 404
    assert res.json()["error_code"] == 404
Ejemplo n.º 12
0
async def test_internal(rest_async, admin_client):
    topic_name = new_topic(admin_client)
    for p in [0, None]:
        result = await rest_async.produce_message(topic=topic_name, key=b"key", value=b"value", partition=p)
        assert "error" not in result, "Valid result should not contain 'error' key"
        assert "offset" in result, "Valid result is missing 'offset' key"
        assert "partition" in result, "Valid result is missing 'partition' key"
        actual_part = result["partition"]
        assert actual_part == 0, "Returned partition id should be %d but is %d" % (0, actual_part)
    result = await rest_async.produce_message(topic=topic_name, key=b"key", value=b"value", partition=100)
    assert "error" in result, "Invalid result missing 'error' key"
    assert result["error"] == "Unrecognized partition"
    assert "error_code" in result, "Invalid result missing 'error_code' key"
    assert result["error_code"] == 1
    assert rest_async.all_empty({"records": [{"key": {"foo": "bar"}}]}, "key") is False
    assert rest_async.all_empty({"records": [{"value": {"foo": "bar"}}]}, "value") is False
    assert rest_async.all_empty({"records": [{"value": {"foo": "bar"}}]}, "key") is True
Ejemplo n.º 13
0
async def test_seek(rest_async_client, admin_client, trail):
    group = "seek_group"
    instance_id = await new_consumer(rest_async_client, group, trail=trail)
    seek_path = f"/consumers/{group}/instances/{instance_id}/positions{trail}"
    # one partition assigned, we can
    topic_name = new_topic(admin_client)
    assign_path = f"/consumers/{group}/instances/{instance_id}/assignments{trail}"
    assign_payload = {"partitions": [{"topic": topic_name, "partition": 0}]}
    res = await rest_async_client.post(assign_path,
                                       headers=REST_HEADERS["json"],
                                       json=assign_payload)
    assert res.ok
    seek_payload = {
        "offsets": [{
            "topic": topic_name,
            "partition": 0,
            "offset": 10
        }]
    }
    res = await rest_async_client.post(seek_path,
                                       json=seek_payload,
                                       headers=REST_HEADERS["json"])
    assert res.ok, f"Unexpected status for {res}"
    extreme_payload = {"partitions": [{"topic": topic_name, "partition": 0}]}
    for pos in ["beginning", "end"]:
        url = f"{seek_path}/{pos}"
        res = await rest_async_client.post(url,
                                           json=extreme_payload,
                                           headers=REST_HEADERS["json"])
        assert res.ok, f"Expecting a successful response: {res}"
    # unassigned seeks should fail
    invalid_payload = {
        "offsets": [{
            "topic": "faulty",
            "partition": 0,
            "offset": 10
        }]
    }
    res = await rest_async_client.post(seek_path,
                                       json=invalid_payload,
                                       headers=REST_HEADERS["json"])
    assert res.status == 409, f"Expecting a failure for unassigned partition seek: {res}"
Ejemplo n.º 14
0
async def test_admin_client(admin_client, producer):
    topic_names = [new_topic(admin_client) for i in range(10, 13)]
    topic_info = admin_client.cluster_metadata()
    retrieved_names = list(topic_info["topics"].keys())
    assert set(topic_names).difference(set(retrieved_names)) == set(), \
        "Returned value %r differs from written one %r" % (retrieved_names, topic_names)
    assert len(topic_info["brokers"]) == 1, "Only one broker during tests"
    for t in topic_names:
        v = topic_info["topics"][t]
        assert len(
            v["partitions"]) == 1, "Should only have data for one partition"
        details = v["partitions"][0]
        assert len(details["replicas"]) == 1, "Should have only 1 replica"
    one_topic_info = admin_client.cluster_metadata(topic_names[:1])
    retrieved_names = list(one_topic_info["topics"].keys())
    assert len(retrieved_names) == 1
    assert retrieved_names[0] == topic_names[
        0], f"Returned value %r differs from expected {retrieved_names[0]}"
    cfg = admin_client.get_topic_config(topic_names[0])
    assert "cleanup.policy" in cfg
    for _ in range(5):
        fut = producer.send(topic_names[0], value=b"foo_val")
        producer.flush()
        _ = fut.get()
    offsets = admin_client.get_offsets(topic_names[0], 0)
    assert offsets[
        "beginning_offset"] == 0, f"Start offset should be 0 for {topic_names[0]}, partition 0"
    assert offsets[
        "end_offset"] == 5, f"End offset should be 0 for {topic_names[0]}, partition 0"
    # invalid requests
    off = admin_client.get_offsets("invalid_topic", 0)
    assert off["beginning_offset"] == -1
    assert off["end_offset"] == -1
    off = admin_client.get_offsets(topic_names[0], 10)
    assert off["beginning_offset"] == -1
    assert off["end_offset"] == -1
    with raises(UnknownTopicOrPartitionError):
        admin_client.get_topic_config("another_invalid_name")
    with raises(UnknownTopicOrPartitionError):
        admin_client.cluster_metadata(topics=["another_invalid_name"])
Ejemplo n.º 15
0
async def test_content_types(rest_async_client, admin_client):
    tn = new_topic(admin_client)
    valid_headers = [
        "application/vnd.kafka.v1+json",
        "application/vnd.kafka.binary.v1+json",
        "application/vnd.kafka.avro.v1+json",
        "application/vnd.kafka.json+json",
        "application/vnd.kafka.v1+json",
        "application/vnd.kafka+json",
        "application/json",
        "application/octet-stream",
    ]
    invalid_headers = [
        "application/vnd.kafka.v3+json",
        "application/vnd.kafka.binary.v1+foo",
        "application/vnd.kafka.avro.v0+json",
        "application/vnd.kafka.json+avro",
        "application/vnd.kafka",
        "application/vnd.kafka+binary",
        "application/text",
        "bar/baz",
    ]

    invalid_accept_headers = [
        "application/octet-stream",
        "application/vnd.kafka+foo",
        "application/text",
        "foo/*",
        "bar/json",
        "*/baz",
    ]

    valid_accept_headers = [
        "application/vnd.kafka.v1+json",
        "application/vnd.kafka+json",
        "application/json",
        "application/*",
        "*/json",
        "*/*",
    ]

    avro_payload = {"value_schema": schema_avro_json, "records": [{"value": o} for o in test_objects_avro]}
    json_payload = {"records": [{"value": {"foo": "bar"}}]}
    binary_payload = {"records": [{"value": "Zm9v"}]}
    valid_payloads = [
        binary_payload,
        binary_payload,
        avro_payload,
        json_payload,
        binary_payload,
        binary_payload,
        binary_payload,
        binary_payload,
    ]
    # post / put requests should get validated
    for hv, pl in zip(valid_headers, valid_payloads):
        res = await rest_async_client.post(f"topics/{tn}", pl, headers={"Content-Type": hv})
        assert res.ok
    for hv, pl in zip(invalid_headers, valid_payloads):
        res = await rest_async_client.post(f"topics/{tn}", pl, headers={"Content-Type": hv})
        assert not res.ok

    for ah in valid_accept_headers:
        res = await rest_async_client.get("/brokers", headers={"Accept": ah})
        assert res.ok

    for ah in invalid_accept_headers:
        res = await rest_async_client.get("/brokers", headers={"Accept": ah})
        assert not res.ok
Ejemplo n.º 16
0
async def test_subscription(rest_async_client, admin_client, producer, trail):
    # The random name is necessary to avoid test errors, without it the second
    # parametrize test will fail. Issue: #178
    group_name = new_random_name("group")

    header = REST_HEADERS["binary"]
    topic_name = new_topic(admin_client)
    instance_id = await new_consumer(rest_async_client, group_name, fmt="binary", trail=trail)
    sub_path = f"/consumers/{group_name}/instances/{instance_id}/subscription{trail}"
    consume_path = f"/consumers/{group_name}/instances/{instance_id}/records{trail}?timeout=1000"
    res = await rest_async_client.get(sub_path, headers=header)
    assert res.ok
    data = res.json()
    assert "topics" in data and len(data["topics"]) == 0, \
        f"Expecting no subscription on freshly created consumer: {data}"
    # simple sub
    res = await rest_async_client.post(sub_path, json={"topics": [topic_name]}, headers=header)
    assert res.ok
    res = await rest_async_client.get(sub_path, headers=header)
    assert res.ok
    data = res.json()
    assert "topics" in data and len(data["topics"]) == 1 and data["topics"][0] == topic_name, \
        f"expecting {topic_name} in {data}"
    for _ in range(3):
        producer.send(topic_name, b"foo").get()
    resp = await rest_async_client.get(consume_path, headers=header)
    data = resp.json()
    assert resp.ok, f"Expected a successful response: {data['message']}"
    assert len(data) == 3, f"Expected to consume 3 messages but got {data}"

    # on delete it's empty again
    res = await rest_async_client.delete(sub_path, headers=header)
    assert res.ok
    res = await rest_async_client.get(sub_path, headers=header)
    assert res.ok
    data = res.json()
    assert "topics" in data and len(data["topics"]) == 0, f"expecting {data} to be empty"
    # one pattern sub will get all 3
    prefix = f"{hash(random.random())}"
    pattern_topics = [new_topic(admin_client, prefix=f"{prefix}{i}") for i in range(3)]
    res = await rest_async_client.post(sub_path, json={"topic_pattern": f"{prefix}.*"}, headers=REST_HEADERS["json"])
    assert res.ok

    # Consume so confluent rest reevaluates the subscription
    resp = await rest_async_client.get(consume_path, headers=header)
    assert resp.ok
    # Should we keep this behaviour

    res = await rest_async_client.get(sub_path, headers=header)
    assert res.ok
    data = res.json()
    assert "topics" in data and len(data["topics"]) == 3, "expecting subscription to 3 topics by pattern"
    subscribed_to = set(data["topics"])
    expected = set(pattern_topics)
    assert expected == subscribed_to, f"Expecting {expected} as subscribed to topics, but got {subscribed_to} instead"
    # writing to all 3 will get us results from all 3
    for t in pattern_topics:
        for _ in range(3):
            producer.send(t, b"bar").get()
    resp = await rest_async_client.get(consume_path, headers=header)
    data = resp.json()
    assert resp.ok, f"Expected a successful response: {data['message']}"
    assert len(data) == 9, f"Expected to consume 3 messages but got {data}"

    # topic name sub along with pattern will fail
    res = await rest_async_client.post(
        sub_path, json={
            "topics": [topic_name],
            "topic_pattern": "baz"
        }, headers=REST_HEADERS["json"]
    )
    assert res.status == 409, f"Invalid state error expected: {res.status}"
    data = res.json()
    assert data["error_code"] == 40903, f"Invalid state error expected: {data}"
    # assign after subscribe will fail
    assign_path = f"/consumers/{group_name}/instances/{instance_id}/assignments{trail}"
    assign_payload = {"partitions": [{"topic": topic_name, "partition": 0}]}
    res = await rest_async_client.post(assign_path, headers=REST_HEADERS["json"], json=assign_payload)
    assert res.status == 409, "Expecting status code 409 on assign after subscribe on the same consumer instance"
Ejemplo n.º 17
0
async def test_request_body_too_large(rest_async_client, admin_client):
    tn = new_topic(admin_client)
    await wait_for_topics(rest_async_client, topic_names=[tn], timeout=NEW_TOPIC_TIMEOUT, sleep=1)
    pl = {"records": [{"value": 1_048_576 * "a"}]}