Example #1
0
    def __init__(self, source, image, **kwargs):
        """
        """
        LastLogger.__init__(self)
        BuilderStateMachine.__init__(self)

        print_version_of_tools()

        self.tasker = DockerTasker()

        info, version = self.tasker.get_info(), self.tasker.get_version()
        logger.debug(json.dumps(info, indent=2))
        logger.info(json.dumps(version, indent=2))

        # arguments for build
        self.source = source
        self.base_image_id = None
        self.image_id = None
        self.built_image_info = None
        self.image = ImageName.parse(image)

        # get info about base image from dockerfile
        self.df_path, self.df_dir = self.source.get_dockerfile_path()
        self.set_base_image(df_parser(self.df_path).baseimage)
        logger.debug("base image specified in dockerfile = '%s'",
                     self.base_image)
        if not self.base_image.tag:
            self.base_image.tag = 'latest'
def test_inspect_image():
    if MOCK:
        mock_docker()

    t = DockerTasker()
    inspect_data = t.inspect_image(input_image_name)
    assert isinstance(inspect_data, dict)
def test_hostdocker_error_build(source_params):
    if MOCK:
        mock_docker(wait_should_fail=True)

    image_name = TEST_IMAGE
    m = DockerhostBuildManager(
        "buildroot-dh-fedora",
        {
            "source": {
                "provider": "git",
                "uri": DOCKERFILE_GIT,
                "provider_params": {
                    "git_commit": "error-build"
                }
            },
            "image": image_name,
            "parent_registry": LOCALHOST_REGISTRY,  # faster
            "target_registries_insecure": True,
            "parent_registry_insecure": True,
        })
    results = m.build()
    dt = DockerTasker()
    assert len(results.build_logs) > 0
    assert results.return_code != 0
    dt.remove_container(results.container_id)
Example #4
0
def test_inspect_image():
    if MOCK:
        mock_docker()

    t = DockerTasker()
    inspect_data = t.inspect_image(input_image_name)
    assert isinstance(inspect_data, dict)
Example #5
0
    def __init__(self, source, image, **kwargs):
        """
        """
        LastLogger.__init__(self)
        BuilderStateMachine.__init__(self)

        print_version_of_tools()

        self.tasker = DockerTasker()

        info, version = self.tasker.get_info(), self.tasker.get_version()
        logger.debug(json.dumps(info, indent=2))
        logger.info(json.dumps(version, indent=2))

        # arguments for build
        self.source = source
        self.base_image = None
        self.image_id = None
        self.built_image_info = None
        self.image = ImageName.parse(image)

        # get info about base image from dockerfile
        build_file_path, build_file_dir = self.source.get_build_file_path()

        self.df_dir = build_file_dir
        self._df_path = None

        # If the Dockerfile will be entirely generated from the container.yaml
        # (in the Flatpak case, say), then a plugin needs to create the Dockerfile
        # and set the base image
        if build_file_path.endswith(DOCKERFILE_FILENAME):
            self.set_df_path(build_file_path)
Example #6
0
def test_get_image_info_by_id_nonexistent():
    if MOCK:
        mock_docker()

    t = DockerTasker()
    response = t.get_image_info_by_image_id("asd")
    assert response is None
Example #7
0
def test_get_image_info_by_name_tag_in_name():
    if MOCK:
        mock_docker()

    t = DockerTasker()
    response = t.get_image_info_by_image_name(input_image_name)
    assert len(response) == 1
Example #8
0
 def __init__(self):
     self.tasker = DockerTasker(retry_times=0)
     self.dockerfile_images = DockerfileImages(['Fedora:22'])
     self.image_id = 'image_id'
     self.image = 'image'
     self.df_path = 'df_path'
     self.df_dir = 'df_dir'
def test_get_image_info_by_name_tag_in_name():
    if MOCK:
        mock_docker()

    t = DockerTasker()
    response = t.get_image_info_by_image_name(input_image_name)
    assert len(response) == 1
Example #10
0
def test_get_version():
    if MOCK:
        mock_docker()

    t = DockerTasker()
    response = t.get_info()
    assert isinstance(response, dict)
Example #11
0
def test_image_doesnt_exist():
    image = "lerknglekrnglekrnglekrnglekrng"
    if MOCK:
        mock_docker(should_raise_error={'inspect_image': [image]})

    t = DockerTasker()
    assert t.image_exists(image) is False
def test_get_image_info_by_id_nonexistent():
    if MOCK:
        mock_docker()

    t = DockerTasker()
    response = t.get_image_info_by_image_id("asd")
    assert response is None
def test_get_image_info_by_name_tag_in_name_nonexisten(temp_image_name):
    if MOCK:
        mock_docker()

    t = DockerTasker()
    response = t.get_image_info_by_image_name(temp_image_name)
    assert len(response) == 0
def test_get_version():
    if MOCK:
        mock_docker()

    t = DockerTasker()
    response = t.get_info()
    assert isinstance(response, dict)
def test_image_doesnt_exist():
    image = "lerknglekrnglekrnglekrnglekrng"
    if MOCK:
        mock_docker(should_raise_error={'inspect_image': [image]})

    t = DockerTasker()
    assert t.image_exists(image) is False
