示例#1
0
    def setUp(self):
        connection.initialize()
        self.patch1 = mock.patch(
            'pulp.server.webservices.controllers.decorators.'
            'check_preauthenticated')
        self.patch2 = mock.patch(
            'pulp.server.webservices.controllers.decorators.'
            'is_consumer_authorized')
        self.patch3 = mock.patch('pulp.server.webservices.http.resource_path')
        self.patch4 = mock.patch('pulp.server.webservices.http.header')
        self.patch5 = mock.patch('web.webapi.HTTPError')
        self.patch6 = mock.patch(
            'pulp.server.managers.factory.principal_manager')
        self.patch7 = mock.patch(
            'pulp.server.managers.factory.user_query_manager')

        self.patch8 = mock.patch('pulp.server.webservices.http.uri_path')
        self.mock_check_pre_auth = self.patch1.start()
        self.mock_check_pre_auth.return_value = 'ws-user'
        self.mock_check_auth = self.patch2.start()
        self.mock_check_auth.return_value = True
        self.mock_http_resource_path = self.patch3.start()
        self.patch4.start()
        self.patch5.start()
        self.patch6.start()
        self.mock_user_query_manager = self.patch7.start()
        self.mock_user_query_manager.return_value.is_superuser.return_value = False
        self.mock_user_query_manager.return_value.is_authorized.return_value = True
        self.mock_uri_path = self.patch8.start()
        self.mock_uri_path.return_value = "/mock/"
示例#2
0
    def test_multiple_calls_errors(self, mongoengine):
        """
        This test asserts that more than one call to initialize() raises a RuntimeError.
        """
        mongoengine.connect.return_value.server_info.return_value = {
            'version': '2.6.0'
        }
        # The first call to initialize should be fine
        connection.initialize()

        # A second call to initialize() should raise a RuntimeError
        self.assertRaises(RuntimeError, connection.initialize)

        # The connection should still be initialized
        self.assertEqual(connection._CONNECTION,
                         mongoengine.connect.return_value)
        self.assertEqual(connection._DATABASE,
                         mongoengine.connection.get_db.return_value)
        # Connect should still have been called correctly
        name = config.config.get('database', 'name')
        host = config.config.get('database', 'seeds').split(':')[0]
        port = int(config.config.get('database', 'seeds').split(':')[1])
        mongoengine.connect.assert_called_once_with(name,
                                                    host=host,
                                                    max_pool_size=10,
                                                    port=port)
示例#3
0
文件: scheduler.py 项目: hjensas/pulp
    def setup_schedule(self):
        """
        This loads enabled schedules from the database and adds them to the
        "_schedule" dictionary as instances of celery.beat.ScheduleEntry
        """
        if not Scheduler._mongo_initialized:
            _logger.debug('Initializing Mongo client connection to read celerybeat schedule')
            db_connection.initialize()
            Scheduler._mongo_initialized = True
        _logger.debug(_('loading schedules from app'))
        self._schedule = {}
        for key, value in self.app.conf.CELERYBEAT_SCHEDULE.iteritems():
            self._schedule[key] = beat.ScheduleEntry(**dict(value, name=key))

        # include a "0" as the default in case there are no schedules to load
        update_timestamps = [0]

        _logger.debug(_('loading schedules from DB'))
        ignored_db_count = 0
        self._loaded_from_db_count = 0
        for call in itertools.imap(ScheduledCall.from_db, utils.get_enabled()):
            if call.remaining_runs == 0:
                _logger.debug(
                    _('ignoring schedule with 0 remaining runs: %(id)s') % {'id': call.id})
                ignored_db_count += 1
            else:
                self._schedule[call.id] = call.as_schedule_entry()
                update_timestamps.append(call.last_updated)
                self._loaded_from_db_count += 1

        _logger.debug('loaded %(count)d schedules' % {'count': self._loaded_from_db_count})

        self._most_recent_timestamp = max(update_timestamps)
示例#4
0
    def test_ssl_is_configured_with_ssl_certfile(self, mock_mongoengine, mock_ssl):
        mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}
        host = 'champs.example.com:27018'
        replica_set = ''

        connection.initialize()

        database = config.config.get('database', 'name')
        max_pool_size = connection._DEFAULT_MAX_POOL_SIZE
        ssl_cert_reqs = mock_ssl.CERT_NONE
        ssl_ca_certs = config.config.get('database', 'ca_path')
        # There should be two calls to connect. The first will use the server version to determine
        # which write concern is safe to use.
        self.assertEqual(mock_mongoengine.connect.call_count, 2)
        self.assertEqual(mock_mongoengine.connect.mock_calls[0][1], (database,))
        self.assertEqual(
            mock_mongoengine.connect.mock_calls[0][2],
            {'host': host, 'maxPoolSize': max_pool_size, 'ssl': True,
             'ssl_cert_reqs': ssl_cert_reqs, 'ssl_ca_certs': ssl_ca_certs,
             'ssl_certfile': 'certfilepath', 'replicaSet': replica_set})
        self.assertEqual(mock_mongoengine.connect.mock_calls[4][1], (database,))
        self.assertEqual(
            mock_mongoengine.connect.mock_calls[4][2],
            {'host': host, 'maxPoolSize': max_pool_size, 'ssl': True,
             'ssl_cert_reqs': ssl_cert_reqs, 'ssl_ca_certs': ssl_ca_certs,
             'ssl_certfile': 'certfilepath', 'replicaSet': replica_set, 'w': 'majority'})
