コード例 #1
0
    def test_copy_db(self):
        c = Connection(self.host, self.port)
        self.assertTrue(c.in_request())

        self.assertRaises(TypeError, c.copy_database, 4, "foo")
        self.assertRaises(TypeError, c.copy_database, "foo", 4)

        self.assertRaises(InvalidName, c.copy_database, "foo", "$foo")

        c.pymongo_test.test.drop()
        c.drop_database("pymongo_test1")
        c.drop_database("pymongo_test2")

        c.pymongo_test.test.insert({"foo": "bar"})

        # Due to SERVER-2329, databases may not disappear from a master in a
        # master-slave pair
        if not server_is_master_with_slave(c):
            self.assertFalse("pymongo_test1" in c.database_names())
            self.assertFalse("pymongo_test2" in c.database_names())

        c.copy_database("pymongo_test", "pymongo_test1")
        # copy_database() didn't accidentally end the request
        self.assertTrue(c.in_request())

        self.assertTrue("pymongo_test1" in c.database_names())
        self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])

        c.end_request()
        self.assertFalse(c.in_request())
        c.copy_database("pymongo_test", "pymongo_test2", "%s:%d" % (self.host, self.port))
        # copy_database() didn't accidentally restart the request
        self.assertFalse(c.in_request())

        self.assertTrue("pymongo_test2" in c.database_names())
        self.assertEqual("bar", c.pymongo_test2.test.find_one()["foo"])

        if version.at_least(c, (1, 3, 3, 1)):
            c.drop_database("pymongo_test1")

            c.pymongo_test.add_user("mike", "password")

            self.assertRaises(
                OperationFailure, c.copy_database, "pymongo_test", "pymongo_test1", username="******", password="******"
            )
            if not server_is_master_with_slave(c):
                self.assertFalse("pymongo_test1" in c.database_names())

            self.assertRaises(
                OperationFailure, c.copy_database, "pymongo_test", "pymongo_test1", username="******", password="******"
            )

            if not server_is_master_with_slave(c):
                self.assertFalse("pymongo_test1" in c.database_names())

            if not is_mongos(c):
                # See SERVER-6427
                c.copy_database("pymongo_test", "pymongo_test1", username="******", password="******")
                self.assertTrue("pymongo_test1" in c.database_names())
                self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])
コード例 #2
0
    def drop_databases(self, database_names):
        cx = self.get_client()
        for test_db_name in database_names:
            yield cx.drop_database(test_db_name)

        # Due to SERVER-2329, databases may not disappear from a master
        # in a master-slave pair.
        if not (yield server_is_master_with_slave(cx)):
            start = time.time()

            # There may be a race condition in the server's dropDatabase. Wait
            # for it to update its namespaces.
            db_names = yield cx.database_names()
            while time.time() - start < 30:
                remaining_test_dbs = set(database_names).intersection(db_names)

                if not remaining_test_dbs:
                    # All test DBs are removed.
                    break

                yield self.pause(0.1)
                db_names = yield cx.database_names()

            for test_db_name in database_names:
                self.assertFalse(test_db_name in db_names, "%s not dropped" % test_db_name)
コード例 #3
0
ファイル: test_motor_client.py プロジェクト: ZoeyYoung/motor
    def drop_databases(self, database_names):
        for test_db_name in database_names:
            # Setup code has configured a short timeout, and the copying
            # has put Mongo under enough load that we risk timeouts here
            # unless we override. command() takes no network_timeout but
            # find_one does.
            self.sync_cx[test_db_name]['$cmd'].find_one(
                {'dropDatabase': 1}, network_timeout=30)

        # Due to SERVER-2329, databases may not disappear from a master
        # in a master-slave pair.
        if not server_is_master_with_slave(self.sync_cx):
            start = time.time()
            
            # There may be a race condition in the server's dropDatabase. Wait
            # for it to update its namespaces.
            db_names = self.sync_cx.database_names()
            while time.time() - start < 10:
                remaining_test_dbs = (
                    set(database_names).intersection(db_names))
                
                if not remaining_test_dbs:
                    # All test DBs are removed.
                    break

                db_names = self.sync_cx.database_names()
                
            for test_db_name in database_names:
                self.assertFalse(
                    test_db_name in db_names,
                    "%s not dropped" % test_db_name)