def test_pull_base_image_plugin(df_base, parent_registry, expected_w_reg,
                                expected_wo_reg):
    if MOCK:
        mock_docker(remember_images=True)

    tasker = DockerTasker()
    workflow = DockerBuildWorkflow(MOCK_SOURCE, 'test-image')
    workflow.builder = MockBuilder()
    workflow.builder.base_image = ImageName.parse(df_base)

    assert not tasker.image_exists(BASE_IMAGE)
    assert not tasker.image_exists(BASE_IMAGE_W_REGISTRY)

    runner = PreBuildPluginsRunner(tasker, workflow,
                                   [{
                                       'name': PullBaseImagePlugin.key,
                                       'args': {
                                           'parent_registry': parent_registry,
                                           'parent_registry_insecure': True
                                       }
                                   }])

    runner.run()

    assert tasker.image_exists(BASE_IMAGE) == expected_wo_reg
    assert tasker.image_exists(BASE_IMAGE_W_REGISTRY) == expected_w_reg

    try:
        tasker.remove_image(BASE_IMAGE)
        tasker.remove_image(BASE_IMAGE_W_REGISTRY)
    except:
        pass
Example #17
0
def test_get_image_info_by_name_tag_in_name_nonexisten(temp_image_name):
    if MOCK:
        mock_docker()

    t = DockerTasker()
    response = t.get_image_info_by_image_name(temp_image_name)
    assert len(response) == 0
Example #18
0
def test_tag_by_labels_plugin(tmpdir, args):
    if MOCK:
        mock_docker()

    tasker = DockerTasker()
    workflow = DockerBuildWorkflow({
        "provider": "git",
        "uri": "asd"
    }, "test-image")
    version = "1.0"
    release = "1"
    workflow.built_image_inspect = {
        INSPECT_CONFIG: {
            "Labels": {
                "name": TEST_IMAGE,
                "version": version,
                "release": release
            }
        }
    }
    workflow.push_conf.add_docker_registry(LOCALHOST_REGISTRY, insecure=True)
    image = ImageName(repo=TEST_IMAGE,
                      tag="%s_%s" % (version, release),
                      registry=LOCALHOST_REGISTRY)

    setattr(workflow, 'builder', X)

    runner = PostBuildPluginsRunner(tasker, workflow,
                                    [{
                                        'name': TagByLabelsPlugin.key,
                                        'args': args,
                                    }])
    output = runner.run()
    assert TagByLabelsPlugin.key in output.keys()

    images = [i.to_str() for i in workflow.tag_conf.images]
    primary_images = [i.to_str() for i in workflow.tag_conf.primary_images]
    unique_images = [i.to_str() for i in workflow.tag_conf.unique_images]

    if args.get('unique_tag_only'):
        assert len(workflow.tag_conf.images) == 1
        assert len(primary_images) == 0

    else:
        assert len(workflow.tag_conf.images) == 4
        assert len(primary_images) == 3

        assert ("%s:%s-%s" % (TEST_IMAGE, version, release)) in images
        assert ("%s:%s" % (TEST_IMAGE, version)) in images
        assert ("%s:latest" % (TEST_IMAGE, )) in images

        assert ("%s:%s-%s" % (TEST_IMAGE, version, release)) in primary_images
        assert ("%s:%s" % (TEST_IMAGE, version)) in primary_images
        assert ("%s:latest" % (TEST_IMAGE, )) in primary_images

    assert len(unique_images) == 1
    assert ("%s:%s" % (TEST_IMAGE, "unique_tag_123")) in images
    assert ("%s:%s" % (TEST_IMAGE, "unique_tag_123")) in unique_images
    tasker.remove_image(image)
def test_tag_and_push_plugin(tmpdir, image_name, logs, should_raise,
                             use_secret):
    if MOCK:
        mock_docker()
        flexmock(docker.Client,
                 push=lambda iid, **kwargs: iter(logs),
                 login=lambda username, registry, dockercfg_path:
                 {'Status': 'Login Succeeded'})

    tasker = DockerTasker()
    workflow = DockerBuildWorkflow({
        "provider": "git",
        "uri": "asd"
    }, TEST_IMAGE)
    workflow.tag_conf.add_primary_image(image_name)
    setattr(workflow, 'builder', X)

    secret_path = None
    if use_secret:
        temp_dir = mkdtemp()
        with open(os.path.join(temp_dir, ".dockercfg"), "w+") as dockerconfig:
            dockerconfig_contents = {
                LOCALHOST_REGISTRY: {
                    "username": "******",
                    "email": "*****@*****.**",
                    "password": "******"
                }
            }
            dockerconfig.write(json.dumps(dockerconfig_contents))
            dockerconfig.flush()
            secret_path = temp_dir

    runner = PostBuildPluginsRunner(tasker, workflow,
                                    [{
                                        'name': TagAndPushPlugin.key,
                                        'args': {
                                            'registries': {
                                                LOCALHOST_REGISTRY: {
                                                    'insecure': True,
                                                    'secret': secret_path
                                                }
                                            }
                                        },
                                    }])

    if should_raise:
        with pytest.raises(Exception):
            runner.run()
    else:
        output = runner.run()
        image = output[TagAndPushPlugin.key][0]
        tasker.remove_image(image)
        assert len(workflow.push_conf.docker_registries) > 0

        if MOCK:
            # we only test this when mocking docker because we don't expect
            # running actual docker against v2 registry
            assert workflow.push_conf.docker_registries[0].digests[
                image_name] == DIGEST1
