|
| 1 | +""" |
| 2 | +AWS asyncio test fixtures |
| 3 | +""" |
| 4 | + |
| 5 | +import aiobotocore.client |
| 6 | +import aiobotocore.config |
| 7 | +import pytest |
| 8 | + |
| 9 | +from tests.aws.aio.aiomoto_services import MotoService |
| 10 | +from tests.aws.utils import AWS_ACCESS_KEY_ID |
| 11 | +from tests.aws.utils import AWS_SECRET_ACCESS_KEY |
| 12 | + |
| 13 | + |
| 14 | +# |
| 15 | +# Asyncio AWS Services |
| 16 | +# |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +async def aio_aws_batch_server(): |
| 21 | + async with MotoService("batch") as svc: |
| 22 | + yield svc.endpoint_url |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +async def aio_aws_cloudformation_server(): |
| 27 | + async with MotoService("cloudformation") as svc: |
| 28 | + yield svc.endpoint_url |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +async def aio_aws_ec2_server(): |
| 33 | + async with MotoService("ec2") as svc: |
| 34 | + yield svc.endpoint_url |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +async def aio_aws_ecs_server(): |
| 39 | + async with MotoService("ecs") as svc: |
| 40 | + yield svc.endpoint_url |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture |
| 44 | +async def aio_aws_iam_server(): |
| 45 | + async with MotoService("iam") as svc: |
| 46 | + yield svc.endpoint_url |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture |
| 50 | +async def aio_aws_dynamodb2_server(): |
| 51 | + async with MotoService("dynamodb2") as svc: |
| 52 | + yield svc.endpoint_url |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture |
| 56 | +async def aio_aws_logs_server(): |
| 57 | + # cloud watch logs |
| 58 | + async with MotoService("logs") as svc: |
| 59 | + yield svc.endpoint_url |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture |
| 63 | +async def aio_aws_s3_server(): |
| 64 | + async with MotoService("s3") as svc: |
| 65 | + yield svc.endpoint_url |
| 66 | + |
| 67 | + |
| 68 | +@pytest.fixture |
| 69 | +async def aio_aws_sns_server(): |
| 70 | + async with MotoService("sns") as svc: |
| 71 | + yield svc.endpoint_url |
| 72 | + |
| 73 | + |
| 74 | +@pytest.fixture |
| 75 | +async def aio_aws_sqs_server(): |
| 76 | + async with MotoService("sqs") as svc: |
| 77 | + yield svc.endpoint_url |
| 78 | + |
| 79 | + |
| 80 | +# |
| 81 | +# Asyncio AWS Clients |
| 82 | +# |
| 83 | + |
| 84 | + |
| 85 | +@pytest.fixture |
| 86 | +def aio_aws_session(aws_credentials, aws_region, event_loop): |
| 87 | + # pytest-asyncio provides and manages the `event_loop` |
| 88 | + |
| 89 | + session = aiobotocore.get_session(loop=event_loop) |
| 90 | + session.user_agent_name = "aiomoto" |
| 91 | + |
| 92 | + assert session.get_default_client_config() is None |
| 93 | + aioconfig = aiobotocore.config.AioConfig(max_pool_connections=1, region_name=aws_region) |
| 94 | + |
| 95 | + # Note: tried to use proxies for the aiobotocore.endpoint, to replace |
| 96 | + # 'https://batch.us-west-2.amazonaws.com/v1/describejobqueues', but |
| 97 | + # the moto.server does not behave as a proxy server. Leaving this |
| 98 | + # here for the record to avoid trying to do it again sometime later. |
| 99 | + # proxies = { |
| 100 | + # 'http': os.getenv("HTTP_PROXY", "http://127.0.0.1:5000/moto-api/"), |
| 101 | + # 'https': os.getenv("HTTPS_PROXY", "http://127.0.0.1:5000/moto-api/"), |
| 102 | + # } |
| 103 | + # assert aioconfig.proxies is None |
| 104 | + # aioconfig.proxies = proxies |
| 105 | + |
| 106 | + session.set_default_client_config(aioconfig) |
| 107 | + assert session.get_default_client_config() == aioconfig |
| 108 | + |
| 109 | + session.set_credentials(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) |
| 110 | + session.set_debug_logger(logger_name="aiomoto") |
| 111 | + |
| 112 | + yield session |
| 113 | + |
| 114 | + |
| 115 | +@pytest.fixture |
| 116 | +async def aio_aws_client(aio_aws_session): |
| 117 | + async def _get_client(service_name): |
| 118 | + async with MotoService(service_name) as srv: |
| 119 | + async with aio_aws_session.create_client( |
| 120 | + service_name, endpoint_url=srv.endpoint_url |
| 121 | + ) as client: |
| 122 | + yield client |
| 123 | + |
| 124 | + return _get_client |
| 125 | + |
| 126 | + |
| 127 | +@pytest.fixture |
| 128 | +async def aio_aws_batch_client(aio_aws_session, aio_aws_batch_server): |
| 129 | + async with aio_aws_session.create_client( |
| 130 | + "batch", endpoint_url=aio_aws_batch_server |
| 131 | + ) as client: |
| 132 | + yield client |
| 133 | + |
| 134 | + |
| 135 | +@pytest.fixture |
| 136 | +async def aio_aws_ec2_client(aio_aws_session, aio_aws_ec2_server): |
| 137 | + async with aio_aws_session.create_client("ec2", endpoint_url=aio_aws_ec2_server) as client: |
| 138 | + yield client |
| 139 | + |
| 140 | + |
| 141 | +@pytest.fixture |
| 142 | +async def aio_aws_ecs_client(aio_aws_session, aio_aws_ecs_server): |
| 143 | + async with aio_aws_session.create_client("ecs", endpoint_url=aio_aws_ecs_server) as client: |
| 144 | + yield client |
| 145 | + |
| 146 | + |
| 147 | +@pytest.fixture |
| 148 | +async def aio_aws_iam_client(aio_aws_session, aio_aws_iam_server): |
| 149 | + async with aio_aws_session.create_client("iam", endpoint_url=aio_aws_iam_server) as client: |
| 150 | + client.meta.config.region_name = "aws-global" # not AWS_REGION |
| 151 | + yield client |
| 152 | + |
| 153 | + |
| 154 | +@pytest.fixture |
| 155 | +async def aio_aws_logs_client(aio_aws_session, aio_aws_logs_server): |
| 156 | + async with aio_aws_session.create_client( |
| 157 | + "logs", endpoint_url=aio_aws_logs_server |
| 158 | + ) as client: |
| 159 | + yield client |
| 160 | + |
| 161 | + |
| 162 | +@pytest.fixture |
| 163 | +async def aio_aws_s3_client(aio_aws_session, aio_aws_s3_server): |
| 164 | + async with aio_aws_session.create_client("s3", endpoint_url=aio_aws_s3_server) as client: |
| 165 | + yield client |
| 166 | + |
0 commit comments