示例#5
0
def main():
    """
    Populate ldap server with some test data
    """
    print("See populate.log for descriptive output.")
    factory.initialize()
    connection.initialize()
    ldapserv = LDAPConnection(admin='cn=Directory Manager',
                              password='******',
                              server='ldap://*****:*****@redhat.com' % userid)
        lattr.setDN("uid=%s,dc=rdu,dc=redhat,dc=com" % userid)
        attr, dn = lattr.buildBody()
        ldapserv.add_users(dn, attrs=attr)
    ldapserv.lookup_user("dc=rdu,dc=redhat,dc=com", "pulpuser1")
    ldapserv.authenticate_user("dc=rdu,dc=redhat,dc=com", "pulpuser1",
                               "redhat")
    ldapserv.disconnect()
示例#6
0
 def test_ssl_is_skipped_if_off(self, mock_mongoengine):
     mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}
     config.config.set('database', 'ssl', 'false')
     connection.initialize()
     seeds = config.config.get('database', 'seeds')
     max_pool_size = connection._DEFAULT_MAX_POOL_SIZE
     mock_mongoengine.connect.assert_called_once_with(seeds, max_pool_size=max_pool_size)
示例#7
0
    def test_database_max_pool_size(self, mock_mongoengine):
        """
        Assert that the max_pool_size parameter to initialize() is handled appropriately.
        """
        mock_mongoengine.connect.return_value.server_info.return_value = {
            'version': '2.6.0'
        }

        connection.initialize(max_pool_size=5)

        database = config.config.get('database', 'name')
        host = config.config.get('database', 'seeds')
        # There should be two calls to connect. The first will use the server version to determine
        # which write concern is safe to use.
        self.assertEqual(mock_mongoengine.connect.call_count, 2)
        self.assertEqual(mock_mongoengine.connect.mock_calls[0][1],
                         (database, ))
        self.assertEqual(mock_mongoengine.connect.mock_calls[0][2], {
            'host': host,
            'maxPoolSize': 5
        })
        self.assertEqual(mock_mongoengine.connect.mock_calls[4][1],
                         (database, ))
        self.assertEqual(mock_mongoengine.connect.mock_calls[4][2], {
            'host': host,
            'maxPoolSize': 5,
            'w': 'majority'
        })
示例#8
0
    def test_ssl_is_skipped_if_off(self, mock_mongoengine):
        mock_mongoengine.connect.return_value.server_info.return_value = {
            'version': '2.6.0'
        }
        host = 'champs.example.com:27018'
        replica_set = ''

        connection.initialize()

        database = config.config.get('database', 'name')
        max_pool_size = connection._DEFAULT_MAX_POOL_SIZE
        # There should be two calls to connect. The first will use the server version to determine
        # which write concern is safe to use.
        self.assertEqual(mock_mongoengine.connect.call_count, 2)
        self.assertEqual(mock_mongoengine.connect.mock_calls[0][1],
                         (database, ))
        self.assertEqual(mock_mongoengine.connect.mock_calls[0][2], {
            'host': host,
            'maxPoolSize': max_pool_size,
            'replicaSet': replica_set
        })
        self.assertEqual(mock_mongoengine.connect.mock_calls[4][1],
                         (database, ))
        self.assertEqual(
            mock_mongoengine.connect.mock_calls[4][2], {
                'host': host,
                'maxPoolSize': max_pool_size,
                'replicaSet': replica_set,
                'w': 'majority'
            })
示例#9
0
    def test_seeds_invalid(self, mock_mongoengine):
        mock_mongoengine.connect.return_value.server_info.return_value = {
            'version': '2.6.0'
        }

        connection.initialize(seeds='localhost:27017:1234')

        max_pool_size = connection._DEFAULT_MAX_POOL_SIZE
        database = config.config.get('database', 'name')
        # There should be two calls to connect. The first will use the server version to determine
        # which write concern is safe to use.
        self.assertEqual(mock_mongoengine.connect.call_count, 2)
        self.assertEqual(mock_mongoengine.connect.mock_calls[0][1],
                         (database, ))
        self.assertEqual(mock_mongoengine.connect.mock_calls[0][2], {
            'host': 'localhost:27017:1234',
            'maxPoolSize': max_pool_size
        })
        self.assertEqual(mock_mongoengine.connect.mock_calls[4][1],
                         (database, ))
        self.assertEqual(
            mock_mongoengine.connect.mock_calls[4][2], {
                'host': 'localhost:27017:1234',
                'maxPoolSize': max_pool_size,
                'w': 'majority'
            })