def test_tag_by_labels_plugin(tmpdir, args):
    if MOCK:
        mock_docker()

    tasker = DockerTasker()
    workflow = DockerBuildWorkflow({"provider": "git", "uri": "asd"}, "test-image")
    version = "1.0"
    release = "1"
    workflow.built_image_inspect = {
        INSPECT_CONFIG: {
            "Labels": {
                "name": TEST_IMAGE,
                "version": version,
                "release": release
            }
        }
    }
    workflow.push_conf.add_docker_registry(LOCALHOST_REGISTRY, insecure=True)
    image = ImageName(repo=TEST_IMAGE,
                      tag="%s_%s" % (version, release),
                      registry=LOCALHOST_REGISTRY)

    setattr(workflow, 'builder', X)

    runner = PostBuildPluginsRunner(
        tasker,
        workflow,
        [{
            'name': TagByLabelsPlugin.key,
            'args': args,
        }]
    )
    output = runner.run()
    assert TagByLabelsPlugin.key in output.keys()

    images = [i.to_str() for i in workflow.tag_conf.images]
    primary_images = [i.to_str() for i in workflow.tag_conf.primary_images]
    unique_images = [i.to_str() for i in workflow.tag_conf.unique_images]

    if args.get('unique_tag_only'):
        assert len(workflow.tag_conf.images) == 1
        assert len(primary_images) == 0

    else:
        assert len(workflow.tag_conf.images) == 4
        assert len(primary_images) == 3

        assert ("%s:%s-%s" % (TEST_IMAGE, version, release)) in images
        assert ("%s:%s" % (TEST_IMAGE, version)) in images
        assert ("%s:latest" % (TEST_IMAGE, )) in images

        assert ("%s:%s-%s" % (TEST_IMAGE, version, release)) in primary_images
        assert ("%s:%s" % (TEST_IMAGE, version)) in primary_images
        assert ("%s:latest" % (TEST_IMAGE, )) in primary_images

    assert len(unique_images) == 1
    assert ("%s:%s" % (TEST_IMAGE, "unique_tag_123")) in images
    assert ("%s:%s" % (TEST_IMAGE, "unique_tag_123")) in unique_images
    tasker.remove_image(image)
Example #21
0
 def __init__(self):
     self.tasker = DockerTasker(retry_times=0)
     self.base_image = ImageName(repo='Fedora', tag='22')
     self.base_from_scratch = False
     self.image_id = 'image_id'
     self.image = 'image'
     self.df_path = 'df_path'
     self.df_dir = 'df_dir'
def test_get_image_info_by_name_tag_in_name_library():
    if MOCK:
        mock_docker()

    t = DockerTasker()
    image_name = ImageName.parse("library/busybox")
    response = t.get_image_info_by_image_name(image_name)
    assert len(response) == 1
Example #23
0
def test_get_image_info_by_name_tag_in_name_library():
    if MOCK:
        mock_docker()

    t = DockerTasker()
    image_name = ImageName.parse("library/busybox")
    response = t.get_image_info_by_image_name(image_name)
    assert len(response) == 1
def test_get_image_info_by_id():
    if MOCK:
        mock_docker(provided_image_repotags=input_image_name.to_str())

    t = DockerTasker()
    image_id = t.get_image_info_by_image_name(input_image_name)[0]['Id']
    response = t.get_image_info_by_image_id(image_id)
    assert isinstance(response, dict)
Example #25
0
def test_get_image_info_by_id():
    if MOCK:
        mock_docker(provided_image_repotags=input_image_name.to_str())

    t = DockerTasker()
    image_id = t.get_image_info_by_image_name(input_image_name)[0]['Id']
    response = t.get_image_info_by_image_id(image_id)
    assert isinstance(response, dict)
def test_tag_image_same_name(temp_image_name):
    if MOCK:
        mock_docker()

    t = DockerTasker()
    temp_image_name.registry = "somewhere.example.com"
    temp_image_name.tag = "1"
    flexmock(t.d).should_receive('tag').never()
    img = t.tag_image(temp_image_name, temp_image_name.copy())
Example #27
0
def test_tag_image_same_name(temp_image_name):  # noqa
    if MOCK:
        mock_docker()

    t = DockerTasker()
    temp_image_name.registry = "somewhere.example.com"
    temp_image_name.tag = "1"
    flexmock(t.d).should_receive('tag').never()
    t.tag_image(temp_image_name, temp_image_name.copy())
def test_build_image_from_git(temp_image_name):
    if MOCK:
        mock_docker()

    t = DockerTasker()
    response = t.build_image_from_git(DOCKERFILE_GIT, temp_image_name, use_cache=True)
    list(response)
    assert response is not None
    assert t.image_exists(temp_image_name)
    t.remove_image(temp_image_name)
def test_pull_base_image_plugin(df_base, parent_registry, expected_w_reg, expected_wo_reg):
    if MOCK:
        mock_docker(remember_images=True)

    tasker = DockerTasker()
    workflow = DockerBuildWorkflow(MOCK_SOURCE, 'test-image')
    workflow.builder = MockBuilder()
    workflow.builder.base_image = ImageName.parse(df_base)

    assert not tasker.image_exists(BASE_IMAGE)
    assert not tasker.image_exists(BASE_IMAGE_W_REGISTRY)

    runner = PreBuildPluginsRunner(
        tasker,
        workflow,
        [{
            'name': PullBaseImagePlugin.key,
            'args': {'parent_registry': parent_registry, 'parent_registry_insecure': True}
        }]
    )

    runner.run()

    assert tasker.image_exists(BASE_IMAGE) == expected_wo_reg
    assert tasker.image_exists(BASE_IMAGE_W_REGISTRY) == expected_w_reg

    try:
        tasker.remove_image(BASE_IMAGE)
        tasker.remove_image(BASE_IMAGE_W_REGISTRY)
    except:
        pass
