Exemplo n.º 1
0
    def initialize_database_for_testing(self, db_file_path=None):
        """Initialize a SQLite database for testing.

        To force monitor_db and the host_scheduler to use the same SQLite file
        database, call this method before initializing the database through
        frontend_test_utils. The host_scheduler is setup to look for the
        host_scheduler_db when invoked with --testing.

        @param db_file_path: The name of the file to use to create
            a SQLite database. Since this database is shared across different
            processes using a file is closer to the real world.
        """
        if not db_file_path:
            db_file_path = self.DB_FILE
        # TODO: Move the translating database elsewhere. Monitor_db circular
        # imports host_scheduler.
        from autotest_lib.frontend import setup_test_environment
        from django.conf import settings
        self.old_django_db_name = settings.DATABASES['default']['NAME']
        settings.DATABASES['default']['NAME'] = db_file_path
        self.db_file_path = db_file_path
        _db_manager = scheduler_lib.ConnectionManager(autocommit=False)
        _db_manager.db_connection = (
                database_connection.TranslatingDatabase.get_test_database(
                translators=scheduler_lib._DB_TRANSLATORS))
    def _set_monitor_stubs(self):
        self.mock_config = global_config.FakeGlobalConfig()
        self.god.stub_with(global_config, 'global_config', self.mock_config)

        # Clear the instance cache as this is a brand new database.
        scheduler_models.DBObject._clear_instance_cache()

        self._database = (
            database_connection.TranslatingDatabase.get_test_database(
                translators=scheduler_lib._DB_TRANSLATORS))
        self._database.connect(db_type='django')
        self._database.debug = _DEBUG

        connection_manager = scheduler_lib.ConnectionManager(autocommit=False)
        self.god.stub_with(connection_manager, 'db_connection', self._database)
        self.god.stub_with(monitor_db, '_db_manager', connection_manager)
        self.god.stub_with(monitor_db, '_db', self._database)

        self.god.stub_with(monitor_db.BaseDispatcher,
                           '_get_pending_queue_entries',
                           self._get_pending_hqes)
        self.god.stub_with(scheduler_models, '_db', self._database)
        self.god.stub_with(drone_manager.instance(), '_results_dir',
                           '/test/path')
        self.god.stub_with(drone_manager.instance(), '_temporary_directory',
                           '/test/path/tmp')
        self.god.stub_with(drone_manager.instance(), 'initialize',
                           lambda *args: None)
        self.god.stub_with(drone_manager.instance(), 'execute_actions',
                           lambda *args: None)

        monitor_db.initialize_globals()
        scheduler_models.initialize_globals()
    def testConnectionManagerSingleton(self):
        """Test that the singleton works as expected."""
        # Confirm that instantiating the class applies global db settings.
        connection_manager = scheduler_lib.ConnectionManager()
        readonly_connection.set_globally_disabled.assert_called_once_with(True)
        setup_django_environment.enable_autocommit.assert_called_once_with()

        readonly_connection.set_globally_disabled.reset_mock()
        setup_django_environment.enable_autocommit.reset_mock()

        # Confirm that instantiating another connection manager doesn't change
        # the database settings, and in fact, returns the original manager.
        connection_manager_2 = scheduler_lib.ConnectionManager()
        self.assertTrue(connection_manager == connection_manager_2)
        self.assertTrue(
            readonly_connection.set_globally_disabled.call_count == 0)
        self.assertTrue(
            setup_django_environment.enable_autocommit.call_count == 0)

        # Confirm that we don't open the connection when the class is
        # instantiated.
        self.assertTrue(connection_manager.db_connection is None)
 def testConnectionReconnect(self):
     """Test that retries don't destroy the connection."""
     database_connection._DjangoBackend.execute = mock.MagicMock()
     database_connection._DjangoBackend.execute.side_effect = (
         django_utils.DatabaseError('Database Error'))
     connection_manager = scheduler_lib.ConnectionManager()
     connection = connection_manager.get_connection()
     self.assertRaises(django_utils.DatabaseError, connection.execute,
                       *('', None, True))
     self.assertTrue(
         database_connection._DjangoBackend.execute.call_count == 2)
     database_connection._DjangoBackend.execute.reset_mock()
     self.assertTrue(connection_manager.db_connection ==
                     connection_manager.get_connection())
    def testConnectionDisconnect(self):
        """Test connection and disconnecting from the database."""
        # Test that the connection manager only opens a connection once.
        connection_manager = scheduler_lib.ConnectionManager()
        connection_manager.open_connection = mock.MagicMock()
        connection = connection_manager.get_connection()
        connection_manager.open_connection.assert_called_once_with()
        connection_manager.open_connection.reset_mock()
        connection = connection_manager.get_connection()
        self.assertTrue(connection_manager.open_connection.call_count == 0)
        connection_manager.open_connection.reset_mock()

        # Test that del on the connection manager closes the connection
        connection_manager.disconnect = mock.MagicMock()
        connection_manager.__del__()
        connection_manager.disconnect.assert_called_once_with()
