2022-01-26 19:41:04 +00:00
|
|
|
import boto3
|
|
|
|
import pytest
|
|
|
|
from botocore.exceptions import ClientError
|
2023-11-30 15:55:51 +00:00
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
from moto import mock_aws
|
2022-08-13 09:49:43 +00:00
|
|
|
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
|
2022-01-26 19:41:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_stream(client):
|
|
|
|
stream_name = "my-stream"
|
|
|
|
client.create_stream(StreamName=stream_name, ShardCount=4)
|
|
|
|
stream = client.describe_stream(StreamName=stream_name)["StreamDescription"]
|
|
|
|
return stream["StreamARN"]
|
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-01-26 19:41:04 +00:00
|
|
|
def test_list_stream_consumers():
|
|
|
|
client = boto3.client("kinesis", region_name="eu-west-1")
|
|
|
|
stream_arn = create_stream(client)
|
|
|
|
|
|
|
|
resp = client.list_stream_consumers(StreamARN=stream_arn)
|
|
|
|
|
2023-08-01 09:47:40 +00:00
|
|
|
assert resp["Consumers"] == []
|
2022-01-26 19:41:04 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-01-26 19:41:04 +00:00
|
|
|
def test_register_stream_consumer():
|
|
|
|
client = boto3.client("kinesis", region_name="eu-west-1")
|
|
|
|
stream_arn = create_stream(client)
|
|
|
|
|
|
|
|
resp = client.register_stream_consumer(
|
|
|
|
StreamARN=stream_arn, ConsumerName="newconsumer"
|
|
|
|
)
|
2023-08-01 09:47:40 +00:00
|
|
|
assert "Consumer" in resp
|
2022-01-26 19:41:04 +00:00
|
|
|
|
|
|
|
consumer = resp["Consumer"]
|
|
|
|
|
2023-08-01 09:47:40 +00:00
|
|
|
assert consumer["ConsumerName"] == "newconsumer"
|
|
|
|
assert (
|
|
|
|
consumer["ConsumerARN"]
|
|
|
|
== f"arn:aws:kinesis:eu-west-1:{ACCOUNT_ID}:stream/my-stream/consumer/newconsumer"
|
2022-01-26 19:41:04 +00:00
|
|
|
)
|
2023-08-01 09:47:40 +00:00
|
|
|
assert consumer["ConsumerStatus"] == "ACTIVE"
|
|
|
|
assert "ConsumerCreationTimestamp" in consumer
|
2022-01-26 19:41:04 +00:00
|
|
|
|
|
|
|
resp = client.list_stream_consumers(StreamARN=stream_arn)
|
|
|
|
|
2023-08-01 09:47:40 +00:00
|
|
|
assert len(resp["Consumers"]) == 1
|
2022-01-26 19:41:04 +00:00
|
|
|
consumer = resp["Consumers"][0]
|
2023-08-01 09:47:40 +00:00
|
|
|
assert consumer["ConsumerName"] == "newconsumer"
|
|
|
|
assert (
|
|
|
|
consumer["ConsumerARN"]
|
|
|
|
== f"arn:aws:kinesis:eu-west-1:{ACCOUNT_ID}:stream/my-stream/consumer/newconsumer"
|
2022-01-26 19:41:04 +00:00
|
|
|
)
|
2023-08-01 09:47:40 +00:00
|
|
|
assert consumer["ConsumerStatus"] == "ACTIVE"
|
|
|
|
assert "ConsumerCreationTimestamp" in consumer
|
2022-01-26 19:41:04 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-01-26 19:41:04 +00:00
|
|
|
def test_describe_stream_consumer_by_name():
|
|
|
|
client = boto3.client("kinesis", region_name="us-east-2")
|
|
|
|
stream_arn = create_stream(client)
|
|
|
|
client.register_stream_consumer(StreamARN=stream_arn, ConsumerName="newconsumer")
|
|
|
|
|
|
|
|
resp = client.describe_stream_consumer(
|
|
|
|
StreamARN=stream_arn, ConsumerName="newconsumer"
|
|
|
|
)
|
2023-08-01 09:47:40 +00:00
|
|
|
assert "ConsumerDescription" in resp
|
2022-01-26 19:41:04 +00:00
|
|
|
|
|
|
|
consumer = resp["ConsumerDescription"]
|
2023-08-01 09:47:40 +00:00
|
|
|
assert consumer["ConsumerName"] == "newconsumer"
|
|
|
|
assert "ConsumerARN" in consumer
|
|
|
|
assert consumer["ConsumerStatus"] == "ACTIVE"
|
|
|
|
assert "ConsumerCreationTimestamp" in consumer
|
|
|
|
assert consumer["StreamARN"] == stream_arn
|
2022-01-26 19:41:04 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-01-26 19:41:04 +00:00
|
|
|
def test_describe_stream_consumer_by_arn():
|
|
|
|
client = boto3.client("kinesis", region_name="us-east-2")
|
|
|
|
stream_arn = create_stream(client)
|
|
|
|
resp = client.register_stream_consumer(
|
|
|
|
StreamARN=stream_arn, ConsumerName="newconsumer"
|
|
|
|
)
|
|
|
|
consumer_arn = resp["Consumer"]["ConsumerARN"]
|
|
|
|
|
|
|
|
resp = client.describe_stream_consumer(ConsumerARN=consumer_arn)
|
2023-08-01 09:47:40 +00:00
|
|
|
assert "ConsumerDescription" in resp
|
2022-01-26 19:41:04 +00:00
|
|
|
|
|
|
|
consumer = resp["ConsumerDescription"]
|
2023-08-01 09:47:40 +00:00
|
|
|
assert consumer["ConsumerName"] == "newconsumer"
|
|
|
|
assert "ConsumerARN" in consumer
|
|
|
|
assert consumer["ConsumerStatus"] == "ACTIVE"
|
|
|
|
assert "ConsumerCreationTimestamp" in consumer
|
|
|
|
assert consumer["StreamARN"] == stream_arn
|
2022-01-26 19:41:04 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-01-26 19:41:04 +00:00
|
|
|
def test_describe_stream_consumer_unknown():
|
|
|
|
client = boto3.client("kinesis", region_name="us-east-2")
|
|
|
|
create_stream(client)
|
|
|
|
|
2022-12-16 15:53:27 +00:00
|
|
|
unknown_arn = f"arn:aws:kinesis:us-east-2:{ACCOUNT_ID}:stream/unknown"
|
2022-01-26 19:41:04 +00:00
|
|
|
with pytest.raises(ClientError) as exc:
|
2022-12-16 15:53:27 +00:00
|
|
|
client.describe_stream_consumer(ConsumerARN=unknown_arn)
|
2022-01-26 19:41:04 +00:00
|
|
|
err = exc.value.response["Error"]
|
2023-08-01 09:47:40 +00:00
|
|
|
assert err["Code"] == "ResourceNotFoundException"
|
|
|
|
assert err["Message"] == f"Consumer {unknown_arn}, account {ACCOUNT_ID} not found."
|
2022-01-26 19:41:04 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-01-26 19:41:04 +00:00
|
|
|
def test_deregister_stream_consumer_by_name():
|
|
|
|
client = boto3.client("kinesis", region_name="ap-southeast-1")
|
|
|
|
stream_arn = create_stream(client)
|
|
|
|
|
|
|
|
client.register_stream_consumer(StreamARN=stream_arn, ConsumerName="consumer1")
|
|
|
|
client.register_stream_consumer(StreamARN=stream_arn, ConsumerName="consumer2")
|
|
|
|
|
2023-08-01 09:47:40 +00:00
|
|
|
assert len(client.list_stream_consumers(StreamARN=stream_arn)["Consumers"]) == 2
|
2022-01-26 19:41:04 +00:00
|
|
|
|
|
|
|
client.deregister_stream_consumer(StreamARN=stream_arn, ConsumerName="consumer1")
|
|
|
|
|
2023-08-01 09:47:40 +00:00
|
|
|
assert len(client.list_stream_consumers(StreamARN=stream_arn)["Consumers"]) == 1
|
2022-01-26 19:41:04 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-01-26 19:41:04 +00:00
|
|
|
def test_deregister_stream_consumer_by_arn():
|
|
|
|
client = boto3.client("kinesis", region_name="ap-southeast-1")
|
|
|
|
stream_arn = create_stream(client)
|
|
|
|
|
|
|
|
resp = client.register_stream_consumer(
|
|
|
|
StreamARN=stream_arn, ConsumerName="consumer1"
|
|
|
|
)
|
|
|
|
consumer1_arn = resp["Consumer"]["ConsumerARN"]
|
|
|
|
client.register_stream_consumer(StreamARN=stream_arn, ConsumerName="consumer2")
|
|
|
|
|
2023-08-01 09:47:40 +00:00
|
|
|
assert len(client.list_stream_consumers(StreamARN=stream_arn)["Consumers"]) == 2
|
2022-01-26 19:41:04 +00:00
|
|
|
|
|
|
|
client.deregister_stream_consumer(ConsumerARN=consumer1_arn)
|
|
|
|
|
2023-08-01 09:47:40 +00:00
|
|
|
assert len(client.list_stream_consumers(StreamARN=stream_arn)["Consumers"]) == 1
|