def __init__(self,
                 tasker,
                 workflow,
                 pulp_registry_name,
                 pulp_secret_path=None,
                 username=None,
                 password=None,
                 dockpulp_loglevel=None):
        """
        constructor

        :param tasker: DockerTasker instance
        :param workflow: DockerBuildWorkflow instance
        :param pulp_registry_name: str, name of pulp registry to use, specified in /etc/
                                   dockpulp.conf
        :param pulp_secret_path: path to pulp.cer and pulp.key; $SOURCE_SECRET_PATH otherwise
        :param username: pulp username, used in preference to certificate and key
        :param password: pulp password, used in preference to certificate and key
        """
        # call parent constructor
        super(PulpPublishPlugin, self).__init__(tasker, workflow)
        self.workflow = workflow
        self.pulp_handler = PulpHandler(self.workflow,
                                        pulp_registry_name,
                                        self.log,
                                        pulp_secret_path=pulp_secret_path,
                                        username=username,
                                        password=password,
                                        dockpulp_loglevel=dockpulp_loglevel)
Exemplo n.º 2
0
def test_upload(unsupported, caplog):
    log = logging.getLogger("tests.test_pulp_util")
    pulp_registry_name = 'registry.example.com'
    testfile = 'foo'
    upload_file = 'test_file'
    repo_id = 'redhat-myproject-hello-world'

    _, workflow = prepare(testfile)
    image_names = [ImageName(repo="myproject-hello-world")]
    handler = PulpHandler(workflow, pulp_registry_name, log)
    handler.create_dockpulp_and_repos(image_names)

    if unsupported:
        (flexmock(dockpulp.Pulp).should_receive('upload').with_args(
            upload_file, repo_id).and_raise(TypeError).once().ordered())
        (flexmock(dockpulp.Pulp).should_receive('upload').with_args(
            upload_file).and_return(True).once().ordered())
    else:
        (flexmock(dockpulp.Pulp).should_receive('upload').with_args(
            upload_file, repo_id).and_return(False).once())

    handler.upload(upload_file, repo_id)

    assert "Uploading %s to %s" % (upload_file, repo_id) in caplog.text()

    if unsupported:
        assert "Falling back to uploading %s to redhat-everything repo" %\
               upload_file in caplog.text()
Exemplo n.º 3
0
def test_get_pulp_instance():
    log = logging.getLogger("tests.test_pulp_util")
    pulp_registry_name = 'registry.example.com'
    testfile = 'foo'

    _, workflow = prepare(testfile)
    handler = PulpHandler(workflow, pulp_registry_name, log)
    assert handler.get_pulp_instance() == pulp_registry_name
    def __init__(self,
                 tasker,
                 workflow,
                 pulp_registry_name,
                 load_squashed_image=None,
                 load_exported_image=None,
                 image_names=None,
                 pulp_secret_path=None,
                 username=None,
                 password=None,
                 dockpulp_loglevel=None,
                 publish=True):
        """
        constructor

        :param tasker: DockerTasker instance
        :param workflow: DockerBuildWorkflow instance
        :param pulp_registry_name: str, name of pulp registry to use, specified in /etc/
                                   dockpulp.conf
        :param load_squashed_image: obsolete name for load_exported_image, please don't use
        :param load_exported_image: bool, use exported tar instead of image from Docker
        :param image_names: list of additional image names
        :param pulp_secret_path: path to pulp.cer and pulp.key; $SOURCE_SECRET_PATH otherwise
        :param username: pulp username, used in preference to certificate and key
        :param password: pulp password, used in preference to certificate and key
        :param publish: Bool, whether to publish to crane or not
        """
        # call parent constructor
        super(PulpPushPlugin, self).__init__(tasker, workflow)
        self.pulp_registry_name = pulp_registry_name
        self.image_names = image_names
        if load_squashed_image is not None and load_exported_image is not None and \
                (load_squashed_image != load_exported_image):
            raise RuntimeError("Can\'t use load_squashed_image and "
                               "load_exported_image with different values")
        if load_squashed_image is not None:
            self.log.warning(
                'load_squashed_image argument is obsolete and will be '
                'removed in a future version; please use load_exported_image instead'
            )
        self.load_exported_image = load_exported_image or load_squashed_image or False
        self.pulp_secret_path = pulp_secret_path
        self.username = username
        self.password = password

        self.publish = publish and not are_plugins_in_order(
            self.workflow.postbuild_plugins_conf, self.key,
            PLUGIN_PULP_SYNC_KEY)

        self.dockpulp_loglevel = dockpulp_loglevel
        self.pulp_handler = PulpHandler(
            self.workflow,
            self.pulp_registry_name,
            self.log,
            pulp_secret_path=self.pulp_secret_path,
            username=self.username,
            password=self.password,
            dockpulp_loglevel=self.dockpulp_loglevel)
Exemplo n.º 5
0
def test_get_registry_hostname():
    log = logging.getLogger("tests.test_pulp_util")
    pulp_registry_name = 'registry.example.com'
    testfile = 'foo'

    _, workflow = prepare(testfile)
    handler = PulpHandler(workflow, pulp_registry_name, log)
    handler.create_dockpulp_and_repos([])
    assert handler.get_registry_hostname() == pulp_registry_name
