Esempio n. 1
0
    def setUp(self):
        super(DatabaseTestCase, self).setUp()

        # We don't want our SQLAlchemy session thrown away post-request because that rolls
        # back the transaction and no database assertions can be made.
        self.flask_app.teardown_request_funcs = {}

        if engine is None:
            # In the future we could provide a postgres URI to test against various
            # databases!
            _configure_db()

        self.connection = engine.connect()
        Base.metadata.create_all(bind=self.connection)
        PSABase.metadata.create_all(bind=self.connection)
        self.transaction = self.connection.begin()

        Session.remove()
        Session.configure(bind=self.connection, autoflush=False)
        self.session = Session()

        # Start a transaction after creating the schema, but before anything is
        # placed into the database. We'll roll back to the start of this
        # transaction at the end of every test, and code under test will start
        # nested transactions
        self.session.begin_nested()
Esempio n. 2
0
 def tearDown(self):
     """Roll back all the changes from the test and clean up the session."""
     self.session.close()
     self.transaction.rollback()
     self.connection.close()
     Session.remove()
     super(DatabaseTestCase, self).tearDown()
Esempio n. 3
0
 def tearDown(self):
     """Roll back all the changes from the test and clean up the session."""
     self.session.close()
     self.transaction.rollback()
     self.connection.close()
     Session.remove()
     super(DatabaseTestCase, self).tearDown()
Esempio n. 4
0
    def setUp(self):
        super(DatabaseTestCase, self).setUp()

        # We don't want our SQLAlchemy session thrown away post-request because that rolls
        # back the transaction and no database assertions can be made.
        self.flask_app.teardown_request_funcs = {}

        if engine is None:
            # In the future we could provide a postgres URI to test against various
            # databases!
            _configure_db()

        self.connection = engine.connect()
        Base.metadata.create_all(bind=self.connection)
        PSABase.metadata.create_all(bind=self.connection)
        self.transaction = self.connection.begin()

        Session.remove()
        Session.configure(bind=self.connection, autoflush=False)
        self.session = Session()

        # Start a transaction after creating the schema, but before anything is
        # placed into the database. We'll roll back to the start of this
        # transaction at the end of every test, and code under test will start
        # nested transactions
        self.session.begin_nested()
Esempio n. 5
0
    def test_db_config(self):
        """Assert creating the application configures the scoped session."""
        # Assert the scoped session is not bound.
        self.assertRaises(UnboundExecutionError, Session.get_bind)
        Session.remove()

        app.create(
            {"DB_URL": "sqlite://", "SOCIAL_AUTH_USER_MODEL": "anitya.db.models.User"}
        )
        self.assertEqual("sqlite://", str(Session().get_bind().url))
Esempio n. 6
0
    def test_db_config(self):
        """Assert creating the application configures the scoped session."""
        # Assert the scoped session is not bound.
        self.assertRaises(UnboundExecutionError, Session.get_bind)
        Session.remove()

        app.create({
            'DB_URL': 'sqlite://',
            'SOCIAL_AUTH_USER_MODEL': 'anitya.db.models.User',
        })
        self.assertEqual('sqlite://', str(Session().get_bind().url))
Esempio n. 7
0
def shutdown_session(exception=None):
    ''' Remove the DB session at the end of each request. '''
    Session.remove()
Esempio n. 8
0
 def setUp(self):
     # Make sure each test starts with a clean session
     Session.remove()
     Session.configure(bind=None)
