Skip to content

Commit

Permalink
add command tests to verify storage configuration is setup correctly …
Browse files Browse the repository at this point in the history
…when setting storage option in command
  • Loading branch information
mjlabe committed Dec 26, 2020
1 parent 4a16ff8 commit 8c22363
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 10 deletions.
41 changes: 40 additions & 1 deletion dbbackup/tests/commands/test_dbbackup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
from mock import patch

from django.test import TestCase
from storages.backends.s3boto3 import S3Boto3Storage

from dbbackup.management.commands.dbbackup import Command as DbbackupCommand
from dbbackup.db.base import get_connector
from dbbackup.storage import get_storage, get_backup_storage
from dbbackup.tests.utils import (TEST_DATABASE, add_public_gpg, clean_gpg_keys,
DEV_NULL)
DEV_NULL, FakeStorage)


@patch('dbbackup.settings.GPG_RECIPIENT', 'test@test')
Expand Down Expand Up @@ -115,3 +116,41 @@ def test_fake_func(self):
def test_default(self):
self.storage = get_backup_storage('default')
self.assertIsInstance(self.storage.storage, FileSystemStorage)


class DbbackupCommandMultipleStorages(TestCase):
def setUp(self):
self.command = DbbackupCommand()
self.command.stdout = DEV_NULL
self.command.uncompress = False
self.command.decrypt = False
self.command.backup_extension = 'bak'
self.command.filename = 'foofile'
self.command.database = TEST_DATABASE
self.command.passphrase = None
self.command.interactive = True
self.command.database_name = 'default'
self.command.connector = get_connector('default')

@staticmethod
def fake_backup():
return True

def test_default(self):
self.command.handle(storage='default', verbosity=1)
self.assertIsInstance(self.command.storage.storage, FileSystemStorage)

@patch.object(DbbackupCommand, '_save_new_backup')
def test_fake(self, fake_backup):
self.command.handle(storage='fake_storage', verbosity=1)
self.assertIsInstance(self.command.storage.storage, FakeStorage)

@patch.object(DbbackupCommand, '_save_new_backup')
def test_S3(self, fake_backup):
self.command.handle(storage='s3_storage', verbosity=1)
self.assertIsInstance(self.command.storage.storage, S3Boto3Storage)
self.assertEqual(vars(self.command.storage.storage)['_constructor_args'][1],
{'access_key': 'my_id',
'secret_key': 'my_secret',
'bucket_name': 'my_bucket_name',
'default_acl': 'private'})
60 changes: 51 additions & 9 deletions dbbackup/tests/commands/test_dbrestore.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
"""
Tests for dbrestore command.
"""
from django.core.exceptions import ImproperlyConfigured
from django.core.management import execute_from_command_line
from mock import patch
from tempfile import mktemp
from shutil import copyfileobj
from tempfile import mktemp

from django.test import TestCase
from django.core.management.base import CommandError
from django.core.files import File
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.files import File
from django.core.files.storage import FileSystemStorage
from django.core.management import execute_from_command_line
from django.core.management.base import CommandError
from django.test import TestCase
from mock import patch
from six import StringIO
from storages.backends.s3boto3 import S3Boto3Storage

from dbbackup import utils
from dbbackup.db.base import get_connector
from dbbackup.db.mongodb import MongoDumpConnector
from dbbackup.management.commands.dbrestore import Command as DbrestoreCommand
from dbbackup.storage import get_storage
from dbbackup.settings import HOSTNAME
from dbbackup.storage import get_storage
from dbbackup.tests.utils import (TEST_DATABASE, add_private_gpg, DEV_NULL,
clean_gpg_keys, HANDLED_FILES, TEST_MONGODB, TARED_FILE,
get_dump, get_dump_name)
get_dump, get_dump_name, FakeStorage)


@patch('dbbackup.management.commands._base.input', return_value='y')
Expand Down Expand Up @@ -152,3 +154,43 @@ def test_mongo_settings_backup_command(self, mock_runcommands, *args):
HANDLED_FILES['written_files'].append((TARED_FILE, open(TARED_FILE, 'rb')))
self.command._restore_backup()
self.assertTrue(mock_runcommands.called)


class DbrestoreCommandRestoreMultipleBackupTest(TestCase):
def setUp(self):
self.command = DbrestoreCommand()
self.command.stdout = DEV_NULL
self.command.uncompress = False
self.command.decrypt = False
self.command.backup_extension = 'bak'
self.command.filename = 'foofile'
self.command.database = TEST_DATABASE
self.command.passphrase = None
self.command.interactive = True
self.command.servername = HOSTNAME
self.command.database_name = 'default'
self.command.connector = get_connector('default')
HANDLED_FILES.clean()

@staticmethod
def fake_restore():
return True

def test_default(self):
self.command.handle(storage='default', verbosity=1)
self.assertIsInstance(self.command.storage.storage, FileSystemStorage)

@patch.object(DbrestoreCommand, '_restore_backup')
def test_fake(self, fake_restore):
self.command.handle(storage='fake_storage', verbosity=1)
self.assertIsInstance(self.command.storage.storage, FakeStorage)

@patch.object(DbrestoreCommand, '_restore_backup')
def test_S3(self, fake_restore):
self.command.handle(storage='s3_storage', verbosity=1)
self.assertIsInstance(self.command.storage.storage, S3Boto3Storage)
self.assertEqual(vars(self.command.storage.storage)['_constructor_args'][1],
{'access_key': 'my_id',
'secret_key': 'my_secret',
'bucket_name': 'my_bucket_name',
'default_acl': 'private'})

0 comments on commit 8c22363

Please sign in to comment.