示例#10
0
    def test_database_max_pool_size_default_is_10(self, mock_mongoengine):
        """
        Assert that the max_pool_size parameter defaults to 10.
        """
        mock_mongoengine.connect.return_value.server_info.return_value = {
            'version': '2.6.0'
        }
        host = 'champs.example.com:27018'

        connection.initialize()

        database = config.config.get('database', 'name')

        # There should be two calls to connect. The first will use the server version to determine
        # which write concern is safe to use.
        self.assertEqual(mock_mongoengine.connect.call_count, 2)
        self.assertEqual(mock_mongoengine.connect.mock_calls[0][1],
                         (database, ))
        self.assertEqual(mock_mongoengine.connect.mock_calls[0][2], {
            'host': host,
            'maxPoolSize': 10
        })
        self.assertEqual(mock_mongoengine.connect.mock_calls[4][1],
                         (database, ))
        self.assertEqual(mock_mongoengine.connect.mock_calls[4][2], {
            'host': host,
            'maxPoolSize': 10,
            'w': 'majority'
        })
示例#11
0
    def test_name_is_set_from_argument(self, mock_mongoengine):
        """
        Assert that passing a name to initialize() overrides the value from the config.
        """
        mock_mongoengine.connect.return_value.server_info.return_value = {
            'version': '2.6.0'
        }
        name = 'name_set_from_argument'
        host = 'champs.example.com:27018'

        connection.initialize(name=name)

        # There should be two calls to connect. The first will use the server version to determine
        # which write concern is safe to use.
        self.assertEqual(mock_mongoengine.connect.call_count, 2)
        self.assertEqual(mock_mongoengine.connect.mock_calls[0][1], (name, ))
        self.assertEqual(mock_mongoengine.connect.mock_calls[0][2], {
            'host': host,
            'maxPoolSize': 10
        })
        self.assertEqual(mock_mongoengine.connect.mock_calls[4][1], (name, ))
        self.assertEqual(mock_mongoengine.connect.mock_calls[4][2], {
            'host': host,
            'maxPoolSize': 10,
            'w': 'majority'
        })
示例#12
0
    def test_database_replica_set_from_config(self, mock_mongoengine):
        """
        Assert that replica set configuration defaults to the configured value if not provided.
        """
        mock_mongoengine.connect.return_value.server_info.return_value = {
            'version': '2.6.0'
        }

        connection.initialize()

        max_pool_size = connection._DEFAULT_MAX_POOL_SIZE
        # There should be two calls to connect. The first will use the server version to determine
        # which write concern is safe to use.
        self.assertEqual(mock_mongoengine.connect.call_count, 2)
        self.assertEqual(mock_mongoengine.connect.mock_calls[0][1],
                         ('nbachamps', ))
        self.assertEqual(
            mock_mongoengine.connect.mock_calls[0][2], {
                'host': 'champs.example.com:27018',
                'maxPoolSize': max_pool_size,
                'replicaSet': 'real_replica_set'
            })
        self.assertEqual(mock_mongoengine.connect.mock_calls[4][1],
                         ('nbachamps', ))
        self.assertEqual(
            mock_mongoengine.connect.mock_calls[4][2], {
                'host': 'champs.example.com:27018',
                'maxPoolSize': max_pool_size,
                'replicaSet': 'real_replica_set',
                'w': 'majority'
            })
示例#13
0
    def test__DATABASE_uses_default_name(self, mock_mongoengine):
        """
        Assert that the name from the database config is used if not provided as a parameter to
        initialize().
        """
        mock_mongoengine.connect.return_value.server_info.return_value = {
            'version': '2.6.0'
        }
        host = 'champs.example.com:27018'

        connection.initialize()

        name = config.config.get('database', 'name')
        # There should be two calls to connect. The first will use the server version to determine
        # which write concern is safe to use.
        self.assertEqual(mock_mongoengine.connect.call_count, 2)
        self.assertEqual(mock_mongoengine.connect.mock_calls[0][1], (name, ))
        self.assertEqual(mock_mongoengine.connect.mock_calls[0][2], {
            'host': host,
            'maxPoolSize': 10
        })
        self.assertEqual(mock_mongoengine.connect.mock_calls[4][1], (name, ))
        self.assertEqual(mock_mongoengine.connect.mock_calls[4][2], {
            'host': host,
            'maxPoolSize': 10,
            'w': 'majority'
        })
示例#14
0
    def test_multiple_calls_errors(self, mongoengine):
        """
        This test asserts that more than one call to initialize() raises a RuntimeError.
        """
        mongoengine.connect.return_value.server_info.return_value = {
            'version': '2.6.0'
        }
        # The first call to initialize should be fine
        connection.initialize()

        # A second call to initialize() should raise a RuntimeError
        self.assertRaises(RuntimeError, connection.initialize)

        # The connection should still be initialized
        self.assertEqual(connection._CONNECTION,
                         mongoengine.connect.return_value)
        self.assertEqual(connection._DATABASE,
                         mongoengine.connection.get_db.return_value)
        # Connect should still have been called correctly
        name = config.config.get('database', 'name')
        host = config.config.get('database', 'seeds')
        # There should be two calls to connect. The first will use the server version to determine
        # which write concern is safe to use.
        self.assertEqual(mongoengine.connect.call_count, 2)
        self.assertEqual(mongoengine.connect.mock_calls[0][1], (name, ))
        self.assertEqual(mongoengine.connect.mock_calls[0][2], {
            'host': host,
            'maxPoolSize': 10
        })
        self.assertEqual(mongoengine.connect.mock_calls[4][1], (name, ))
        self.assertEqual(mongoengine.connect.mock_calls[4][2], {
            'host': host,
            'maxPoolSize': 10,
            'w': 'majority'
        })
