Ejemplo n.º 1
0
    def test_list_databases(
            self,  # mocked_ignored_dbs,
            mocked_admin_user,
            mocked_client):
        # This list contains the special 'admin', 'local' and 'config' dbs;
        # the special dbs should be skipped in the output.
        # Pagination is tested by starting at 'db1', so 'db0' should not
        # be in the output. The limit is set to 2, meaning the result
        # should be 'db1' and 'db2'. The next_marker should be 'db3'.
        mocked_list = mock.MagicMock(return_value=[
            'admin', 'local', 'config', 'db0', 'db1', 'db2', 'db3'
        ])
        mocked_client().__enter__().database_names = mocked_list

        marker = models.MongoDBSchema('db1').serialize()
        dbs, next_marker = self.manager.list_databases(self.context,
                                                       limit=2,
                                                       marker=marker,
                                                       include_marker=True)

        mocked_list.assert_any_call()
        self.assertEqual([
            models.MongoDBSchema('db1').serialize(),
            models.MongoDBSchema('db2').serialize()
        ], dbs)
        self.assertEqual(models.MongoDBSchema('db2').serialize(), next_marker)
Ejemplo n.º 2
0
    def test_create_databases(self, mocked_admin_user, mocked_client):
        schema = models.MongoDBSchema('testdb').serialize()
        db_client = mocked_client().__enter__()['testdb']

        self.manager.create_database(self.context, [schema])

        db_client['dummy'].insert.assert_called_with({'dummy': True})
        db_client.drop_collection.assert_called_with('dummy')
Ejemplo n.º 3
0
 def revoke_access(self, username, database):
     """Removes the RW role from the user for the specified database."""
     user = self._get_user_record(username)
     # verify the database name
     models.MongoDBSchema(database)
     role = {'db': database, 'role': 'readWrite'}
     LOG.debug('Removing role %s from user %s.' % (str(role), username))
     user.revoke_role(role)
     LOG.debug('Updating user %s.' % username)
     self._update_user_roles(user)
Ejemplo n.º 4
0
 def list_databases(self, limit=None, marker=None, include_marker=False):
     """Lists the databases."""
     db_names = self.list_database_names()
     for hidden in IGNORED_DBS:
         if hidden in db_names:
             db_names.remove(hidden)
     databases = [models.MongoDBSchema(db_name).serialize()
                  for db_name in db_names]
     LOG.debug('databases = ' + str(databases))
     return pagination.paginate_list(databases, limit, marker,
                                     include_marker)
Ejemplo n.º 5
0
 def list_databases(self, limit=None, marker=None, include_marker=False):
     """Lists the databases."""
     db_names = self.list_database_names()
     for hidden in cfg.get_ignored_dbs():
         if hidden in db_names:
             db_names.remove(hidden)
     databases = [models.MongoDBSchema(db_name) for db_name in db_names]
     LOG.debug('databases = ' + str(databases))
     return guestagent_utils.serialize_list(databases,
                                            limit=limit,
                                            marker=marker,
                                            include_marker=include_marker)
Ejemplo n.º 6
0
 def revoke_access(self, username, database):
     """Removes the RW role from the user for the specified database."""
     user = self._get_user_record(username)
     if not user:
         raise exception.BadRequest(
             _('Cannot revoke access for reserved or non-existant user '
               '%(user)s') % {'user': username})
     # verify the database name
     models.MongoDBSchema(database)
     role = {'db': database, 'role': 'readWrite'}
     LOG.debug('Removing role %s from user %s.' % (str(role), username))
     user.revoke_role(role)
     LOG.debug('Updating user %s.' % username)
     self._update_user_roles(user)
Ejemplo n.º 7
0
 def grant_access(self, username, databases):
     """Adds the RW role to the user for each specified database."""
     user = self._get_user_record(username)
     for db_name in databases:
         # verify the database name
         models.MongoDBSchema(db_name)
         role = {'db': db_name, 'role': 'readWrite'}
         if role not in user.roles:
             LOG.debug('Adding role %s to user %s.' % (str(role), username))
             user.roles = role
         else:
             LOG.debug('User %s already has role %s.' %
                       (username, str(role)))
     LOG.debug('Updating user %s.' % username)
     self._update_user_roles(user)
Ejemplo n.º 8
0
 def grant_access(self, username, databases):
     """Adds the RW role to the user for each specified database."""
     user = self._get_user_record(username)
     if not user:
         raise exception.BadRequest(
             _('Cannot grant access for reserved or non-existant user '
               '%(user)s') % {'user': username})
     for db_name in databases:
         # verify the database name
         models.MongoDBSchema(db_name)
         role = {'db': db_name, 'role': 'readWrite'}
         if role not in user.roles:
             LOG.debug('Adding role %s to user %s.' % (str(role), username))
             user.roles = role
         else:
             LOG.debug('User %s already has role %s.' %
                       (username, str(role)))
     LOG.debug('Updating user %s.' % username)
     self._update_user_roles(user)
Ejemplo n.º 9
0
 def db_data_size(self, db_name):
     schema = models.MongoDBSchema(db_name)
     return MongoDBAdmin().db_stats(schema.serialize())['dataSize']
Ejemplo n.º 10
0
    def test_delete_database(self, mocked_admin_user, mocked_client):
        schema = models.MongoDBSchema('testdb').serialize()

        self.manager.delete_database(self.context, schema)

        mocked_client().__enter__().drop_database.assert_called_with('testdb')