Beispiel #1
0
    def test_limbo_deletion_threshold_not_passed_for_access(self, vault_mock):
        vault_file_one = self.vault.add(Branch.Limbo, self.file_one)
        self.file_one.unlink()

        walk = [(self.vault, make_file_seem_old_but_read_recently(
            vault_file_one.path), VaultExc.PhysicalVaultFile("File is in Limbo"))]
        dummy_walker = _DummyWalker(walk)
        dummy_persistence = MagicMock()
        Sweeper(dummy_walker, dummy_persistence, True)

        self.assertFalse(os.path.isfile(self.file_one))
        self.assertTrue(os.path.isfile(vault_file_one.path))
Beispiel #2
0
    def test_staged_not_deleted(self, vault_mock):
        vault_file_one = self.vault.add(Branch.Staged, self.file_one)
        self.file_one.unlink()

        walk = [(self.vault, make_file_seem_old(
            vault_file_one.path), VaultExc.PhysicalVaultFile())]
        dummy_walker = _DummyWalker(walk)
        dummy_persistence = MagicMock()
        Sweeper(dummy_walker, dummy_persistence, True)

        self.assertFalse(os.path.isfile(self.file_one))
        self.assertTrue(os.path.isfile(vault_file_one.path))
Beispiel #3
0
    def test_limbo_deletion_threshold_not_passed(self, vault_mock):
        vault_file_one = self.vault.add(Branch.Limbo, self.file_one)
        new_time = time.now() - config.deletion.limbo + time.delta(seconds=1)
        self.file_one.unlink()

        walk = [(self.vault, _DummyFile.FromFS(vault_file_one.path, idm, ctime=new_time,
                 mtime=new_time, atime=new_time), VaultExc.PhysicalVaultFile("File is in Limbo"))]
        dummy_walker = _DummyWalker(walk)
        dummy_persistence = MagicMock()
        Sweeper(dummy_walker, dummy_persistence, True)

        self.assertFalse(os.path.isfile(self.file_one))
        self.assertTrue(os.path.isfile(vault_file_one.path))
Beispiel #4
0
    def test_unactionable_file_wont_be_actioned(self):
        """Gets the Sweeper to try and action a file
        with the wrong permissions. The file won't be actionable,
        and it should throw the exception.

        Anything in the file `can_add` criteria will throw this
        exception.

        """
        dummy_walker = _DummyWalker(
            [(self.vault, File.FromFS(self.wrong_perms), None)])
        dummy_persistence = MagicMock()
        self.assertRaises(core.file.exception.UnactionableFile,
                          lambda: Sweeper(dummy_walker, dummy_persistence, True))
Beispiel #5
0
    def test_deletion_threshold_passed(self, vault_mock):
        walk = [(self.vault, make_file_seem_old(self.file_one), None)]
        dummy_walker = _DummyWalker(walk)
        dummy_persistence = MagicMock()

        vault_file_path = self.determine_vault_path(
            self.file_one, Branch.Limbo)

        Sweeper(dummy_walker, dummy_persistence, True)

        # Check if the untracked file has been deleted
        self.assertFalse(os.path.isfile(self.file_one))
        # Check if the file has been added to Limbo
        self.assertTrue(os.path.isfile(vault_file_path))
Beispiel #6
0
    def test_archive_corruption_case_actual(self, vault_mock):
        vault_file_one = self.vault.add(Branch.Staged, self.file_one)
        vault_file_two = self.vault.add(Branch.Limbo, self.file_two)
        walk = [(self.vault, File.FromFS(vault_file_one.path),
                VaultExc.PhysicalVaultFile("File is in Staged and can have to hardlinks if the file was archived with the stash option")),
                (self.vault, File.FromFS(vault_file_two.path), VaultExc.PhysicalVaultFile("File is in Limbo and has two hardlinks"))]
        dummy_walker = _DummyWalker(walk)
        dummy_persistence = MagicMock()
        Sweeper(dummy_walker, dummy_persistence, True)

        self.assertTrue(os.path.isfile(self.file_one))
        self.assertTrue(os.path.isfile(vault_file_one.path))
        self.assertTrue(os.path.isfile(self.file_two))
        self.assertTrue(os.path.isfile(vault_file_two.path))
Beispiel #7
0
    def test_tracked_file_stashed(self, vault_mock):
        vault_file_one_stash = self.vault.add(Branch.Stash, self.file_one)

        walk = [(self.vault, File.FromFS(self.file_one), Branch.Stash)]

        vault_file_one_staged = self.determine_vault_path(
            self.file_one, Branch.Staged)

        dummy_walker = _DummyWalker(walk)
        dummy_persistence = MagicMock()
        Sweeper(dummy_walker, dummy_persistence, True)

        self.assertTrue(os.path.isfile(self.file_one))
        self.assertFalse(os.path.isfile(vault_file_one_stash.path))
        self.assertTrue(os.path.isfile(vault_file_one_staged))