示例#15
0
def main():
    """
    Populate ldap server with some test data
    """
    print("See populate.log for descriptive output.")
    factory.initialize()
    connection.initialize()
    ldapserv = LDAPConnection(admin='cn=Directory Manager',
                              password='******',
                              server='ldap://*****:*****@redhat.com' % userid)
        lattr.setDN("uid=%s,dc=rdu,dc=redhat,dc=com" % userid)
        attr, dn = lattr.buildBody()
        ldapserv.add_users(dn, attrs=attr)
    ldapserv.lookup_user("dc=rdu,dc=redhat,dc=com", "pulpuser1")
    ldapserv.authenticate_user("dc=rdu,dc=redhat,dc=com", "pulpuser1", "redhat")
    ldapserv.disconnect()
示例#16
0
 def test_seeds_from_config(self, mock_mongoengine):
     mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}
     config.config.set('database', 'seeds', 'other_value_for_seeds')
     connection.initialize()
     max_pool_size = connection._DEFAULT_MAX_POOL_SIZE
     mock_mongoengine.connect.assert_called_once_with('other_value_for_seeds',
                                                      max_pool_size=max_pool_size)
示例#17
0
    def test_initialize_username_and_shadows_password(self, mock_mongoengine, mock_log):
        """
        Assert that the password and password length are not logged.
        """
        mock_mongoengine_instance = mock_mongoengine.connect.return_value
        mock_mongoengine_instance.server_info.return_value = {"version":
                                                              MONGO_MIN_TEST_VERSION}

        connection.initialize()

        # There should be two calls to connect. The first will use the server version to determine
        # which write concern is safe to use.
        self.assertEqual(mock_mongoengine.connect.call_count, 2)
        self.assertEqual(mock_mongoengine.connect.mock_calls[0][1], ('nbachamps',))
        self.assertEqual(
            mock_mongoengine.connect.mock_calls[0][2],
            {'username': '******', 'host': 'champs.example.com:27018', 'password': '******',
             'maxPoolSize': 10, 'replicaSet': ''})
        self.assertEqual(mock_mongoengine.connect.mock_calls[4][1], ('nbachamps',))
        self.assertEqual(
            mock_mongoengine.connect.mock_calls[4][2],
            {'username': '******', 'host': 'champs.example.com:27018', 'password': '******',
                'maxPoolSize': 10, 'replicaSet': '', 'w': 1})
        expected_calls = [
            call('Attempting username and password authentication.'),
            call("Connection Arguments: {'username': '******', 'host': "
                 "'champs.example.com:27018', 'password': '******', 'maxPoolSize': 10, "
                 "'replicaSet': ''}"),
            call("Connection Arguments: {'username': '******', 'replicaSet': '', 'host': "
                 "'champs.example.com:27018', 'maxPoolSize': 10, 'w': 1, 'password': '******'}"),
            call('Querying the database to validate the connection.')]
        mock_log.assert_has_calls(expected_calls)
示例#18
0
    def test_initialize_username_and_shadows_password(self, mock_mongoengine,
                                                      mock_log):
        """
        Assert that the password and password length are not logged.
        """
        mock_mongoengine_instance = mock_mongoengine.connect.return_value
        mock_mongoengine_instance.server_info.return_value = {
            "version": MONGO_MIN_TEST_VERSION
        }

        connection.initialize()

        mock_mongoengine.connect.assert_called_once_with(
            'nbachamps',
            username='******',
            host='champs.example.com:27018',
            password='******',
            max_pool_size=10,
            replicaSet='')
        expected_calls = [
            call('Attempting username and password authentication.'),
            call(
                "Connection Arguments: {'username': '******', 'host': "
                "'champs.example.com:27018', 'password': '******', 'max_pool_size': 10, "
                "'replicaSet': ''}"),
            call('Querying the database to validate the connection.')
        ]
        mock_log.assert_has_calls(expected_calls)
示例#19
0
文件: manage.py 项目: seandst/pulp
def main():
    """
    This is the high level entry method. It does logging if any Exceptions are raised.
    """
    if os.getuid() == 0:
        print >> sys.stderr, _('This must not be run as root, but as the same user apache runs as.')
        return os.EX_USAGE
    try:
        options = parse_args()
        _start_logging()
        connection.initialize(max_timeout=1)

        # Prompt the user if there are workers that have not timed out
        if filter(lambda worker: (UTCDateTimeField().to_python(datetime.now()) -
                                  worker['last_heartbeat']) <
                  timedelta(seconds=constants.CELERY_TIMEOUT_SECONDS), status.get_workers()):
            if not _user_input_continue('There are still running workers, continuing could '
                                        'corrupt your Pulp installation. Are you sure you wish '
                                        'to continue?'):
                return os.EX_OK
        return _auto_manage_db(options)
    except UnperformedMigrationException:
        return 1
    except DataError, e:
        _logger.critical(str(e))
        _logger.critical(''.join(traceback.format_exception(*sys.exc_info())))
        return os.EX_DATAERR
