def test_create_missing_repo(self):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'
        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry)
        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('getRepos').with_args(
            [prefixed_pulp_repoid],
            fields=['id']).and_return([]).once().ordered())
        (flexmock(mockpulp).should_receive('createRepo').with_args(
            prefixed_pulp_repoid,
            None,
            registry_id=docker_repository,
            prefix_with='redhat-').once().ordered())
        (flexmock(mockpulp).should_receive('syncRepo').with_args(
            repo=prefixed_pulp_repoid, feed=docker_registry).and_return(
                ([], [])).once().ordered())
        (flexmock(mockpulp).should_receive('crane').with_args(
            [prefixed_pulp_repoid], wait=True).once().ordered())
        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        plugin.run()
    def test_pulp_repo_prefix(self,
                              get_prefix,
                              pulp_repo_prefix,
                              expected_prefix, reactor_config_map):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = '{}prod-myrepository'.format(expected_prefix)
        env = 'pulp'
        kwargs = {}
        if pulp_repo_prefix:
            kwargs['pulp_repo_prefix'] = pulp_repo_prefix

        if reactor_config_map:
            self.workflow.plugin_workspace = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1, 'pulp': {'name': env}})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry,
                                **kwargs)

        mockpulp = MockPulp()
        if get_prefix:
            (flexmock(mockpulp)
                .should_receive('getPrefix')
                .with_args()
                .and_return(expected_prefix))
        else:
            (flexmock(mockpulp)
                .should_receive('getPrefix')
                .with_args()
                .and_raise(AttributeError))

        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args([prefixed_pulp_repoid], fields=['id'])
            .and_return([{'id': prefixed_pulp_repoid}])
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .with_args(repo=prefixed_pulp_repoid,
                       feed=docker_registry)
            .and_return(([], []))
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('crane')
            .with_args([prefixed_pulp_repoid], wait=True)
            .once()
            .ordered())
        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        plugin.run()
    def test_store_registry(self, already_exists):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        env = 'pulp'
        workflow = self.workflow([docker_repository])

        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('login').never())
        (flexmock(mockpulp).should_receive('set_certs').never())
        (flexmock(mockpulp).should_receive('syncRepo').with_args(
            object,
            'prod-myrepository',  # pulp repository name
            config_file=object).and_return([{
                'id': 'prefix-prod-myrepository'
            }])  # repo id
         .once().ordered())
        (flexmock(mockpulp).should_receive('crane').with_args(
            ['prefix-prod-myrepository'],  # repo id
            wait=True).once().ordered())
        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        if already_exists:
            workflow.push_conf.add_pulp_registry(env, mockpulp.registry)

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=workflow,
                                pulp_registry_name=env,
                                docker_registry=docker_registry)

        num_registries = len(workflow.push_conf.pulp_registries)
        assert num_registries == (1 if already_exists else 0)
        plugin.run()
        assert len(workflow.push_conf.pulp_registries) == 1
    def test_delete_not_implemented(self, caplog, reactor_config_map):  # noqa
        """
        Should log an error (but not raise an exception) when
        delete_from_registry is True.
        """
        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('getRepos').with_args(
            ['redhat-prod-myrepository'], fields=['id']).and_return([{
                'id':
                'redhat-prod-myrepository'
            }]).once().ordered())
        (flexmock(mockpulp).should_receive('syncRepo').and_return(([], [])))
        flexmock(dockpulp).should_receive('Pulp').and_return(mockpulp)

        if reactor_config_map:
            self.workflow.plugin_workspace = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1, 'pulp': {'name': 'pulp'}})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow(['prod/myrepository']),
                                pulp_registry_name='pulp',
                                docker_registry='http://registry.example.com',
                                delete_from_registry=True)
        plugin.run()

        errors = [
            record.getMessage() for record in caplog.records()
            if record.levelname == 'ERROR'
        ]

        assert [message for message in errors if 'not implemented' in message]
    def test_create_missing_repo(self, reactor_config_map):  # noqa
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'

        if reactor_config_map:
            self.workflow.plugin_workspace = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1, 'pulp': {'name': env}})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry)

        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('getRepos').with_args(
            [prefixed_pulp_repoid],
            fields=['id']).and_return([]).once().ordered())
        (flexmock(mockpulp).should_receive('createRepo').with_args(
            prefixed_pulp_repoid,
            None,
            registry_id=docker_repository,
            prefix_with='redhat-').once().ordered())
        (flexmock(mockpulp).should_receive('syncRepo').with_args(
            repo=prefixed_pulp_repoid, feed=docker_registry).and_return(
                ([], [])).once().ordered())
        (flexmock(mockpulp).should_receive('crane').with_args(
            [prefixed_pulp_repoid], wait=True).once().ordered())
        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        plugin.run()
    def test_dockercfg_missing_or_invalid(self, tmpdir, content, reactor_config_map):
        env = 'pulp'

        if content is not None:
            registry_secret = os.path.join(str(tmpdir), '.dockercfg')
            with open(registry_secret, 'w') as fp:
                fp.write(content)

        if reactor_config_map:
            self.workflow.plugin_workspace = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1, 'pulp': {'name': env}})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow(['repo']),
                                pulp_registry_name=env,
                                docker_registry='http://registry.example.com',
                                registry_secret_path=str(tmpdir))

        mockpulp = MockPulp()
        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        with pytest.raises(RuntimeError):
            plugin.run()
    def test_auth_none(self):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        env = 'pulp'
        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry)

        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('login').never())
        (flexmock(mockpulp).should_receive('set_certs').never())
        (flexmock(mockpulp).should_receive('syncRepo').with_args(
            object,
            'prod-myrepository',  # pulp repository name
            config_file=object).and_return([{
                'id': 'prefix-prod-myrepository'
            }])  # repo id
         .once().ordered())
        (flexmock(mockpulp).should_receive('crane').with_args(
            ['prefix-prod-myrepository'],  # repo id
            wait=True).once().ordered())
        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        plugin.run()
    def test_delete_not_implemented(self, caplog):
        """
        Should log an error (but not raise an exception) when
        delete_from_registry is True.
        """
        mockpulp = MockPulp()
        (
            flexmock(mockpulp)
            .should_receive("getRepos")
            .with_args(["redhat-prod-myrepository"], fields=["id"])
            .and_return([{"id": "redhat-prod-myrepository"}])
            .once()
            .ordered()
        )
        (flexmock(mockpulp).should_receive("syncRepo").and_return(([], [])))
        flexmock(dockpulp).should_receive("Pulp").and_return(mockpulp)
        plugin = PulpSyncPlugin(
            tasker=None,
            workflow=self.workflow(["prod/myrepository"]),
            pulp_registry_name="pulp",
            docker_registry="http://registry.example.com",
            delete_from_registry=True,
        )

        plugin.run()

        errors = [record.getMessage() for record in caplog.records() if record.levelname == "ERROR"]

        assert [message for message in errors if "not implemented" in message]
    def test_insecure_registry(self, insecure_registry, ssl_validation):
        docker_registry = "http://registry.example.com"
        docker_repository = "prod/myrepository"
        pulp_repoid = "prod-myrepository"
        prefixed_pulp_repoid = "redhat-prod-myrepository"
        env = "pulp"
        plugin = PulpSyncPlugin(
            tasker=None,
            workflow=self.workflow([docker_repository]),
            pulp_registry_name=env,
            docker_registry=docker_registry,
            insecure_registry=insecure_registry,
        )

        mockpulp = MockPulp()
        (
            flexmock(mockpulp)
            .should_receive("getRepos")
            .with_args([prefixed_pulp_repoid], fields=["id"])
            .and_return([{"id": prefixed_pulp_repoid}])
            .once()
            .ordered()
        )
        sync_exp = flexmock(mockpulp).should_receive("syncRepo")
        if ssl_validation is None:
            sync_exp = sync_exp.with_args(repo=prefixed_pulp_repoid, feed=docker_registry)
        else:
            sync_exp = sync_exp.with_args(
                repo=prefixed_pulp_repoid, feed=docker_registry, ssl_validation=ssl_validation
            )

        (sync_exp.and_return(([], [])).once().ordered())
        (flexmock(dockpulp).should_receive("Pulp").with_args(env=env).and_return(mockpulp))

        plugin.run()
