Esempio n. 1
0
class BaseDbBackupCommandSetLoggerLevelTest(TestCase):
    def setUp(self):
        self.command = BaseDbBackupCommand()

    def test_0_level(self):
        self.command.verbosity = 0
        self.command._set_logger_level()
        self.assertEqual(self.command.logger.level, logging.WARNING)

    def test_1_level(self):
        self.command.verbosity = 1
        self.command._set_logger_level()
        self.assertEqual(self.command.logger.level, logging.INFO)

    def test_2_level(self):
        self.command.verbosity = 2
        self.command._set_logger_level()
        self.assertEqual(self.command.logger.level, logging.DEBUG)

    def test_3_level(self):
        self.command.verbosity = 3
        self.command._set_logger_level()
        self.assertEqual(self.command.logger.level, logging.DEBUG)

    def test_quiet(self):
        self.command.quiet = True
        self.command._set_logger_level()
        self.assertGreater(self.command.logger.level, logging.ERROR)
Esempio n. 2
0
class BaseDbBackupCommandCleanupOldBackupsTest(TestCase):
    def setUp(self):
        HANDLED_FILES.clean()
        self.command = BaseDbBackupCommand()
        self.command.stdout = DEV_NULL
        self.command.encrypt = False
        self.command.compress = False
        self.command.servername = 'foo-server'
        self.command.storage = get_storage()
        HANDLED_FILES['written_files'] = [(f, None) for f in [
            '2015-02-06-042810.tar',
            '2015-02-07-042810.tar',
            '2015-02-08-042810.tar',
            'foodb-2015-02-06-042810.dump',
            'foodb-2015-02-07-042810.dump',
            'foodb-2015-02-08-042810.dump',
        ]]

    @patch('dbbackup.settings.CLEANUP_KEEP', 1)
    def test_clean_db(self):
        self.command.content_type = 'db'
        self.command.database = 'foodb'
        self.command._cleanup_old_backups()
        self.assertEqual(2, len(HANDLED_FILES['deleted_files']))
        self.assertNotIn('foodb-2015-02-08-042810.dump',
                         HANDLED_FILES['deleted_files'])

    @patch('dbbackup.settings.CLEANUP_KEEP_MEDIA', 1)
    def test_clean_media(self):
        self.command.content_type = 'media'
        self.command._cleanup_old_backups()
        self.assertEqual(2, len(HANDLED_FILES['deleted_files']))
        self.assertNotIn('2015-02-08-042810.tar',
                         HANDLED_FILES['deleted_files'])
Esempio n. 3
0
class BaseDbBackupCommandCleanupOldBackupsTest(TestCase):
    def setUp(self):
        HANDLED_FILES.clean()
        self.command = BaseDbBackupCommand()
        self.command.stdout = DEV_NULL
        self.command.encrypt = False
        self.command.compress = False
        self.command.servername = 'foo-server'
        self.command.storage = get_storage()
        HANDLED_FILES['written_files'] = [(f, None) for f in [
            '2015-02-06-042810.tar',
            '2015-02-07-042810.tar',
            '2015-02-08-042810.tar',
            'foodb-2015-02-06-042810.dump',
            'foodb-2015-02-07-042810.dump',
            'foodb-2015-02-08-042810.dump',
        ]]

    @patch('dbbackup.settings.CLEANUP_KEEP', 1)
    def test_clean_db(self):
        self.command.content_type = 'db'
        self.command.database = 'foodb'
        self.command._cleanup_old_backups()
        self.assertEqual(2, len(HANDLED_FILES['deleted_files']))
        self.assertNotIn('foodb-2015-02-08-042810.dump', HANDLED_FILES['deleted_files'])

    @patch('dbbackup.settings.CLEANUP_KEEP_MEDIA', 1)
    def test_clean_media(self):
        self.command.content_type = 'media'
        self.command._cleanup_old_backups()
        self.assertEqual(2, len(HANDLED_FILES['deleted_files']))
        self.assertNotIn('2015-02-08-042810.tar', HANDLED_FILES['deleted_files'])
Esempio n. 4
0
class BaseDbBackupCommandSetLoggerLevelTest(TestCase):
    def setUp(self):
        self.command = BaseDbBackupCommand()

    def test_0_level(self):
        self.command.verbosity = 0
        self.command._set_logger_level()
        self.assertEqual(self.command.logger.level, logging.WARNING)

    def test_1_level(self):
        self.command.verbosity = 1
        self.command._set_logger_level()
        self.assertEqual(self.command.logger.level, logging.INFO)

    def test_2_level(self):
        self.command.verbosity = 2
        self.command._set_logger_level()
        self.assertEqual(self.command.logger.level, logging.DEBUG)

    def test_3_level(self):
        self.command.verbosity = 3
        self.command._set_logger_level()
        self.assertEqual(self.command.logger.level, logging.DEBUG)

    def test_quiet(self):
        self.command.quiet = True
        self.command._set_logger_level()
        self.assertGreater(self.command.logger.level, logging.ERROR)