コード例 #4
0
    def drop_databases(self, database_names):
        for test_db_name in database_names:
            # Setup code has configured a short timeout, and the copying
            # has put Mongo under enough load that we risk timeouts here
            # unless we override. command() takes no network_timeout but
            # find_one does.
            self.sync_cx[test_db_name]['$cmd'].find_one({'dropDatabase': 1},
                                                        network_timeout=30)

        # Due to SERVER-2329, databases may not disappear from a master
        # in a master-slave pair.
        if not server_is_master_with_slave(self.sync_cx):
            start = time.time()

            # There may be a race condition in the server's dropDatabase. Wait
            # for it to update its namespaces.
            db_names = self.sync_cx.database_names()
            while time.time() - start < 10:
                remaining_test_dbs = (
                    set(database_names).intersection(db_names))

                if not remaining_test_dbs:
                    # All test DBs are removed.
                    break

                db_names = self.sync_cx.database_names()

            for test_db_name in database_names:
                self.assertFalse(test_db_name in db_names,
                                 "%s not dropped" % test_db_name)
コード例 #5
0
    def drop_databases(self, database_names, authenticated_client=None):
        cx = authenticated_client or self.get_client()
        for test_db_name in database_names:
            yield cx.drop_database(test_db_name)

        # Due to SERVER-2329, databases may not disappear from a master
        # in a master-slave pair.
        if not (yield server_is_master_with_slave(cx)):
            start = time.time()

            # There may be a race condition in the server's dropDatabase. Wait
            # for it to update its namespaces.
            db_names = yield cx.database_names()
            while time.time() - start < 30:
                remaining_test_dbs = (
                    set(database_names).intersection(db_names))

                if not remaining_test_dbs:
                    # All test DBs are removed.
                    break

                yield self.pause(0.1)
                db_names = yield cx.database_names()

            for test_db_name in database_names:
                self.assertFalse(test_db_name in db_names,
                                 "%s not dropped" % test_db_name)
コード例 #6
0
    def test_fsync_lock_unlock(self):
        if server_is_master_with_slave(client_context.client) and client_context.version.at_least(2, 3, 0):
            raise SkipTest("SERVER-7714")

        self.assertFalse(self.client.is_locked)
        # async flushing not supported on windows...
        if sys.platform not in ("cygwin", "win32"):
            self.client.fsync(async=True)
            self.assertFalse(self.client.is_locked)
        self.client.fsync(lock=True)
        self.assertTrue(self.client.is_locked)
        locked = True
        self.client.unlock()
        for _ in range(5):
            locked = self.client.is_locked
            if not locked:
                break
            time.sleep(1)
        self.assertFalse(locked)
コード例 #7
0
    def test_fsync_lock_unlock(self):
        if (server_is_master_with_slave(client_context.client)
                and client_context.version.at_least(2, 3, 0)):
            raise SkipTest('SERVER-7714')

        self.assertFalse(self.client.is_locked)
        # async flushing not supported on windows...
        if sys.platform not in ('cygwin', 'win32'):
            self.client.fsync(async=True)
            self.assertFalse(self.client.is_locked)
        self.client.fsync(lock=True)
        self.assertTrue(self.client.is_locked)
        locked = True
        self.client.unlock()
        for _ in range(5):
            locked = self.client.is_locked
            if not locked:
                break
            time.sleep(1)
        self.assertFalse(locked)