示例#20
0
 def test_seeds_is_set_from_argument(self, mock_mongoengine):
     mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}
     connection.initialize(seeds='firsthost:1234,secondhost:5678')
     max_pool_size = connection._DEFAULT_MAX_POOL_SIZE
     database = config.config.get('database', 'name')
     mock_mongoengine.connect.assert_called_once_with(database, max_pool_size=max_pool_size,
                                                      host='firsthost', port=1234)
示例#21
0
    def test_initialize_username_and_shadows_password(self, mock_mongoengine, mock_log):
        """
        Assert that the password and password length are not logged.
        """
        mock_mongoengine_instance = mock_mongoengine.connect.return_value
        mock_mongoengine_instance.server_info.return_value = {"version":
                                                              MONGO_MIN_TEST_VERSION}
        config.config.set('database', 'name', 'nbachamps')
        config.config.set('database', 'username', 'larrybird')
        config.config.set('database', 'password', 'celtics1981')
        config.config.set('database', 'seeds', 'champs.example.com:27018')
        config.config.set('database', 'replica_set', '')

        connection.initialize()

        mock_mongoengine.connect.assert_called_once_with(
            'nbachamps', username='******', host='champs.example.com:27018',
            password='******', max_pool_size=10, replicaSet='')
        expected_calls = [
            call('Attempting username and password authentication.'),
            call("Connection Arguments: {'username': '******', 'host': "
                 "'champs.example.com:27018', 'password': '******', 'max_pool_size': 10, "
                 "'replicaSet': ''}"),
            call('Querying the database to validate the connection.')]
        mock_log.assert_has_calls(expected_calls)
示例#22
0
    def test_multiple_calls_errors(self, mongoengine):
        """
        This test asserts that more than one call to initialize() raises a RuntimeError.
        """
        mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}
        # The first call to initialize should be fine
        connection.initialize()

        # A second call to initialize() should raise a RuntimeError
        self.assertRaises(RuntimeError, connection.initialize)

        # The connection should still be initialized
        self.assertEqual(connection._CONNECTION, mongoengine.connect.return_value)
        self.assertEqual(connection._DATABASE, mongoengine.connection.get_db.return_value)
        # Connect should still have been called correctly
        name = config.config.get('database', 'name')
        host = config.config.get('database', 'seeds')
        # There should be two calls to connect. The first will use the server version to determine
        # which write concern is safe to use.
        self.assertEqual(mongoengine.connect.call_count, 2)
        self.assertEqual(mongoengine.connect.mock_calls[0][1], (name,))
        self.assertEqual(
            mongoengine.connect.mock_calls[0][2],
            {'host': host, 'maxPoolSize': 10})
        self.assertEqual(mongoengine.connect.mock_calls[4][1], (name,))
        self.assertEqual(
            mongoengine.connect.mock_calls[4][2],
            {'host': host, 'maxPoolSize': 10, 'w': 'majority'})
示例#23
0
def main():
    """
    This is the high level entry method. It does logging if any Exceptions are raised.
    """
    if os.getuid() == 0:
        print >> sys.stderr, _(
            'This must not be run as root, but as the same user apache runs as.'
        )
        return os.EX_USAGE
    try:
        options = parse_args()
        _start_logging()
        connection.initialize(max_timeout=1)

        # Prompt the user if there are workers that have not timed out
        if filter(
                lambda worker: (UTCDateTimeField().to_python(datetime.now()) -
                                worker['last_heartbeat']) < timedelta(
                                    seconds=constants.CELERY_TIMEOUT_SECONDS),
                status.get_workers()):
            if not _user_input_continue(
                    'There are still running workers, continuing could '
                    'corrupt your Pulp installation. Are you sure you wish '
                    'to continue?'):
                return os.EX_OK
        return _auto_manage_db(options)
    except UnperformedMigrationException:
        return 1
    except DataError, e:
        _logger.critical(str(e))
        _logger.critical(''.join(traceback.format_exception(*sys.exc_info())))
        return os.EX_DATAERR
示例#24
0
    def test_ssl_is_configured_with_ssl_certfile(self, mock_mongoengine,
                                                 mock_ssl):
        mock_mongoengine.connect.return_value.server_info.return_value = {
            'version': '2.6.0'
        }
        host = 'champs.example.com:27018'
        replica_set = ''
        config.config.set('database', 'ssl_certfile', 'certfilepath')
        config.config.set('database', 'verify_ssl', 'false')
        config.config.set('database', 'ssl', 'true')
        config.config.set('database', 'seeds', host)
        config.config.set('database', 'replica_set', replica_set)

        connection.initialize()

        database = config.config.get('database', 'name')
        max_pool_size = connection._DEFAULT_MAX_POOL_SIZE
        ssl_cert_reqs = mock_ssl.CERT_NONE
        ssl_ca_certs = config.config.get('database', 'ca_path')
        mock_mongoengine.connect.assert_called_once_with(
            database,
            max_pool_size=max_pool_size,
            ssl=True,
            ssl_cert_reqs=ssl_cert_reqs,
            ssl_ca_certs=ssl_ca_certs,
            ssl_certfile='certfilepath',
            host=host,
            replicaSet=replica_set)