Esempio n. 5
0
 def setUp(self):
     HANDLED_FILES.clean()
     self.command = BaseDbBackupCommand()
     self.command.stdout = DEV_NULL
     self.command.encrypt = False
     self.command.compress = False
     self.command.servername = 'foo-server'
     self.command.storage = get_storage()
     HANDLED_FILES['written_files'] = [(f, None) for f in [
         '2015-02-06-042810.tar',
         '2015-02-07-042810.tar',
         '2015-02-08-042810.tar',
         'foodb-2015-02-06-042810.dump',
         'foodb-2015-02-07-042810.dump',
         'foodb-2015-02-08-042810.dump',
     ]]
Esempio n. 6
0
 def setUp(self):
     HANDLED_FILES.clean()
     self.command = BaseDbBackupCommand()
     self.command.stdout = DEV_NULL
     self.command.encrypt = False
     self.command.compress = False
     self.command.servername = 'foo-server'
     self.command.storage = get_storage()
     HANDLED_FILES['written_files'] = [(f, None) for f in [
         '2015-02-06-042810.tar',
         '2015-02-07-042810.tar',
         '2015-02-08-042810.tar',
         'foodb-2015-02-06-042810.dump',
         'foodb-2015-02-07-042810.dump',
         'foodb-2015-02-08-042810.dump',
     ]]
Esempio n. 7
0
class BaseDbBackupCommandSetLoggerLevelTest(TestCase):
    def setUp(self):
        self.command = BaseDbBackupCommand()

    def test_debug_level(self):
        self.command.verbosity = 0
        self.command._set_logger_level()
        self.assertEqual(self.command.logger.level, 10)

    def test_info_level(self):
        self.command.verbosity = 1
        self.command._set_logger_level()
        self.assertEqual(self.command.logger.level, 20)

    def test_quiet(self):
        self.command.quiet = True
        self.command._set_logger_level()
        self.assertGreater(self.command.logger.level, 50)
Esempio n. 8
0
class BaseDbBackupCommandLogTest(TestCase):
    def setUp(self):
        self.command = BaseDbBackupCommand()
        self.command.stdout = StringIO()

    def test_less_level(self):
        self.command.verbosity = 1
        self.command.log("foo", 2)
        self.command.stdout.seek(0)
        self.assertFalse(self.command.stdout.read())

    def test_more_level(self):
        self.command.verbosity = 1
        self.command.log("foo", 0)
        self.command.stdout.seek(0)
        self.assertEqual('foo', self.command.stdout.read())

    def test_quiet(self):
        self.command.quiet = True
        self.command.verbosity = 1
        self.command.log("foo", 0)
        self.command.stdout.seek(0)
        self.assertFalse(self.command.stdout.read())
Esempio n. 9
0
 def setUp(self):
     self.command = BaseDbBackupCommand()
     self.command.stdout = StringIO()
Esempio n. 10
0
 def setUp(self):
     HANDLED_FILES.clean()
     self.command = BaseDbBackupCommand()
     self.command.storage = get_storage()
Esempio n. 11
0
class BaseDbBackupCommandMethodsTest(TestCase):
    def setUp(self):
        HANDLED_FILES.clean()
        self.command = BaseDbBackupCommand()
        self.command.storage = get_storage()

    def test_read_from_storage(self):
        HANDLED_FILES['written_files'].append(['foo', File(six.BytesIO(b'bar'))])
        file_ = self.command.read_from_storage('foo')
        self.assertEqual(file_.read(), b'bar')

    def test_write_to_storage(self):
        self.command.write_to_storage(six.BytesIO(b'foo'), 'bar')
        self.assertEqual(HANDLED_FILES['written_files'][0][0], 'bar')

    def test_read_local_file(self):
        # setUp
        self.command.path = '/tmp/foo.bak'
        open(self.command.path, 'w').close()
        # Test
        output_file = self.command.read_local_file(self.command.path)
        # tearDown
        os.remove(self.command.path)

    def test_write_local_file(self):
        fd, path = File(six.BytesIO(b"foo")), '/tmp/foo.bak'
        self.command.write_local_file(fd, path)
        self.assertTrue(os.path.exists(path))
        # tearDown
        os.remove(path)

    def test_ask_confirmation(self):
        # Yes
        with patch('dbbackup.management.commands._base.input', return_value='y'):
            self.command._ask_confirmation()
        with patch('dbbackup.management.commands._base.input', return_value='Y'):
            self.command._ask_confirmation()
        with patch('dbbackup.management.commands._base.input', return_value=''):
            self.command._ask_confirmation()
        with patch('dbbackup.management.commands._base.input', return_value='foo'):
            self.command._ask_confirmation()
        # No
        with patch('dbbackup.management.commands._base.input', return_value='n'):
            with self.assertRaises(SystemExit):
                self.command._ask_confirmation()
        with patch('dbbackup.management.commands._base.input', return_value='N'):
            with self.assertRaises(SystemExit):
                self.command._ask_confirmation()
        with patch('dbbackup.management.commands._base.input', return_value='No'):
            with self.assertRaises(SystemExit):
                self.command._ask_confirmation()
Esempio n. 12
0
 def setUp(self):
     self.command = BaseDbBackupCommand()