コード例 #8
0
    def test_copy_db(self):
        c = MongoClient(host, port)
        # Due to SERVER-2329, databases may not disappear
        # from a master in a master-slave pair.
        if server_is_master_with_slave(c):
            raise SkipTest("SERVER-2329")

        ctx = catch_warnings()
        try:
            warnings.simplefilter("ignore", DeprecationWarning)
            self.assertRaises(TypeError, c.copy_database, 4, "foo")
            self.assertRaises(TypeError, c.copy_database, "foo", 4)
            self.assertRaises(InvalidName, c.copy_database, "foo", "$foo")

            c.pymongo_test.test.drop()
            c.pymongo_test.test.insert({"foo": "bar"})

            c.drop_database("pymongo_test1")
            self.assertFalse("pymongo_test1" in c.database_names())

            c.copy_database("pymongo_test", "pymongo_test1")
            self.assertTrue("pymongo_test1" in c.database_names())
            self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])
            c.drop_database("pymongo_test1")

            # XXX - SERVER-15318
            if not (version.at_least(c, (2, 6, 4)) and is_mongos(c)):
                self.assertFalse(c.in_request())
                c.copy_database("pymongo_test", "pymongo_test1",
                                "%s:%d" % (host, port))
                # copy_database() didn't accidentally restart the request
                self.assertFalse(c.in_request())

                self.assertTrue("pymongo_test1" in c.database_names())
                self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])

            c.drop_database("pymongo_test1")
        finally:
            ctx.exit()
コード例 #9
0
    def test_copy_db(self):
        c = MongoClient(host, port)
        # Due to SERVER-2329, databases may not disappear
        # from a master in a master-slave pair.
        if server_is_master_with_slave(c):
            raise SkipTest("SERVER-2329")

        ctx = catch_warnings()
        try:
            warnings.simplefilter("ignore", DeprecationWarning)
            self.assertRaises(TypeError, c.copy_database, 4, "foo")
            self.assertRaises(TypeError, c.copy_database, "foo", 4)
            self.assertRaises(InvalidName, c.copy_database, "foo", "$foo")

            c.pymongo_test.test.drop()
            c.pymongo_test.test.insert({"foo": "bar"})

            c.drop_database("pymongo_test1")
            self.assertFalse("pymongo_test1" in c.database_names())

            c.copy_database("pymongo_test", "pymongo_test1")
            self.assertTrue("pymongo_test1" in c.database_names())
            self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])
            c.drop_database("pymongo_test1")

            # XXX - SERVER-15318
            if not (version.at_least(c, (2, 6, 4)) and is_mongos(c)):
                self.assertFalse(c.in_request())
                c.copy_database("pymongo_test", "pymongo_test1",
                                "%s:%d" % (host, port))
                # copy_database() didn't accidentally restart the request
                self.assertFalse(c.in_request())

                self.assertTrue("pymongo_test1" in c.database_names())
                self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])

            c.drop_database("pymongo_test1")
        finally:
            ctx.exit()
コード例 #10
0
    def test_copy_db(self):
        c = MongoClient(host, port)
        # We test copy twice; once starting in a request and once not. In
        # either case the copy should succeed (because it starts a request
        # internally) and should leave us in the same state as before the copy.
        c.start_request()

        self.assertRaises(TypeError, c.copy_database, 4, "foo")
        self.assertRaises(TypeError, c.copy_database, "foo", 4)

        self.assertRaises(InvalidName, c.copy_database, "foo", "$foo")

        c.pymongo_test.test.drop()
        c.drop_database("pymongo_test1")
        c.drop_database("pymongo_test2")

        c.pymongo_test.test.insert({"foo": "bar"})

        # Due to SERVER-2329, databases may not disappear from a master in a
        # master-slave pair
        if not server_is_master_with_slave(c):
            self.assertFalse("pymongo_test1" in c.database_names())
            self.assertFalse("pymongo_test2" in c.database_names())

        c.copy_database("pymongo_test", "pymongo_test1")
        # copy_database() didn't accidentally end the request
        self.assertTrue(c.in_request())

        self.assertTrue("pymongo_test1" in c.database_names())
        self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])

        c.end_request()
        self.assertFalse(c.in_request())
        c.copy_database("pymongo_test", "pymongo_test2",
                        "%s:%d" % (host, port))
        # copy_database() didn't accidentally restart the request
        self.assertFalse(c.in_request())

        self.assertTrue("pymongo_test2" in c.database_names())
        self.assertEqual("bar", c.pymongo_test2.test.find_one()["foo"])

        if version.at_least(c, (1, 3, 3, 1)):
            c.drop_database("pymongo_test1")

            c.pymongo_test.add_user("mike", "password")

            self.assertRaises(OperationFailure,
                              c.copy_database,
                              "pymongo_test",
                              "pymongo_test1",
                              username="******",
                              password="******")
            if not server_is_master_with_slave(c):
                self.assertFalse("pymongo_test1" in c.database_names())

            self.assertRaises(OperationFailure,
                              c.copy_database,
                              "pymongo_test",
                              "pymongo_test1",
                              username="******",
                              password="******")

            if not server_is_master_with_slave(c):
                self.assertFalse("pymongo_test1" in c.database_names())

            if not is_mongos(c):
                # See SERVER-6427
                c.copy_database("pymongo_test",
                                "pymongo_test1",
                                username="******",
                                password="******")
                self.assertTrue("pymongo_test1" in c.database_names())
                self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])
