def test__backup__auth_args(self, *args): m = mongobar.Mongobar() m.config.add({ "connections": { "default": { "host": "localhost", "port": 27017, "username": "******", "password": "******", "authdb": "authdb" } } }) m.backup() self.assertIn( mock.call([ "mongodump", "--host", "localhost", "--port", "27017", "-u", "user", "-p", "pass", "--authenticationDatabase", "authdb", "--db", "d1", "--out", os.path.join(m.config.connection_dir, "foo-bar"), "--quiet", "--gzip" ]), args[0].call_args_list )
def test__restore__authentication_options(self, *args): m = mongobar.Mongobar() m.config.add({ "connections": { "default": { "host": "localhost", "port": 27017, "username": "******", "password": "******", "authdb": "authdb" } } }) m.restore("backup") directory = os.path.join(m.config.connection_dir, "backup") self.assertIn( mock.call([ "mongorestore", "--host", "localhost", "--port", "27017", "-u", "user", "-p", "pass", "--authenticationDatabase", "authdb", "--nsInclude", "d1.*", "--drop", "--dir", directory, "--gzip" ]), args[1].call_args_list )
def test__create_pymongo_client__default_connection(self, mongoclient): m = mongobar.Mongobar() m.create_pymongo_client() mongoclient.assert_called_with( host="localhost", port=27017 )
def test__read_metadata__file_not_found(self, loads, open_, mongoclient): m = mongobar.Mongobar() self.assertEqual(m.read_metadata("name"), { "host": "localhost", "port": 27017, "date": "0001-01-01T00:00:00.0000", "databases": [], "message": "Metadata not found" })
def test__backup__message_arg(self, *args): m = mongobar.Mongobar() m.backup(message="foo") # extract metadata arg passed to self.write_metadata write_metadata_mock = args[1] write_metadata_calls = write_metadata_mock.call_args_list[0] write_metadata_args = write_metadata_calls[0] metadata = write_metadata_args[1] self.assertEqual(metadata["message"], "foo")
def test__get_connection_directories__return_names_and_counts(self, *args): m = mongobar.Mongobar() m.get_connection_directories(count=True) self.assertEqual( args[0].call_args_list, [ mock.call(m.config.root), mock.call(os.path.join(m.config.root, "host")) ] )
def test__write_metadata(self, dump, open_, mongoclient): m = mongobar.Mongobar() m.write_metadata("name", {"key": "value"}) path = os.path.join( m.config.connection_dir, "name", "metadata.json" ) open_.assert_called_with(path, "w+") file_handle = open_() dump.assert_called_with({"key": "value"}, file_handle)
def test__generate_metadata__databases_arg(self, mongoclient): m = mongobar.Mongobar() metadata = m.generate_metadata(databases=["d1", "d2", "d3"]) self.assertIn("host", metadata) self.assertIn("port", metadata) self.assertIn("date", metadata) self.assertIn("databases", metadata) for database in metadata["databases"]: self.assertIn("name", database) self.assertIn("collections", database)
def test__restore__destination_databases_arg(self, *args): m = mongobar.Mongobar() m.restore("backup", databases=["d1"], destination_databases=["destination"]) directory = os.path.expanduser("~/.mongobar_backups/localhost:27017/backup/d1") args[1].assert_called_with([ "mongorestore", "--host", "localhost", "--port", "27017", "--db", "destination", "--nsInclude", "d1.*", "--drop", "--dir", directory, "--gzip" ])
def test__read_metadata(self, loads, open_, mongoclient): m = mongobar.Mongobar() m.read_metadata("name") path = os.path.join( m.config.connection_dir, "name", "metadata.json" ) open_.assert_called_with(path, "r") file_handle = open_() file_handle.read.assert_called() loads.assert_called_with("")
def test__restore__collection_arg(self, *args): m = mongobar.Mongobar() m.restore("backup", collections=["c1"]) self.assertIn( mock.call([ "mongorestore", "--host", "localhost", "--port", "27017", "--nsInclude", "d1.c1", "--drop", "--dir", os.path.join(m.config.connection_dir, "backup"), "--gzip" ]), args[1].call_args_list )
def test__create_pymongo_client__custom_connection(self, mongoclient): m = mongobar.Mongobar() m.config.add({ "connections": { "custom": { "host": "custom", "port": 27017 } } }) m.config.connection = "custom" m.create_pymongo_client() mongoclient.assert_called_with( host="custom", port=27017 )
def test__backup__db_does_not_exist__command_called(self, check_output, *args): m = mongobar.Mongobar() m.backup(databases=["foobar"]) self.assertIn( mock.call([ "mongodump", "--host", "localhost", "--port", "27017", "--db", "foobar", "--out", os.path.join(m.config.connection_dir, "foo-bar"), "--quiet", "--gzip" ]), check_output.call_args_list )
def test__backup__collection_arg(self, check_output, *args): m = mongobar.Mongobar() m.backup(databases=["d1"], collections=["c1"]) self.assertIn( mock.call([ "mongodump", "--host", "localhost", "--port", "27017", "--db", "d1", "--collection", "c1", "--out", os.path.join(m.config.connection_dir, "foo-bar"), "--quiet", "--gzip" ]), check_output.call_args_list )
def test__backup(self, check_output, *args): m = mongobar.Mongobar() m.backup() directory = os.path.join(m.config.connection_dir, "foo-bar") self.assertIn( mock.call([ "mongodump", "--host", "localhost", "--port", "27017", "--db", "d1", "--out", directory, "--quiet", "--gzip" ]), check_output.call_args_list )
def test__restore(self, *args): m = mongobar.Mongobar() m.restore("d1") directory = os.path.join(m.config.connection_dir, "d1") self.assertIn( mock.call([ "mongorestore", "--host", "localhost", "--port", "27017", "--nsInclude", "d1.*", "--drop", "--dir", directory, "--gzip" ]), args[1].call_args_list )
def test__create_pymongo_client__auth_options(self, mongoclient): mongoclient.side_effect = pymongo.errors.PyMongoError() m = mongobar.Mongobar() m.config.add({ "connections": { "default": { "host": "localhost", "port": 27017, "username": "******", "password": "******", "authdb": "authdb" } } }) with self.assertRaises(mongobar.exceptions.ServerConnectionError): m.create_pymongo_client()
def test__create_pymongo_client__auth_options(self, mongoclient): m = mongobar.Mongobar() m.config.add({ "connections": { "default": { "host": "localhost", "port": 27017, "username": "******", "password": "******", "authdb": "authdb" } } }) m.create_pymongo_client() mongoclient.assert_called_with( host="localhost", port=27017, username="******", password="******", authSource="authdb" )
def test__remove_backup__raises_BackupNotFoundError(self, *args): m = mongobar.Mongobar() with self.assertRaises(mongobar.exceptions.BackupNotFoundError): m.remove_backup("foo")
def test__remove_backup(self, *args): m = mongobar.Mongobar() m.remove_backup("foo") backup_directory = m.config.connection_dir args[1].assert_called_with(os.path.join(backup_directory, "foo"))
def test__get_backups__directory_does_not_exist__return_empty_list(self, *args): m = mongobar.Mongobar() self.assertEqual(m.get_backups(), [])
def test__get_backups(self, *args): m = mongobar.Mongobar() m.get_backups() args[0].assert_called_with(m.config.connection_dir)
def test__get_connection_directories__return_names(self, *args): m = mongobar.Mongobar() m.get_connection_directories() args[0].assert_called_with(m.config.root)
def test__get_hosts__directory_does_not_exist(self, *args): m = mongobar.Mongobar() self.assertEqual(m.get_connection_directories(), [])
def test__restore__collection_arg__raises_CommandError(self, *args): m = mongobar.Mongobar() with self.assertRaises(mongobar.exceptions.CommandError): m.restore("backup", collections=["c1"])
def test__backup__create_host_directory(self, *args): m = mongobar.Mongobar() m.backup() self.assertIn(mock.call(m.config.connection_dir), args[3].call_args_list)
def test__backup__collection_arg__raises_CalledProcessError(self, check_output, *args): m = mongobar.Mongobar() with self.assertRaises(mongobar.exceptions.CommandError): m.backup(collections=["foobar"])
def test__restore__destination_databases_arg__raises_DestinationDatabasesLengthError(self, *args): m = mongobar.Mongobar() with self.assertRaises(mongobar.exceptions.DestinationDatabasesLengthError): m.restore("backup", destination_databases=["foobar"])
def test__restore__collections_arg__raises_CollectionNotFoundInBackupError(self, *args): m = mongobar.Mongobar() with self.assertRaises(mongobar.exceptions.CollectionNotFoundInBackupError): m.restore("backup", collections=["foobar"])
def test__restore__databases_arg__raises_DatabaseNotFoundInBackupError(self, *args): m = mongobar.Mongobar() with self.assertRaises(mongobar.exceptions.DatabaseNotFoundInBackupError): m.restore("backup", databases=["foobar"])