Esempio n. 9
0
    def process_message(self, message):
        """
        This method is called when a incoming SSE message is received.

        If the project is unknown to Anitya, a new :class:`Project` is created,
        but only if the platform is whitelisted. See `self.whitelist`.
        If it's an existing project, this will call Anitya's ``check_project_release``
        API to update the latest version information. This is because we are
        subscribed to libraries.io over HTTP and therefore have no
        authentication.

        Checking the version ourselves serves two purposes. Firstly, we connect
        to libraries.io over HTTP so we don't have any authentication.
        Secondly, we might map the libraries.io project to the wrong Anitya
        project, so this is a good opportunity to catch those problems.

        Args:
            message (dict): The fedmsg to process.
        """
        # The SSE spec requires all data to be UTF-8 encoded
        try:
            librariesio_msg = json.loads(message.data, encoding="utf-8")
        except json.JSONDecodeError:
            _log.warning(
                "Dropping librariesio update message. Invalid json '{}'.".
                format(message.data))
            return
        name = librariesio_msg["name"]
        platform = librariesio_msg["platform"].lower()
        version = librariesio_msg["version"]
        homepage = librariesio_msg["package_manager_url"]
        for ecosystem in plugins.ECOSYSTEM_PLUGINS.get_plugins():
            if platform == ecosystem.name or platform in ecosystem.aliases:
                break
        else:
            _log.debug("Dropped librariesio update to {} for {} ({}) since"
                       " it is on the unsupported {} platform".format(
                           version, name, homepage, platform))
            return

        session = Session()
        project = models.Project.query.filter_by(
            name=name, ecosystem_name=ecosystem.name).one_or_none()
        if project is None and platform in self.whitelist:
            try:
                project = utilities.create_project(
                    session,
                    name,
                    homepage,
                    "anitya",
                    backend=ecosystem.default_backend)
                utilities.check_project_release(project, Session)
                _log.info(
                    "Discovered new project at version {} via libraries.io: {}"
                    .format(version, project))
            except exceptions.AnityaException as e:
                _log.error(
                    "A new project was discovered via libraries.io, {}, "
                    'but we failed with "{}"'.format(name, str(e)))
        elif project is None:
            _log.info(
                "Discovered new project, {}, on the {} platform, but anitya is "
                "configured to not create the project".format(name, platform))
        else:
            _log.info(
                "libraries.io has found an update (version {}) for project {}".
                format(version, project.name))
            # This will fetch the version, emit fedmsgs, add log entries, and
            # commit the transaction.
            try:
                utilities.check_project_release(project, session)
            except exceptions.AnityaPluginException as e:
                _log.warning(
                    "libraries.io found an update for {}, but we failed with {}"
                    .format(project, str(e)))

        # Refresh the project object that was committed by either
        # ``create_project`` or ``check_project_release`` above
        project = models.Project.query.filter_by(
            name=name, ecosystem_name=ecosystem.name).one_or_none()
        if project and project.latest_version != version:
            _log.info(
                "libraries.io found {} had a latest version of {}, but Anitya found {}"
                .format(project, version, project.latest_version))

        Session.remove()
Esempio n. 10
0
 def setUp(self):
     # Make sure each test starts with a clean session
     Session.remove()
     Session.configure(bind=None)
Esempio n. 11
0
    def consume(self, message):
        """
        This method is called when a message with a topic this consumer
        subscribes to arrives.

        If the project is unknown to Anitya, a new :class:`Project` is created,
        but only if the 'create_librariesio_projects' configuration key is set.
        If it's an existing project, this will call Anitya's ``check_project_release``
        API to update the latest version information. This is because we are
        subscribed to libraries.io over HTTP and therefore have no
        authentication.

        Checking the version ourselves serves two purposes. Firstly, we connect
        to libraries.io over HTTP so we don't have any authentication.
        Secondly, we might map the libraries.io project to the wrong Anitya
        project, so this is a good opportunity to catch those problems.

        Args:
            message (dict): The fedmsg to process.
        """
        librariesio_msg = message['body']['msg']['data']
        name = librariesio_msg['name']
        platform = librariesio_msg['platform'].lower()
        version = librariesio_msg['version']
        homepage = librariesio_msg['package_manager_url']
        for ecosystem in plugins.ECOSYSTEM_PLUGINS.get_plugins():
            if platform == ecosystem.name or platform in ecosystem.aliases:
                break
        else:
            _log.debug(
                'Dropped librariesio update to %s for %s (%s) since it is on the '
                'unsupported %s platform', version, name, homepage, platform)
            return

        session = Session()
        project = models.Project.by_name_and_ecosystem(session, name,
                                                       ecosystem.name)
        if project is None and platform in config.config[
                'LIBRARIESIO_PLATFORM_WHITELIST']:
            try:
                project = utilities.create_project(
                    session,
                    name,
                    homepage,
                    'anitya',
                    backend=ecosystem.default_backend,
                    check_release=True,
                )
                _log.info(
                    'Discovered new project at version %s via libraries.io: %r',
                    version, project)
            except exceptions.AnityaException as e:
                _log.error(
                    'A new project was discovered via libraries.io, %r, '
                    'but we failed with "%s"', project, str(e))
        elif project is None:
            _log.info(
                'Discovered new project, %s, on the %s platform, but anitya is '
                'configured to not create the project', name, platform)
        else:
            _log.info(
                'libraries.io has found an update (version %s) for project %r',
                version, project)
            # This will fetch the version, emit fedmsgs, add log entries, and
            # commit the transaction.
            try:
                utilities.check_project_release(project, session)
            except exceptions.AnityaPluginException as e:
                _log.warning(
                    'libraries.io found an update for %r, but we failed with %s',
                    project, str(e))

        # Refresh the project object that was committed by either
        # ``create_project`` or ``check_project_release`` above
        project = models.Project.by_name_and_ecosystem(session, name,
                                                       ecosystem.name)
        if project and project.latest_version != version:
            _log.info(
                'libraries.io found %r had a latest version of %s, but Anitya found %s',
                project, version, project.latest_version)

        Session.remove()