コード例 #11
0
    def test_copy_db(self):
        c = MongoClient(host, port)
        # Due to SERVER-2329, databases may not disappear
        # from a master in a master-slave pair.
        if server_is_master_with_slave(c):
            raise SkipTest("SERVER-2329")
        # We test copy twice; once starting in a request and once not. In
        # either case the copy should succeed (because it starts a request
        # internally) and should leave us in the same state as before the copy.
        c.start_request()

        self.assertRaises(TypeError, c.copy_database, 4, "foo")
        self.assertRaises(TypeError, c.copy_database, "foo", 4)

        self.assertRaises(InvalidName, c.copy_database, "foo", "$foo")

        c.pymongo_test.test.drop()
        c.drop_database("pymongo_test1")
        c.drop_database("pymongo_test2")
        self.assertFalse("pymongo_test1" in c.database_names())
        self.assertFalse("pymongo_test2" in c.database_names())

        c.pymongo_test.test.insert({"foo": "bar"})

        c.copy_database("pymongo_test", "pymongo_test1")
        # copy_database() didn't accidentally end the request
        self.assertTrue(c.in_request())

        self.assertTrue("pymongo_test1" in c.database_names())
        self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])

        c.end_request()
        self.assertFalse(c.in_request())
        c.copy_database("pymongo_test", "pymongo_test2",
                        "%s:%d" % (host, port))
        # copy_database() didn't accidentally restart the request
        self.assertFalse(c.in_request())

        self.assertTrue("pymongo_test2" in c.database_names())
        self.assertEqual("bar", c.pymongo_test2.test.find_one()["foo"])

        # See SERVER-6427 for mongos
        if (version.at_least(c, (1, 3, 3, 1)) and
            not is_mongos(c) and server_started_with_auth(c)):

            c.drop_database("pymongo_test1")

            c.admin.add_user("admin", "password")
            c.admin.authenticate("admin", "password")
            try:
                c.pymongo_test.add_user("mike", "password")

                self.assertRaises(OperationFailure, c.copy_database,
                                  "pymongo_test", "pymongo_test1",
                                  username="******", password="******")
                self.assertFalse("pymongo_test1" in c.database_names())

                self.assertRaises(OperationFailure, c.copy_database,
                                  "pymongo_test", "pymongo_test1",
                                  username="******", password="******")
                self.assertFalse("pymongo_test1" in c.database_names())

                c.copy_database("pymongo_test", "pymongo_test1",
                                username="******", password="******")
                self.assertTrue("pymongo_test1" in c.database_names())
                self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])
            finally:
                # Cleanup
                remove_all_users(c.pymongo_test)
                c.admin.remove_user("admin")
                c.disconnect()