def test_tag_and_push(temp_image_name):
    if MOCK:
        mock_docker()

    t = DockerTasker()
    temp_image_name.registry = LOCALHOST_REGISTRY
    temp_image_name.tag = "1"
    output = t.tag_and_push_image(INPUT_IMAGE, temp_image_name, insecure=True)
    assert output is not None
    assert t.image_exists(temp_image_name)
    t.remove_image(temp_image_name)
Example #31
0
def test_image_creation_local_repo():
    if MOCK:
        mock_docker()

    b = BuildImageBuilder(reactor_local_path=PARENT_DIR)
    df_dir_path = os.path.join(PARENT_DIR, 'images', 'privileged-builder')
    b.create_image(df_dir_path, TEST_BUILD_IMAGE)

    dt = DockerTasker()
    assert dt.image_exists(TEST_BUILD_IMAGE)
    dt.remove_image(TEST_BUILD_IMAGE)
def test_image_creation_local_repo():
    if MOCK:
        mock_docker()

    b = BuildImageBuilder(reactor_local_path=PARENT_DIR)
    df_dir_path = os.path.join(PARENT_DIR, 'images', 'privileged-builder')
    b.create_image(df_dir_path, TEST_BUILD_IMAGE)

    dt = DockerTasker()
    assert dt.image_exists(TEST_BUILD_IMAGE)
    dt.remove_image(TEST_BUILD_IMAGE)
Example #33
0
    def __init__(self, build_image, build_args):
        BuilderStateMachine.__init__(self)
        self.build_image = build_image
        self.build_args = build_args
        self.image = build_args['image']
        self.uri = build_args['source']['uri']

        self.temp_dir = None
        # build image after build
        self.buildroot_image_id = None
        self.buildroot_image_name = None
        self.dt = DockerTasker()
def test_tag_and_push_plugin(tmpdir, image_name, logs, should_raise, use_secret):
    if MOCK:
        mock_docker()
        flexmock(docker.Client, push=lambda iid, **kwargs: iter(logs),
                 login=lambda username, registry, dockercfg_path: {'Status': 'Login Succeeded'})

    tasker = DockerTasker()
    workflow = DockerBuildWorkflow({"provider": "git", "uri": "asd"}, TEST_IMAGE)
    workflow.tag_conf.add_primary_image(image_name)
    setattr(workflow, 'builder', X)

    secret_path = None
    if use_secret:
        temp_dir = mkdtemp()
        with open(os.path.join(temp_dir, ".dockercfg"), "w+") as dockerconfig:
            dockerconfig_contents = {
                LOCALHOST_REGISTRY: {
                    "username": "******", "email": "*****@*****.**", "password": "******"}}
            dockerconfig.write(json.dumps(dockerconfig_contents))
            dockerconfig.flush()
            secret_path = temp_dir


    runner = PostBuildPluginsRunner(
        tasker,
        workflow,
        [{
            'name': TagAndPushPlugin.key,
            'args': {
                'registries': {
                    LOCALHOST_REGISTRY: {
                        'insecure': True,
                        'secret': secret_path
                    }
                }
            },
        }]
    )

    if should_raise:
        with pytest.raises(Exception):
            runner.run()
    else:
        output = runner.run()
        image = output[TagAndPushPlugin.key][0]
        tasker.remove_image(image)
        assert len(workflow.push_conf.docker_registries) > 0

        if MOCK:
            # we only test this when mocking docker because we don't expect
            # running actual docker against v2 registry
            assert workflow.push_conf.docker_registries[0].digests[image_name] == DIGEST1
def test_commit_container(temp_image_name):
    if MOCK:
        mock_docker()

    t = DockerTasker()
    container_id = t.run(INPUT_IMAGE, command="id")
    t.wait(container_id)
    image_id = t.commit_container(container_id, message="test message", image=temp_image_name)
    try:
        assert t.image_exists(image_id)
    finally:
        t.remove_container(container_id)
        t.remove_image(image_id)
def test_remove_image(temp_image_name):
    if MOCK:
        mock_docker(inspect_should_fail=True)

    t = DockerTasker()
    container_id = t.run(input_image_name, command="id")
    t.wait(container_id)
    image_id = t.commit_container(container_id, image=temp_image_name)
    try:
        t.remove_container(container_id)
    finally:
        t.remove_image(image_id)
    assert not t.image_exists(temp_image_name)
def test_tag_image(temp_image_name):
    if MOCK:
        mock_docker()

    t = DockerTasker()
    temp_image_name.registry = "somewhere.example.com"
    temp_image_name.tag = "1"
    img = t.tag_image(INPUT_IMAGE, temp_image_name)
    try:
        assert t.image_exists(temp_image_name)
        assert img == temp_image_name.to_str()
    finally:
        t.remove_image(temp_image_name)
Example #38
0
def test_build_image(tmpdir, source_params):
    provided_image = "test-build:test_tag"
    if MOCK:
        mock_docker(provided_image_repotags=provided_image)

    source_params.update({'tmpdir': str(tmpdir)})
    s = get_source_instance_for(source_params)
    t = DockerTasker()
    b = InsideBuilder(s, provided_image)
    build_result = b.build()
    assert t.inspect_image(build_result.image_id)
    # clean
    t.remove_image(build_result.image_id)
