|
| 1 | +"""Integration tests for streamDeliveryResources on MemoryClient. |
| 2 | +
|
| 3 | +Requires environment variables: |
| 4 | + MEMORY_KINESIS_ARN: ARN of a pre-existing Kinesis stream |
| 5 | + MEMORY_ROLE_ARN: ARN of an IAM role the memory service can assume |
| 6 | +
|
| 7 | +Run with: |
| 8 | + pytest tests_integ/memory/test_stream_delivery.py -xvs --log-cli-level=INFO |
| 9 | +""" |
| 10 | + |
| 11 | +import os |
| 12 | +import time |
| 13 | +import uuid |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from bedrock_agentcore.memory import MemoryClient |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.integration |
| 21 | +class TestStreamDeliveryResources: |
| 22 | + """Integration tests for streamDeliveryResources.""" |
| 23 | + |
| 24 | + @classmethod |
| 25 | + def setup_class(cls): |
| 26 | + cls.kinesis_stream_arn = os.environ.get("MEMORY_KINESIS_ARN") |
| 27 | + cls.execution_role_arn = os.environ.get("MEMORY_ROLE_ARN") |
| 28 | + |
| 29 | + if not cls.kinesis_stream_arn or not cls.execution_role_arn: |
| 30 | + pytest.fail("MEMORY_KINESIS_ARN and MEMORY_ROLE_ARN must be set") |
| 31 | + |
| 32 | + cls.region = os.environ.get("BEDROCK_TEST_REGION", "us-west-2") |
| 33 | + cls.client = MemoryClient(region_name=cls.region) |
| 34 | + cls.test_prefix = f"test_stream_{int(time.time())}" |
| 35 | + cls.memory_ids = [] |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def teardown_class(cls): |
| 39 | + for memory_id in cls.memory_ids: |
| 40 | + try: |
| 41 | + cls.client.delete_memory(memory_id) |
| 42 | + except Exception as e: |
| 43 | + print(f"Failed to delete memory {memory_id}: {e}") |
| 44 | + |
| 45 | + def _make_delivery_config(self, level="FULL_CONTENT"): |
| 46 | + return { |
| 47 | + "resources": [ |
| 48 | + { |
| 49 | + "kinesis": { |
| 50 | + "dataStreamArn": self.kinesis_stream_arn, |
| 51 | + "contentConfigurations": [{"type": "MEMORY_RECORDS", "level": level}], |
| 52 | + } |
| 53 | + } |
| 54 | + ] |
| 55 | + } |
| 56 | + |
| 57 | + def test_stream_delivery_create_and_update(self): |
| 58 | + """Create memory with FULL_CONTENT delivery, update to METADATA_ONLY via passthrough.""" |
| 59 | + delivery_config = self._make_delivery_config("FULL_CONTENT") |
| 60 | + |
| 61 | + memory = self.client.create_memory_and_wait( |
| 62 | + name=f"{self.test_prefix}_stream", |
| 63 | + strategies=[], |
| 64 | + memory_execution_role_arn=self.execution_role_arn, |
| 65 | + stream_delivery_resources=delivery_config, |
| 66 | + ) |
| 67 | + |
| 68 | + memory_id = memory["id"] |
| 69 | + self.__class__.memory_ids.append(memory_id) |
| 70 | + |
| 71 | + assert memory["streamDeliveryResources"] == delivery_config |
| 72 | + |
| 73 | + # Test update via MemoryClient.__getattr__ passthrough to boto3 client. |
| 74 | + # Uses camelCase params because the passthrough forwards directly to boto3 |
| 75 | + # without the snake_case translation that explicit SDK methods provide. |
| 76 | + updated_config = self._make_delivery_config("METADATA_ONLY") |
| 77 | + response = self.client.update_memory( |
| 78 | + memoryId=memory_id, |
| 79 | + clientToken=str(uuid.uuid4()), |
| 80 | + streamDeliveryResources=updated_config, |
| 81 | + ) |
| 82 | + assert response["memory"]["streamDeliveryResources"] == updated_config |
0 commit comments