def _mock_aws_api_required_calls(mocker): supported_instance_types = [ "t2.nano", "t2.micro", "t2.large", "c5.xlarge", "g3.8xlarge", "m6g.xlarge", "p4d.24xlarge", ] mock_aws_api(mocker, mock_instance_type_info=False) mocker.patch("pcluster.aws.ec2.Ec2Client.get_default_instance_type", return_value="t2.micro") mocker.patch("pcluster.aws.ec2.Ec2Client.list_instance_types", return_value=supported_instance_types) mocker.patch("pcluster.aws.ec2.Ec2Client.get_subnet_avail_zone", return_value="mocked_avail_zone") mocker.patch( "pcluster.aws.ec2.Ec2Client.get_instance_type_info", side_effect=[ InstanceTypeInfo( { "InstanceType": instance_type, "VCpuInfo": {"DefaultVCpus": 4, "DefaultCores": 2}, "NetworkInfo": {"EfaSupported": False}, } ) for instance_type in supported_instance_types ], )
def instance_type_info_mock(aws_api_mock): aws_api_mock.ec2.get_instance_type_info.return_value = InstanceTypeInfo( { "InstanceType": "g4ad.16xlarge", "VCpuInfo": {"DefaultVCpus": 64}, "GpuInfo": {"Gpus": [{"Name": "*", "Manufacturer": "AMD", "Count": 4}]}, "NetworkInfo": {"EfaSupported": False, "MaximumNetworkCards": 1}, "ProcessorInfo": {"SupportedArchitectures": ["x86_64"]}, } )
def _mock_instance_type_info(mocker, instance_type="t2.micro"): mocker.patch( "pcluster.aws.ec2.Ec2Client.get_instance_type_info", InstanceTypeInfo( { "InstanceType": instance_type, "VCpuInfo": {"DefaultVCpus": 4, "DefaultCores": 2}, "NetworkInfo": {"EfaSupported": False}, } ), )
def test_capacity_type_validator(mocker, capacity_type, supported_usage_classes, expected_message): mock_aws_api(mocker) mocker.patch( "pcluster.aws.ec2.Ec2Client.get_instance_type_info", return_value=InstanceTypeInfo({ "InstanceType": "instance-type", "SupportedUsageClasses": supported_usage_classes }), ) actual_failures = CapacityTypeValidator().execute( capacity_type=capacity_type, instance_type="instance-type") assert_failure_messages(actual_failures, expected_message)
def test_efa_validator(mocker, boto3_stubber, instance_type, efa_enabled, gdr_support, efa_supported, expected_message): mock_aws_api(mocker) get_instance_type_info_mock = mocker.patch( "pcluster.aws.ec2.Ec2Client.get_instance_type_info", return_value=InstanceTypeInfo( { "InstanceType": instance_type, "VCpuInfo": {"DefaultVCpus": 4, "DefaultCores": 2}, "NetworkInfo": {"EfaSupported": instance_type == "c5n.18xlarge"}, } ), ) actual_failures = EfaValidator().execute(instance_type, efa_enabled, gdr_support) assert_failure_messages(actual_failures, expected_message) if efa_enabled: get_instance_type_info_mock.assert_called_with(instance_type)
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_compute_instance_type_validator(mocker, instance_type, max_vcpus, expected_message): mock_aws_api(mocker) mocker.patch("pcluster.aws.ec2.Ec2Client.list_instance_types", return_value=["t2.micro", "p4d.24xlarge"]) mocker.patch( "pcluster.aws.ec2.Ec2Client.get_instance_type_info", return_value=InstanceTypeInfo({ "InstanceType": instance_type, "VCpuInfo": { "DefaultVCpus": 4, "DefaultCores": 2 }, "NetworkInfo": { "EfaSupported": False }, }), ) actual_failures = AwsBatchComputeInstanceTypeValidator().execute( [instance_type], max_vcpus) assert_failure_messages(actual_failures, expected_message)
def test_init_from_instance_type(mocker, caplog): mock_aws_api(mocker, mock_instance_type_info=False) mocker.patch( "pcluster.aws.ec2.Ec2Client.get_instance_type_info", return_value=InstanceTypeInfo({ "InstanceType": "c4.xlarge", "VCpuInfo": { "DefaultVCpus": 4, "DefaultCores": 2, "DefaultThreadsPerCore": 2, "ValidCores": [1, 2], "ValidThreadsPerCore": [1, 2], }, "NetworkInfo": { "EfaSupported": False, "MaximumNetworkCards": 1 }, "ProcessorInfo": { "SupportedArchitectures": ["x86_64"] }, }), ) c4_instance_info = AWSApi.instance().ec2.get_instance_type_info( "c4.xlarge") assert_that(c4_instance_info.gpu_count()).is_equal_to(0) assert_that(caplog.text).is_empty() assert_that(c4_instance_info.max_network_interface_count()).is_equal_to(1) assert_that(c4_instance_info.default_threads_per_core()).is_equal_to(2) assert_that(c4_instance_info.valid_threads_per_core()).is_equal_to([1, 2]) assert_that(c4_instance_info.vcpus_count()).is_equal_to(4) assert_that(c4_instance_info.supported_architecture()).is_equal_to( ["x86_64"]) assert_that(c4_instance_info.is_efa_supported()).is_equal_to(False) mocker.patch( "pcluster.aws.ec2.Ec2Client.get_instance_type_info", return_value=InstanceTypeInfo({ "InstanceType": "g4dn.metal", "VCpuInfo": { "DefaultVCpus": 96, "DefaultCores": 48, "DefaultThreadsPerCore": 2 }, "GpuInfo": { "Gpus": [{ "Name": "T4", "Manufacturer": "NVIDIA", "Count": 8 }] }, "NetworkInfo": { "EfaSupported": True, "MaximumNetworkCards": 4 }, "ProcessorInfo": { "SupportedArchitectures": ["x86_64"] }, }), ) g4dn_instance_info = AWSApi.instance().ec2.get_instance_type_info( "g4dn.metal") assert_that(g4dn_instance_info.gpu_count()).is_equal_to(8) assert_that(caplog.text).is_empty() assert_that( g4dn_instance_info.max_network_interface_count()).is_equal_to(4) assert_that(g4dn_instance_info.default_threads_per_core()).is_equal_to(2) assert_that(g4dn_instance_info.valid_threads_per_core()).is_equal_to([]) assert_that(g4dn_instance_info.vcpus_count()).is_equal_to(96) assert_that(g4dn_instance_info.supported_architecture()).is_equal_to( ["x86_64"]) assert_that(g4dn_instance_info.is_efa_supported()).is_equal_to(True) mocker.patch( "pcluster.aws.ec2.Ec2Client.get_instance_type_info", return_value=InstanceTypeInfo({ "InstanceType": "g4ad.16xlarge", "VCpuInfo": { "DefaultVCpus": 64, "DefaultCores": 32, "DefaultThreadsPerCore": 2, "ValidCores": [2, 4, 8, 16, 32], "ValidThreadsPerCore": [1, 2], }, "GpuInfo": { "Gpus": [{ "Name": "*", "Manufacturer": "AMD", "Count": 4 }] }, "NetworkInfo": { "EfaSupported": False, "MaximumNetworkCards": 1 }, "ProcessorInfo": { "SupportedArchitectures": ["x86_64"] }, }), ) g4ad_instance_info = AWSApi.instance().ec2.get_instance_type_info( "g4ad.16xlarge") assert_that(g4ad_instance_info.gpu_count()).is_equal_to(0) assert_that( caplog.text).matches("not offer native support for 'AMD' GPUs.") assert_that( g4ad_instance_info.max_network_interface_count()).is_equal_to(1) assert_that(g4ad_instance_info.default_threads_per_core()).is_equal_to(2) assert_that(g4ad_instance_info.valid_threads_per_core()).is_equal_to( [1, 2]) assert_that(g4ad_instance_info.vcpus_count()).is_equal_to(64) assert_that(g4ad_instance_info.supported_architecture()).is_equal_to( ["x86_64"]) assert_that(g4ad_instance_info.is_efa_supported()).is_equal_to(False)
def get_instance_type_info(self, instance_type): """Return the results of calling EC2's DescribeInstanceTypes API for the given instance type.""" return InstanceTypeInfo( self.additional_instance_types_data.get(instance_type) or self._client.describe_instance_types(InstanceTypes=[instance_type]).get("InstanceTypes")[0] )