Пример #1
0
    def test_dir(self):
        """Most basic test of a single directory backup."""
        try:
            # Setup the test state.
            tempdir, in_dir, out_dir = self._create_tempdir_structure('input', \
                'output')
            in_struct, out_struct, cfg_file = self._create_single_dir_test(\
                'struct', 'struct', tempdir, in_dir, out_dir, \
                [_ConfigSection('input/struct', out_dir)])
            in_dir_hash = test.get_dir_md5(in_dir)

            # 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.isdir(in_struct))
            self.assertTrue(os.path.isdir(out_struct))
            self.assertEqual(in_dir_hash, test.get_dir_md5(in_dir))
            self.assertEqual(in_dir_hash, test.get_dir_md5(out_dir))

        finally:
            shutil.rmtree(tempdir)
Пример #2
0
    def test_dir_full_pipeline(self):
        """Basic test of a single directory archive, encrypt and backup."""
        try:
            # Setup the test state.
            tempdir, in_dir, out_dir = self._create_tempdir_structure('input', \
                'output')
            in_struct, out_struct, cfg_file = self._create_single_dir_test(\
                'struct', 'struct.tar.xz.gpg', tempdir, in_dir, tempdir, \
                [_ConfigSection('input/struct', tempdir, archive='yes',
                                compress='yes', encrypt='yes')])
            in_dir_hash = test.get_dir_md5(in_dir)

            # 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.isdir(in_struct))
            self.assertTrue(os.path.isfile(out_struct))
            self.assertEqual(in_dir_hash, test.get_dir_md5(in_dir))
            undo_out_file = os.path.join(out_dir, 'struct')
            backup.unencrypt_path(os.path.join(tempdir, 'struct.tar.xz'),
                                  out_struct, homedir=test.GPG_HOME)
            backup.uncompress_path(os.path.join(tempdir, 'struct.tar'),
                                   os.path.join(tempdir, 'struct.tar.xz'))
            backup.unarchive_path(out_dir, os.path.join(tempdir, 'struct.tar'))
            self.assertTrue(os.path.isdir(undo_out_file))
            self.assertEqual(in_dir_hash, test.get_dir_md5(out_dir))

        finally:
            shutil.rmtree(tempdir)
Пример #3
0
    def test_file_encrypt(self):
        """Basic test of a single file 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.gpg', tempdir, in_dir, out_dir, \
                [_ConfigSection('input/file.txt', out_dir, 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(undo_out_file, out_file, homedir=test.GPG_HOME)
            self.assertTrue(os.path.isfile(undo_out_file))
            self.assertEqual(in_file_hash, test.get_file_md5(undo_out_file))

        finally:
            shutil.rmtree(tempdir)
Пример #4
0
    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)