示例#10
0
    def test_delete_not_implemented(self, caplog):
        """
        Should log an error (but not raise an exception) when
        delete_from_registry is True.
        """
        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args(['redhat-prod-myrepository'], fields=['id'])
            .and_return([{'id': 'redhat-prod-myrepository'}])
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .and_return(([], [])))
        flexmock(dockpulp).should_receive('Pulp').and_return(mockpulp)
        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow(['prod/myrepository']),
                                pulp_registry_name='pulp',
                                docker_registry='http://registry.example.com',
                                delete_from_registry=True)

        plugin.run()

        errors = [record.getMessage() for record in caplog.records()
                  if record.levelname == 'ERROR']

        assert [message for message in errors
                if 'not implemented' in message]
示例#11
0
    def test_dockercfg_missing_or_invalid(self, tmpdir, content,
                                          reactor_config_map):
        env = 'pulp'

        if content is not None:
            registry_secret = os.path.join(str(tmpdir), '.dockercfg')
            with open(registry_secret, 'w') as fp:
                fp.write(content)

        if reactor_config_map:
            self.workflow.plugin_workspace = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1, 'pulp': {'name': env}})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow(['repo']),
                                pulp_registry_name=env,
                                docker_registry='http://registry.example.com',
                                registry_secret_path=str(tmpdir))

        mockpulp = MockPulp()
        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        with pytest.raises(RuntimeError):
            plugin.run()
    def test_store_registry(self, already_exists):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'
        workflow = self.workflow([docker_repository])

        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('login').never())
        (flexmock(mockpulp).should_receive('set_certs').never())
        (flexmock(mockpulp).should_receive('getRepos').with_args(
            [prefixed_pulp_repoid], fields=['id']).and_return([{
                'id':
                prefixed_pulp_repoid
            }]).once().ordered())
        (flexmock(mockpulp).should_receive('syncRepo').with_args(
            repo=prefixed_pulp_repoid, feed=docker_registry).and_return(
                ([], [])).once().ordered())
        (flexmock(mockpulp).should_receive('crane').with_args(
            [prefixed_pulp_repoid], wait=True).once().ordered())
        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        if already_exists:
            workflow.push_conf.add_pulp_registry(env, mockpulp.registry)

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=workflow,
                                pulp_registry_name=env,
                                docker_registry=docker_registry)

        num_registries = len(workflow.push_conf.pulp_registries)
        assert num_registries == (1 if already_exists else 0)
        plugin.run()
        assert len(workflow.push_conf.pulp_registries) == 1
    def test_insecure_registry(self, insecure_registry, ssl_validation):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'
        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry,
                                insecure_registry=insecure_registry)

        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('getRepos').with_args(
            [prefixed_pulp_repoid], fields=['id']).and_return([{
                'id':
                prefixed_pulp_repoid
            }]).once().ordered())
        sync_exp = flexmock(mockpulp).should_receive('syncRepo')
        if ssl_validation is None:
            sync_exp = sync_exp.with_args(repo=prefixed_pulp_repoid,
                                          feed=docker_registry)
        else:
            sync_exp = sync_exp.with_args(repo=prefixed_pulp_repoid,
                                          feed=docker_registry,
                                          ssl_validation=ssl_validation)

        (sync_exp.and_return(([], [])).once().ordered())
        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        plugin.run()
    def test_delete_not_implemented(self, caplog):
        """
        Should log an error (but not raise an exception) when
        delete_from_registry is True.
        """
        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('getRepos').with_args(
            ['redhat-prod-myrepository'], fields=['id']).and_return([{
                'id':
                'redhat-prod-myrepository'
            }]).once().ordered())
        (flexmock(mockpulp).should_receive('syncRepo').and_return(([], [])))
        flexmock(dockpulp).should_receive('Pulp').and_return(mockpulp)
        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow(['prod/myrepository']),
                                pulp_registry_name='pulp',
                                docker_registry='http://registry.example.com',
                                delete_from_registry=True)

        plugin.run()

        errors = [
            record.getMessage() for record in caplog.records()
            if record.levelname == 'ERROR'
        ]

        assert [message for message in errors if 'not implemented' in message]
    def test_workspace_updated(self, reactor_config_map):  # noqa
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args([prefixed_pulp_repoid], fields=['id'])
            .and_return([{'id': prefixed_pulp_repoid}])
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .with_args(repo=prefixed_pulp_repoid,
                       feed=docker_registry)
            .and_return(([], []))
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('crane')
            .with_args([prefixed_pulp_repoid], wait=True)
            .once()
            .ordered())

        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        workflow = self.workflow([docker_repository], mockpulp.registry)
        workflow.postbuild_plugins_conf.append(
            {
                'name': PulpSyncPlugin.key,
            },
        )

        kwargs = {
            'pulp_registry_name': env,
            'docker_registry': docker_registry,
        }

        if reactor_config_map:
            workflow.plugin_workspace = {}
            workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1,
                               'pulp': {'name': env,
                                        'auth': {'user': '', 'password': ''}}})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=workflow,
                                **kwargs)
        plugin.run()

        manifests = get_manifests_in_pulp_repository(workflow)
        assert manifests == ['sha256:{}'.format(prefixed_pulp_repoid)]
    def test_store_registry(self, already_exists, reactor_config_map):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'
        workflow = self.workflow([docker_repository])

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('login')
            .never())
        (flexmock(mockpulp)
            .should_receive('set_certs')
            .never())
        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args([prefixed_pulp_repoid], fields=['id'])
            .and_return([{'id': prefixed_pulp_repoid}])
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .with_args(repo=prefixed_pulp_repoid,
                       feed=docker_registry)
            .and_return(([], []))
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('crane')
            .with_args([prefixed_pulp_repoid], wait=True)
            .once()
            .ordered())
        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        if already_exists:
            workflow.push_conf.add_pulp_registry(env, mockpulp.registry,
                                                 server_side_sync=False)

        if reactor_config_map:
            workflow.plugin_workspace = {}
            workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1,
                               'pulp': {'name': env,
                                        'auth': {'user': '', 'password': ''}}})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=workflow,
                                pulp_registry_name=env,
                                docker_registry=docker_registry)

        num_registries = len(workflow.push_conf.pulp_registries)
        assert num_registries == (1 if already_exists else 0)
        plugin.run()
        assert len(workflow.push_conf.pulp_registries) == 1
    def test_pulp_auth(self, tmpdir, cer_exists, key_exists):
        pulp_secret_path = str(tmpdir)
        cer = pulp_secret_path + "/pulp.cer"
        key = pulp_secret_path + "/pulp.key"
        if cer_exists:
            open(cer, "w").close()

        if key_exists:
            open(key, "w").close()

        docker_registry = "http://registry.example.com"
        docker_repository = "prod/myrepository"
        pulp_repoid = "prod-myrepository"
        prefixed_pulp_repoid = "redhat-prod-myrepository"
        env = "pulp"
        plugin = PulpSyncPlugin(
            tasker=None,
            workflow=self.workflow([docker_repository]),
            pulp_registry_name=env,
            docker_registry=docker_registry,
            pulp_secret_path=pulp_secret_path,
        )

        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive("login").never())
        if cer_exists and key_exists:
            (flexmock(mockpulp).should_receive("set_certs").with_args(cer, key).once().ordered())
            (
                flexmock(mockpulp)
                .should_receive("getRepos")
                .with_args([prefixed_pulp_repoid], fields=["id"])
                .and_return([{"id": prefixed_pulp_repoid}])
                .once()
                .ordered()
            )
            (
                flexmock(mockpulp)
                .should_receive("syncRepo")
                .with_args(repo=prefixed_pulp_repoid, feed=docker_registry)
                .and_return(([], []))
                .once()
                .ordered()
            )
            (flexmock(mockpulp).should_receive("crane").with_args([prefixed_pulp_repoid], wait=True).once().ordered())
        else:
            (flexmock(mockpulp).should_receive("set_certs").never())
            (flexmock(mockpulp).should_receive("syncRepo").never())
            (flexmock(mockpulp).should_receive("crane").never())

        (flexmock(dockpulp).should_receive("Pulp").with_args(env=env).and_return(mockpulp))

        if cer_exists and key_exists:
            plugin.run()
        else:
            with pytest.raises(RuntimeError):
                plugin.run()
