diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-18 05:52:35 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-18 05:52:35 +0000 |
commit | 7fec0b69a082aaeec72fee0612766aa42f6b1b4d (patch) | |
tree | efb569b86ca4da888717f5433e757145fa322e08 /ansible_collections/amazon/aws/tests/unit/utils | |
parent | Releasing progress-linux version 7.7.0+dfsg-3~progress7.99u1. (diff) | |
download | ansible-7fec0b69a082aaeec72fee0612766aa42f6b1b4d.tar.xz ansible-7fec0b69a082aaeec72fee0612766aa42f6b1b4d.zip |
Merging upstream version 9.4.0+dfsg.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ansible_collections/amazon/aws/tests/unit/utils')
-rw-r--r-- | ansible_collections/amazon/aws/tests/unit/utils/__init__.py | 0 | ||||
-rw-r--r-- | ansible_collections/amazon/aws/tests/unit/utils/amazon_placebo_fixtures.py | 118 |
2 files changed, 62 insertions, 56 deletions
diff --git a/ansible_collections/amazon/aws/tests/unit/utils/__init__.py b/ansible_collections/amazon/aws/tests/unit/utils/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/ansible_collections/amazon/aws/tests/unit/utils/__init__.py diff --git a/ansible_collections/amazon/aws/tests/unit/utils/amazon_placebo_fixtures.py b/ansible_collections/amazon/aws/tests/unit/utils/amazon_placebo_fixtures.py index 6912c2e32..afe91adad 100644 --- a/ansible_collections/amazon/aws/tests/unit/utils/amazon_placebo_fixtures.py +++ b/ansible_collections/amazon/aws/tests/unit/utils/amazon_placebo_fixtures.py @@ -1,9 +1,13 @@ -from __future__ import absolute_import, division, print_function +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + __metaclass__ = type import errno import os import time + import mock import pytest @@ -54,19 +58,19 @@ def placeboify(request, monkeypatch): namespace `placebo_recordings/{testfile name}/{test function name}` to distinguish them. """ - session = boto3.Session(region_name='us-west-2') + session = boto3.Session(region_name="us-west-2") recordings_path = os.path.join( request.fspath.dirname, - 'placebo_recordings', - request.fspath.basename.replace('.py', ''), + "placebo_recordings", + request.fspath.basename.replace(".py", ""), request.function.__name__ # remove the test_ prefix from the function & file name - ).replace('test_', '') + ).replace("test_", "") - if not os.getenv('PLACEBO_RECORD'): + if not os.getenv("PLACEBO_RECORD"): if not os.path.isdir(recordings_path): - raise NotImplementedError('Missing Placebo recordings in directory: %s' % recordings_path) + raise NotImplementedError(f"Missing Placebo recordings in directory: {recordings_path}") else: try: # make sure the directory for placebo test recordings is available @@ -76,21 +80,22 @@ def placeboify(request, monkeypatch): raise pill = placebo.attach(session, data_path=recordings_path) - if os.getenv('PLACEBO_RECORD'): + if os.getenv("PLACEBO_RECORD"): pill.record() else: pill.playback() - def boto3_middleman_connection(module, conn_type, resource, region='us-west-2', **kwargs): - if conn_type != 'client': + def boto3_middleman_connection(module, conn_type, resource, region="us-west-2", **kwargs): + if conn_type != "client": # TODO support resource-based connections - raise ValueError('Mocker only supports client, not %s' % conn_type) + raise ValueError(f"Mocker only supports client, not {conn_type}") return session.client(resource, region_name=region) import ansible_collections.amazon.aws.plugins.module_utils.ec2 + monkeypatch.setattr( ansible_collections.amazon.aws.plugins.module_utils.ec2, - 'boto3_conn', + "boto3_conn", boto3_middleman_connection, ) yield session @@ -99,113 +104,114 @@ def placeboify(request, monkeypatch): pill.stop() -@pytest.fixture(scope='module') +@pytest.fixture(scope="module") def basic_launch_config(): """Create an EC2 launch config whose creation *is not* recorded and return its name This fixture is module-scoped, since launch configs are immutable and this can be reused for many tests. """ - if not os.getenv('PLACEBO_RECORD'): - yield 'pytest_basic_lc' + if not os.getenv("PLACEBO_RECORD"): + yield "pytest_basic_lc" return # use a *non recording* session to make the launch config # since that's a prereq of the ec2_asg module, and isn't what # we're testing. - asg = boto3.client('autoscaling') + asg = boto3.client("autoscaling") asg.create_launch_configuration( - LaunchConfigurationName='pytest_basic_lc', - ImageId='ami-9be6f38c', # Amazon Linux 2016.09 us-east-1 AMI, can be any valid AMI + LaunchConfigurationName="pytest_basic_lc", + ImageId="ami-9be6f38c", # Amazon Linux 2016.09 us-east-1 AMI, can be any valid AMI SecurityGroups=[], - UserData='#!/bin/bash\necho hello world', - InstanceType='t2.micro', - InstanceMonitoring={'Enabled': False}, - AssociatePublicIpAddress=True + UserData="#!/bin/bash\necho hello world", + InstanceType="t2.micro", + InstanceMonitoring={"Enabled": False}, + AssociatePublicIpAddress=True, ) - yield 'pytest_basic_lc' + yield "pytest_basic_lc" try: - asg.delete_launch_configuration(LaunchConfigurationName='pytest_basic_lc') + asg.delete_launch_configuration(LaunchConfigurationName="pytest_basic_lc") except botocore.exceptions.ClientError as e: - if 'not found' in e.message: + if "not found" in e.message: return raise -@pytest.fixture(scope='module') +@pytest.fixture(scope="module") def scratch_vpc(): - if not os.getenv('PLACEBO_RECORD'): + if not os.getenv("PLACEBO_RECORD"): yield { - 'vpc_id': 'vpc-123456', - 'cidr_range': '10.0.0.0/16', - 'subnets': [ + "vpc_id": "vpc-123456", + "cidr_range": "10.0.0.0/16", + "subnets": [ { - 'id': 'subnet-123456', - 'az': 'us-east-1d', + "id": "subnet-123456", + "az": "us-east-1d", }, { - 'id': 'subnet-654321', - 'az': 'us-east-1e', + "id": "subnet-654321", + "az": "us-east-1e", }, - ] + ], } return # use a *non recording* session to make the base VPC and subnets - ec2 = boto3.client('ec2') + ec2 = boto3.client("ec2") vpc_resp = ec2.create_vpc( - CidrBlock='10.0.0.0/16', + CidrBlock="10.0.0.0/16", AmazonProvidedIpv6CidrBlock=False, ) subnets = ( ec2.create_subnet( - VpcId=vpc_resp['Vpc']['VpcId'], - CidrBlock='10.0.0.0/24', + VpcId=vpc_resp["Vpc"]["VpcId"], + CidrBlock="10.0.0.0/24", ), ec2.create_subnet( - VpcId=vpc_resp['Vpc']['VpcId'], - CidrBlock='10.0.1.0/24', - ) + VpcId=vpc_resp["Vpc"]["VpcId"], + CidrBlock="10.0.1.0/24", + ), ) time.sleep(3) yield { - 'vpc_id': vpc_resp['Vpc']['VpcId'], - 'cidr_range': '10.0.0.0/16', - 'subnets': [ + "vpc_id": vpc_resp["Vpc"]["VpcId"], + "cidr_range": "10.0.0.0/16", + "subnets": [ { - 'id': s['Subnet']['SubnetId'], - 'az': s['Subnet']['AvailabilityZone'], - } for s in subnets - ] + "id": s["Subnet"]["SubnetId"], + "az": s["Subnet"]["AvailabilityZone"], + } + for s in subnets + ], } try: for s in subnets: try: - ec2.delete_subnet(SubnetId=s['Subnet']['SubnetId']) + ec2.delete_subnet(SubnetId=s["Subnet"]["SubnetId"]) except botocore.exceptions.ClientError as e: - if 'not found' in e.message: + if "not found" in e.message: continue raise - ec2.delete_vpc(VpcId=vpc_resp['Vpc']['VpcId']) + ec2.delete_vpc(VpcId=vpc_resp["Vpc"]["VpcId"]) except botocore.exceptions.ClientError as e: - if 'not found' in e.message: + if "not found" in e.message: return raise -@pytest.fixture(scope='module') +@pytest.fixture(scope="module") def maybe_sleep(): """If placebo is reading saved sessions, make sleep always take 0 seconds. AWS modules often perform polling or retries, but when using recorded sessions there's no reason to wait. We can still exercise retry and other code paths without waiting for wall-clock time to pass.""" - if not os.getenv('PLACEBO_RECORD'): - p = mock.patch('time.sleep', return_value=None) + if not os.getenv("PLACEBO_RECORD"): + p = mock.patch("time.sleep", return_value=None) p.start() yield p.stop() |