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