def test_dry_run(self, mock_sh): sys.argv = [sys.argv[0], '-n', '-vvvv', self.conf_file] Backup() self.assertIn( call.mysqldump('--defaults-extra-file=~/.my.cnf', '--all-databases', _out=None), mock_sh.method_calls) self.assertIn(call.pg_dumpall(_out=None), mock_sh.method_calls)
def test_exception(self): sys.argv = [sys.argv[0], '-vvvv', self.conf_file] Backup() stderr = sys.stderr.getvalue() self.assertIn( "CRITICAL Backup failure: %s" % self.mock_sh_rsync.ErrorReturnCode.stderr, stderr)
def test_backup_exists(self): os.makedirs(self.destination, exist_ok=True) date_stamp = datetime.now().strftime(self.frequency) bak = os.path.join(self.destination, 'test_basic_inc.%s.tar.gz' % date_stamp) open(bak, 'w').close() sys.argv = [sys.argv[0], '-vvvv', self.conf_file] Backup() stderr = sys.stderr.getvalue() self.assertIn("DEBUG Backup file test_basic_inc.%s already exists" % date_stamp, stderr) os.remove(bak)
def test_snar_exists(self): os.makedirs(self.destination, exist_ok=True) snar = os.path.join(self.destination, 'test_basic_inc.snar') open(snar, 'w').close() sys.argv = [sys.argv[0], '-vvvv', self.conf_file] Backup() stderr = sys.stderr.getvalue() self.assertNotIn("DEBUG Snar file not exists", stderr) self.assertIn("DEBUG Full backup frequency not defined", stderr) os.remove(snar)
def test_snar_exists_outdated(self): os.makedirs(self.destination, exist_ok=True) snar = os.path.join(self.destination, 'test_basic_inc_full.snar') open(snar, 'w').close() os.utime(snar, (1, 1)) # set a very old time sys.argv = [sys.argv[0], '-vvvv', self.conf_file] Backup() stderr = sys.stderr.getvalue() self.assertNotIn("DEBUG Snar file exists in current full backup period", stderr) self.assertIn("DEBUG Removed outdated snar file", stderr) os.remove(snar)
def test_external(self, mock_sh): sys.argv = [sys.argv[0], '-vvvv', self.conf_file] # Let this test create destination to increase branch coverage os.makedirs(self.destination, exist_ok=True) Backup() self.assertIn(call.mount(self.device, self.destination), mock_sh.method_calls) self.assertIn(call.umount('-l', self.destination), mock_sh.method_calls) stderr = sys.stderr.getvalue() self.assertIn(self.conf_file, stderr) self.assertIn("DEBUG Mounted device", stderr) self.assertIn("DEBUG Unmounted device", stderr)
def test_local(self): sys.argv = [sys.argv[0], '-vvvv', self.conf_file] Backup() stderr = sys.stderr.getvalue() self.assertIn(self.conf_file, stderr) self.assertNotIn("DEBUG Mounted device", stderr) self.assertIn("DEBUG Not incremental backup", stderr) self.assertIn("DEBUG Elapsed time", stderr) stdout = sys.stdout.getvalue() self.assertIn('sysadmin-backup', stdout) self.assertIn("Performing backup", stdout) self.assertIn('Backup finished', stdout)
def test_db(self, mock_sh): sys.argv = [sys.argv[0], '-vvvv', self.conf_file] Backup() self.assertIn( call.mysqldump( '--defaults-extra-file=~/.my.cnf', '--all-databases', _out=os.path.join(self.destination, 'mysqldump.sql')), mock_sh.method_calls) self.assertIn(call.pg_dumpall(_out=os.path.join(self.destination, 'postgresdump.sql')), mock_sh.method_calls) stderr = sys.stderr.getvalue() self.assertIn(self.conf_file, stderr) self.assertIn("DEBUG Performed mysql dump", stderr) self.assertIn("DEBUG Performed postgres dump", stderr)
def test_inc(self): sys.argv = [sys.argv[0], '-vvvv', self.conf_file] Backup() stderr = sys.stderr.getvalue() self.assertIn(self.conf_file, stderr) self.assertNotIn("DEBUG Mounted device", stderr) self.assertNotIn("DEBUG Not incremental backup", stderr) self.assertIn("DEBUG Snar file not exists", stderr) self.assertIn('cpzvf', stderr) self.assertIn("DEBUG Elapsed time", stderr) stdout = sys.stdout.getvalue() self.assertIn('sysadmin-backup', stdout) self.assertIn("Performing backup", stdout) self.assertIn('Backup finished', stdout) date_stamp = datetime.now().strftime(self.frequency) bak = os.path.join(self.destination, 'test_basic_inc.%s.tar.gz' % date_stamp) self.assertTrue(os.path.exists(bak))
def test_external_encrypted(self, mock_sh): sys.argv = [sys.argv[0], '-vvvv', self.conf_file] Backup() self.assertIn( call.cryptsetup('luksOpen', '-d', '/root/backup-external-key', self.device, self.encrypted_map), mock_sh.method_calls) self.assertIn( call.mount('/dev/mapper/%s' % self.encrypted_map, self.destination), mock_sh.method_calls) self.assertIn(call.umount('-l', self.destination), mock_sh.method_calls) self.assertIn(call.cryptsetup('luksClose', self.encrypted_map), mock_sh.method_calls) stderr = sys.stderr.getvalue() self.assertIn(self.conf_file, stderr) self.assertIn("DEBUG Opened encrypted device", stderr) self.assertIn("DEBUG Closed encrypted device", stderr)
def test_snar_exists(self): os.makedirs(self.destination, exist_ok=True) snar = os.path.join(self.destination, 'test_basic_inc_full.snar') open(snar, 'w').close() sys.argv = [sys.argv[0], '-vvvv', self.conf_file] Backup() stderr = sys.stderr.getvalue() self.assertIn(self.conf_file, stderr) self.assertNotIn("DEBUG Mounted device", stderr) self.assertNotIn("DEBUG Not incremental backup", stderr) self.assertNotIn("DEBUG Snar file not exists", stderr) self.assertNotIn("DEBUG Full backup frequency not defined", stderr) self.assertIn("DEBUG Snar file exists in current full backup period", stderr) self.assertIn("DEBUG Elapsed time", stderr) stdout = sys.stdout.getvalue() self.assertIn('sysadmin-backup', stdout) self.assertIn("Performing backup", stdout) self.assertIn('Backup finished', stdout) os.remove(snar)
#!/usr/bin/python3 from core.app import Backup if __name__ == '__main__': Backup()
def test_dry_run(self): sys.argv = [sys.argv[0], '-vvvv', '-n', self.conf_file] Backup() stderr = sys.stderr.getvalue() self.assertIn("-n", stderr)
def test_no_verbosity(self): sys.argv = [sys.argv[0], self.conf_file] Backup() stdout = sys.stdout.getvalue() self.assertIn('Backup finished', stdout)
def test_unhandled(self): sys.argv = [sys.argv[0], '-vvvv', self.conf_file] Backup() stderr = sys.stderr.getvalue() self.assertIn("ERROR Unable to perform mysqldump: %s" % self.mock_sh_dump.ErrorReturnCode.stderr, stderr) self.assertIn("ERROR Unable to perform postgresdump: %s" % self.mock_sh_dump.ErrorReturnCode.stderr, stderr)
def test_mounted(self): sys.argv = [sys.argv[0], '-vvvv', self.conf_file] Backup() stderr = sys.stderr.getvalue() self.assertIn("DEBUG Device already mounted", stderr) self.assertIn("DEBUG Unmounted device", stderr)
def test_dev_unavailable(self): sys.argv = [sys.argv[0], '-vvvv', self.conf_file] with self.assertRaises(SystemExit): Backup() stderr = sys.stderr.getvalue() self.assertIn("DEBUG Device unavailable", stderr)