コード例 #12
0
    def test_copy_db(self):
        c = MongoClient(self.host, self.port)
        # We test copy twice; once starting in a request and once not. In
        # either case the copy should succeed (because it starts a request
        # internally) and should leave us in the same state as before the copy.
        c.start_request()

        self.assertRaises(TypeError, c.copy_database, 4, "foo")
        self.assertRaises(TypeError, c.copy_database, "foo", 4)

        self.assertRaises(InvalidName, c.copy_database, "foo", "$foo")

        c.pymongo_test.test.drop()
        c.drop_database("pymongo_test1")
        c.drop_database("pymongo_test2")

        c.pymongo_test.test.insert({"foo": "bar"})

        # Due to SERVER-2329, databases may not disappear from a master in a
        # master-slave pair
        if not server_is_master_with_slave(c):
            self.assertFalse("pymongo_test1" in c.database_names())
            self.assertFalse("pymongo_test2" in c.database_names())

        c.copy_database("pymongo_test", "pymongo_test1")
        # copy_database() didn't accidentally end the request
        self.assertTrue(c.in_request())

        self.assertTrue("pymongo_test1" in c.database_names())
        self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])

        c.end_request()
        self.assertFalse(c.in_request())
        c.copy_database("pymongo_test", "pymongo_test2",
                        "%s:%d" % (self.host, self.port))
        # copy_database() didn't accidentally restart the request
        self.assertFalse(c.in_request())

        self.assertTrue("pymongo_test2" in c.database_names())
        self.assertEqual("bar", c.pymongo_test2.test.find_one()["foo"])

        if version.at_least(c, (1, 3, 3, 1)):
            c.drop_database("pymongo_test1")

            c.pymongo_test.add_user("mike", "password")

            self.assertRaises(OperationFailure, c.copy_database,
                              "pymongo_test", "pymongo_test1",
                              username="******", password="******")
            if not server_is_master_with_slave(c):
                self.assertFalse("pymongo_test1" in c.database_names())

            self.assertRaises(OperationFailure, c.copy_database,
                              "pymongo_test", "pymongo_test1",
                              username="******", password="******")

            if not server_is_master_with_slave(c):
                self.assertFalse("pymongo_test1" in c.database_names())

            if not is_mongos(c):
                # See SERVER-6427
                c.copy_database("pymongo_test", "pymongo_test1",
                                username="******", password="******")
                self.assertTrue("pymongo_test1" in c.database_names())
                self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])
コード例 #13
0
    def test_copy_db(self):
        c = Connection(self.host, self.port)
        self.assertTrue(c.in_request())

        self.assertRaises(TypeError, c.copy_database, 4, "foo")
        self.assertRaises(TypeError, c.copy_database, "foo", 4)

        self.assertRaises(InvalidName, c.copy_database, "foo", "$foo")

        c.pymongo_test.test.drop()
        c.drop_database("pymongo_test1")
        c.drop_database("pymongo_test2")

        c.pymongo_test.test.insert({"foo": "bar"})

        # Due to SERVER-2329, databases may not disappear from a master in a
        # master-slave pair
        if not server_is_master_with_slave(c):
            self.assertFalse("pymongo_test1" in c.database_names())
            self.assertFalse("pymongo_test2" in c.database_names())

        c.copy_database("pymongo_test", "pymongo_test1")
        # copy_database() didn't accidentally end the request
        self.assertTrue(c.in_request())

        self.assertTrue("pymongo_test1" in c.database_names())
        self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])

        c.end_request()
        c.copy_database("pymongo_test", "pymongo_test2",
                        "%s:%d" % (self.host, self.port))
        # copy_database() didn't accidentally restart the request
        self.assertFalse(c.in_request())

        self.assertTrue("pymongo_test2" in c.database_names())
        self.assertEqual("bar", c.pymongo_test2.test.find_one()["foo"])

        if version.at_least(c, (1, 3, 3, 1)):
            c.drop_database("pymongo_test1")

            c.pymongo_test.add_user("mike", "password")

            self.assertRaises(OperationFailure,
                              c.copy_database,
                              "pymongo_test",
                              "pymongo_test1",
                              username="******",
                              password="******")
            if not server_is_master_with_slave(c):
                self.assertFalse("pymongo_test1" in c.database_names())

            self.assertRaises(OperationFailure,
                              c.copy_database,
                              "pymongo_test",
                              "pymongo_test1",
                              username="******",
                              password="******")

            if not server_is_master_with_slave(c):
                self.assertFalse("pymongo_test1" in c.database_names())

            if not is_mongos(c):
                # See SERVER-6427
                c.copy_database("pymongo_test",
                                "pymongo_test1",
                                username="******",
                                password="******")
                self.assertTrue("pymongo_test1" in c.database_names())
                self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])
