def test_config_from_json(mocker, boto3_stubber, test_datadir, pcluster_config_reader, queues): def mock_get_avail_zone(subnet_id): # Mock az detection by returning a mock az if subnet has a value return "my-avail-zone" if subnet_id and subnet_id != "NONE" else None mocker.patch("pcluster.config.cfn_param_types.get_availability_zone_of_subnet", mock_get_avail_zone) # Created expected json params based on active queues expected_json_params = _prepare_json_config(queues, test_datadir) # Mock expected boto3 calls _mock_boto3(boto3_stubber, expected_json_params, head_node_instance_type="t2.micro") pcluster_config = get_mocked_pcluster_config(mocker, auto_refresh=False) cluster_section = CfnSection(CLUSTER_HIT, pcluster_config, section_label="default") cluster_section.from_storage(StorageData(cfn_params=[], json_params=expected_json_params)) pcluster_config.add_section(cluster_section) pcluster_config.refresh() dashboard_section = pcluster_config.get_section("dashboard", "dashboard1") assert_that(dashboard_section).is_not_none() assert_that(dashboard_section.get_param_value("enable")).is_equal_to(False) for queue in queues: assert_that(pcluster_config.get_section("queue", queue)).is_not_none() _check_queue_section_from_json( expected_json_params, pcluster_config, pcluster_config.get_section("queue", queue) )
def test_param_to_cfn(mocker, section_definition, param_key, param_value, expected_cfn_params): pcluster_config = get_mocked_pcluster_config(mocker) param_definition, param_type = get_param_definition(section_definition, param_key) param = param_type(section_definition.get("key"), "default", param_key, param_definition, pcluster_config) param.value = param_value cfn_params = param.to_cfn() assert_that(cfn_params).is_equal_to(expected_cfn_params)
def test_reset_param_value(mocker, section_definition, param_key, value): param_definition, param_type = get_cfnparam_definition(section_definition, param_key) pcluster_config = get_mocked_pcluster_config(mocker) param = param_type(section_definition.get("key"), "default", param_key, param_definition, pcluster_config) param.value = value param.reset_value() assert_that(param.value).is_equal_to(param.get_default_value())
def test_param_from_cfn_value(mocker, section_definition, param_key, cfn_value, expected_value): """Test conversion from cfn value of simple parameters, that don't depends from multiple CFN parameters.""" param_definition, param_type = get_cfnparam_definition(section_definition, param_key) pcluster_config = get_mocked_pcluster_config(mocker) param_value = param_type( section_definition.get("key"), "default", param_key, param_definition, pcluster_config ).get_value_from_string(cfn_value) assert_that(param_value).is_equal_to(expected_value)
def test_param_from_cfn(mocker, section_definition, param_key, cfn_params_dict, expected_value): """Test conversion of simple parameters, that don't depends from multiple CFN parameters.""" param_definition, param_type = get_cfnparam_definition(section_definition, param_key) cfn_params = [] for cfn_key, cfn_value in cfn_params_dict.items(): cfn_params.append({"ParameterKey": cfn_key, "ParameterValue": cfn_value}) pcluster_config = get_mocked_pcluster_config(mocker) param_type = param_type(section_definition.get("key"), "default", param_key, param_definition, pcluster_config) param = param_type.from_cfn_params(cfn_params) assert_that(param.value, description="param key {0}".format(param_key)).is_equal_to(expected_value)
def test_param_to_file(mocker, section_definition, param_key, param_value, expected_value): section_label = "default" section_name = section_definition.get("key") + " " + section_label config_parser = configparser.ConfigParser() config_parser.add_section(section_name) pcluster_config = get_mocked_pcluster_config(mocker) param_definition, param_type = get_cfnparam_definition(section_definition, param_key) param = param_type(section_definition.get("key"), section_label, param_key, param_definition, pcluster_config) param.value = param_value or param_definition.get("default") param.to_file(config_parser) if expected_value: assert_that(config_parser.has_option(section_name, param_key)) assert_that(config_parser.get(section_name, param_key)).is_equal_to(expected_value) else: assert_that(config_parser.has_option(section_name, param_key)).is_false()
def test_volume_size_refresh(mocker, section_dict, expected_value): mocked_pcluster_config = utils.get_mocked_pcluster_config(mocker) ebs_section = CfnSection(EBS, mocked_pcluster_config, "default") for param_key, param_value in section_dict.items(): param = EBS.get("params").get(param_key).get("type", CfnParam) param.value = param_value ebs_section.set_param(param_key, param) mocked_pcluster_config.add_section(ebs_section) config_parser = configparser.ConfigParser() config_parser_dict = { "cluster default": { "ebs_settings": "default" }, "ebs default": section_dict } config_parser.read_dict(config_parser_dict) volume_size = VolumeSizeParam( section_key="ebs", section_label="default", param_key="volume_size", param_definition=EBS.get("params").get("volume_size"), pcluster_config=mocked_pcluster_config, owner_section=ebs_section, ).from_file(config_parser) describe_snapshots_response = { "Description": "This is my snapshot", "Encrypted": False, "VolumeId": "vol-049df61146c4d7901", "State": "completed", "VolumeSize": 50, "StartTime": "2014-02-28T21:28:32.000Z", "Progress": "100%", "OwnerId": "012345678910", "SnapshotId": "snap-1234567890abcdef0", } mocker.patch("pcluster.config.cfn_param_types.get_ebs_snapshot_info", return_value=describe_snapshots_response) volume_size.refresh() assert_that(volume_size.value).is_equal_to(expected_value)
def test_get_latest_alinux_ami_id(mocker, boto3_stubber, path, boto3_response, expected_message): mocked_requests = [ MockedBoto3Request( method="get_parameters_by_path", response=boto3_response, expected_params={"Path": path}, generate_error=True if expected_message else False, ) ] boto3_stubber("ssm", mocked_requests) pcluster_config = get_mocked_pcluster_config(mocker) if expected_message: with pytest.raises(SystemExit, match=expected_message): _ = pcluster_config._PclusterConfig__get_latest_alinux_ami_id() else: latest_linux_ami_id = pcluster_config._PclusterConfig__get_latest_alinux_ami_id( ) assert_that(latest_linux_ami_id).is_equal_to( boto3_response.get("Parameters")[0].get("Value"))
def test_load_json_config(mocker, cfn_params_dict, version, expected_json): cfn_params = [] for cfn_key, cfn_value in cfn_params_dict.items(): cfn_params.append({ "ParameterKey": cfn_key, "ParameterValue": cfn_value }) cfn_stack = { "Parameters": cfn_params, "Tags": [{ "Key": "Version", "Value": version }] } pcluster_config = get_mocked_pcluster_config(mocker) patched_read_remote_file = mocker.patch.object( pcluster_config, "_PclusterConfig__retrieve_cluster_config", auto_spec=True) patched_read_remote_file.return_value = expected_json assert_that(pcluster_config._PclusterConfig__load_json_config( cfn_stack)).is_equal_to(expected_json)
def test_get_cluster_ami_id( mocker, boto3_stubber, custom_ami_id, os, architecture, expected_ami_suffix, expected_public_ami_id, expected_self_ami_id, expected_error_message, raise_boto3_error, ): if not custom_ami_id: # Expected request for public ami mocked_requests = [ MockedBoto3Request( method="describe_images", response={ "Images": [{ "Architecture": architecture, "CreationDate": "2020-12-22T13:30:33.000Z", "ImageId": expected_public_ami_id, }] if expected_public_ami_id else [] }, expected_params={ "Filters": [ { "Name": "name", "Values": [ "aws-parallelcluster-{version}-{suffix}*". format(version=get_installed_version(), suffix=expected_ami_suffix) ], }, { "Name": "is-public", "Values": ["true"] }, ], "Owners": ["amazon"], }, generate_error=raise_boto3_error, ) ] if not expected_public_ami_id and not raise_boto3_error: # Expected request for self ami mocked_requests.append( MockedBoto3Request( method="describe_images", response={ "Images": [{ "Architecture": architecture, "CreationDate": "2020-12-22T13:30:33.000Z", "ImageId": expected_self_ami_id, }] if expected_self_ami_id else [] }, expected_params={ "Filters": [ { "Name": "name", "Values": [ "aws-parallelcluster-{version}-{suffix}*". format(version=get_installed_version(), suffix=expected_ami_suffix) ], }, ], "Owners": ["self"], }, generate_error=raise_boto3_error, )) boto3_stubber("ec2", mocked_requests) pcluster_config = get_mocked_pcluster_config(mocker) pcluster_config.get_section("cluster").get_param( "custom_ami").value = custom_ami_id pcluster_config.get_section("cluster").get_param("base_os").value = os pcluster_config.get_section("cluster").get_param( "architecture").value = architecture if expected_error_message: with pytest.raises(SystemExit, match=expected_error_message): _ = pcluster_config.cluster_model._get_cluster_ami_id( pcluster_config) else: expected_ami_id = expected_public_ami_id if expected_public_ami_id else expected_self_ami_id cluster_ami_id = pcluster_config.cluster_model._get_cluster_ami_id( pcluster_config) assert_that(cluster_ami_id).is_equal_to(expected_ami_id)