示例#18
0
    def test_pulp_repo_prefix(self,
                              get_prefix,
                              pulp_repo_prefix,
                              expected_prefix):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        pulp_repoid = 'prod-myrepository'
        prefixed_pulp_repoid = '{}prod-myrepository'.format(expected_prefix)
        env = 'pulp'
        kwargs = {}
        if pulp_repo_prefix:
            kwargs['pulp_repo_prefix'] = pulp_repo_prefix

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry,
                                **kwargs)

        mockpulp = MockPulp()
        if get_prefix:
            (flexmock(mockpulp)
                .should_receive('getPrefix')
                .with_args()
                .and_return(expected_prefix))
        else:
            (flexmock(mockpulp)
                .should_receive('getPrefix')
                .with_args()
                .and_raise(AttributeError))

        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args([prefixed_pulp_repoid], fields=['id'])
            .and_return([{'id': prefixed_pulp_repoid}])
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .with_args(repo=prefixed_pulp_repoid,
                       feed=docker_registry)
            .and_return(([], []))
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('crane')
            .with_args([prefixed_pulp_repoid], wait=True)
            .once()
            .ordered())
        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        plugin.run()
    def test_dockercfg(self, tmpdir, scheme, reactor_config_map):
        docker_registry = '{}://registry.example.com'.format(scheme)
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        user = '******'
        pw = 'pass'
        env = 'pulp'

        registry_secret = os.path.join(str(tmpdir), '.dockercfg')
        dockercfg = {
            'registry.example.com': {
                'username': user,
                'password': pw,
                'email': '*****@*****.**',
            },
        }

        with open(registry_secret, 'w') as fp:
            json.dump(dockercfg, fp)

        if reactor_config_map:
            self.workflow.plugin_workspace = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1, 'pulp': {'name': env}})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry,
                                registry_secret_path=str(tmpdir))

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args([prefixed_pulp_repoid], fields=['id'])
            .and_return([{'id': prefixed_pulp_repoid}])
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .with_args(repo=prefixed_pulp_repoid,
                       feed=docker_registry,
                       basic_auth_username=user,
                       basic_auth_password=pw)
            .and_return(([], []))
            .once()
            .ordered())
        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        plugin.run()
    def test_publish(self, publish, has_pulp_push, should_publish, caplog):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'

        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('login').never())
        (flexmock(mockpulp).should_receive('set_certs').never())
        (flexmock(mockpulp).should_receive('getRepos').with_args(
            [prefixed_pulp_repoid], fields=['id']).and_return([{
                'id':
                prefixed_pulp_repoid
            }]).once().ordered())
        (flexmock(mockpulp).should_receive('syncRepo').with_args(
            repo=prefixed_pulp_repoid, feed=docker_registry).and_return(
                ([], [])).once().ordered())
        if should_publish:
            (flexmock(mockpulp).should_receive('crane').with_args(
                [prefixed_pulp_repoid], wait=True).once().ordered())
        else:
            (flexmock(mockpulp).should_receive('crane').never())

        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        workflow = self.workflow([docker_repository], mockpulp.registry)
        workflow.postbuild_plugins_conf.append({
            'name': PulpSyncPlugin.key,
        }, )
        if has_pulp_push:
            workflow.postbuild_plugins_conf.append(
                {
                    'name': PLUGIN_PULP_PUSH_KEY,
                }, )

        kwargs = {
            'pulp_registry_name': env,
            'docker_registry': docker_registry,
        }
        if publish is not None:
            kwargs['publish'] = publish

        plugin = PulpSyncPlugin(tasker=None, workflow=workflow, **kwargs)

        plugin.run()
        log_messages = [l.getMessage() for l in caplog.records()]

        for image in workflow.tag_conf.images:
            expected_log = 'image available at %s' % image.to_str()
            if should_publish:
                assert expected_log in log_messages
            else:
                assert expected_log not in log_messages
示例#21
0
    def test_pulp_repo_prefix(self,
                              get_prefix,
                              pulp_repo_prefix,
                              expected_prefix):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = '{}prod-myrepository'.format(expected_prefix)
        env = 'pulp'
        kwargs = {}
        if pulp_repo_prefix:
            kwargs['pulp_repo_prefix'] = pulp_repo_prefix

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry,
                                **kwargs)

        mockpulp = MockPulp()
        if get_prefix:
            (flexmock(mockpulp)
                .should_receive('getPrefix')
                .with_args()
                .and_return(expected_prefix))
        else:
            (flexmock(mockpulp)
                .should_receive('getPrefix')
                .with_args()
                .and_raise(AttributeError))

        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args([prefixed_pulp_repoid], fields=['id'])
            .and_return([{'id': prefixed_pulp_repoid}])
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .with_args(repo=prefixed_pulp_repoid,
                       feed=docker_registry)
            .and_return(([], []))
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('crane')
            .with_args([prefixed_pulp_repoid], wait=True)
            .once()
            .ordered())
        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        plugin.run()
示例#22
0
    def test_workspace_updated(self):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args([prefixed_pulp_repoid], fields=['id'])
            .and_return([{'id': prefixed_pulp_repoid}])
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .with_args(repo=prefixed_pulp_repoid,
                       feed=docker_registry)
            .and_return(([], []))
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('crane')
            .with_args([prefixed_pulp_repoid], wait=True)
            .once()
            .ordered())

        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        workflow = self.workflow([docker_repository], mockpulp.registry)
        workflow.postbuild_plugins_conf.append(
            {
                'name': PulpSyncPlugin.key,
            },
        )

        kwargs = {
            'pulp_registry_name': env,
            'docker_registry': docker_registry,
        }

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=workflow,
                                **kwargs)

        plugin.run()

        manifests = get_manifests_in_pulp_repository(workflow)
        assert manifests == ['sha256:{}'.format(prefixed_pulp_repoid)]
