Esempio 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

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

        mocked_list.assert_any_call()
        self.assertEqual([
            models.MongoDBSchema('db1').serialize(),
            models.MongoDBSchema('db2').serialize()
        ], dbs)
        self.assertEqual('db2', next_marker)
Esempio 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')
Esempio n. 3
0
 def revoke_access(self, username, database):
     """Removes the RW role from the user for the specified database."""
     user = self.get_existing_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)
Esempio n. 4
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])

        # FIXME(songjian):can not create database with null content,
        # so create a collection
        # db_client['dummy'].insert.assert_called_with({'dummy': True})
        # db_client.drop_collection.assert_called_with('dummy')
        db_client.create_collection.assert_called_with('dummy')
Esempio n. 5
0
 def list_databases(self, limit=None, marker=None, include_marker=False):
     """Lists the databases."""
     databases = []
     for db_name in self.list_database_names():
         schema = models.MongoDBSchema(name=db_name)
         if not schema.is_ignored():
             databases.append(schema)
     LOG.debug('databases = ' + str(databases))
     return guestagent_utils.serialize_list(databases,
                                            limit=limit,
                                            marker=marker,
                                            include_marker=include_marker)
Esempio n. 6
0
 def grant_access(self, username, databases):
     """Adds the RW role to the user for each specified database."""
     user = self.get_existing_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)
Esempio n. 7
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')
Esempio n. 8
0
 def db_data_size(self, db_name):
     schema = models.MongoDBSchema(db_name)
     return MongoDBAdmin().db_stats(schema.serialize())['dataSize']