Example #39
0
def test_build_image(tmpdir, source_params):
    provided_image = "test-build:test_tag"
    if MOCK:
        mock_docker(provided_image_repotags=provided_image)

    source_params.update({'tmpdir': str(tmpdir)})
    s = get_source_instance_for(source_params)
    t = DockerTasker()
    b = InsideBuilder(s, provided_image)
    build_result = b.build()
    assert t.inspect_image(build_result.image_id)
    # clean
    t.remove_image(build_result.image_id)
def test_run_invalid_command():
    if MOCK:
        mock_docker(should_raise_error={'start': []})

    t = DockerTasker()
    try:
        with pytest.raises(docker.errors.APIError):
            t.run(input_image_name, command=COMMAND)
    finally:
        # remove the container
        containers = t.d.containers(all=True)
        container_id = [c for c in containers if c["Command"] == COMMAND][0]['Id']
        t.remove_container(container_id)
Example #41
0
 def __init__(self, reactor_tarball_path=None, reactor_local_path=None,
              reactor_remote_path=None, use_official_reactor_git=False):
     self.tasker = DockerTasker()
     self.reactor_tarball_path = reactor_tarball_path
     self.reactor_local_path = reactor_local_path
     self.reactor_remote_path = reactor_remote_path
     self.use_official_reactor_git = use_official_reactor_git
     if not self.reactor_tarball_path and \
        not self.reactor_local_path and \
        not self.reactor_remote_path and \
        not self.use_official_reactor_git:
         logger.error("no atomic_reactor source specified, can't proceed")
         raise RuntimeError("You have to specify atomic_reactor source: either local gitrepo, "
                            "path to atomic_reactor tarball, or use upstream git repo.")
def test_build_image_from_path(tmpdir, temp_image_name):
    if MOCK:
        mock_docker()

    tmpdir_path = str(tmpdir.realpath())
    clone_git_repo(DOCKERFILE_GIT, tmpdir_path)
    df = tmpdir.join("Dockerfile")
    assert df.check()
    t = DockerTasker()
    response = t.build_image_from_path(tmpdir_path, temp_image_name, use_cache=True)
    list(response)
    assert response is not None
    assert t.image_exists(temp_image_name)
    t.remove_image(temp_image_name)
def test_privileged_build(caplog, source_params):
    if MOCK:
        mock_docker()

    image_name = ImageName(repo=TEST_IMAGE)
    remote_image = image_name.copy()
    remote_image.registry = LOCALHOST_REGISTRY
    m = PrivilegedBuildManager(
        "buildroot-fedora",
        {
            "source": source_params,
            "image": remote_image.to_str(),
            "parent_registry": LOCALHOST_REGISTRY,  # faster
            "target_registries_insecure": True,
            "parent_registry_insecure": True,
        })
    results = m.build()
    dt = DockerTasker()
    dt.pull_image(remote_image, insecure=True)

    if source_params['provider'] == 'path':
        assert_source_from_path_mounted_ok(caplog, m.temp_dir)

    assert len(results.build_logs) > 0
    # assert isinstance(results.built_img_inspect, dict)
    # assert len(results.built_img_inspect.items()) > 0
    # assert isinstance(results.built_img_info, dict)
    # assert len(results.built_img_info.items()) > 0
    # assert isinstance(results.base_img_info, dict)
    # assert len(results.base_img_info.items()) > 0
    # assert len(results.base_plugins_output) > 0
    # assert len(results.built_img_plugins_output) > 0
    dt.remove_container(results.container_id)
    dt.remove_image(remote_image)
Example #44
0
def test_login(tmpdir, dockerconfig_contents, should_raise):
    if MOCK:
        mock_docker()
        fake_api = flexmock(docker.APIClient,
                            login=lambda username, registry, dockercfg_path:
                            {'Status': 'Login Succeeded'})

    tmpdir_path = str(tmpdir.realpath())
    file_name = '.dockercfg'
    dockercfg_path = os.path.join(tmpdir_path, file_name)
    with open(dockercfg_path, "w+") as dockerconfig:
        dockerconfig.write(json.dumps(dockerconfig_contents))
        dockerconfig.flush()
    t = DockerTasker()
    if should_raise:
        if 'auth' in dockerconfig_contents[LOCALHOST_REGISTRY]:
            with pytest.raises(ValueError) as exc:
                t.login(LOCALHOST_REGISTRY, tmpdir_path)
                assert "Failed to parse 'auth'" in str(exc)
        else:
            with pytest.raises(RuntimeError) as exc:
                t.login(LOCALHOST_REGISTRY, tmpdir_path)
                assert "Failed to extract a username" in str(exc)
    else:
        if MOCK:
            (fake_api.should_receive('login').with_args(
                username='******',
                registry=LOCALHOST_REGISTRY,
                dockercfg_path=dockercfg_path).once().and_return(
                    {'Status': 'Login Succeeded'}))
        t.login(LOCALHOST_REGISTRY, tmpdir_path)