示例#23
0
    def test_store_registry(self, already_exists):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'
        workflow = self.workflow([docker_repository])

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('login')
            .never())
        (flexmock(mockpulp)
            .should_receive('set_certs')
            .never())
        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args([prefixed_pulp_repoid], fields=['id'])
            .and_return([{'id': prefixed_pulp_repoid}])
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .with_args(repo=prefixed_pulp_repoid,
                       feed=docker_registry)
            .and_return(([], []))
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('crane')
            .with_args([prefixed_pulp_repoid], wait=True)
            .once()
            .ordered())
        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        if already_exists:
            workflow.push_conf.add_pulp_registry(env, mockpulp.registry,
                                                 server_side_sync=False)

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=workflow,
                                pulp_registry_name=env,
                                docker_registry=docker_registry)

        num_registries = len(workflow.push_conf.pulp_registries)
        assert num_registries == (1 if already_exists else 0)
        plugin.run()
        assert len(workflow.push_conf.pulp_registries) == 1
示例#24
0
    def test_dockercfg(self, tmpdir, scheme):
        docker_registry = '{}://registry.example.com'.format(scheme)
        docker_repository = 'prod/myrepository'
        pulp_repoid = 'prod-myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        user = '******'
        pw = 'pass'
        env = 'pulp'

        registry_secret = os.path.join(str(tmpdir), '.dockercfg')
        dockercfg = {
            'registry.example.com': {
                'username': user,
                'password': pw,
                'email': '*****@*****.**',
            },
        }

        with open(registry_secret, 'w') as fp:
            json.dump(dockercfg, fp)

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry,
                                registry_secret_path=str(tmpdir))

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args([prefixed_pulp_repoid], fields=['id'])
            .and_return([{'id': prefixed_pulp_repoid}])
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .with_args(repo=prefixed_pulp_repoid,
                       feed=docker_registry,
                       basic_auth_username=user,
                       basic_auth_password=pw)
            .and_return(([], []))
            .once()
            .ordered())
        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        plugin.run()
    def test_create_missing_repo(self, reactor_config_map):  # noqa
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'

        if reactor_config_map:
            self.workflow.plugin_workspace = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1, 'pulp': {'name': env}})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry)

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args([prefixed_pulp_repoid], fields=['id'])
            .and_return([])
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('createRepo')
            .with_args(prefixed_pulp_repoid, None,
                       registry_id=docker_repository,
                       prefix_with='redhat-')
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .with_args(repo=prefixed_pulp_repoid,
                       feed=docker_registry)
            .and_return(([], []))
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('crane')
            .with_args([prefixed_pulp_repoid], wait=True)
            .once()
            .ordered())
        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        plugin.run()
    def test_dockercfg(self, tmpdir, scheme):
        docker_registry = "{}://registry.example.com".format(scheme)
        docker_repository = "prod/myrepository"
        pulp_repoid = "prod-myrepository"
        prefixed_pulp_repoid = "redhat-prod-myrepository"
        user = "******"
        pw = "pass"
        env = "pulp"

        registry_secret = os.path.join(str(tmpdir), ".dockercfg")
        dockercfg = {"registry.example.com": {"username": user, "password": pw, "email": "*****@*****.**"}}

        with open(registry_secret, "w") as fp:
            json.dump(dockercfg, fp)

        plugin = PulpSyncPlugin(
            tasker=None,
            workflow=self.workflow([docker_repository]),
            pulp_registry_name=env,
            docker_registry=docker_registry,
            registry_secret_path=str(tmpdir),
        )

        mockpulp = MockPulp()
        (
            flexmock(mockpulp)
            .should_receive("getRepos")
            .with_args([prefixed_pulp_repoid], fields=["id"])
            .and_return([{"id": prefixed_pulp_repoid}])
            .once()
            .ordered()
        )
        (
            flexmock(mockpulp)
            .should_receive("syncRepo")
            .with_args(
                repo=prefixed_pulp_repoid, feed=docker_registry, basic_auth_username=user, basic_auth_password=pw
            )
            .and_return(([], []))
            .once()
            .ordered()
        )
        (flexmock(dockpulp).should_receive("Pulp").with_args(env=env).and_return(mockpulp))

        plugin.run()
示例#27
0
    def test_store_registry(self, already_exists, reactor_config_map):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'
        workflow = self.workflow([docker_repository])

        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('login').never())
        (flexmock(mockpulp).should_receive('set_certs').never())
        (flexmock(mockpulp).should_receive('getRepos').with_args(
            [prefixed_pulp_repoid], fields=['id']).and_return([{
                'id':
                prefixed_pulp_repoid
            }]).once().ordered())
        (flexmock(mockpulp).should_receive('syncRepo').with_args(
            repo=prefixed_pulp_repoid, feed=docker_registry).and_return(
                ([], [])).once().ordered())
        (flexmock(mockpulp).should_receive('crane').with_args(
            [prefixed_pulp_repoid], wait=True).once().ordered())
        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        if already_exists:
            workflow.push_conf.add_pulp_registry(env,
                                                 mockpulp.registry,
                                                 server_side_sync=False)

        if reactor_config_map:
            workflow.plugin_workspace = {}
            workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1,
                               'pulp': {'name': env,
                                        'auth': {'user': '', 'password': ''}}})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=workflow,
                                pulp_registry_name=env,
                                docker_registry=docker_registry)

        num_registries = len(workflow.push_conf.pulp_registries)
        assert num_registries == (1 if already_exists else 0)
        plugin.run()
        assert len(workflow.push_conf.pulp_registries) == 1
    def test_dockpulp_loglevel(self, fail, caplog, reactor_config_map):
        loglevel = 3

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args(['redhat-prod-myrepository'], fields=['id'])
            .and_return([{'id': 'redhat-prod-myrepository'}])
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .and_return(([], [])))
        flexmock(dockpulp).should_receive('Pulp').and_return(mockpulp)
        logger = flexmock()
        expectation = (logger
                       .should_receive('setLevel')
                       .with_args(loglevel)
                       .once())

        if fail:
            expectation.and_raise(ValueError)

        flexmock(PulpLogWrapper).should_receive('get_pulp_logger').and_return(logger).once()

        if reactor_config_map:
            self.workflow.plugin_workspace = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1, 'pulp': {'name': 'pulp'}})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow(['prod/myrepository']),
                                pulp_registry_name='pulp',
                                docker_registry='http://registry.example.com',
                                dockpulp_loglevel=loglevel)
        plugin.run()

        errors = [record.getMessage() for record in caplog.records
                  if record.levelname == 'ERROR']

        if fail:
            assert len(errors) >= 1
        else:
            assert not errors
示例#29
0
    def test_dockercfg_registry_not_present(self, tmpdir,
                                            reactor_config_map):  # noqa
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'

        registry_secret = os.path.join(str(tmpdir), '.dockercfg')
        dockercfg = {
            'other-registry.example.com': {
                'username': '******',
                'password': '******',
                'email': '*****@*****.**',
            },
        }

        with open(registry_secret, 'w') as fp:
            json.dump(dockercfg, fp)

        if reactor_config_map:
            self.workflow.plugin_workspace = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1, 'pulp': {'name': env}})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry,
                                registry_secret_path=str(tmpdir))

        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('getRepos').with_args(
            [prefixed_pulp_repoid], fields=['id']).and_return([{
                'id':
                prefixed_pulp_repoid
            }]).once().ordered())
        (flexmock(mockpulp).should_receive('syncRepo').with_args(
            repo=prefixed_pulp_repoid, feed=docker_registry).and_return(
                ([], [])).once().ordered())
        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        plugin.run()