示例#25
0
 def test_seeds_invalid(self, mock_mongoengine):
     mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}
     connection.initialize(seeds='localhost:27017:1234')
     max_pool_size = connection._DEFAULT_MAX_POOL_SIZE
     database = config.config.get('database', 'name')
     mock_mongoengine.connect.assert_called_once_with(database, max_pool_size=max_pool_size,
                                                      host='localhost')
示例#26
0
    def _test_initialize(self, version_str, mock_mongoengine):
        mock_mongoclient_connect = mock_mongoengine.connect.return_value
        mock_mongoclient_connect.server_info.return_value = {
            'version': version_str
        }

        connection.initialize()
示例#27
0
文件: base.py 项目: signull/pulp
    def setUp(self):
        connection.initialize()
        self.patch1 = mock.patch('pulp.server.webservices.controllers.decorators.'
                                 'check_preauthenticated')
        self.patch2 = mock.patch('pulp.server.webservices.controllers.decorators.'
                                 'is_consumer_authorized')
        self.patch3 = mock.patch('pulp.server.webservices.http.resource_path')
        self.patch4 = mock.patch('pulp.server.webservices.http.header')
        self.patch5 = mock.patch('web.webapi.HTTPError')
        self.patch6 = mock.patch('pulp.server.managers.factory.principal_manager')
        self.patch7 = mock.patch('pulp.server.managers.factory.user_query_manager')

        self.patch8 = mock.patch('pulp.server.webservices.http.uri_path')
        self.mock_check_pre_auth = self.patch1.start()
        self.mock_check_pre_auth.return_value = 'ws-user'
        self.mock_check_auth = self.patch2.start()
        self.mock_check_auth.return_value = True
        self.mock_http_resource_path = self.patch3.start()
        self.patch4.start()
        self.patch5.start()
        self.patch6.start()
        self.mock_user_query_manager = self.patch7.start()
        self.mock_user_query_manager.return_value.is_superuser.return_value = False
        self.mock_user_query_manager.return_value.is_authorized.return_value = True
        self.mock_uri_path = self.patch8.start()
        self.mock_uri_path.return_value = "/mock/"
示例#28
0
 def test_database_max_pool_size_uses_default(self, mock_mongoengine):
     mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}
     connection.initialize()
     database = config.config.get('database', 'name')
     max_pool_size = connection._DEFAULT_MAX_POOL_SIZE
     mock_mongoengine.connect.assert_called_once_with(database, host='localhost',
                                                      max_pool_size=max_pool_size, port=27017)
示例#29
0
文件: scheduler.py 项目: omps/pulp
    def setup_schedule(self):
        """
        This loads enabled schedules from the database and adds them to the
        "_schedule" dictionary as instances of celery.beat.ScheduleEntry
        """
        if not Scheduler._mongo_initialized:
            _logger.debug('Initializing Mongo client connection to read celerybeat schedule')
            db_connection.initialize()
            Scheduler._mongo_initialized = True
        _logger.debug(_('loading schedules from app'))
        self._schedule = {}
        for key, value in self.app.conf.CELERYBEAT_SCHEDULE.iteritems():
            self._schedule[key] = beat.ScheduleEntry(**dict(value, name=key))

        # include a "0" as the default in case there are no schedules to load
        update_timestamps = [0]

        _logger.debug(_('loading schedules from DB'))
        ignored_db_count = 0
        self._loaded_from_db_count = 0
        for call in itertools.imap(ScheduledCall.from_db, utils.get_enabled()):
            if call.remaining_runs == 0:
                _logger.debug(_('ignoring schedule with 0 remaining runs: %(id)s') % {'id': call.id})
                ignored_db_count += 1
            else:
                self._schedule[call.id] = call.as_schedule_entry()
                update_timestamps.append(call.last_updated)
                self._loaded_from_db_count += 1

        _logger.debug('loaded %(count)d schedules' % {'count': self._loaded_from_db_count})

        self._most_recent_timestamp = max(update_timestamps)
示例#30
0
文件: manage.py 项目: lanky/pulp
def main():
    """
    This is the high level entry method. It does logging if any Exceptions are raised.
    """
    if os.getuid() == 0:
        print >> sys.stderr, _(
            'This must not be run as root, but as the same user apache runs as.'
        )
        return os.EX_USAGE
    try:
        options = parse_args()
        _start_logging()
        connection.initialize(max_timeout=1)
        active_workers = None

        if not options.dry_run:
            active_workers = status.get_workers()

        if active_workers:
            last_worker_time = max(
                [worker['last_heartbeat'] for worker in active_workers])
            time_from_last = UTCDateTimeField().to_python(
                datetime.utcnow()) - last_worker_time
            wait_time = timedelta(
                seconds=constants.MIGRATION_WAIT_TIME) - time_from_last

            if wait_time > timedelta(0):
                print _('\nThe following processes might still be running:')
                for worker in active_workers:
                    print _('\t%s' % worker['name'])

                for i in range(wait_time.seconds, 0, -1):
                    print _(
                        '\rPlease wait %s seconds while Pulp confirms this.' %
                        i),
                    sys.stdout.flush()
                    time.sleep(1)

                still_active_workers = [
                    worker for worker in status.get_workers()
                    if worker['last_heartbeat'] > last_worker_time
                ]

                if still_active_workers:
                    print >> sys.stderr, _(
                        '\n\nThe following processes are still running, please'
                        ' stop the running workers before retrying the'
                        ' pulp-manage-db command.')
                    for worker in still_active_workers:
                        print _('\t%s' % worker['name'])

                    return os.EX_SOFTWARE
        return _auto_manage_db(options)
    except UnperformedMigrationException:
        return 1
    except DataError, e:
        _logger.critical(str(e))
        _logger.critical(''.join(traceback.format_exception(*sys.exc_info())))
        return os.EX_DATAERR