Exemplo n.º 6
0
def initialize(testing=False):
    """Initialize the host scheduler."""
    if testing:
        # Don't import testing utilities unless we're in testing mode,
        # as the database imports have side effects.
        from autotest_lib.scheduler import rdb_testing_utils
        rdb_testing_utils.FileDatabaseHelper().initialize_database_for_testing(
            db_file_path=rdb_testing_utils.FileDatabaseHelper.DB_FILE)
    global _db_manager
    _db_manager = scheduler_lib.ConnectionManager()
    scheduler_lib.setup_logging(os.environ.get('AUTOTEST_SCHEDULER_LOG_DIR',
                                               None),
                                None,
                                timestamped_logfile_prefix='host_scheduler')
    logging.info("Setting signal handler")
    signal.signal(signal.SIGINT, handle_signal)
    signal.signal(signal.SIGTERM, handle_signal)
    scheduler_models.initialize()
Exemplo n.º 7
0
    def setUp(self, inline_host_acquisition=True, setup_tables=True):
        """Common setup module for tests that need a jobs/host database.

        @param inline_host_acquisition: If True, the dispatcher tries to acquire
            hosts inline with the rest of the tick.
        """
        self.db_helper = DBHelper()
        self._database = self.db_helper.database
        # Runs syncdb setting up initial database conditions
        self._frontend_common_setup(setup_tables=setup_tables)
        connection_manager = scheduler_lib.ConnectionManager(autocommit=False)
        self.god.stub_with(connection_manager, 'db_connection', self._database)
        self.god.stub_with(monitor_db, '_db_manager', connection_manager)
        self.god.stub_with(scheduler_models, '_db', self._database)
        self.god.stub_with(monitor_db, '_inline_host_acquisition',
                           inline_host_acquisition)
        self._dispatcher = monitor_db.Dispatcher()
        self.host_scheduler = self._dispatcher._host_scheduler
        self.host_query_manager = query_managers.AFEHostQueryManager()
        self.job_query_manager = self._dispatcher._job_query_manager
        self._release_unused_hosts()
Exemplo n.º 8
0
def initialize():
    global _db
    _db = scheduler_lib.ConnectionManager().get_connection()

    notify_statuses_list = global_config.global_config.get_config_value(
        scheduler_config.CONFIG_SECTION, "notify_email_statuses", default='')
    global _notify_email_statuses
    _notify_email_statuses = [
        status for status in re.split(r'[\s,;:]', notify_statuses_list.lower())
        if status
    ]

    # AUTOTEST_WEB.base_url is still a supported config option as some people
    # may wish to override the entire url.
    global _base_url
    config_base_url = global_config.global_config.get_config_value(
        scheduler_config.CONFIG_SECTION, 'base_url', default='')
    if config_base_url:
        _base_url = config_base_url
    else:
        _base_url = afe_urls.ROOT_URL

    initialize_globals()
    def __init__(self):
        """Create an AFEHostQueryManager.

        @param db: A connection to the database with the afe_hosts table.
        """
        self._db = scheduler_lib.ConnectionManager().get_connection()