Esempio n. 12
0
def shutdown_session(exception=None):
    """ Remove the DB session at the end of each request. """
    Session.remove()
Esempio n. 13
0
    def consume(self, message):
        """
        This method is called when a message with a topic this consumer
        subscribes to arrives.

        If the project is unknown to Anitya, a new :class:`Project` is created,
        but only if the 'create_librariesio_projects' configuration key is set.
        If it's an existing project, this will call Anitya's ``check_project_release``
        API to update the latest version information. This is because we are
        subscribed to libraries.io over HTTP and therefore have no
        authentication.

        Checking the version ourselves serves two purposes. Firstly, we connect
        to libraries.io over HTTP so we don't have any authentication.
        Secondly, we might map the libraries.io project to the wrong Anitya
        project, so this is a good opportunity to catch those problems.

        Args:
            message (dict): The fedmsg to process.
        """
        librariesio_msg = message["body"]["msg"]["data"]
        name = librariesio_msg["name"]
        platform = librariesio_msg["platform"].lower()
        version = librariesio_msg["version"]
        homepage = librariesio_msg["package_manager_url"]
        for ecosystem in plugins.ECOSYSTEM_PLUGINS.get_plugins():
            if platform == ecosystem.name or platform in ecosystem.aliases:
                break
        else:
            _log.debug(
                "Dropped librariesio update to %s for %s (%s) since it is on the "
                "unsupported %s platform",
                version,
                name,
                homepage,
                platform,
            )
            return

        session = Session()
        project = models.Project.by_name_and_ecosystem(session, name, ecosystem.name)
        if (
            project is None
            and platform in config.config["LIBRARIESIO_PLATFORM_WHITELIST"]
        ):
            try:
                project = utilities.create_project(
                    session,
                    name,
                    homepage,
                    "anitya",
                    backend=ecosystem.default_backend,
                    check_release=True,
                )
                _log.info(
                    "Discovered new project at version %s via libraries.io: %r",
                    version,
                    project,
                )
            except exceptions.AnityaException as e:
                _log.error(
                    "A new project was discovered via libraries.io, %r, "
                    'but we failed with "%s"',
                    project,
                    str(e),
                )
        elif project is None:
            _log.info(
                "Discovered new project, %s, on the %s platform, but anitya is "
                "configured to not create the project",
                name,
                platform,
            )
        else:
            _log.info(
                "libraries.io has found an update (version %s) for project %r",
                version,
                project,
            )
            # This will fetch the version, emit fedmsgs, add log entries, and
            # commit the transaction.
            try:
                utilities.check_project_release(project, session)
            except exceptions.AnityaPluginException as e:
                _log.warning(
                    "libraries.io found an update for %r, but we failed with %s",
                    project,
                    str(e),
                )

        # Refresh the project object that was committed by either
        # ``create_project`` or ``check_project_release`` above
        project = models.Project.by_name_and_ecosystem(session, name, ecosystem.name)
        if project and project.latest_version != version:
            _log.info(
                "libraries.io found %r had a latest version of %s, but Anitya found %s",
                project,
                version,
                project.latest_version,
            )

        Session.remove()