def test_get_docker_registry(self, config, fallback, valid):
        tasker, workflow = self.prepare()
        workflow.plugin_workspace[ReactorConfigPlugin.key] = {}

        config_json = read_yaml(config, 'schemas/config.json')

        docker_reg = {
            'version': 'v2',
            'insecure': False,
            'secret': '/var/run/secrets/atomic-reactor/v2-registry-dockercfg',
            'url': 'https://container-registry.example.com/v2',
        }

        if fallback:
            if valid:
                docker_fallback = docker_reg
                expected = docker_reg
            else:
                docker_fallback = NO_FALLBACK
        else:
            docker_fallback = {}
            expected = {
                'url': 'https://container-registry.example.com',
                'insecure': False,
                'secret':
                '/var/run/secrets/atomic-reactor/v2-registry-dockercfg'
            }
            workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig(config_json)

        if valid:
            docker_registry = get_docker_registry(workflow, docker_fallback)
            assert docker_registry == expected
        else:
            if fallback:
                with pytest.raises(KeyError):
                    get_docker_registry(workflow, docker_fallback)
            else:
                with pytest.raises(RuntimeError):
                    get_docker_registry(workflow, docker_fallback)
    def test_get_docker_registry(self, config, fallback, valid):
        tasker, workflow = self.prepare()
        workflow.plugin_workspace[ReactorConfigPlugin.key] = {}

        config_json = read_yaml(config, 'schemas/config.json')

        docker_reg = {
            'version': 'v2',
            'insecure': False,
            'secret': '/var/run/secrets/atomic-reactor/v2-registry-dockercfg',
            'url': 'https://container-registry.example.com/v2',
        }

        if fallback:
            if valid:
                docker_fallback = docker_reg
                expected = docker_reg
            else:
                docker_fallback = NO_FALLBACK
        else:
            docker_fallback = {}
            expected = {
                'url': 'https://container-registry.example.com',
                'insecure': False,
                'secret': '/var/run/secrets/atomic-reactor/v2-registry-dockercfg'
            }
            workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig(config_json)

        if valid:
            docker_registry = get_docker_registry(workflow, docker_fallback)
            assert docker_registry == expected
        else:
            if fallback:
                with pytest.raises(KeyError):
                    get_docker_registry(workflow, docker_fallback)
            else:
                with pytest.raises(OsbsValidationException):
                    get_docker_registry(workflow, docker_fallback)
    def __init__(self,
                 tasker,
                 workflow,
                 pulp_registry_name=None,
                 docker_registry=None,
                 pulp_secret_path=None,
                 registry_secret_path=None,
                 insecure_registry=None,
                 dockpulp_loglevel=None,
                 pulp_repo_prefix=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 docker_registry: str, URL of docker registry to sync from
               including scheme e.g. https://registry.example.com
        :param pulp_secret_path: path to pulp.cer and pulp.key
        :param registry_secret_path: path to .dockercfg for the V2 registry
        :param insecure_registry: True if SSL validation should be skipped
        :param dockpulp_loglevel: int, logging level for dockpulp
        :param pulp_repo_prefix: str, prefix for pulp repo IDs
        """
        # call parent constructor
        super(PulpSyncPlugin, self).__init__(tasker, workflow)

        self.pulp_fallback = {
            'name': pulp_registry_name,
            'loglevel': dockpulp_loglevel,
            'auth': {
                'ssl_certs_dir': pulp_secret_path,
            }
        }
        pulp = get_pulp(self.workflow, self.pulp_fallback)
        self.pulp_registry_name = pulp['name']
        self.pulp_secret_path = pulp['auth'].get('ssl_certs_dir')

        docker_fallback = {
            'version': 'v2',
            'insecure': insecure_registry,
            'secret': registry_secret_path,
            'url': docker_registry,
        }
        docker_reg = get_docker_registry(self.workflow, docker_fallback)
        self.docker_registry = docker_reg['url']
        self.registry_secret_path = docker_reg['secret']
        self.insecure_registry = docker_reg['insecure']
        self.pulp_repo_prefix = pulp_repo_prefix
        logger = PulpLog.get_pulp_logger()

        loglevel = pulp.get('loglevel')
        if loglevel is not None:
            self.log.info("attempting to set loglevel")
            try:
                logger.setLevel(loglevel)
            except (ValueError, TypeError) as ex:
                self.log.error("Can't set provided log level %r: %r", loglevel,
                               ex)

        self.publish = (
            publish
            and not are_plugins_in_order(self.workflow.postbuild_plugins_conf,
                                         self.key, PLUGIN_PULP_PUSH_KEY))
    def __init__(self, tasker, workflow,
                 pulp_registry_name=None,
                 docker_registry=None,
                 pulp_secret_path=None,
                 registry_secret_path=None,
                 insecure_registry=None,
                 dockpulp_loglevel=None,
                 pulp_repo_prefix=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 docker_registry: str, URL of docker registry to sync from
               including scheme e.g. https://registry.example.com
        :param pulp_secret_path: path to pulp.cer and pulp.key
        :param registry_secret_path: path to .dockercfg for the V2 registry
        :param insecure_registry: True if SSL validation should be skipped
        :param dockpulp_loglevel: int, logging level for dockpulp
        :param pulp_repo_prefix: str, prefix for pulp repo IDs
        """
        # call parent constructor
        super(PulpSyncPlugin, self).__init__(tasker, workflow)

        self.pulp_fallback = {
            'name': pulp_registry_name,
            'loglevel': dockpulp_loglevel,
            'auth': {
                'ssl_certs_dir': pulp_secret_path,
            }
        }
        pulp = get_pulp(self.workflow, self.pulp_fallback)
        self.pulp_registry_name = pulp['name']
        self.pulp_secret_path = pulp['auth'].get('ssl_certs_dir')

        docker_fallback = {
            'version': 'v2',
            'insecure': insecure_registry,
            'secret': registry_secret_path,
            'url': docker_registry,
        }
        docker_reg = get_docker_registry(self.workflow, docker_fallback)
        self.docker_registry = docker_reg['url']
        self.registry_secret_path = docker_reg['secret']
        self.insecure_registry = docker_reg['insecure']
        self.pulp_repo_prefix = pulp_repo_prefix
        logger = PulpLog.get_pulp_logger()

        loglevel = pulp.get('loglevel')
        if loglevel is not None:
            self.log.info("attempting to set loglevel")
            try:
                logger.setLevel(loglevel)
            except (ValueError, TypeError) as ex:
                self.log.error("Can't set provided log level %r: %r",
                               loglevel, ex)

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