示例#31
0
 def test_database_replica_set_from_config(self, mock_mongoengine):
     mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}
     config.config.set('database', 'replica_set', 'real_replica_set')
     connection.initialize()
     database = config.config.get('database', 'seeds')
     max_pool_size = connection._DEFAULT_MAX_POOL_SIZE
     mock_mongoengine.connect.assert_called_once_with(database, max_pool_size=max_pool_size,
                                                      replicaset='real_replica_set')
示例#32
0
 def test__DATABASE_is_returned_from_get_db_call(self, mock_mongoengine):
     mock_mongoengine.connect.return_value.server_info.return_value = {
         'version': '2.6.0'
     }
     connection.initialize()
     expected_database = mock_mongoengine.connection.get_db.return_value
     self.assertTrue(connection._DATABASE is expected_database)
     mock_mongoengine.connection.get_db.assert_called_once_with()
示例#33
0
 def test__DATABASE_uses_default_name(self, mock_mongoengine):
     mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}
     connection.initialize()
     name = config.config.get('database', 'name')
     mock_mongoengine.connect.assert_called_once_with(name,
                                                      host='localhost',
                                                      max_pool_size=10,
                                                      port=27017)
示例#34
0
 def test_name_is_set_from_argument(self, mock_mongoengine):
     mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}
     name = 'name_set_from_argument'
     connection.initialize(name=name)
     mock_mongoengine.connect.assert_called_once_with(name,
                                                      host='localhost',
                                                      max_pool_size=10,
                                                      port=27017)
示例#35
0
文件: base.py 项目: maxamillion/pulp
def start_database_connection():
    """
    Start the database connection, if it is not already established.
    """
    # It's important to only call this once during the process
    if not connection._CONNECTION:
        _load_test_config()
        connection.initialize()
示例#36
0
 def test_name_is_set_from_argument(self, mock_mongoengine):
     mock_mongoengine.connect.return_value.server_info.return_value = {
         'version': '2.6.0'
     }
     connection.initialize(name='name_set_from_argument')
     expected_database = getattr(mock_mongoengine.connect.return_value,
                                 'name_set_from_argument')
     self.assertEquals(connection._DATABASE, expected_database)
示例#37
0
    def test_retry_uses_itertools_chain_and_repeat(self, mock_itertools, mock_mongoengine):
        mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}

        connection.initialize()

        mock_itertools.repeat.assert_called_once_with(32)
        mock_itertools.chain.assert_called_once_with([1, 2, 4, 8, 16],
                                                     mock_itertools.repeat.return_value)
示例#38
0
 def test_initialize_username_and_password(self, mock_end, mock_mongoclient):
     mock_mongoclient_instance = mock_mongoclient.return_value
     mock_mongoclient_instance.server_info.return_value = {"version":
                                                           connection.MONGO_MINIMUM_VERSION}
     config.config.set('database', 'username', 'admin')
     config.config.set('database', 'password', 'admin')
     connection.initialize()
     self.assertTrue(connection._DATABASE.authenticate.called)
示例#39
0
 def test_initialize_username_no_password(self, mock_mongoengine):
     mock_mongoengine_instance = mock_mongoengine.connect.return_value
     mock_mongoengine_instance.server_info.return_value = {"version":
                                                           connection.MONGO_MINIMUM_VERSION}
     config.config.set('database', 'username', 'admin')
     config.config.set('database', 'password', '')
     # ensure no exception is raised (redmine #708)
     connection.initialize()
示例#40
0
文件: base.py 项目: taftsanders/pulp
def start_database_connection():
    """
    Start the database connection, if it is not already established.
    """
    # It's important to only call this once during the process
    if not connection._CONNECTION:
        _load_test_config()
        connection.initialize()
示例#41
0
    def test_retry_uses_itertools_chain_and_repeat(self, mock_itertools, mock_mongoengine):
        mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}

        connection.initialize()

        mock_itertools.repeat.assert_called_once_with(32)
        mock_itertools.chain.assert_called_once_with([1, 2, 4, 8, 16],
                                                     mock_itertools.repeat.return_value)
示例#42
0
 def test_initialize_no_username_or_password(self, mock_mongoengine):
     mock_mongoengine_instance = mock_mongoengine.connect.return_value
     mock_mongoengine_instance.server_info.return_value = {"version":
                                                           connection.MONGO_MINIMUM_VERSION}
     config.config.set('database', 'username', '')
     config.config.set('database', 'password', '')
     connection.initialize()
     self.assertFalse(connection._DATABASE.authenticate.called)
示例#43
0
    def test_mongoengine_connect_is_called(self, mock_mongoengine):
        """
        Assert that mongoengine.connect() is called.
        """
        mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}

        connection.initialize()

        mock_mongoengine.connect.assert_called_once()