示例#30
0
    def test_store_registry(self, already_exists):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        env = 'pulp'
        workflow = self.workflow([docker_repository])

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('login')
            .never())
        (flexmock(mockpulp)
            .should_receive('set_certs')
            .never())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .with_args(object,
                       'prod-myrepository',  # pulp repository name
                       config_file=object)
            .and_return([{'id': 'prefix-prod-myrepository'}])  # repo id
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('crane')
            .with_args(['prefix-prod-myrepository'],  # repo id
                       wait=True)
            .once()
            .ordered())
        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        if already_exists:
            workflow.push_conf.add_pulp_registry(env, mockpulp.registry)

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=workflow,
                                pulp_registry_name=env,
                                docker_registry=docker_registry)

        num_registries = len(workflow.push_conf.pulp_registries)
        assert num_registries == (1 if already_exists else 0)
        plugin.run()
        assert len(workflow.push_conf.pulp_registries) == 1
示例#31
0
    def test_pulp_repo_prefix(self, get_prefix, pulp_repo_prefix,
                              expected_prefix, reactor_config_map):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = '{}prod-myrepository'.format(expected_prefix)
        env = 'pulp'
        kwargs = {}
        if pulp_repo_prefix:
            kwargs['pulp_repo_prefix'] = pulp_repo_prefix

        if reactor_config_map:
            self.workflow.plugin_workspace = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1, 'pulp': {'name': env}})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry,
                                **kwargs)

        mockpulp = MockPulp()
        if get_prefix:
            (flexmock(mockpulp).should_receive(
                'getPrefix').with_args().and_return(expected_prefix))
        else:
            (flexmock(mockpulp).should_receive(
                'getPrefix').with_args().and_raise(AttributeError))

        (flexmock(mockpulp).should_receive('getRepos').with_args(
            [prefixed_pulp_repoid], fields=['id']).and_return([{
                'id':
                prefixed_pulp_repoid
            }]).once().ordered())
        (flexmock(mockpulp).should_receive('syncRepo').with_args(
            repo=prefixed_pulp_repoid, feed=docker_registry).and_return(
                ([], [])).once().ordered())
        (flexmock(mockpulp).should_receive('crane').with_args(
            [prefixed_pulp_repoid], wait=True).once().ordered())
        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        plugin.run()
示例#32
0
    def test_dockercfg_registry_not_present(self, tmpdir):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'

        registry_secret = os.path.join(str(tmpdir), '.dockercfg')
        dockercfg = {
            'other-registry.example.com': {
                'username': '******',
                'password': '******',
                'email': '*****@*****.**',
            },
        }

        with open(registry_secret, 'w') as fp:
            json.dump(dockercfg, fp)

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry,
                                registry_secret_path=str(tmpdir))

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args([prefixed_pulp_repoid], fields=['id'])
            .and_return([{'id': prefixed_pulp_repoid}])
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .with_args(repo=prefixed_pulp_repoid,
                       feed=docker_registry)
            .and_return(([], []))
            .once()
            .ordered())
        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        plugin.run()
    def test_pulp_repo_prefix(self, get_prefix, pulp_repo_prefix, expected_prefix):
        docker_registry = "http://registry.example.com"
        docker_repository = "prod/myrepository"
        pulp_repoid = "prod-myrepository"
        prefixed_pulp_repoid = "{}prod-myrepository".format(expected_prefix)
        env = "pulp"
        kwargs = {}
        if pulp_repo_prefix:
            kwargs["pulp_repo_prefix"] = pulp_repo_prefix

        plugin = PulpSyncPlugin(
            tasker=None,
            workflow=self.workflow([docker_repository]),
            pulp_registry_name=env,
            docker_registry=docker_registry,
            **kwargs
        )

        mockpulp = MockPulp()
        if get_prefix:
            (flexmock(mockpulp).should_receive("getPrefix").with_args().and_return(expected_prefix))
        else:
            (flexmock(mockpulp).should_receive("getPrefix").with_args().and_raise(AttributeError))

        (
            flexmock(mockpulp)
            .should_receive("getRepos")
            .with_args([prefixed_pulp_repoid], fields=["id"])
            .and_return([{"id": prefixed_pulp_repoid}])
            .once()
            .ordered()
        )
        (
            flexmock(mockpulp)
            .should_receive("syncRepo")
            .with_args(repo=prefixed_pulp_repoid, feed=docker_registry)
            .and_return(([], []))
            .once()
            .ordered()
        )
        (flexmock(mockpulp).should_receive("crane").with_args([prefixed_pulp_repoid], wait=True).once().ordered())
        (flexmock(dockpulp).should_receive("Pulp").with_args(env=env).and_return(mockpulp))

        plugin.run()
示例#34
0
    def test_workspace_updated(self, reactor_config_map):  # noqa
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'

        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('getRepos').with_args(
            [prefixed_pulp_repoid], fields=['id']).and_return([{
                'id':
                prefixed_pulp_repoid
            }]).once().ordered())
        (flexmock(mockpulp).should_receive('syncRepo').with_args(
            repo=prefixed_pulp_repoid, feed=docker_registry).and_return(
                ([], [])).once().ordered())
        (flexmock(mockpulp).should_receive('crane').with_args(
            [prefixed_pulp_repoid], wait=True).once().ordered())

        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        workflow = self.workflow([docker_repository], mockpulp.registry)
        workflow.postbuild_plugins_conf.append({
            'name': PulpSyncPlugin.key,
        }, )

        kwargs = {
            'pulp_registry_name': env,
            'docker_registry': docker_registry,
        }

        if reactor_config_map:
            workflow.plugin_workspace = {}
            workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1,
                               'pulp': {'name': env,
                                        'auth': {'user': '', 'password': ''}}})

        plugin = PulpSyncPlugin(tasker=None, workflow=workflow, **kwargs)
        plugin.run()

        manifests = get_manifests_in_pulp_repository(workflow)
        assert manifests == ['sha256:{}'.format(prefixed_pulp_repoid)]
    def test_insecure_registry(self, insecure_registry, ssl_validation, reactor_config_map):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'

        if reactor_config_map:
            self.workflow.plugin_workspace = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1, 'pulp': {'name': env}})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry,
                                insecure_registry=insecure_registry)

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args([prefixed_pulp_repoid], fields=['id'])
            .and_return([{'id': prefixed_pulp_repoid}])
            .once()
            .ordered())
        sync_exp = flexmock(mockpulp).should_receive('syncRepo')
        if ssl_validation is None:
            sync_exp = sync_exp.with_args(repo=prefixed_pulp_repoid,
                                          feed=docker_registry)
        else:
            sync_exp = sync_exp.with_args(repo=prefixed_pulp_repoid,
                                          feed=docker_registry,
                                          ssl_validation=ssl_validation)

        (sync_exp
            .and_return(([], []))
            .once()
            .ordered())
        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        plugin.run()
