def test_parent_images_to_str(tmpdir, caplog):
    if MOCK:
        mock_docker()

    source = {
        'provider': 'path',
        'uri': 'file://' + DOCKERFILE_OK_PATH,
        'tmpdir': str(tmpdir)
    }
    b = InsideBuilder(get_source_instance_for(source), 'built-img')
    b.dockerfile_images = DockerfileImages(['fedora:latest', 'bacon'])
    b.dockerfile_images['fedora:latest'] = "spam"
    expected_results = {"fedora:latest": "spam:latest"}
    assert b.parent_images_to_str() == expected_results
    assert "None in: base bacon:latest has parent None" in caplog.text
def test_base_image_inspect(tmpdir, source_params, parents_pulled, insecure,
                            base_exist):
    if MOCK:
        mock_docker()

    source_params.update({'tmpdir': str(tmpdir)})
    s = get_source_instance_for(source_params)
    b = InsideBuilder(s, '')
    b.tasker.build_method = default_build_method
    b.parents_pulled = parents_pulled
    if b.dockerfile_images.base_from_scratch:
        base_exist = True
    registry_name = "registry.example.com"

    original_parents = b.dockerfile_images.original_parents
    new_parents = []
    for parent in original_parents:
        if parent == 'scratch':
            new_parents.append(parent)
        else:
            mod_parent = ImageName.parse(parent)
            mod_parent.registry = registry_name
            new_parents.append(mod_parent.to_str())

    b.dockerfile_images = DockerfileImages(new_parents)
    b.pull_registries = {
        registry_name: {
            'insecure': insecure,
            'dockercfg_path': str(tmpdir)
        }
    }

    if base_exist:
        if b.dockerfile_images.base_from_scratch:
            built_inspect = b.base_image_inspect
            assert built_inspect == {}
        else:
            if not parents_pulled:
                (flexmock(atomic_reactor.util).should_receive(
                    'get_inspect_for_image').with_args(
                        b.dockerfile_images.base_image,
                        b.dockerfile_images.base_image.registry, insecure,
                        str(tmpdir)).and_return({'Id': 123}))

            built_inspect = b.base_image_inspect

            assert built_inspect is not None
            assert built_inspect["Id"] is not None
    else:
        if parents_pulled or b.dockerfile_images.custom_base_image:
            response = flexmock(content="not found", status_code=404)
            (flexmock(
                docker.APIClient).should_receive('inspect_image').and_raise(
                    docker.errors.NotFound, "xyz", response))
            with pytest.raises(KeyError):
                b.base_image_inspect  # pylint: disable=pointless-statement; is a property
        else:
            (flexmock(atomic_reactor.util).should_receive(
                'get_inspect_for_image').and_raise(NotImplementedError))
            with pytest.raises(NotImplementedError):
                b.base_image_inspect  # pylint: disable=pointless-statement; is a property