def test_determine_build_spec_with_file_and_inline_specified_in_deploy( buildspec_mock): codebuild_id = 'some-id' spec_inline = { 'Some-Object': 'Some-Value', } spec_filename = 'some-file-name.yml' buildspec_mock.from_object.return_value = 'From-Object' buildspec_mock.from_source_filename.return_value = 'From-Source' correct_error_message = ( "The spec_filename and spec_inline are both present " "inside the default deploy stage definition of {0}. " "Whereas only one of these two is allowed.".format(codebuild_id)) with pytest.raises(Exception) as excinfo: CodeBuild.determine_build_spec(codebuild_id=codebuild_id, default_props={ 'spec_filename': spec_filename, 'spec_inline': spec_inline, }, target={ 'properties': {}, }) error_message = str(excinfo.value) assert error_message.find(correct_error_message) >= 0 buildspec_mock.from_object.assert_not_called() buildspec_mock.from_source_filename.assert_not_called()
def test_determine_build_image_build_str(ecr_repo, build_image): """ Scenario: Target: Not set. Build: Specific config set, as str, should use this. Deploy: Specific config set, as str. Tests: Since the target is not set, it will determine that it is a build step. As specific config for the default build provider is set it should use these, not the deploy specific config. """ scope = core.Stack() target = None map_params = deepcopy(CODEBUILD_BASE_MAP_PARAMS) map_params['default_providers']['build'] = \ CODEBUILD_SPECIFIC_MAP_PARAMS_STR # Set deploy one to alternative, so we can test # that it is not using this in build steps map_params['default_providers']['deploy'] = \ CODEBUILD_SPECIFIC_MAP_PARAMS_ALT_STR result = CodeBuild.determine_build_image( scope=scope, target=target, map_params=map_params, ) assert result == getattr( _codebuild.LinuxBuildImage, SPECIFIC_CODEBUILD_IMAGE_STR, ) ecr_repo.from_repository_arn.assert_not_called() build_image.from_ecr_repository.assert_not_called()
def test_determine_build_image_deploy_defaults(ecr_repo, build_image): """ Scenario: Target: Set, no config. Build: Specific config set, as str. Build: No specifics, i.e. use defaults. Tests: Since the target is set, it will determine that it is a deploy step. As no specific config for the default deploy provider is set it should return the default config. """ scope = core.Stack() target = SIMPLE_TARGET map_params = deepcopy(CODEBUILD_BASE_MAP_PARAMS) # Set build one to alternative, so we can test # that it is not using this in deploy steps map_params['default_providers']['build'] = \ CODEBUILD_SPECIFIC_MAP_PARAMS_ALT_STR result = CodeBuild.determine_build_image( scope=scope, target=target, map_params=map_params, ) assert result == getattr( _codebuild.LinuxBuildImage, DEFAULT_CODEBUILD_IMAGE, ) ecr_repo.from_repository_arn.assert_not_called() build_image.from_ecr_repository.assert_not_called()
def test_determine_build_spec_with_filename_specified_in_target( buildspec_mock): codebuild_id = 'some-id' spec_inline = { 'Some-Object': 'Some-Value', } spec_filename = 'some-file-name.yml' buildspec_mock.from_object.return_value = 'From-Object' buildspec_mock.from_source_filename.return_value = 'From-Source' return_value = CodeBuild.determine_build_spec( codebuild_id=codebuild_id, default_props={ 'spec_inline': spec_inline, }, target={ 'properties': { 'spec_filename': spec_filename, }, }, ) assert return_value == buildspec_mock.from_source_filename.return_value buildspec_mock.from_object.assert_not_called() buildspec_mock.from_source_filename.assert_called_once_with(spec_filename)
def test_determine_build_spec_with_no_spec_no_target(buildspec_mock): codebuild_id = 'some-id' buildspec_mock.from_object.return_value = 'From-Object' buildspec_mock.from_source_filename.return_value = 'From-Source' return_value = CodeBuild.determine_build_spec( codebuild_id=codebuild_id, default_props={}, ) assert return_value == buildspec_mock.from_source_filename.return_value buildspec_mock.from_object.assert_not_called() buildspec_mock.from_source_filename.assert_called_once_with( DEFAULT_BUILD_SPEC_FILENAME, )
def test_determine_build_image_deploy_ecr_no_tag_too(ecr_repo, build_image): """ Scenario: Target: Specific config set, as ECR dict, should use these, but has no specific tag set, so should use 'latest'. Build: Specific config set, as str. Deploy: Specific config set, as ECR dict. Tests: Since the target is set, it will determine that it is a deploy step. As specific config for the target is set it should use these with ECR, not the default build or deploy specific config. Plus setting the 'latest' default tag, as that is not specified. """ scope = core.Stack() target = deepcopy(CODEBUILD_SPECIFIC_MAP_PARAMS_ECR) target['properties']['image']['repository_arn'] = 'arn:other:one' del target['properties']['image']['tag'] map_params = deepcopy(CODEBUILD_BASE_MAP_PARAMS) map_params['default_providers']['deploy'] = deepcopy( CODEBUILD_SPECIFIC_MAP_PARAMS_ECR ) # Set build one to alternative, so we can test # that it is not using this in deploy steps map_params['default_providers']['build'] = \ CODEBUILD_SPECIFIC_MAP_PARAMS_ALT_STR from_repo_arn_return_value = {'Some': 'Value'} ecr_repo.from_repository_arn.return_value = from_repo_arn_return_value from_ecr_repo_return_value = {'Another': 'Object'} build_image.from_ecr_repository.return_value = from_ecr_repo_return_value result = CodeBuild.determine_build_image( scope=scope, target=target, map_params=map_params, ) assert result == from_ecr_repo_return_value ecr_repo.from_repository_arn.assert_called_once_with( scope, 'custom_repo', target['properties']['image']['repository_arn'], ) build_image.from_ecr_repository.assert_called_once_with( from_repo_arn_return_value, 'latest', )
def test_determine_build_image_deploy_ecr(ecr_repo, build_image): """ Scenario: Target: Set, no specific config. Build: Specific config set, as str. Deploy: Specific config set, as ECR dict, should use this. Tests: Since the target is set, it will determine that it is a deploy step. As specific config for the default deploy provider is set it should use these with ECR, not the build specific config. Plus setting the 'specific' tag, as that is specified. """ scope = core.Stack() target = SIMPLE_TARGET map_params = deepcopy(CODEBUILD_BASE_MAP_PARAMS) map_params['default_providers']['deploy'] = \ CODEBUILD_SPECIFIC_MAP_PARAMS_ECR # Set build one to alternative, so we can test # that it is not using this in deploy steps map_params['default_providers']['build'] = \ CODEBUILD_SPECIFIC_MAP_PARAMS_ALT_STR from_repo_arn_return_value = {'Some': 'Value'} ecr_repo.from_repository_arn.return_value = from_repo_arn_return_value from_ecr_repo_return_value = {'Another': 'Object'} build_image.from_ecr_repository.return_value = from_ecr_repo_return_value result = CodeBuild.determine_build_image( scope=scope, target=target, map_params=map_params, ) assert result == from_ecr_repo_return_value ecr_repo.from_repository_arn.assert_called_once_with( scope, 'custom_repo', SPECIFIC_CODEBUILD_IMAGE_ECR.get('repository_arn'), ) build_image.from_ecr_repository.assert_called_once_with( from_repo_arn_return_value, SPECIFIC_CODEBUILD_IMAGE_ECR.get('tag'), )