Beispiel #8
0
    def test_tracked_file_non_archive(self, vault_mock):
        vault_file_one = self.vault.add(Branch.Keep, self.file_one)
        vault_file_two = self.vault.add(Branch.Staged, self.file_two)
        vault_file_three = self.vault.add(Branch.Limbo, self.file_three)

        walk = [(self.vault, File.FromFS(self.file_one), Branch.Keep), (self.vault, File.FromFS(self.file_two), Branch.Stash), (self.vault, File.FromFS(
            self.file_three), VaultExc.VaultCorruption(f"{self.file_three} is limboed in the vault in {self.vault.root}, but also exists outside the vault"))]
        dummy_walker = _DummyWalker(walk)
        dummy_persistence = MagicMock()
        Sweeper(dummy_walker, dummy_persistence, True)

        self.assertTrue(os.path.isfile(self.file_one))
        self.assertTrue(os.path.isfile(vault_file_one.path))
        self.assertTrue(os.path.isfile(self.file_two))
        self.assertTrue(os.path.isfile(vault_file_two.path))
        self.assertTrue(os.path.isfile(self.file_three))
        self.assertTrue(os.path.isfile(vault_file_three.path))
Beispiel #9
0
    def test_keep_corruption_case_actual(self, vault_mock):
        vault_file_one = self.vault.add(Branch.Keep, self.file_one)
        vault_file_two = self.vault.add(Branch.Archive, self.file_two)
        vault_file_three = self.vault.add(Branch.Limbo, self.file_three)
        self.file_one.unlink()

        walk = [(self.vault, File.FromFS(vault_file_one.path), VaultExc.PhysicalVaultFile()),
                (self.vault, File.FromFS(vault_file_two.path),
                 VaultExc.PhysicalVaultFile()),
                (self.vault, File.FromFS(vault_file_three.path), VaultExc.PhysicalVaultFile())]
        dummy_walker = _DummyWalker(walk)
        dummy_persistence = MagicMock()
        Sweeper(dummy_walker, dummy_persistence, True)

        self.assertFalse(os.path.isfile(self.file_one))
        self.assertFalse(os.path.isfile(vault_file_one.path))
        self.assertTrue(os.path.isfile(self.file_two))
        self.assertTrue(os.path.isfile(vault_file_two.path))
        self.assertTrue(os.path.isfile(self.file_three))
        self.assertTrue(os.path.isfile(vault_file_three.path))
Beispiel #10
0
    def test_dryrun_basic(self, vault_mock):
        vault_file_one = self.vault.add(Branch.Keep, self.file_one)
        vault_file_two = self.vault.add(Branch.Archive, self.file_two)
        vault_file_three = self.vault.add(Branch.Limbo, self.file_three)

        walk = [(self.vault, make_file_seem_old(vault_file_one.path), VaultExc.PhysicalVaultFile()),
                (self.vault, make_file_seem_old(vault_file_two.path),
                 VaultExc.PhysicalVaultFile()),
                (self.vault, make_file_seem_old(vault_file_three.path),
                 VaultExc.PhysicalVaultFile()),
                (self.vault, make_file_seem_old(self.file_three), None)]
        dummy_walker = _DummyWalker(walk)
        dummy_persistence = MagicMock()
        Sweeper(dummy_walker, dummy_persistence, False)

        self.assertTrue(os.path.isfile(self.file_one))
        self.assertTrue(os.path.isfile(self.file_two))
        self.assertTrue(os.path.isfile(self.file_three))
        self.assertTrue(os.path.isfile(vault_file_one.path))
        self.assertTrue(os.path.isfile(vault_file_two.path))
        self.assertTrue(os.path.isfile(vault_file_three.path))
Beispiel #11
0
    def test_emails_stakeholders(self):
        """We're going to get a file close to the threshold,
        and then check if the email that is generated mentions
        the right information
        """
        new_time: T.DateTime = time.now() - config.deletion.threshold + \
            max(config.deletion.warnings) - time.delta(seconds=1)
        dummy_walker = _DummyWalker([(self.vault, _DummyFile.FromFS(
            self.file_one, idm, ctime=new_time, mtime=new_time, atime=new_time), None)])
        dummy_persistence = _DummyPersistence(config.persistence, idm)
        MockMailer.file_path = T.Path(self._tmp.name).resolve() / "mail"
        Sweeper(dummy_walker, dummy_persistence, True,
                MockMailer)  # this will make the email

        # Now we'll see what it says
        # Nothing in all the thresholds except the largest
        # Nothing staged for archival
        def _search_file(file: T.Path, phrase: str) -> T.List[int]:
            """returns first line number that the phrase was
            found in the file"""
            locations: T.List[int] = []
            with open(file) as f:
                for line_num, line in enumerate(f):
                    if phrase in line:
                        locations.append(line_num)
            return locations

        # The filepath should only be listed once in the whole email
        filepath_line_nums = _search_file(
            MockMailer.file_path, str(self.file_one))
        self.assertEquals(len(filepath_line_nums), 1)

        # That should be at the bottom of all the warnings
        for _line_num in _search_file(
                MockMailer.file_path, "Your files will be DELETED"):
            self.assertLess(_line_num, filepath_line_nums[0])