def _create_official_image_info(version, os, architecture):
    return ImageInfo({
        "Name":
        f"aws-parallelcluster-{version}-{OS_TO_IMAGE_NAME_PART_MAP[os]}-{architecture}-other",
        "Architecture": "x86_64",
        "ImageId": "ami-test",
    })
예제 #2
0
 def describe_image(self, ami_id):
     """Describe image by image id, return an object of ImageInfo."""
     result = self._client.describe_images(ImageIds=[ami_id])
     if result.get("Images"):
         return ImageInfo(result.get("Images")[0])
     raise AWSClientError(function_name="describe_images",
                          message=f"Image {ami_id} not found")
def _create_image_info(image_id):
    return ImageInfo({
        "Name":
        image_id,
        "ImageId":
        image_id,
        "State":
        Ec2AmiState.AVAILABLE,
        "Architecture":
        "x86_64",
        "CreationDate":
        datetime(2021, 4, 12),
        "Description":
        "description",
        "Tags": [
            {
                "Key": "parallelcluster:image_id",
                "Value": image_id
            },
            {
                "Key": "parallelcluster:version",
                "Value": "3.0.0"
            },
            {
                "Key": "parallelcluster:build_config",
                "Value": "s3://bucket/key"
            },
        ],
    })
예제 #4
0
 def describe_images(self, ami_ids, filters, owners):
     """Return a list of objects of ImageInfo."""
     result = self._client.describe_images(ImageIds=ami_ids,
                                           Filters=filters,
                                           Owners=owners)
     if result.get("Images"):
         return [ImageInfo(image) for image in result.get("Images")]
     raise ImageNotFoundError(function_name="describe_images")
def test_ami_volume_size_validator(mocker, image, volume_size,
                                   expected_message, ami_response):
    mocker.patch("pcluster.imagebuilder_utils.get_ami_id",
                 return_value="ami-0185634c5a8a37250")
    mock_aws_api(mocker)
    mocker.patch(
        "pcluster.aws.ec2.Ec2Client.describe_image",
        return_value=ImageInfo(ami_response),
    )
    actual_failures = AMIVolumeSizeValidator().execute(volume_size, image)
    assert_failure_messages(actual_failures, expected_message)
예제 #6
0
def test_image(mocker, stack_resources_response, describe_image_response,
               expected_image_property_value):
    mock_aws_api(mocker)
    mocker.patch("pcluster.aws.cfn.CfnClient.describe_stack_resource",
                 return_value=stack_resources_response)
    mocker.patch("pcluster.aws.imagebuilder.ImageBuilderClient.get_image_id",
                 return_value="ami-06b66530ba9f43a96")
    mocker.patch("pcluster.aws.ec2.Ec2Client.describe_image",
                 return_value=ImageInfo(describe_image_response))
    imagebuilder_stack = ImageBuilderStack(
        {"StackName": FAKE_IMAGEBUILDER_STACK_NAME})
    image = imagebuilder_stack.image
    for prop in filter(lambda p: not p.startswith("_"), image.__dir__()):
        attr = getattr(image, prop)
        if prop not in expected_image_property_value:
            assert_that(attr in describe_image_response.values()).is_true()
        else:
            assert_that(attr).is_equal_to(
                expected_image_property_value.get(prop))
def test_instance_type_base_ami_compatible_validator(
    mocker,
    instance_type,
    parent_image,
    expected_message,
    ami_response,
    ami_side_effect,
    instance_response,
    instance_architectures,
):
    mocker.patch("pcluster.imagebuilder_utils.get_ami_id",
                 return_value="ami-0185634c5a8a37250")
    mock_aws_api(mocker)
    mocker.patch("pcluster.aws.ec2.Ec2Client.describe_image",
                 return_value=ImageInfo(ami_response),
                 side_effect=ami_side_effect)
    mocker.patch("pcluster.aws.ec2.Ec2Client.list_instance_types",
                 return_value=instance_response)
    mocker.patch("pcluster.aws.ec2.Ec2Client.get_supported_architectures",
                 return_value=instance_architectures)
    actual_failures = InstanceTypeBaseAMICompatibleValidator().execute(
        instance_type=instance_type, image=parent_image)
    assert_failure_messages(actual_failures, expected_message)
예제 #8
0
def _test_imagebuilder(
    mocker,
    resource,
    ami_response,
    ami_side_effect,
    instance_response,
    url_response,
    url_side_effect,
    url_open_side_effect,
    supported_architecture,
    expected_failure_messages,
    expected_failure_levels,
):
    mocker.patch("pcluster.imagebuilder_utils.get_ami_id",
                 return_value="ami-0185634c5a8a37250")
    mock_aws_api(mocker)
    mocker.patch("pcluster.aws.ec2.Ec2Client.get_supported_architectures",
                 return_value=supported_architecture)
    mocker.patch("pcluster.aws.ec2.Ec2Client.describe_image",
                 return_value=ImageInfo(ami_response),
                 side_effect=ami_side_effect)
    mocker.patch("pcluster.aws.ec2.Ec2Client.list_instance_types",
                 return_value=instance_response)
    mocker.patch("pcluster.aws.s3.S3Client.head_object",
                 return_value=url_response,
                 side_effect=url_side_effect)
    mocker.patch("pcluster.validators.s3_validators.urlopen",
                 side_effect=url_open_side_effect)
    mocker.patch("pcluster.aws.kms.KmsClient.describe_key", return_value=None)

    imagebuilder = imagebuilder_factory(resource).get("imagebuilder")
    validation_failures = imagebuilder.validate()
    for validation_failure, expected_failure_level, expected_failure_message in zip(
            validation_failures, expected_failure_levels,
            expected_failure_messages):
        assert_validation_result(validation_failure, expected_failure_level,
                                 expected_failure_message)
예제 #9
0
     None,
     {
         "Images": [
             {
                 "Name":
                 "aws-parallelcluster-3.0.0-amzn2-hvm-x86_64-other"
             },
             {
                 "Name":
                 "ami-parallelcluster-3.0.0-centos7-hvm-x86_64-other"
             },
         ]
     },
     [
         ImageInfo({
             "Name":
             "aws-parallelcluster-3.0.0-amzn2-hvm-x86_64-other"
         }),
         ImageInfo({
             "Name":
             "ami-parallelcluster-3.0.0-centos7-hvm-x86_64-other"
         }),
     ],
     None,
     id="test with no filter",
 ),
 pytest.param(
     "alinux2",
     None,
     {
         "Images": [{
             "Name":
            "SupportedUsageClasses":
            supported_usage_classes
        }),
    )
    actual_failures = CapacityTypeValidator().execute(
        capacity_type=capacity_type, instance_type="instance-type")
    assert_failure_messages(actual_failures, expected_message)


@pytest.mark.parametrize(
    "image_id, os, ami_info, expected_message",
    [
        ("ami-000000000000", "alinux2",
         ImageInfo(
             {"Tags": [{
                 "Key": "parallelcluster:os",
                 "Value": "alinux2"
             }]}), None),
        (
            "ami-111111111111",
            "alinux2",
            ImageInfo({
                "Tags": [{
                    "Key": "parallelcluster:os",
                    "Value": "ubuntu1804"
                }]
            }),
            "The OS of compute node AMI ami-111111111111 is ubuntu1804, it is not compatible with cluster OS alinux2.",
        ),
        (
            "ami-222222222222",