Example #1
0
def test_macros_are_added_correctly(macro_spec_path, prepared_macros):
    """Macros are safely added without messing with current file contents."""

    new_macro_dict = {
        'bootstrap': 1,
        'with_bootstrap': 1,
    }

    with BaseBuilder.edit_spec_file(macro_spec_path) as (original, modified):
        content_stream = BaseBuilder.add_macros(original, new_macro_dict)
        modified.write(''.join(content_stream))

    with macro_spec_path.open() as spec_file:
        spec_contents = spec_file.read()

    # Count the macro occurrences
    matches = MACRO_REGEX.finditer(spec_contents)
    macro_counts = Counter(match.group('name') for match in matches)

    # Each expected macro is present exactly once
    assert all(cnt == 1 for cnt in macro_counts.values()), macro_counts

    expected_macro_names = new_macro_dict.keys() | prepared_macros.keys()
    present_macro_names = macro_counts.keys()
    assert expected_macro_names == present_macro_names, present_macro_names
Example #2
0
def builder(valid_recipe_path):
    """Provide initialized BaseBuilder."""

    recipe = Recipe(str(valid_recipe_path), 'rh-ror50')
    work = Work(recipe)
    builder = BaseBuilder(work, pkg_cmd='testpkg')
    return builder
Example #3
0
    def run_internally(self, argv):
        work = None

        try:
            args = self.parse_argv(argv)

            # Load recipe
            recipe = Recipe(args.recipe_file, args.recipe_id)

            args_dict = vars(args)
            work = Work(recipe, **args_dict)
            # Load downloader
            downloader = BaseDownloader.get_instance(args.download)
            # Load builder
            builder = BaseBuilder.get_instance(args.build)

            # Run downloader
            LOG.info('Downloading...')
            downloader.run(work, **args_dict)

            # Run builder
            LOG.info('Building...')
            builder.run(work, **args_dict)
            LOG.info('Done successfully.')
        except Exception as e:
            # work.close
            raise e
        finally:
            # work.close
            pass
Example #4
0
def test_macros_are_replaced_correctly(macro_spec_path, prepared_macros):
    """Macros are correctly replaced."""

    replaced_macro_dict = dict.fromkeys(prepared_macros, 'REPLACED')

    with BaseBuilder.edit_spec_file(macro_spec_path) as (original, modified):
        content_stream = BaseBuilder.replace_macros(
            original,
            replaced_macro_dict,
        )
        modified.write(''.join(content_stream))

    with macro_spec_path.open() as spec_file:
        spec_contents = spec_file.read()

    result_macros = dict(MACRO_REGEX.findall(spec_contents))

    # All expected macros have their contents replaced
    assert all(val == 'REPLACED' for val in result_macros.values()), \
        result_macros  # show the macro values when debugging
Example #5
0
def test_run_exception():
    builder = BaseBuilder()
    builder.build = mock.Mock(side_effect=ValueError('test'))

    mock_work = mock.MagicMock()
    package_dicts = [
        {
            'name': 'a'
        },
        {
            'name': 'b'
        },
    ]
    num_names = [
        '1',
        '2',
    ]
    mock_work.each_package_dir.return_value = iter(
        zip(package_dicts, num_names))
    type(mock_work).working_dir = mock.PropertyMock(return_value='work_dir')

    called = False
    error_message = None
    try:
        builder.run(mock_work)
    except RuntimeError as e:
        called = True
        error_message = str(e)
    assert called
    expected_error_message = ("pacakge_dict: {'name': 'a'}, "
                              "num: 1, work_dir: work_dir")
    assert error_message == expected_error_message
    if sys.version_info >= (3, 6):
        builder.build.assert_called()
    # bulid should be called 3 times for retry setting.
    calls = [
        mock.call({'name': 'a'}),
        mock.call({'name': 'a'}),
        mock.call({'name': 'a'}),
    ]
    builder.build.assert_has_calls(calls)
Example #6
0
def test_run():
    builder = BaseBuilder()
    builder.build = mock.MagicMock(return_value=True)

    mock_work = mock.MagicMock()
    package_dicts = [
        {
            'name': 'a'
        },
        {
            'name': 'b'
        },
    ]
    num_names = [
        '1',
        '2',
    ]
    mock_work.each_package_dir.return_value = iter(
        zip(package_dicts, num_names))

    result = builder.run(mock_work)
    assert result
Example #7
0
def test_init_raises_error_without_pkg_cmd_option(valid_recipe_path):
    recipe = Recipe(str(valid_recipe_path), 'rh-ror50')
    work = Work(recipe)
    with pytest.raises(ValueError):
        BaseBuilder(work)
Example #8
0
    def edit_file(path, indicator='Test edit'):
        """Single edit round"""

        with BaseBuilder.edit_spec_file(str(path)) as (src, dst):
            print(indicator, file=dst)
            dst.write(src.read())
Example #9
0
def test_init():
    builder = BaseBuilder()
    assert builder