def test_file_full_pipeline(self): """Basic test of a single file archive, compress, encrypt and backup.""" try: # Setup the test state. tempdir, in_dir, out_dir = self._create_tempdir_structure('input', \ 'output') in_file, out_file, cfg_file = self._create_single_file_test(\ 'file.txt', 'file.txt.tar.xz.gpg', tempdir, in_dir, out_dir, \ [_ConfigSection('input/file.txt', out_dir, archive='yes', compress='yes', encrypt='yes')]) in_dir_hash = test.get_dir_md5(in_dir) in_file_hash = test.get_file_md5(in_file) # Run backup. with redir_stdstreams() as (stdout, stderr): backup.main(['--config', cfg_file, '--gpg-home', test.GPG_HOME]) self.assertEqual('', stdout.getvalue().strip()) self.assertEqual('', stderr.getvalue().strip()) # Assert the output state looks as we expect. self.assertTrue(os.path.isfile(in_file)) self.assertTrue(os.path.isfile(out_file)) self.assertEqual(in_dir_hash, test.get_dir_md5(in_dir)) undo_out_file = os.path.join(tempdir, 'file.txt') backup.unencrypt_path(os.path.join(tempdir, 'file.txt.tar.xz'), out_file, homedir=test.GPG_HOME) backup.uncompress_path(os.path.join(tempdir, 'file.txt.tar'), os.path.join(tempdir, 'file.txt.tar.xz')) backup.unarchive_path(tempdir, os.path.join(tempdir, 'file.txt.tar')) self.assertTrue(os.path.isfile(undo_out_file)) self.assertEqual(in_file_hash, test.get_file_md5(undo_out_file)) finally: shutil.rmtree(tempdir)
def test_file_compress(self): """Basic test of a single file compress and backup.""" try: # Setup the test state. tempdir, in_dir, out_dir = self._create_tempdir_structure('input', \ 'output') in_file, out_file, cfg_file = self._create_single_file_test(\ 'file.txt', 'file.txt.xz', tempdir, in_dir, out_dir, \ [_ConfigSection('input/file.txt', out_dir, compress='yes')]) in_dir_hash = test.get_dir_md5(in_dir) in_file_hash = test.get_file_md5(in_file) # Run backup. with redir_stdstreams() as (stdout, stderr): backup.main(['--config', cfg_file]) self.assertEqual('', stdout.getvalue().strip()) self.assertEqual('', stderr.getvalue().strip()) # Assert the output state looks as we expect. self.assertTrue(os.path.isfile(in_file)) self.assertTrue(os.path.isfile(out_file)) self.assertEqual(in_dir_hash, test.get_dir_md5(in_dir)) undo_out_file = os.path.join(tempdir, 'file.txt') backup.uncompress_path(undo_out_file, out_file) self.assertTrue(os.path.isfile(undo_out_file)) self.assertEqual(in_file_hash, test.get_file_md5(undo_out_file)) finally: shutil.rmtree(tempdir)
def _assert_file_processing(self, processing_func, unprocessing_func, processed_file_types, input_filename, processed_filename, output_filename, is_ascii, output_is_dir=False, processed_is_same=False): """ Creates a single testing file either ASCII or binary in the root tempdir and then performs a generic 'processing function' to mutate this into a 'processed' file. Finally it performs a generic 'unprocessing function' to un-mutate this processed file into an output directory (to remove the chance of name collision) in an 'output' directory. Asserts that the input and output files are the same and that the processed file state is as expected. Args: processing_func: A callable which has a signature like processing_func(dest, src) where dest is a path to the processed output and src is a path to the input for processing. unprocessing_func: A callable which has a signature like unprocessing_func(dest, src) where dest is a path to the unprocessed output and src is a path to the input for unprocessing. processed_file_types: A list of strings describing the acceptable file types of the processed output. input_filename: string name of the input file. processed_filename: string name of the processed file. output_filename: string name of the unprocessed file. is_ascii: boolean, True to create an ASCII file type, False to create a binary file type. output_is_dir: boolean, True if the output is a directory. processed_is_same: boolean, True if the processed output is the same as the input. """ try: tempdir = tempfile.mkdtemp() out_dir = os.path.join(tempdir, 'output') os.makedirs(out_dir) test_input = os.path.join(tempdir, input_filename) test_processed = os.path.join(tempdir, processed_filename) test_output = os.path.join(out_dir, output_filename) # Create the file. if is_ascii: test.create_ascii_file(test_input) input_file_type = self._FILE_TYPE_ASCII else: test.create_binary_file(test_input) input_file_type = self._FILE_TYPE_BINARY # Assert the starting state looks as we expect. self.assertTrue(os.path.isfile(test_input)) self.assertFalse(os.path.exists(test_processed)) self.assertFalse(os.path.exists(test_output)) self.assertEqual(input_file_type, test.get_file_type(test_input)) test_input_hash = test.get_file_md5(test_input) # Perform the processing operation on the file. processing_func(test_processed, test_input) # Assert the processed state looks as we expect. self.assertTrue(os.path.isfile(test_input)) self.assertTrue(os.path.isfile(test_processed)) self.assertFalse(os.path.exists(test_output)) self.assertEqual(input_file_type, test.get_file_type(test_input)) self.assertEqual(test_input_hash, test.get_file_md5(test_input)) self.assertIn(test.get_file_type(test_processed), processed_file_types) test_processed_hash = test.get_file_md5(test_processed) self.assertEqual(32, len(test_processed_hash)) if processed_is_same: self.assertEqual(test_input_hash, test_processed_hash) else: self.assertNotEqual(test_input_hash, test_processed_hash) # Delete the file (so we can check it doesn't get recreated). os.remove(test_input) self.assertFalse(os.path.exists(test_input)) # Perform the reverse processing operation on the file. unprocessing_func(out_dir if output_is_dir else test_output, test_processed) # Assert the unprocessed state looks as we expect. self.assertFalse(os.path.exists(test_input)) self.assertTrue(os.path.isfile(test_processed)) self.assertTrue(os.path.isfile(test_output)) self.assertEqual(input_file_type, test.get_file_type(test_output)) self.assertEqual(test_input_hash, test.get_file_md5(test_output)) self.assertIn(test.get_file_type(test_processed), processed_file_types) self.assertEqual(test_processed_hash, test.get_file_md5(test_processed)) finally: shutil.rmtree(tempdir)
def _assert_dir_processing(self, processing_func, unprocessing_func, processed_file_types, input_dirname, processed_dirname, output_dirname, output_is_dir=False, processed_is_dir=False, processed_is_same=False): """ Creates a directory structure in an 'input' directory. Then performs a generic 'processing function' to mutate this input directory structure into something in a 'processed' directory. Finally it performs a generic 'unprocessing function' to un-mutate this processed directory into a directory structure in an 'output' directory. Asserts throughout that the contents of input and output directories is the same, and the processed directory state is as expected. Args: processing_func: A callable which has a signature like processing_func(dest, src) where dest is a path to the processed output and src is a path to the input for processing. unprocessing_func: A callable which has a signature like unprocessing_func(dest, src) where dest is a path to the unprocessed output and src is a path to the input for unprocessing. processed_file_types: A list of strings describing the acceptable file types of the processed output. input_dirname: string name of the input dir structure. processed_dirname: string name of the processed dir structure. output_dirname: string name of the unprocessed dir structure output_is_dir: boolean, True if the output is a directory. processed_is_dir: boolean, True if the processed output is a directory. processed_is_same: boolean, True if the processed output is the same as the input. """ try: tempdir = tempfile.mkdtemp() in_dir = os.path.join(tempdir, 'input') prc_dir = os.path.join(tempdir, 'processed') out_dir = os.path.join(tempdir, 'output') os.makedirs(in_dir) os.makedirs(prc_dir) os.makedirs(out_dir) test_input = os.path.join(in_dir, input_dirname) test_processed = os.path.join(prc_dir, processed_dirname) test_output = os.path.join(out_dir, output_dirname) # Create the structure. test.create_test_structure(test_input) # Assert the starting state looks as we expect. self.assertTrue(os.path.isdir(test_input)) self.assertFalse(os.path.exists(test_processed)) self.assertFalse(os.path.exists(test_output)) self.assertEqual(self._FILE_TYPE_DIR, test.get_file_type(test_input)) test_input_hash = test.get_dir_md5(test_input) # Perform the processing operation on the directory. processing_func(prc_dir if processed_is_dir else test_processed, test_input) # Assert the processed state looks as we expect. self.assertTrue(os.path.isdir(test_input)) if processed_is_dir: self.assertTrue(os.path.isdir(test_processed)) else: self.assertTrue(os.path.isfile(test_processed)) self.assertFalse(os.path.exists(test_output)) self.assertEqual(self._FILE_TYPE_DIR, test.get_file_type(test_input)) self.assertEqual(test_input_hash, test.get_dir_md5(test_input)) self.assertIn(test.get_file_type(test_processed), processed_file_types) if processed_is_dir: test_processed_hash = test.get_dir_md5(test_processed) else: test_processed_hash = test.get_file_md5(test_processed) self.assertEqual(32, len(test_processed_hash)) if processed_is_same: self.assertEqual(test_input_hash, test_processed_hash) else: self.assertNotEqual(test_input_hash, test_processed_hash) # Delete the struct (so we can check it doesn't get recreated). shutil.rmtree(test_input) self.assertFalse(os.path.exists(test_input)) # Perform the reverse processing operation on the file. unprocessing_func(out_dir if output_is_dir else test_output, test_processed) # Assert the unprocessed state looks as we expect. self.assertFalse(os.path.exists(test_input)) if processed_is_dir: self.assertTrue(os.path.isdir(test_processed)) else: self.assertTrue(os.path.isfile(test_processed)) self.assertTrue(os.path.isdir(test_output)) self.assertEqual(self._FILE_TYPE_DIR, test.get_file_type(test_output)) self.assertEqual(test_input_hash, test.get_dir_md5(test_output)) self.assertIn(test.get_file_type(test_processed), processed_file_types) if processed_is_dir: self.assertEqual(test_processed_hash, test.get_dir_md5(test_processed)) else: self.assertEqual(test_processed_hash, test.get_file_md5(test_processed)) finally: shutil.rmtree(tempdir)