def test_get_ebs_snapshot_info(boto3_stubber, snapshot_id, error_message): """Verify that get_snapshot_info makes the expected API call.""" response = { "Description": "This is my snapshot", "Encrypted": False, "VolumeId": "vol-049df61146c4d7901", "State": "completed", "VolumeSize": 120, "StartTime": "2014-02-28T21:28:32.000Z", "Progress": "100%", "OwnerId": "012345678910", "SnapshotId": "snap-1234567890abcdef0", } describe_snapshots_response = {"Snapshots": [response]} mocked_requests = [ MockedBoto3Request( method="describe_snapshots", response=describe_snapshots_response if error_message is None else error_message, expected_params={"SnapshotIds": ["snap-1234567890abcdef0"]}, generate_error=error_message is not None, ) ] boto3_stubber("ec2", mocked_requests) if error_message is None: assert_that(Ec2Client().get_ebs_snapshot_info( snapshot_id)).is_equal_to(response) elif error_message: with pytest.raises(AWSClientError, match=error_message) as clienterror: Ec2Client().get_ebs_snapshot_info(snapshot_id) assert_that(clienterror.value.code).is_not_equal_to(0)
def test_get_default_instance(boto3_stubber, region, free_tier_instance_type, default_instance_type, stub_boto3): os_lib.environ["AWS_DEFAULT_REGION"] = region if free_tier_instance_type: response = { "InstanceTypes": [{ "InstanceType": free_tier_instance_type }] } else: response = {"InstanceTypes": []} if stub_boto3: mocked_requests = [ MockedBoto3Request( method="describe_instance_types", response=response, expected_params={ "Filters": [ { "Name": "free-tier-eligible", "Values": ["true"] }, { "Name": "current-generation", "Values": ["true"] }, ] }, ) ] boto3_stubber("ec2", mocked_requests) assert_that(Ec2Client().get_default_instance_type()).is_equal_to( default_instance_type)
def _image_info_to_ami_info(image): return AmiInfo( ami_id=image.id, os=Ec2Client.extract_os_from_official_image_name(image.name), name=image.name, architecture=image.architecture, version=get_installed_version(), )
def test_run_instances_dryrun(boto3_stubber, error_code, raise_exception): """Verify that if run_instance doesn't generate exception if the error code is DryRunOperation.""" error_message = "fake error message" mocked_requests = [ MockedBoto3Request( method="run_instances", response=error_message, expected_params=None, generate_error=True, error_code=error_code, ) ] boto3_stubber("ec2", mocked_requests) kwargs = {"MaxCount": 10, "MinCount": 0, "DryRun": True} if raise_exception: with pytest.raises(AWSClientError, match=error_message) as clienterror: Ec2Client().run_instances(**kwargs) assert_that(clienterror.value.code).is_not_equal_to(0) else: Ec2Client().run_instances(**kwargs)
def test_get_official_images(boto3_stubber, os, architecture, boto3_response, expected_response, error_message): filter_version = get_installed_version() filter_os = OS_TO_IMAGE_NAME_PART_MAP[os] if os else "*" filter_arch = architecture or "*" expected_params = { "Filters": [ { "Name": "name", "Values": [ f"aws-parallelcluster-{filter_version}-{filter_os}-{filter_arch}*" ] }, ], "ImageIds": [], "Owners": ["amazon"], } mocked_requests = [ MockedBoto3Request( method="describe_images", expected_params=expected_params, response=str(boto3_response) if isinstance(boto3_response, Exception) else boto3_response, generate_error=isinstance(boto3_response, Exception), ) ] boto3_stubber("ec2", mocked_requests) if error_message: with pytest.raises(AWSClientError, match=error_message): Ec2Client().get_official_images(os, architecture) else: response = Ec2Client().get_official_images(os, architecture) with soft_assertions(): assert_that(len(response)).is_equal_to(len(expected_response)) for i in range(len(response)): assert_that(response[i].name).is_equal_to( expected_response[i].name)
def test_list_instance_types(boto3_stubber, generate_error): """Verify that list_instance_types behaves as expected.""" dummy_message = "dummy error message" dummy_instance_types = ["c5.xlarge", "m6g.xlarge"] mocked_requests = [ MockedBoto3Request( method="describe_instance_type_offerings", expected_params={}, response=dummy_message if generate_error else { "InstanceTypeOfferings": [{ "InstanceType": instance_type } for instance_type in dummy_instance_types] }, generate_error=generate_error, ) ] boto3_stubber("ec2", mocked_requests) if generate_error: with pytest.raises(AWSClientError, match=dummy_message): Ec2Client().list_instance_types() else: return_value = Ec2Client().list_instance_types() assert_that(return_value).is_equal_to(dummy_instance_types)
def test_get_official_image_id(boto3_stubber, os, architecture, filters, boto3_response, error_message): expected_ami_id = "ami-00e87074e52e6" expected_params = { "Filters": [ { "Name": "name", "Values": [ f"aws-parallelcluster-{get_installed_version()}-amzn2-hvm-{architecture}*" ] }, ], "Owners": [filters.owner if filters and filters.owner else "amazon"], } if filters and filters.tags: expected_params["Filters"].extend([{ "Name": f"tag:{tag.key}", "Values": [tag.value] } for tag in filters.tags]) mocked_requests = [ MockedBoto3Request( method="describe_images", expected_params=expected_params, response=str(boto3_response) if isinstance(boto3_response, Exception) else boto3_response, generate_error=isinstance(boto3_response, Exception), ) ] boto3_stubber("ec2", mocked_requests) if error_message: with pytest.raises(AWSClientError, match=error_message): Ec2Client().get_official_image_id(os, architecture, filters) else: ami_id = Ec2Client().get_official_image_id(os, architecture, filters) assert_that(ami_id).is_equal_to(expected_ami_id)
def test_get_supported_architectures(mocker, instance_type, supported_architectures, error_message): """Verify that get_supported_architectures_for_instance_type behaves as expected for various cases.""" mock_aws_api(mocker) get_instance_types_info_patch = mocker.patch( "pcluster.aws.ec2.Ec2Client.get_instance_type_info", return_value=InstanceTypeInfo({ "ProcessorInfo": { "SupportedArchitectures": supported_architectures } }), ) observed_architectures = Ec2Client().get_supported_architectures( instance_type) expected_architectures = list( set(supported_architectures) & set(["x86_64", "arm64"])) assert_that(observed_architectures).is_equal_to(expected_architectures) get_instance_types_info_patch.assert_called_with(instance_type)
def test_extract_os_from_official_image_name(os_part, expected_os): name = f"aws-parallelcluster-3.0.0-{os_part}-otherstuff" os = Ec2Client.extract_os_from_official_image_name(name) assert_that(os).is_equal_to(expected_os)
def ec2(self): """EC2 client.""" if not self._ec2: self._ec2 = Ec2Client() return self._ec2