def create_test_files(self, backup_extension=".tar.gz"):
        filename = self.create_test_config()
        cfg = get_config(filename)
        cfg['backup_root'] = self.test_dir

        for dir in cfg['backup_dirs']:
            full_dir = os.path.join(cfg['backup_root'], dir)
            if not os.path.exists(full_dir):
                os.makedirs(full_dir)
            for i in range(100):
                filename = str(i) + "-test%s" % backup_extension
                filepath = os.path.join(full_dir, filename)

                # Note the use of fallocate here to create files quickly that have a
                # given size.
                with open(filepath, "w+") as f:
                    size = (i + 1) * 1024
                    with open(filepath, "w+b") as f:
                        fallocate(f, 0, size)
                    f.close()

                # Here we are setting the mtime so that cloud archive manager
                # has some newer and older files to use, as far as its concerned
                stat = os.stat(filepath)
                mtime = stat.st_mtime
                atime = stat.st_atime

                # Make each file one hour older...though this will make file 99
                # the oldest file which is a bit counter intuitive...
                new_mtime = mtime - i * 3600
                os.utime(filepath, (atime, new_mtime))
    def test_read_bad_config(self):
        filename = self.create_test_config()
        cfg = get_config(filename)
        # Remove backup_root to test failing archive creation
        del cfg['backup_root']

        with self.assertRaises(ArchiveManagerException):
            verbose = None
            unused_archive = ArchiveManager(cfg, verbose)
 def generic_archive(self, backup_extension=".tar.gz"):
     """create a generic archive"""
     self.create_test_files(backup_extension)
     filename = self.create_test_config()
     cfg = get_config(filename)
     cfg['backup_root'] = self.test_dir
     cfg['backup_extension'] = backup_extension
     verbose = None
     archive = ArchiveManager(cfg, verbose)
     return archive
    def test_delete_until_max_dir_size(self):
        """Delete until we have a minimum amount of files, which takes precedence over size"""
        self.create_test_files()
        filename = self.create_test_config()
        cfg = get_config(filename)
        cfg['max_dir_size'] = 3145728
        cfg['backup_root'] = self.test_dir
        verbose = None
        archive = ArchiveManager(cfg, verbose)

        for dir in archive.backup_dirs:
            try:
                archive.delete_until_size_or_min(dir)
            except ArchiveManagerException as err:
                self.fail("ERROR: %s\n" % str(err))

            # I guess do this here, as the actual files is in the function
            # FIXME: this suggests this is not the right way to do it
            files = archive.get_files(dir)
            self.assertEqual(len(files), 77)
            self.assertEqual(files[-1], '76-test.tar.gz')
            self.assertEqual(files[0], '0-test.tar.gz')
    def test_read_good_config(self):
        self.create_test_files()
        filename = self.create_test_config()
        cfg = get_config(filename)
        cfg['backup_root'] = self.test_dir

        verbose = None
        try:
            archive = ArchiveManager(cfg, verbose)
        except ArchiveManagerException as err:
            self.fail("ERROR: %s\n" % str(err))

        backup_dirs_test = []
        backup_dirs_test.append("backups-1")
        backup_dirs_test.append("backups-2")

        self.assertEqual(archive.backup_root, self.test_dir)
        self.assertEqual(archive.max_num_backup_files, 36)
        self.assertEqual(archive.min_num_backup_files, 24)
        self.assertEqual(archive.max_dir_size, 5000000000)
        self.assertEqual(archive.backup_dirs, backup_dirs_test)
        self.assertEqual(archive.backup_dirs[0], 'backups-1')
        self.assertEqual(archive.backup_dirs[1], 'backups-2')
    def test_min_larger_than_max(self):
        self.create_test_files()
        filename = self.create_test_config()
        cfg = get_config(filename)
        cfg['max_dir_size'] = 3145728
        cfg['backup_root'] = self.test_dir
        # Max should be larger than min, here we set it to be smaller
        cfg['max_num_backup_files'] = 80
        cfg['min_num_backup_files'] = 90

        # Should fail
        with self.assertRaises(ArchiveManagerException):
            verbose = None
            unused_archive = ArchiveManager(cfg, verbose)

        # Now set min/max properly
        cfg['max_num_backup_files'] = 90
        cfg['min_num_backup_files'] = 80

        try:
            verbose = None
            unused_archive = ArchiveManager(cfg, verbose)
        except ArchiveManagerException as err:
            self.fail("ERROR: %s\n" % str(err))