Example #45
0
def test_retry_generator(exc, in_init, retry_times):
    def simplegen():
        yield "log line"

    my_args = ('some', 'new')
    if not in_init:
        my_kwargs = {
            'one': 'first',
            'two': 'second',
            'retry_times': retry_times
        }
    else:
        my_kwargs = {'one': 'first', 'two': 'second'}

    if in_init:
        t = DockerTasker(retry_times=retry_times)
    else:
        t = DockerTasker()

    (flexmock(time).should_receive('sleep').and_return(None))

    if not exc:
        cr = CommandResult()
        cr._error = "cmd_error"
        cr._error_detail = {"message": "error_detail"}

    if exc == APIError:
        error_message = 'api_error'
        response = flexmock(content=error_message, status_code=408)
        (flexmock(atomic_reactor.util).should_receive(
            'wait_for_command').times(retry_times + 1).and_raise(
                APIError, error_message, response))
    elif exc == ProtocolError:
        error_message = 'protocol_error'
        (flexmock(atomic_reactor.util).should_receive(
            'wait_for_command').times(retry_times + 1).and_raise(
                ProtocolError, error_message))
    elif exc == ReadTimeoutError:
        pool = 'pool'
        message = 'read_timeout_error'
        error_message = '{}: {}'.format(pool, message)
        (flexmock(atomic_reactor.util).should_receive(
            'wait_for_command').times(retry_times + 1).and_raise(
                ReadTimeoutError, pool, 'url', message))
    else:
        (flexmock(atomic_reactor.util).should_receive(
            'wait_for_command').times(retry_times + 1).and_return(cr))
        error_message = 'cmd_error'

    if retry_times >= 0:
        with pytest.raises(RetryGeneratorException) as ex:
            t.retry_generator(lambda *args, **kwargs: simplegen(), *my_args,
                              **my_kwargs)

        assert repr(error_message) in repr(ex.value)
    else:
        t.retry_generator(lambda *args, **kwargs: simplegen(), *my_args,
                          **my_kwargs)
Example #46
0
    def __init__(self, source, image, **kwargs):
        """
        """
        LastLogger.__init__(self)
        BuilderStateMachine.__init__(self)

        print_version_of_tools()

        self.tasker = DockerTasker()

        info, version = self.tasker.get_info(), self.tasker.get_version()
        logger.debug(json.dumps(info, indent=2))
        logger.info(json.dumps(version, indent=2))

        # arguments for build
        self.source = source
        self.base_image_id = None
        self.image_id = None
        self.built_image_info = None
        self.image = ImageName.parse(image)

        # get info about base image from dockerfile
        self.df_path, self.df_dir = self.source.get_dockerfile_path()
        self.base_image = ImageName.parse(DockerfileParser(self.df_path).baseimage)
        logger.debug("base image specified in dockerfile = '%s'", self.base_image)
        if not self.base_image.tag:
            self.base_image.tag = 'latest'
Example #47
0
def test_rpmqa_plugin(remove_container_error, ignore_autogenerated):
    if MOCK:
        should_raise_error = {}
        if remove_container_error:
            should_raise_error['remove_container'] = None
        mock_docker(should_raise_error=should_raise_error)

    tasker = DockerTasker()
    workflow = DockerBuildWorkflow(SOURCE, "test-image")
    setattr(workflow, 'builder', X())
    setattr(workflow.builder, 'image_id', "asd123")
    setattr(workflow.builder, 'base_image', ImageName(repo='fedora', tag='21'))
    setattr(workflow.builder, "source", X())
    setattr(workflow.builder.source, 'dockerfile_path', "/non/existent")
    setattr(workflow.builder.source, 'path', "/non/existent")

    flexmock(docker.Client, logs=mock_logs)
    runner = PostBuildPluginsRunner(
        tasker, workflow, [{
            "name": PostBuildRPMqaPlugin.key,
            "args": {
                'image_id': TEST_IMAGE,
                "ignore_autogenerated_gpg_keys": ignore_autogenerated["ignore"]
            }
        }])
    results = runner.run()
    assert results[
        PostBuildRPMqaPlugin.key] == ignore_autogenerated["package_list"]
Example #48
0
    def test_compress(self, tmpdir, method, load_exported_image, extension):
        if MOCK:
            mock_docker()

        tasker = DockerTasker()
        workflow = DockerBuildWorkflow({
            'provider': 'git',
            'uri': 'asd'
        }, 'test-image')
        workflow.builder = X()
        exp_img = os.path.join(str(tmpdir), 'img.tar')

        if load_exported_image:
            tarfile.open(exp_img, mode='w').close()
            workflow.exported_image_sequence.append({'path': exp_img})

        runner = PostBuildPluginsRunner(
            tasker, workflow, [{
                'name': CompressPlugin.key,
                'args': {
                    'method': method,
                    'load_exported_image': load_exported_image,
                },
            }])

        runner.run()

        compressed_img = os.path.join(
            workflow.source.tmpdir,
            EXPORTED_COMPRESSED_IMAGE_NAME_TEMPLATE.format(extension))
        assert os.path.exists(compressed_img)
        assert workflow.exported_image_sequence[-1]['path'] == compressed_img