示例#36
0
    def test_dockpulp_loglevel(self, fail, caplog, reactor_config_map):
        loglevel = 3

        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('getRepos').with_args(
            ['redhat-prod-myrepository'], fields=['id']).and_return([{
                'id':
                'redhat-prod-myrepository'
            }]).once().ordered())
        (flexmock(mockpulp).should_receive('syncRepo').and_return(([], [])))
        flexmock(dockpulp).should_receive('Pulp').and_return(mockpulp)
        logger = flexmock()
        expectation = (
            logger.should_receive('setLevel').with_args(loglevel).once())

        if fail:
            expectation.and_raise(ValueError)

        flexmock(PulpLogWrapper).should_receive('get_pulp_logger').and_return(
            logger).once()

        if reactor_config_map:
            self.workflow.plugin_workspace = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1, 'pulp': {'name': 'pulp'}})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow(['prod/myrepository']),
                                pulp_registry_name='pulp',
                                docker_registry='http://registry.example.com',
                                dockpulp_loglevel=loglevel)
        plugin.run()

        errors = [
            record.getMessage() for record in caplog.records()
            if record.levelname == 'ERROR'
        ]

        if fail:
            assert len(errors) >= 1
        else:
            assert not errors
示例#37
0
    def test_print_availability_info(self, has_pulp_push, caplog):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        pulp_repoid = 'prod-myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'

        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('login').never())
        (flexmock(mockpulp).should_receive('set_certs').never())
        (flexmock(mockpulp).should_receive('getRepos').with_args(
            [prefixed_pulp_repoid], fields=['id']).and_return([{
                'id':
                prefixed_pulp_repoid
            }]).once().ordered())
        (flexmock(mockpulp).should_receive('syncRepo').with_args(
            repo=prefixed_pulp_repoid, feed=docker_registry).and_return(
                ([], [])).once().ordered())
        (flexmock(mockpulp).should_receive('crane').with_args(
            [prefixed_pulp_repoid], wait=True).once().ordered())
        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        workflow = self.workflow([docker_repository], mockpulp.registry)
        workflow.postbuild_plugins_conf.append({'name': PulpSyncPlugin.key})
        if has_pulp_push:
            # PulpPushPlugin.key is not imported here to avoid circular import
            workflow.postbuild_plugins_conf.append({'name': 'pulp_push'})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=workflow,
                                pulp_registry_name=env,
                                docker_registry=docker_registry)

        plugin.run()
        log_messages = [l.getMessage() for l in caplog.records()]

        for image in workflow.tag_conf.images:
            expected_log = 'image available at %s' % image.to_str()
            if has_pulp_push:
                assert expected_log not in log_messages
            else:
                assert expected_log in log_messages
示例#38
0
    def test_auth_none(self):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry)

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('login')
            .never())
        (flexmock(mockpulp)
            .should_receive('set_certs')
            .never())
        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args([prefixed_pulp_repoid], fields=['id'])
            .and_return([{'id': prefixed_pulp_repoid}])
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .with_args(repo=prefixed_pulp_repoid,
                       feed=docker_registry)
            .and_return(([], []))
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('crane')
            .with_args([prefixed_pulp_repoid], wait=True)
            .once()
            .ordered())
        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        plugin.run()
示例#39
0
    def test_dockpulp_loglevel(self, fail, caplog):
        loglevel = 3

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args(['redhat-prod-myrepository'], fields=['id'])
            .and_return([{'id': 'redhat-prod-myrepository'}])
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .and_return(([], [])))
        flexmock(dockpulp).should_receive('Pulp').and_return(mockpulp)
        logger = flexmock()
        expectation = (logger
                       .should_receive('setLevel')
                       .with_args(loglevel)
                       .once())
        if fail:
            expectation.and_raise(ValueError)

        (flexmock(dockpulp)
            .should_receive('setup_logger')
            .and_return(logger)
            .once())

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow(['prod/myrepository']),
                                pulp_registry_name='pulp',
                                docker_registry='http://registry.example.com',
                                dockpulp_loglevel=loglevel)

        plugin.run()

        errors = [record.getMessage() for record in caplog.records()
                  if record.levelname == 'ERROR']

        if fail:
            assert len(errors) >= 1
        else:
            assert not errors
示例#40
0
    def test_dockpulp_loglevel(self, fail, caplog):
        loglevel = 3

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args(['redhat-prod-myrepository'], fields=['id'])
            .and_return([{'id': 'redhat-prod-myrepository'}])
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .and_return(([], [])))
        flexmock(dockpulp).should_receive('Pulp').and_return(mockpulp)
        logger = flexmock()
        expectation = (logger
                       .should_receive('setLevel')
                       .with_args(loglevel)
                       .once())
        if fail:
            expectation.and_raise(ValueError)

        (flexmock(dockpulp)
            .should_receive('setup_logger')
            .and_return(logger)
            .once())

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow(['prod/myrepository']),
                                pulp_registry_name='pulp',
                                docker_registry='http://registry.example.com',
                                dockpulp_loglevel=loglevel)

        plugin.run()

        errors = [record.getMessage() for record in caplog.records()
                  if record.levelname == 'ERROR']

        if fail:
            assert len(errors) >= 1
        else:
            assert not errors
示例#41
0
    def test_auth_password(self):
        username = '******'
        password = '******'
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        env = 'pulp'
        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry,
                                username=username,
                                password=password)

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('login')
            .with_args(username, password)
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('set_certs')
            .never())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .with_args(object,
                       'prod-myrepository',  # pulp repository name
                       config_file=object)
            .and_return([{'id': 'prefix-prod-myrepository'}])  # repo id
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('crane')
            .with_args(['prefix-prod-myrepository'],  # repo id
                       wait=True)
            .once()
            .ordered())
        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        plugin.run()
    def test_dockercfg_missing_or_invalid(self, tmpdir, content):
        env = 'pulp'

        if content is not None:
            registry_secret = os.path.join(str(tmpdir), '.dockercfg')
            with open(registry_secret, 'w') as fp:
                fp.write(content)

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow(['repo']),
                                pulp_registry_name=env,
                                docker_registry='http://registry.example.com',
                                registry_secret_path=str(tmpdir))

        mockpulp = MockPulp()
        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        with pytest.raises(RuntimeError):
            plugin.run()
    def test_create_missing_repo(self):
        docker_registry = "http://registry.example.com"
        docker_repository = "prod/myrepository"
        pulp_repoid = "prod-myrepository"
        prefixed_pulp_repoid = "redhat-prod-myrepository"
        env = "pulp"
        plugin = PulpSyncPlugin(
            tasker=None,
            workflow=self.workflow([docker_repository]),
            pulp_registry_name=env,
            docker_registry=docker_registry,
        )

        mockpulp = MockPulp()
        (
            flexmock(mockpulp)
            .should_receive("getRepos")
            .with_args([prefixed_pulp_repoid], fields=["id"])
            .and_return([])
            .once()
            .ordered()
        )
        (
            flexmock(mockpulp)
            .should_receive("createRepo")
            .with_args(prefixed_pulp_repoid, None, registry_id=docker_repository, prefix_with="redhat-")
            .once()
            .ordered()
        )
        (
            flexmock(mockpulp)
            .should_receive("syncRepo")
            .with_args(repo=prefixed_pulp_repoid, feed=docker_registry)
            .and_return(([], []))
            .once()
            .ordered()
        )
        (flexmock(mockpulp).should_receive("crane").with_args([prefixed_pulp_repoid], wait=True).once().ordered())
        (flexmock(dockpulp).should_receive("Pulp").with_args(env=env).and_return(mockpulp))

        plugin.run()
    def test_dockercfg_missing_or_invalid(self, tmpdir, content):
        env = "pulp"

        if content is not None:
            registry_secret = os.path.join(str(tmpdir), ".dockercfg")
            with open(registry_secret, "w") as fp:
                fp.write(content)

        plugin = PulpSyncPlugin(
            tasker=None,
            workflow=self.workflow(["repo"]),
            pulp_registry_name=env,
            docker_registry="http://registry.example.com",
            registry_secret_path=str(tmpdir),
        )

        mockpulp = MockPulp()
        (flexmock(dockpulp).should_receive("Pulp").with_args(env=env).and_return(mockpulp))

        with pytest.raises(RuntimeError):
            plugin.run()