示例#44
0
    def test_mongoengine_connect_is_called(self, mock_mongoengine):
        """
        Assert that mongoengine.connect() is called.
        """
        mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}

        connection.initialize()

        mock_mongoengine.connect.assert_called_once()
示例#45
0
 def test__DATABASE_uses_default_name(self, mock_mongoengine):
     mock_mongoengine.connect.return_value.server_info.return_value = {
         'version': '2.6.0'
     }
     connection.initialize()
     name = config.config.get('database', 'name')
     expected_database = getattr(mock_mongoengine.connect.return_value,
                                 name)
     self.assertEquals(connection._DATABASE, expected_database)
示例#46
0
 def test_multiple_seeds_no_replica_set(self, mock_connect_seeds, mock_mongoengine):
     mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}
     mock_connect_seeds.return_value.server_info.return_value = {'version': '2.6.0'}
     seeds = "firsthost:1234,secondhost:5678"
     config.config.set('database', 'write_concern', 'majority')
     config.config.set('database', 'seeds', seeds)
     with self.assertRaises(PulpCodedException) as connection_error:
         connection.initialize()
     self.assertEqual(connection_error.exception.error_code.message, error_codes.PLP0041.message)
示例#47
0
    def test_ssl_is_skipped_if_off(self, mock_mongoengine):
        mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}

        connection.initialize()

        database = config.config.get('database', 'name')
        max_pool_size = connection._DEFAULT_MAX_POOL_SIZE
        mock_mongoengine.connect.assert_called_once_with(database, max_pool_size=max_pool_size,
                                                         host='champs.example.com', port=27018)
示例#48
0
 def test_initialize_username_no_password(self, mock_mongoengine):
     """
     Test that no Exception is raised if a DB username is provided without a password.
     """
     mock_mongoengine_instance = mock_mongoengine.connect.return_value
     mock_mongoengine_instance.server_info.return_value = {"version":
                                                           MONGO_MIN_TEST_VERSION}
     # ensure no exception is raised (redmine #708)
     connection.initialize()
示例#49
0
 def test_initialize_username_no_password(self, mock_mongoengine):
     """
     Test that no Exception is raised if a DB username is provided without a password.
     """
     mock_mongoengine_instance = mock_mongoengine.connect.return_value
     mock_mongoengine_instance.server_info.return_value = {"version":
                                                           MONGO_MIN_TEST_VERSION}
     # ensure no exception is raised (redmine #708)
     connection.initialize()
示例#50
0
    def test_name_is_set_from_argument(self, mock_mongoengine):
        mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}
        name = 'name_set_from_argument'

        connection.initialize(name=name)

        host = config.config.get('database', 'seeds').split(':')[0]
        port = int(config.config.get('database', 'seeds').split(':')[1])
        mock_mongoengine.connect.assert_called_once_with(name, host=host, max_pool_size=10,
                                                         port=port)
示例#51
0
    def test__DATABASE_uses_default_name(self, mock_mongoengine):
        mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}

        connection.initialize()

        name = config.config.get('database', 'name')
        host = config.config.get('database', 'seeds').split(':')[0]
        port = int(config.config.get('database', 'seeds').split(':')[1])
        mock_mongoengine.connect.assert_called_once_with(name, host=host, max_pool_size=10,
                                                         port=port)
示例#52
0
 def setUpClass(cls):
     if not os.path.exists('/tmp/pulp'):
         os.makedirs('/tmp/pulp')
     stop_logging()
     config_filename = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data', 'test-override-pulp.conf')
     config.config.read(config_filename)
     start_logging()
     name = config.config.get('database', 'name')
     connection.initialize(name)
     manager_factory.initialize()
示例#53
0
 def test_initialize_username_and_password(self, mock_mongoengine):
     mock_mongoengine_instance = mock_mongoengine.connect.return_value
     mock_mongoengine_instance.server_info.return_value = {"version":
                                                           connection.MONGO_MINIMUM_VERSION}
     config.config.set('database', 'username', 'admin')
     config.config.set('database', 'password', 'admin')
     connection.initialize()
     mock_mongoengine.connect.assert_called_once_with('pulp_unittest', username='******',
                                                      host='localhost', password='******',
                                                      max_pool_size=10, port=27017)
示例#54
0
    def test_initialize_no_username_or_password(self, mock_mongoengine):
        """
        Assert that no call is made to authenticate() when the username and password are the empty
        string.
        """
        mock_mongoengine_instance = mock_mongoengine.connect.return_value
        mock_mongoengine_instance.server_info.return_value = {"version":
                                                              MONGO_MIN_TEST_VERSION}
        connection.initialize()

        self.assertFalse(connection._DATABASE.authenticate.called)
示例#55
0
    def test_database_max_pool_size_uses_default(self, mock_mongoengine):
        mock_mongoengine.connect.return_value.server_info.return_value = {'version': '2.6.0'}

        connection.initialize()

        database = config.config.get('database', 'name')
        host = config.config.get('database', 'seeds').split(':')[0]
        port = int(config.config.get('database', 'seeds').split(':')[1])
        max_pool_size = connection._DEFAULT_MAX_POOL_SIZE
        mock_mongoengine.connect.assert_called_once_with(database, host=host,
                                                         max_pool_size=max_pool_size, port=port)