コード例 #14
0
ファイル: test_motor_client.py プロジェクト: chiehwen/motor
    def test_copy_db(self, done):
        # 1. Drop old test DBs
        # 2. Copy a test DB N times at once (we need to do it many times at
        #   once to make sure that GreenletPool's start_request() is properly
        #   isolating operations from each other)
        # 3. Create a username and password
        # 4. Copy a database using name and password
        is_ms = server_is_master_with_slave(self.sync_cx)
        ncopies = 10
        nrange = list(range(ncopies))
        test_db_names = ["pymongo_test%s" % i for i in nrange]
        cx = self.motor_connection(host, port)

        def check_copydb_results():
            db_names = self.sync_cx.database_names()
            for test_db_name in test_db_names:
                self.assertTrue(test_db_name in db_names)
                result = self.sync_cx[test_db_name].test_collection.find_one()
                self.assertTrue(result, "No results in %s" % test_db_name)
                self.assertEqual("bar", result.get("foo"), "Wrong result from %s: %s" % (test_db_name, result))

        def drop_all():
            for test_db_name in test_db_names:
                # Setup code has configured a short timeout, and the copying
                # has put Mongo under enough load that we risk timeouts here
                # unless we override.
                self.sync_cx[test_db_name]["$cmd"].find_one({"dropDatabase": 1}, network_timeout=30)

            if not is_ms:
                # Due to SERVER-2329, databases may not disappear from a master
                # in a master-slave pair
                db_names = self.sync_cx.database_names()
                for test_db_name in test_db_names:
                    self.assertFalse(test_db_name in db_names, "%s not dropped" % test_db_name)

        # 1. Drop old test DBs
        yield motor.Op(cx.drop_database, "pymongo_test")
        drop_all()

        # 2. Copy a test DB N times at once
        yield motor.Op(cx.pymongo_test.test_collection.insert, {"foo": "bar"})
        for test_db_name in test_db_names:
            cx.copy_database("pymongo_test", test_db_name, callback=(yield gen.Callback(key=test_db_name)))

        yield motor.WaitAllOps(test_db_names)
        check_copydb_results()

        drop_all()

        # 3. Create a username and password
        yield motor.Op(cx.pymongo_test.add_user, "mike", "password")

        yield AssertRaises(
            pymongo.errors.OperationFailure,
            cx.copy_database,
            "pymongo_test",
            "pymongo_test0",
            username="******",
            password="******",
        )

        yield AssertRaises(
            pymongo.errors.OperationFailure,
            cx.copy_database,
            "pymongo_test",
            "pymongo_test0",
            username="******",
            password="******",
        )

        # 4. Copy a database using name and password
        for test_db_name in test_db_names:
            cx.copy_database(
                "pymongo_test",
                test_db_name,
                username="******",
                password="******",
                callback=(yield gen.Callback(test_db_name)),
            )

        yield motor.WaitAllOps(test_db_names)
        check_copydb_results()

        drop_all()
        done()
コード例 #15
0
    def test_copy_db(self):
        # 1. Drop old test DBs
        # 2. Copy a test DB N times at once (we need to do it many times at
        #   once to make sure that GreenletPool's start_request() is properly
        #   isolating operations from each other)
        # 3. Create a username and password
        # 4. Copy a database using name and password
        is_ms = server_is_master_with_slave(self.sync_cx)
        ncopies = 10
        nrange = list(range(ncopies))
        test_db_names = ['pymongo_test%s' % i for i in nrange]

        def check_copydb_results():
            db_names = self.sync_cx.database_names()
            for test_db_name in test_db_names:
                self.assertTrue(test_db_name in db_names)
                result = self.sync_cx[test_db_name].test_collection.find_one()
                self.assertTrue(result, "No results in %s" % test_db_name)
                self.assertEqual("bar", result.get("foo"),
                    "Wrong result from %s: %s" % (test_db_name, result))

        def drop_all():
            for test_db_name in test_db_names:
                # Setup code has configured a short timeout, and the copying
                # has put Mongo under enough load that we risk timeouts here
                # unless we override.
                self.sync_cx[test_db_name]['$cmd'].find_one(
                    {'dropDatabase': 1}, network_timeout=30)

            if not is_ms:
                # Due to SERVER-2329, databases may not disappear from a master
                # in a master-slave pair
                db_names = self.sync_cx.database_names()
                for test_db_name in test_db_names:
                    self.assertFalse(
                        test_db_name in db_names,
                        "%s not dropped" % test_db_name)

        # 1. Drop old test DBs
        yield self.cx.drop_database('pymongo_test')
        drop_all()

        # 2. Copy a test DB N times at once
        yield self.cx.pymongo_test.test_collection.insert({"foo": "bar"})
        yield [
            self.cx.copy_database("pymongo_test", test_db_name)
            for test_db_name in test_db_names]

        check_copydb_results()
        drop_all()

        # 3. Create a username and password
        yield self.cx.pymongo_test.add_user("mike", "password")

        with assert_raises(pymongo.errors.OperationFailure):
            yield self.cx.copy_database(
                "pymongo_test", "pymongo_test0",
                username="******", password="******")

        with assert_raises(pymongo.errors.OperationFailure):
            yield self.cx.copy_database(
                "pymongo_test", "pymongo_test0",
                username="******", password="******")

        # 4. Copy a database using name and password
        if not self.cx.is_mongos:
            # See SERVER-6427
            yield [
                self.cx.copy_database(
                    "pymongo_test", test_db_name,
                    username="******", password="******")
                for test_db_name in test_db_names]

            check_copydb_results()

        drop_all()