Example #49
0
def prepare(key, value, set_labels_args=None, set_labels_kwargs=None):
    if MOCK:
        mock_docker()
    tasker = DockerTasker()
    workflow = DockerBuildWorkflow(SOURCE, "test-image")
    setattr(workflow, 'builder', X())
    setattr(workflow.builder, 'image_id', 'asd123')
    setattr(workflow.builder, 'base_image', ImageName(repo='Fedora', tag='22'))
    setattr(workflow.builder, 'source', X())
    setattr(workflow.builder.source, 'path', '/tmp')
    setattr(workflow.builder.source, 'dockerfile_path', None)
    expectation = flexmock(OSBS).should_receive('set_labels_on_build_config')
    if set_labels_args is not None:
        if set_labels_kwargs is None:
            set_labels_kwargs = {}

        expectation.with_args(*set_labels_args, **set_labels_kwargs)

    runner = PreBuildPluginsRunner(tasker, workflow,
                                   [{
                                       'name': CheckAndSetRebuildPlugin.key,
                                       'args': {
                                           'label_key': key,
                                           'label_value': value,
                                           'url': '',
                                       },
                                   }])
    return workflow, runner
def prepare(insecure_registry=None):
    """
    Boiler-plate test set-up
    """
    if MOCK:
        mock_docker()
    tasker = DockerTasker()
    workflow = DockerBuildWorkflow(SOURCE, "test-image")
    setattr(workflow, 'builder', X())
    setattr(workflow.builder, 'image_id', 'asd123')
    setattr(workflow.builder, 'source', X())
    setattr(workflow.builder.source, 'dockerfile_path', None)
    setattr(workflow.builder.source, 'path', None)
    fake_conf = osbs.conf.Configuration(conf_file=None, openshift_uri='/')
    flexmock(osbs.conf).should_receive('Configuration').and_return(fake_conf)

    runner = PostBuildPluginsRunner(
        tasker, workflow, [{
            'name': ImportImagePlugin.key,
            'args': {
                'imagestream': TEST_IMAGESTREAM,
                'docker_image_repo': TEST_REPO,
                'url': '',
                'build_json_dir': "",
                'verify_ssl': False,
                'use_auth': False,
                'insecure_registry': insecure_registry,
            }
        }])

    return runner
def test_pull_raises_retry_error(workflow, caplog):
    if MOCK:
        mock_docker(remember_images=True)

    tasker = DockerTasker(retry_times=1)
    workflow.builder = MockBuilder()
    image_name = ImageName.parse(IMAGE_RAISE_RETRYGENERATOREXCEPTION)
    base_image_str = "{}/{}:{}".format(SOURCE_REGISTRY, image_name.repo,
                                       'some')
    source_registry = image_name.registry
    workflow.builder.dockerfile_images = DockerfileImages([base_image_str])
    workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
    workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
        ReactorConfig({'version': 1,
                       'source_registry': {'url': source_registry,
                                           'insecure': True}})

    runner = PreBuildPluginsRunner(
        tasker,
        workflow,
        [{
            'name': PullBaseImagePlugin.key,
            'args': {},
        }],
    )

    with pytest.raises(Exception):
        runner.run()

    exp_img = ImageName.parse(base_image_str)
    exp_img.registry = source_registry
    assert 'failed to pull image: {}'.format(exp_img.to_str()) in caplog.text
 def __init__(self):
     self.tasker = DockerTasker()
     self.base_image = ImageName(repo='Fedora', tag='22')
     self.image_id = 'image_id'
     self.image = 'image'
     self.df_path = 'df_path'
     self.df_dir = 'df_dir'
Example #53
0
    def __init__(self, source, image, **kwargs):
        """
        """
        LastLogger.__init__(self)
        BuilderStateMachine.__init__(self)

        print_version_of_tools()

        self.tasker = DockerTasker()

        info, version = self.tasker.get_info(), self.tasker.get_version()
        logger.debug(json.dumps(info, indent=2))
        logger.info(json.dumps(version, indent=2))

        # arguments for build
        self.source = source
        self.base_image = None
        self.image_id = None
        self.built_image_info = None
        self.image = ImageName.parse(image)

        # get info about base image from dockerfile
        build_file_path, build_file_dir = self.source.get_build_file_path()

        self.df_dir = build_file_dir
        self._df_path = None

        # If the build file isn't a Dockerfile, but say, a flatpak.json then a
        # plugin needs to create the Dockerfile and set the base image
        if build_file_path.endswith(DOCKERFILE_FILENAME):
            self.set_df_path(build_file_path)
 def __init__(self):
     self.tasker = DockerTasker(retry_times=0)
     self.base_image = ImageName(repo='Fedora', tag='22')
     self.base_from_scratch = False
     self.image_id = 'image_id'
     self.image = 'image'
     self.df_path = 'df_path'
     self.df_dir = 'df_dir'
def test_login(tmpdir, dockerconfig_contents, should_raise):
    if MOCK:
        mock_docker()
        fake_api = flexmock(docker.APIClient, login=lambda username, registry,
                            dockercfg_path: {'Status': 'Login Succeeded'})

    tmpdir_path = str(tmpdir.realpath())
    file_name = '.dockercfg'
    dockercfg_path = os.path.join(tmpdir_path, file_name)
    with open(dockercfg_path, "w+") as dockerconfig:
        dockerconfig.write(json.dumps(dockerconfig_contents))
        dockerconfig.flush()
    t = DockerTasker()
    if should_raise:
        if 'auth' in dockerconfig_contents[LOCALHOST_REGISTRY]:
            with pytest.raises(ValueError) as exc:
                t.login(LOCALHOST_REGISTRY, tmpdir_path)
                assert "Failed to parse 'auth'" in str(exc)
        else:
            with pytest.raises(RuntimeError) as exc:
                t.login(LOCALHOST_REGISTRY, tmpdir_path)
                assert "Failed to extract a username" in str(exc)
    else:
        if MOCK:
            (fake_api
             .should_receive('login')
             .with_args(username='******', registry=LOCALHOST_REGISTRY, dockercfg_path=dockercfg_path)
             .once().and_return({'Status': 'Login Succeeded'}))
        t.login(LOCALHOST_REGISTRY, tmpdir_path)