示例#45
0
    def test_insecure_registry(self, insecure_registry, ssl_validation,
                               reactor_config_map):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'

        if reactor_config_map:
            self.workflow.plugin_workspace = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1, 'pulp': {'name': env}})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry,
                                insecure_registry=insecure_registry)

        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('getRepos').with_args(
            [prefixed_pulp_repoid], fields=['id']).and_return([{
                'id':
                prefixed_pulp_repoid
            }]).once().ordered())
        sync_exp = flexmock(mockpulp).should_receive('syncRepo')
        if ssl_validation is None:
            sync_exp = sync_exp.with_args(repo=prefixed_pulp_repoid,
                                          feed=docker_registry)
        else:
            sync_exp = sync_exp.with_args(repo=prefixed_pulp_repoid,
                                          feed=docker_registry,
                                          ssl_validation=ssl_validation)

        (sync_exp.and_return(([], [])).once().ordered())
        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        plugin.run()
    def test_store_registry(self, already_exists):
        docker_registry = "http://registry.example.com"
        docker_repository = "prod/myrepository"
        pulp_repoid = "prod-myrepository"
        prefixed_pulp_repoid = "redhat-prod-myrepository"
        env = "pulp"
        workflow = self.workflow([docker_repository])

        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive("login").never())
        (flexmock(mockpulp).should_receive("set_certs").never())
        (
            flexmock(mockpulp)
            .should_receive("getRepos")
            .with_args([prefixed_pulp_repoid], fields=["id"])
            .and_return([{"id": prefixed_pulp_repoid}])
            .once()
            .ordered()
        )
        (
            flexmock(mockpulp)
            .should_receive("syncRepo")
            .with_args(repo=prefixed_pulp_repoid, feed=docker_registry)
            .and_return(([], []))
            .once()
            .ordered()
        )
        (flexmock(mockpulp).should_receive("crane").with_args([prefixed_pulp_repoid], wait=True).once().ordered())
        (flexmock(dockpulp).should_receive("Pulp").with_args(env=env).and_return(mockpulp))

        if already_exists:
            workflow.push_conf.add_pulp_registry(env, mockpulp.registry)

        plugin = PulpSyncPlugin(tasker=None, workflow=workflow, pulp_registry_name=env, docker_registry=docker_registry)

        num_registries = len(workflow.push_conf.pulp_registries)
        assert num_registries == (1 if already_exists else 0)
        plugin.run()
        assert len(workflow.push_conf.pulp_registries) == 1
    def test_workspace_updated(self):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'

        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('getRepos').with_args(
            [prefixed_pulp_repoid], fields=['id']).and_return([{
                'id':
                prefixed_pulp_repoid
            }]).once().ordered())
        (flexmock(mockpulp).should_receive('syncRepo').with_args(
            repo=prefixed_pulp_repoid, feed=docker_registry).and_return(
                ([], [])).once().ordered())
        (flexmock(mockpulp).should_receive('crane').with_args(
            [prefixed_pulp_repoid], wait=True).once().ordered())

        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        workflow = self.workflow([docker_repository], mockpulp.registry)
        workflow.postbuild_plugins_conf.append({
            'name': PulpSyncPlugin.key,
        }, )

        kwargs = {
            'pulp_registry_name': env,
            'docker_registry': docker_registry,
        }

        plugin = PulpSyncPlugin(tasker=None, workflow=workflow, **kwargs)

        plugin.run()

        manifests = get_manifests_in_pulp_repository(workflow)
        assert manifests == ['sha256:{}'.format(prefixed_pulp_repoid)]
    def test_dockpulp_loglevel(self, fail, caplog):
        loglevel = 3

        mockpulp = MockPulp()
        (
            flexmock(mockpulp)
            .should_receive("getRepos")
            .with_args(["redhat-prod-myrepository"], fields=["id"])
            .and_return([{"id": "redhat-prod-myrepository"}])
            .once()
            .ordered()
        )
        (flexmock(mockpulp).should_receive("syncRepo").and_return(([], [])))
        flexmock(dockpulp).should_receive("Pulp").and_return(mockpulp)
        logger = flexmock()
        expectation = logger.should_receive("setLevel").with_args(loglevel).once()
        if fail:
            expectation.and_raise(ValueError)

        (flexmock(dockpulp).should_receive("setup_logger").and_return(logger).once())

        plugin = PulpSyncPlugin(
            tasker=None,
            workflow=self.workflow(["prod/myrepository"]),
            pulp_registry_name="pulp",
            docker_registry="http://registry.example.com",
            dockpulp_loglevel=loglevel,
        )

        plugin.run()

        errors = [record.getMessage() for record in caplog.records() if record.levelname == "ERROR"]

        if fail:
            assert len(errors) >= 1
        else:
            assert not errors
示例#49
0
    def test_insecure_registry(self, insecure_registry, ssl_validation):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'
        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry,
                                insecure_registry=insecure_registry)

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args([prefixed_pulp_repoid], fields=['id'])
            .and_return([{'id': prefixed_pulp_repoid}])
            .once()
            .ordered())
        sync_exp = flexmock(mockpulp).should_receive('syncRepo')
        if ssl_validation is None:
            sync_exp = sync_exp.with_args(repo=prefixed_pulp_repoid,
                                          feed=docker_registry)
        else:
            sync_exp = sync_exp.with_args(repo=prefixed_pulp_repoid,
                                          feed=docker_registry,
                                          ssl_validation=ssl_validation)

        (sync_exp
            .and_return(([], []))
            .once()
            .ordered())
        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        plugin.run()