コード例 #16
0
    def test_copy_db(self):
        c = MongoClient(host, port)
        # Due to SERVER-2329, databases may not disappear
        # from a master in a master-slave pair.
        if server_is_master_with_slave(c):
            raise SkipTest("SERVER-2329")
        if (not version.at_least(c, (2, 6, 0)) and
                is_mongos(c) and server_started_with_auth(c)):
            raise SkipTest("Need mongos >= 2.6.0 to test with authentication")
        # We test copy twice; once starting in a request and once not. In
        # either case the copy should succeed (because it starts a request
        # internally) and should leave us in the same state as before the copy.
        c.start_request()

        self.assertRaises(TypeError, c.copy_database, 4, "foo")
        self.assertRaises(TypeError, c.copy_database, "foo", 4)

        self.assertRaises(InvalidName, c.copy_database, "foo", "$foo")

        c.pymongo_test.test.drop()
        c.drop_database("pymongo_test1")
        c.drop_database("pymongo_test2")
        self.assertFalse("pymongo_test1" in c.database_names())
        self.assertFalse("pymongo_test2" in c.database_names())

        c.pymongo_test.test.insert({"foo": "bar"})

        c.copy_database("pymongo_test", "pymongo_test1")
        # copy_database() didn't accidentally end the request
        self.assertTrue(c.in_request())

        self.assertTrue("pymongo_test1" in c.database_names())
        self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])

        c.end_request()
        self.assertFalse(c.in_request())

        if not c.is_mongos:
            # On mongos, this would try to copydb from the mongos itself
            # to the mongod that's the target.  The mongod would try to
            # begin a transaction on the mongos, which would fail.
            c.copy_database("pymongo_test", "pymongo_test2",
                            "%s:%d" % (host, port))
            # copy_database() didn't accidentally restart the request
            self.assertFalse(c.in_request())

            self.assertTrue("pymongo_test2" in c.database_names())
            self.assertEqual("bar", c.pymongo_test2.test.find_one()["foo"])

        # See SERVER-6427 for mongos
        if not is_mongos(c) and server_started_with_auth(c):

            c.drop_database("pymongo_test1")

            c.admin.add_user("admin", "password")
            c.admin.authenticate("admin", "password")
            try:
                c.pymongo_test.add_user("mike", "password")

                self.assertRaises(OperationFailure, c.copy_database,
                                  "pymongo_test", "pymongo_test1",
                                  username="******", password="******")
                self.assertFalse("pymongo_test1" in c.database_names())

                self.assertRaises(OperationFailure, c.copy_database,
                                  "pymongo_test", "pymongo_test1",
                                  username="******", password="******")
                self.assertFalse("pymongo_test1" in c.database_names())

                c.copy_database("pymongo_test", "pymongo_test1",
                                username="******", password="******")
                self.assertTrue("pymongo_test1" in c.database_names())
                self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])
            finally:
                # Cleanup
                remove_all_users(c.pymongo_test)
                c.admin.remove_user("admin")
                c.disconnect()