def test_privileged_build(caplog, source_params):
    if MOCK:
        mock_docker()

    image_name = ImageName(repo=TEST_IMAGE)
    remote_image = image_name.copy()
    remote_image.registry = LOCALHOST_REGISTRY
    m = PrivilegedBuildManager(
        "buildroot-fedora",
        {
            "source": source_params,
            "image": remote_image.to_str(),
            "parent_registry": LOCALHOST_REGISTRY,  # faster
            "target_registries_insecure": True,
            "parent_registry_insecure": True,
        },
    )
    results = m.build()
    dt = DockerTasker()
    dt.pull_image(remote_image, insecure=True)

    if source_params["provider"] == "path":
        assert_source_from_path_mounted_ok(caplog, m.temp_dir)

    assert len(results.build_logs) > 0
    # assert isinstance(results.built_img_inspect, dict)
    # assert len(results.built_img_inspect.items()) > 0
    # assert isinstance(results.built_img_info, dict)
    # assert len(results.built_img_info.items()) > 0
    # assert isinstance(results.base_img_info, dict)
    # assert len(results.base_img_info.items()) > 0
    # assert len(results.base_plugins_output) > 0
    # assert len(results.built_img_plugins_output) > 0
    dt.remove_container(results.container_id)
    dt.remove_image(remote_image)
Example #57
0
def test_hostdocker_build(caplog, source_params):
    if MOCK:
        mock_docker()

    image_name = ImageName(repo="atomic-reactor-test-ssh-image")
    remote_image = image_name.copy()
    remote_image.registry = LOCALHOST_REGISTRY
    m = DockerhostBuildManager("buildroot-dh-fedora", {
        "source": source_params,
        "image": remote_image.to_str(),
        "parent_registry": LOCALHOST_REGISTRY,  # faster
        "target_registries_insecure": True,
        "parent_registry_insecure": True,
    })
    results = m.build()
    dt = DockerTasker()
    dt.pull_image(remote_image, insecure=True)

    if source_params['provider'] == 'path':
        assert_source_from_path_mounted_ok(caplog, m.temp_dir)

    assert len(results.build_logs) > 0
    #assert re.search(r'build json mounted in container .+"uri": %s' %
    #        os.path.join(dconstants.CONTAINER_SHARE_PATH, 'source'))
    # assert isinstance(results.built_img_inspect, dict)
    # assert len(results.built_img_inspect.items()) > 0
    # assert isinstance(results.built_img_info, dict)
    # assert len(results.built_img_info.items()) > 0
    # assert isinstance(results.base_img_info, dict)
    # assert len(results.base_img_info.items()) > 0
    # assert len(results.base_plugins_output) > 0
    # assert len(results.built_img_plugins_output) > 0
    dt.remove_container(results.container_id)
    dt.remove_image(remote_image)
Example #58
0
def test_pull_base_image(tmpdir, source_params):
    if MOCK:
        mock_docker()

    source_params.update({"tmpdir": str(tmpdir)})
    s = get_source_instance_for(source_params)
    t = DockerTasker()
    b = InsideBuilder(s, "")
    pulled_tags = b.pull_base_image(LOCALHOST_REGISTRY, insecure=True)
    assert isinstance(pulled_tags, set)
    assert len(pulled_tags) == 2
    for reg_img_name in pulled_tags:
        reg_img_name = ImageName.parse(reg_img_name)
        assert t.inspect_image(reg_img_name) is not None
        assert reg_img_name.repo == git_base_image.repo
        assert reg_img_name.tag == git_base_image.tag
    # clean
    t.remove_image(git_base_image)
def test_tag_and_push_plugin(tmpdir):
    if MOCK:
        mock_docker()

    tasker = DockerTasker()
    workflow = DockerBuildWorkflow({"provider": "git", "uri": "asd"}, TEST_IMAGE)
    workflow.tag_conf.add_primary_image(TEST_IMAGE)
    workflow.push_conf.add_docker_registry(LOCALHOST_REGISTRY, insecure=True)
    setattr(workflow, 'builder', X)

    runner = PostBuildPluginsRunner(
        tasker,
        workflow,
        [{
            'name': TagAndPushPlugin.key,
        }]
    )
    output = runner.run()
    image = output[TagAndPushPlugin.key][0]
    tasker.remove_image(image)
def test_hostdocker_error_build(source_params):
    if MOCK:
        mock_docker(wait_should_fail=True)

    image_name = TEST_IMAGE
    m = DockerhostBuildManager(
        "buildroot-dh-fedora",
        {
            "source": {"provider": "git", "uri": DOCKERFILE_GIT, "provider_params": {"git_commit": "error-build"}},
            "image": image_name,
            "parent_registry": LOCALHOST_REGISTRY,  # faster
            "target_registries_insecure": True,
            "parent_registry_insecure": True,
        },
    )
    results = m.build()
    dt = DockerTasker()
    assert len(results.build_logs) > 0
    assert results.return_code != 0
    dt.remove_container(results.container_id)