示例#50
0
    def test_pulp_auth(self, tmpdir, cer_exists, key_exists,
                       reactor_config_map):
        pulp_secret_path = str(tmpdir)
        cer = pulp_secret_path + '/pulp.cer'
        key = pulp_secret_path + '/pulp.key'
        if cer_exists:
            open(cer, 'w').close()

        if key_exists:
            open(key, 'w').close()

        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'

        if reactor_config_map:
            self.workflow.plugin_workspace = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
            self.workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\
                ReactorConfig({'version': 1, 'pulp': {'name': env}})

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry,
                                pulp_secret_path=pulp_secret_path)

        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('login').never())
        if cer_exists and key_exists:
            (flexmock(mockpulp).should_receive('set_certs').with_args(
                cer, key).once().ordered())
            (flexmock(mockpulp).should_receive('getRepos').with_args(
                [prefixed_pulp_repoid], fields=['id']).and_return([{
                    'id':
                    prefixed_pulp_repoid
                }]).once().ordered())
            (flexmock(mockpulp).should_receive('syncRepo').with_args(
                repo=prefixed_pulp_repoid, feed=docker_registry).and_return(
                    ([], [])).once().ordered())
            (flexmock(mockpulp).should_receive('crane').with_args(
                [prefixed_pulp_repoid], wait=True).once().ordered())
        else:
            (flexmock(mockpulp).should_receive('set_certs').never())
            (flexmock(mockpulp).should_receive('syncRepo').never())
            (flexmock(mockpulp).should_receive('crane').never())

        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        if cer_exists and key_exists:
            plugin.run()
        else:
            with pytest.raises(RuntimeError):
                plugin.run()
    def test_auth_certs(self, tmpdir, cer_exists, key_exists, source_secret,
                        monkeypatch):
        pulp_secret_path = str(tmpdir)
        cer = pulp_secret_path + '/pulp.cer'
        key = pulp_secret_path + '/pulp.key'
        if cer_exists:
            open(cer, 'w').close()

        if key_exists:
            open(key, 'w').close()

        if source_secret:
            monkeypatch.setenv('SOURCE_SECRET_PATH', pulp_secret_path)
            pulp_secret_path = None
        else:
            monkeypatch.delenv('SOURCE_SECRET_PATH', raising=False)

        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        env = 'pulp'
        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry,
                                pulp_secret_path=pulp_secret_path)

        mockpulp = MockPulp()
        (flexmock(mockpulp).should_receive('login').never())
        if cer_exists and key_exists:
            (flexmock(mockpulp).should_receive('set_certs').with_args(
                cer, key).once().ordered())
            (flexmock(mockpulp).should_receive('syncRepo').with_args(
                object,
                'prod-myrepository',  # pulp repository name
                config_file=object).and_return([{
                    'id':
                    'prefix-prod-myrepository'
                }])  # repo id
             .once().ordered())
            (flexmock(mockpulp).should_receive('crane').with_args(
                ['prefix-prod-myrepository'],  # repo id
                wait=True).once().ordered())
        else:
            (flexmock(mockpulp).should_receive('set_certs').never())
            (flexmock(mockpulp).should_receive('syncRepo').never())
            (flexmock(mockpulp).should_receive('crane').never())

        (flexmock(dockpulp).should_receive('Pulp').with_args(
            env=env).and_return(mockpulp))

        if cer_exists and key_exists:
            plugin.run()
        else:
            with pytest.raises(RuntimeError):
                plugin.run()
示例#52
0
    def test_auth_certs(self, tmpdir, cer_exists, key_exists, source_secret, monkeypatch):
        pulp_secret_path = str(tmpdir)
        cer = pulp_secret_path + '/pulp.cer'
        key = pulp_secret_path + '/pulp.key'
        if cer_exists:
            open(cer, 'w').close()

        if key_exists:
            open(key, 'w').close()

        if source_secret:
            monkeypatch.setenv('SOURCE_SECRET_PATH', pulp_secret_path)
            pulp_secret_path = None
        else:
            monkeypatch.delenv('SOURCE_SECRET_PATH', raising=False)

        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        env = 'pulp'
        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry,
                                pulp_secret_path=pulp_secret_path)

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('login')
            .never())
        if cer_exists and key_exists:
            (flexmock(mockpulp)
                .should_receive('set_certs')
                .with_args(cer, key)
                .once()
                .ordered())
            (flexmock(mockpulp)
                .should_receive('syncRepo')
                .with_args(object,
                           'prod-myrepository',  # pulp repository name
                           config_file=object)
                .and_return([{'id': 'prefix-prod-myrepository'}])  # repo id
                .once()
                .ordered())
            (flexmock(mockpulp)
                .should_receive('crane')
                .with_args(['prefix-prod-myrepository'],  # repo id
                           wait=True)
                .once()
                .ordered())
        else:
            (flexmock(mockpulp)
                .should_receive('set_certs')
                .never())
            (flexmock(mockpulp)
                .should_receive('syncRepo')
                .never())
            (flexmock(mockpulp)
                .should_receive('crane')
                .never())

        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        if cer_exists and key_exists:
            plugin.run()
        else:
            with pytest.raises(RuntimeError):
                plugin.run()
示例#53
0
    def test_publish(self, publish, has_pulp_push, should_publish, caplog):
        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('login')
            .never())
        (flexmock(mockpulp)
            .should_receive('set_certs')
            .never())
        (flexmock(mockpulp)
            .should_receive('getRepos')
            .with_args([prefixed_pulp_repoid], fields=['id'])
            .and_return([{'id': prefixed_pulp_repoid}])
            .once()
            .ordered())
        (flexmock(mockpulp)
            .should_receive('syncRepo')
            .with_args(repo=prefixed_pulp_repoid,
                       feed=docker_registry)
            .and_return(([], []))
            .once()
            .ordered())
        if should_publish:
            (flexmock(mockpulp)
                .should_receive('crane')
                .with_args([prefixed_pulp_repoid], wait=True)
                .once()
                .ordered())
        else:
            (flexmock(mockpulp)
                .should_receive('crane')
                .never())

        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        workflow = self.workflow([docker_repository], mockpulp.registry)
        workflow.postbuild_plugins_conf.append(
            {
                'name': PulpSyncPlugin.key,
            },
        )
        if has_pulp_push:
            workflow.postbuild_plugins_conf.append(
                {
                    'name': PLUGIN_PULP_PUSH_KEY,
                },
            )

        kwargs = {
            'pulp_registry_name': env,
            'docker_registry': docker_registry,
        }
        if publish is not None:
            kwargs['publish'] = publish

        plugin = PulpSyncPlugin(tasker=None,
                                workflow=workflow,
                                **kwargs)

        plugin.run()
        log_messages = [l.getMessage() for l in caplog.records()]

        for image in workflow.tag_conf.images:
            expected_log = 'image available at %s' % image.to_str()
            if should_publish:
                assert expected_log in log_messages
            else:
                assert expected_log not in log_messages
示例#54
0
    def test_pulp_auth(self, tmpdir, cer_exists, key_exists):
        pulp_secret_path = str(tmpdir)
        cer = pulp_secret_path + '/pulp.cer'
        key = pulp_secret_path + '/pulp.key'
        if cer_exists:
            open(cer, 'w').close()

        if key_exists:
            open(key, 'w').close()

        docker_registry = 'http://registry.example.com'
        docker_repository = 'prod/myrepository'
        prefixed_pulp_repoid = 'redhat-prod-myrepository'
        env = 'pulp'
        plugin = PulpSyncPlugin(tasker=None,
                                workflow=self.workflow([docker_repository]),
                                pulp_registry_name=env,
                                docker_registry=docker_registry,
                                pulp_secret_path=pulp_secret_path)

        mockpulp = MockPulp()
        (flexmock(mockpulp)
            .should_receive('login')
            .never())
        if cer_exists and key_exists:
            (flexmock(mockpulp)
                .should_receive('set_certs')
                .with_args(cer, key)
                .once()
                .ordered())
            (flexmock(mockpulp)
                .should_receive('getRepos')
                .with_args([prefixed_pulp_repoid], fields=['id'])
                .and_return([{'id': prefixed_pulp_repoid}])
                .once()
                .ordered())
            (flexmock(mockpulp)
                .should_receive('syncRepo')
                .with_args(repo=prefixed_pulp_repoid,
                           feed=docker_registry)
                .and_return(([], []))
                .once()
                .ordered())
            (flexmock(mockpulp)
                .should_receive('crane')
                .with_args([prefixed_pulp_repoid], wait=True)
                .once()
                .ordered())
        else:
            (flexmock(mockpulp)
                .should_receive('set_certs')
                .never())
            (flexmock(mockpulp)
                .should_receive('syncRepo')
                .never())
            (flexmock(mockpulp)
                .should_receive('crane')
                .never())

        (flexmock(dockpulp)
            .should_receive('Pulp')
            .with_args(env=env)
            .and_return(mockpulp))

        if cer_exists and key_exists:
            plugin.run()
        else:
            with pytest.raises(RuntimeError):
                plugin.run()