Exemplo n.º 6
0
def test_get_tar_metadata():
    log = logging.getLogger("tests.test_pulp_util")
    pulp_registry_name = 'registry.example.com'
    testfile = 'foo'

    tasker, workflow = prepare(testfile)
    handler = PulpHandler(workflow, pulp_registry_name, log)

    expected = ("foo", ["foo"])
    assert handler.get_tar_metadata(testfile) == expected
Exemplo n.º 7
0
def get_pulp_session(workflow, logger, fallback):
    config = get_pulp(workflow, fallback)

    from atomic_reactor.pulp_util import PulpHandler
    return PulpHandler(workflow,
                       config['name'],
                       logger,
                       pulp_secret_path=config['auth'].get('ssl_certs_dir'),
                       username=config['auth'].get('username'),
                       password=config['auth'].get('password'),
                       dockpulp_loglevel=config.get('loglevel'))
Exemplo n.º 8
0
    def set_v1_tags(self, v1_image_id):
        image_names = self.workflow.tag_conf.images[:]
        handler = PulpHandler(self.workflow, self.pulp_registry_name, self.log,
                              pulp_secret_path=self.pulp_secret_path, username=self.username,
                              password=self.password, dockpulp_loglevel=self.dockpulp_loglevel)

        pulp_repos = handler.create_dockpulp_and_repos(image_names)
        repo_tags = {}
        for repo_id, pulp_repo in pulp_repos.items():
            repo_tags[repo_id] = {"tag": "%s:%s" % (",".join(pulp_repo.tags), v1_image_id)}
            handler.update_repo(repo_id, repo_tags[repo_id])
        return repo_tags
Exemplo n.º 9
0
def test_check_file(tmpdir, check_repo_retval, should_raise):
    log = logging.getLogger("tests.test_pulp_util")
    pulp_registry_name = 'registry.example.com'
    testfile = 'foo'

    _, workflow = prepare(testfile, check_repo_retval)
    handler = PulpHandler(workflow, pulp_registry_name, log)

    if should_raise:
        with pytest.raises(Exception):
            handler.check_file(testfile)
        return
    handler.check_file(testfile)
Exemplo n.º 10
0
def test_create_dockpulp_and_repos(tmpdir, user, secret_path, secret_env,
                                   invalid, monkeypatch):
    log = logging.getLogger("tests.test_pulp_util")
    pulp_registry_name = 'registry.example.com'
    testfile = 'foo'

    if secret_env:
        monkeypatch.setenv('SOURCE_SECRET_PATH', str(tmpdir))
    if secret_path:
        secret_path = str(tmpdir)
    if not invalid:
        with open(os.path.join(str(tmpdir), "pulp.cer"), "wt") as cer:
            cer.write("pulp certificate\n")
        with open(os.path.join(str(tmpdir), "pulp.key"), "wt") as key:
            key.write("pulp key\n")

    _, workflow = prepare(testfile)
    image_names = workflow.tag_conf.images[:]
    handler = PulpHandler(workflow,
                          pulp_registry_name,
                          log,
                          pulp_secret_path=secret_path,
                          username=user,
                          password=user)

    if invalid:
        with pytest.raises(Exception):
            handler.create_dockpulp_and_repos(image_names)
        return

    expected = {
        'redhat-image-name1':
        PulpRepo(registry_id='image-name1', tags=['latest']),
        'redhat-image-name3':
        PulpRepo(registry_id='image-name3', tags=['asd']),
        'redhat-prefix-image-name2':
        PulpRepo(registry_id='prefix/image-name2', tags=['latest'])
    }

    assert handler.create_dockpulp_and_repos(image_names) == expected
Exemplo n.º 11
0
def test_ensure_repos(auto_publish, unsupported):
    dist_data = [{
        "repo_id": "redhat-myproject-hello-world",
        "auto_publish": auto_publish,
    }]
    mock_get_data = [{"id": "redhat-myproject-hello-world"}]
    data_with_dist = copy.deepcopy(mock_get_data)
    data_with_dist[0]["distributors"] = dist_data

    log = logging.getLogger("tests.test_pulp_util")
    pulp_registry_name = 'registry.example.com'
    testfile = 'foo'

    _, workflow = prepare(testfile)

    if unsupported:
        (flexmock(dockpulp.Pulp).should_receive('getRepos').with_args(
            ['redhat-myproject-hello-world'], fields=['id'],
            distributors=True).and_raise(TypeError).once())
    else:
        (flexmock(dockpulp.Pulp).should_receive('getRepos').with_args(
            ['redhat-myproject-hello-world'], fields=['id'],
            distributors=True).and_return(data_with_dist).once())

    (flexmock(dockpulp.Pulp).should_receive('getRepos').with_args(
        ['redhat-myproject-hello-world'],
        fields=['id'
                ]).and_return(mock_get_data).times(1 if unsupported else 0))

    (flexmock(dockpulp.Pulp).should_receive('updateRepo').with_args(
        data_with_dist[0]["id"], {
            'auto_publish': False
        }).times(1 if auto_publish and not unsupported else 0))

    image_names = [ImageName(repo="myproject-hello-world")]
    handler = PulpHandler(workflow, pulp_registry_name, log)
    handler.create_dockpulp_and_repos(image_names)