2019-04-10 19:23:17 -07:00
|
|
|
"""
|
2019-04-14 14:56:09 -07:00
|
|
|
.. moduleauthor:: Eric Torres
|
2019-04-10 19:23:17 -07:00
|
|
|
|
|
|
|
Tests for the rbackup.struct.repository module.
|
|
|
|
"""
|
2019-04-11 22:12:08 -07:00
|
|
|
import re
|
2019-03-16 07:46:03 -07:00
|
|
|
import unittest
|
2019-04-10 19:23:17 -07:00
|
|
|
from unittest.mock import PropertyMock, patch
|
2019-03-16 07:46:03 -07:00
|
|
|
|
2019-03-28 12:11:52 -07:00
|
|
|
from hypothesis import given
|
2019-04-11 22:12:08 -07:00
|
|
|
from hypothesis.strategies import from_regex, lists, text
|
2019-04-10 18:03:58 -07:00
|
|
|
|
2019-04-09 17:45:38 -07:00
|
|
|
from rbackup.struct.repository import Repository
|
|
|
|
from rbackup.struct.snapshot import Snapshot
|
2019-03-16 07:46:03 -07:00
|
|
|
|
|
|
|
# ========== Constants ==========
|
2019-04-09 17:45:38 -07:00
|
|
|
TESTING_PACKAGE = "rbackup.struct"
|
2019-03-28 12:11:52 -07:00
|
|
|
REPO_MODULE = f"{TESTING_PACKAGE}.repository"
|
|
|
|
SS_MODULE = f"{TESTING_PACKAGE}.snapshot"
|
2019-03-16 07:46:03 -07:00
|
|
|
|
2019-04-11 22:12:08 -07:00
|
|
|
VALID_SNAPSHOT_NAME = r"[\w._+-]+[^/]*"
|
2019-03-16 07:46:03 -07:00
|
|
|
|
|
|
|
|
|
|
|
# ========== Integration Tests ==========
|
2019-03-28 12:11:52 -07:00
|
|
|
class TestRepositoryPreCreate(unittest.TestCase):
|
2019-04-10 18:13:13 -07:00
|
|
|
"""Test properties of the Repository before running create_snapshot().
|
|
|
|
|
|
|
|
Mocked Modules/Classes
|
|
|
|
----------------------
|
|
|
|
rbackup.struct.repository.Snapshot
|
|
|
|
|
|
|
|
Mocked Attributes
|
|
|
|
-----------------
|
|
|
|
* Repository.metadata_path
|
|
|
|
* Repository.read_metadata
|
|
|
|
* Repository.write_metadata
|
|
|
|
"""
|
2019-03-29 15:38:37 -07:00
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
def setUp(self):
|
2019-03-29 15:38:37 -07:00
|
|
|
self.patched_path = patch.object(
|
|
|
|
Repository, "metadata_path", new_callable=PropertyMock
|
|
|
|
)
|
2019-04-10 18:17:21 -07:00
|
|
|
self.patched_r_metadata = patch.object(
|
|
|
|
Repository, "read_metadata", spec_set=list
|
|
|
|
)
|
|
|
|
self.patched_w_metadata = patch.object(
|
|
|
|
Repository, "write_metadata", spec_set=list
|
|
|
|
)
|
2019-03-28 12:11:52 -07:00
|
|
|
self.patched_snapshot = patch(
|
|
|
|
f"{TESTING_PACKAGE}.repository.Snapshot", spec_set=Snapshot
|
|
|
|
)
|
|
|
|
|
2019-03-30 14:13:10 -07:00
|
|
|
self.mocked_r_metadata = self.patched_r_metadata.start()
|
|
|
|
self.mocked_w_metadata = self.patched_w_metadata.start()
|
2019-03-29 15:38:37 -07:00
|
|
|
self.mocked_path = self.patched_path.start()
|
2019-03-28 12:11:52 -07:00
|
|
|
self.mocked_snapshot = self.patched_snapshot.start()
|
2019-03-30 14:14:41 -07:00
|
|
|
|
|
|
|
self.mocked_path.return_value.exists.return_value = True
|
|
|
|
|
2019-04-11 22:12:08 -07:00
|
|
|
@given(lists(from_regex(VALID_SNAPSHOT_NAME, fullmatch=True), unique=True))
|
2019-04-10 18:17:21 -07:00
|
|
|
def test_empty(self, snapshots):
|
|
|
|
self.mocked_r_metadata.return_value = snapshots.copy()
|
2019-03-29 15:38:37 -07:00
|
|
|
repo = Repository("backup")
|
2019-03-28 12:11:52 -07:00
|
|
|
|
2019-04-10 18:17:21 -07:00
|
|
|
if not snapshots:
|
2019-03-28 12:11:52 -07:00
|
|
|
self.assertTrue(repo.empty)
|
|
|
|
else:
|
|
|
|
self.assertFalse(repo.empty)
|
|
|
|
|
2019-04-11 22:18:42 -07:00
|
|
|
@given(lists(from_regex(VALID_SNAPSHOT_NAME, fullmatch=True), unique=True))
|
|
|
|
def test_dunder_len(self, snapshots):
|
2019-04-10 18:17:21 -07:00
|
|
|
self.mocked_r_metadata.return_value = snapshots.copy()
|
|
|
|
repo = Repository("backup")
|
|
|
|
|
|
|
|
self.assertEqual(len(repo.snapshots), len(snapshots))
|
2019-03-29 15:38:37 -07:00
|
|
|
|
2019-04-10 18:17:21 -07:00
|
|
|
@given(text(min_size=1))
|
2019-04-11 22:18:42 -07:00
|
|
|
def test_dunder_contains(self, name):
|
|
|
|
self.mocked_r_metadata.return_value = []
|
2019-03-29 15:38:37 -07:00
|
|
|
repo = Repository("backup")
|
2019-03-28 12:11:52 -07:00
|
|
|
|
2019-04-10 18:17:21 -07:00
|
|
|
self.assertFalse(name in repo)
|
2019-03-28 12:11:52 -07:00
|
|
|
|
2019-04-10 18:17:21 -07:00
|
|
|
@given(text())
|
|
|
|
def test_valid_name(self, name):
|
|
|
|
self.mocked_r_metadata.return_value = []
|
2019-03-29 15:38:37 -07:00
|
|
|
|
2019-04-11 22:12:08 -07:00
|
|
|
if not re.match(VALID_SNAPSHOT_NAME, name):
|
2019-04-10 18:17:21 -07:00
|
|
|
self.assertFalse(Repository.is_valid_snapshot_name(name))
|
2019-03-29 15:38:37 -07:00
|
|
|
else:
|
2019-04-10 18:17:21 -07:00
|
|
|
self.assertTrue(Repository.is_valid_snapshot_name(name))
|
2019-03-28 12:11:52 -07:00
|
|
|
|
2019-04-10 18:17:21 -07:00
|
|
|
def test_snapshots_returns_empty_list(self):
|
2019-04-11 22:18:42 -07:00
|
|
|
repo = Repository("backup")
|
|
|
|
self.assertListEqual(repo.snapshots, [])
|
|
|
|
|
|
|
|
@given(
|
|
|
|
lists(from_regex(VALID_SNAPSHOT_NAME, fullmatch=True), min_size=1, unique=True)
|
|
|
|
)
|
|
|
|
def snapshots_property_contains_snapshot_objects(self, snapshots):
|
|
|
|
self.mocked_r_metadata.return_value = snapshots
|
|
|
|
repo = Repository("backup")
|
|
|
|
|
|
|
|
self.assertTrue(all(isinstance(p, Snapshot) for p in repo))
|
2019-03-28 12:11:52 -07:00
|
|
|
|
|
|
|
def tearDown(self):
|
2019-03-29 15:38:37 -07:00
|
|
|
self.patched_path.stop()
|
2019-03-30 14:13:10 -07:00
|
|
|
self.patched_r_metadata.stop()
|
|
|
|
self.patched_w_metadata.stop()
|
2019-03-28 12:11:52 -07:00
|
|
|
self.patched_snapshot.stop()
|
|
|
|
|
|
|
|
|
|
|
|
class TestRepositoryPostCreate(unittest.TestCase):
|
2019-04-10 18:13:13 -07:00
|
|
|
"""Test properties of the Repository after running create_snapshot().
|
|
|
|
|
|
|
|
Mocked Modules/Classes
|
|
|
|
----------------------
|
|
|
|
rbackup.struct.repository.Snapshot
|
|
|
|
|
|
|
|
Mocked Attributes
|
|
|
|
-----------------
|
|
|
|
* Repository.metadata_path
|
|
|
|
* Repository.read_metadata
|
|
|
|
* Repository.write_metadata
|
|
|
|
"""
|
2019-03-29 15:38:37 -07:00
|
|
|
|
2019-03-17 18:21:04 -07:00
|
|
|
def setUp(self):
|
2019-03-29 15:38:37 -07:00
|
|
|
self.patched_path = patch.object(
|
|
|
|
Repository, "metadata_path", new_callable=PropertyMock
|
|
|
|
)
|
2019-04-10 18:17:21 -07:00
|
|
|
self.patched_r_metadata = patch.object(
|
|
|
|
Repository, "read_metadata", spec_set=list
|
|
|
|
)
|
|
|
|
self.patched_w_metadata = patch.object(
|
|
|
|
Repository, "write_metadata", spec_set=list
|
|
|
|
)
|
2019-03-28 12:11:52 -07:00
|
|
|
self.patched_snapshot = patch(
|
|
|
|
f"{TESTING_PACKAGE}.repository.Snapshot", spec_set=Snapshot
|
|
|
|
)
|
2019-03-18 10:36:30 -07:00
|
|
|
|
2019-03-29 15:38:37 -07:00
|
|
|
self.mocked_path = self.patched_path.start()
|
2019-03-30 14:13:10 -07:00
|
|
|
self.mocked_r_metadata = self.patched_r_metadata.start()
|
|
|
|
self.mocked_w_metadata = self.patched_w_metadata.start()
|
2019-03-28 12:11:52 -07:00
|
|
|
self.mocked_snapshot = self.patched_snapshot.start()
|
2019-03-18 10:36:30 -07:00
|
|
|
|
2019-04-11 22:12:08 -07:00
|
|
|
@given(lists(from_regex(VALID_SNAPSHOT_NAME, fullmatch=True), unique=True))
|
|
|
|
def test_dunder_len(self, snapshots):
|
2019-04-10 18:17:21 -07:00
|
|
|
self.mocked_r_metadata.return_value = snapshots.copy()
|
2019-03-29 15:38:37 -07:00
|
|
|
repo = Repository("backup")
|
2019-03-17 18:21:04 -07:00
|
|
|
|
2019-03-28 12:11:52 -07:00
|
|
|
repo.create_snapshot()
|
2019-03-17 18:21:04 -07:00
|
|
|
|
2019-04-11 22:18:42 -07:00
|
|
|
self.assertEqual(len(repo), len(snapshots) + 1)
|
|
|
|
self.assertEqual(len(repo.snapshots), len(snapshots) + 1)
|
2019-03-17 18:21:04 -07:00
|
|
|
|
2019-04-11 22:12:08 -07:00
|
|
|
@given(from_regex(VALID_SNAPSHOT_NAME, fullmatch=True))
|
|
|
|
def test_dunder_contains(self, name):
|
|
|
|
self.mocked_path.return_value.exists.return_value = False
|
|
|
|
repo = Repository("backup")
|
|
|
|
|
|
|
|
repo.create_snapshot(name)
|
|
|
|
self.assertTrue(name in repo)
|
|
|
|
|
|
|
|
def test_empty(self):
|
|
|
|
self.mocked_r_metadata.return_value = []
|
2019-03-29 15:38:37 -07:00
|
|
|
repo = Repository("backup")
|
2019-03-20 00:23:25 -07:00
|
|
|
|
2019-03-28 12:11:52 -07:00
|
|
|
repo.create_snapshot()
|
2019-03-20 00:23:25 -07:00
|
|
|
|
2019-04-11 22:18:42 -07:00
|
|
|
self.assertFalse(repo.empty)
|
2019-03-20 00:23:25 -07:00
|
|
|
|
2019-04-11 22:18:42 -07:00
|
|
|
def test_snapshot_returns_snapshot_object(self):
|
|
|
|
self.mocked_r_metadata.return_value = []
|
2019-03-29 15:38:37 -07:00
|
|
|
repo = Repository("backup")
|
2019-03-18 10:36:30 -07:00
|
|
|
|
2019-04-11 22:18:42 -07:00
|
|
|
self.assertIsInstance(repo.create_snapshot(), Snapshot)
|
|
|
|
|
|
|
|
def test_create_duplicate_snapshot(self):
|
|
|
|
# Test that if a snapshot is a duplicate, then return that duplicate snapshot
|
|
|
|
self.mocked_r_metadata.return_value = []
|
|
|
|
repo = Repository("backup")
|
|
|
|
name = "new-snapshot"
|
|
|
|
|
|
|
|
first = repo.create_snapshot(name)
|
|
|
|
second = repo.create_snapshot(name)
|
|
|
|
|
|
|
|
self.assertIs(first, second)
|
2019-04-10 18:17:21 -07:00
|
|
|
self.assertTrue(name in repo)
|
2019-04-11 22:13:14 -07:00
|
|
|
self.assertEqual(len(repo), 1)
|
2019-03-18 10:36:30 -07:00
|
|
|
|
2019-03-28 12:11:52 -07:00
|
|
|
def tearDown(self):
|
2019-03-29 15:38:37 -07:00
|
|
|
self.patched_path.stop()
|
2019-03-30 14:13:10 -07:00
|
|
|
self.patched_r_metadata.stop()
|
|
|
|
self.patched_w_metadata.stop()
|
2019-03-28 12:11:52 -07:00
|
|
|
self.patched_snapshot.stop()
|
2019-04-11 22:13:14 -07:00
|
|
|
|
|
|
|
|
|
|
|
class TestRepositoryCleanup(unittest.TestCase):
|
|
|
|
"""Test that repository cleanup works properly.
|
|
|
|
|
|
|
|
Test cases
|
|
|
|
----------
|
|
|
|
* Function stops if system is not symlink attack-resistant
|
|
|
|
* If symlink attack-resistant, then only delete metadata when all others false
|
|
|
|
* Function only deletes snapshots when told to
|
|
|
|
* Function only deletes repository directory when told to
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.patched_path = patch.object(
|
|
|
|
Repository, "metadata_path", new_callable=PropertyMock
|
|
|
|
)
|
|
|
|
self.patched_r_metadata = patch.object(
|
|
|
|
Repository, "read_metadata", spec_set=list
|
|
|
|
)
|
|
|
|
self.patched_w_metadata = patch.object(
|
|
|
|
Repository, "write_metadata", spec_set=list
|
|
|
|
)
|
|
|
|
self.patched_shutil = patch(f"{TESTING_PACKAGE}.repository.shutil")
|
|
|
|
self.patched_snapshot = patch(
|
|
|
|
f"{TESTING_PACKAGE}.repository.Snapshot", spec_set=Snapshot
|
|
|
|
)
|
|
|
|
|
|
|
|
self.mocked_path = self.patched_path.start()
|
|
|
|
self.mocked_r_metadata = self.patched_r_metadata.start()
|
|
|
|
self.mocked_w_metadata = self.patched_w_metadata.start()
|
|
|
|
self.mocked_shutil = self.patched_shutil.start()
|
|
|
|
self.mocked_snapshot = self.patched_snapshot.start()
|
|
|
|
|
2019-04-12 07:51:08 -07:00
|
|
|
self.mocked_shutil.rmtree.avoids_symlink_attacks = True
|
|
|
|
|
2019-04-11 22:13:14 -07:00
|
|
|
def test_stops_on_non_symlink_resistant(self):
|
|
|
|
self.mocked_shutil.rmtree.avoids_symlink_attacks = False
|
|
|
|
repo = Repository("backup")
|
|
|
|
|
|
|
|
repo.cleanup(remove_snapshots=True)
|
|
|
|
|
|
|
|
self.mocked_path.return_value.unlink.assert_not_called()
|
|
|
|
self.mocked_shutil.rmtree.assert_not_called()
|
|
|
|
|
2019-04-12 07:51:08 -07:00
|
|
|
def test_removes_metadata_by_default(self):
|
2019-04-12 07:53:23 -07:00
|
|
|
repo = Repository("backup")
|
2019-04-12 07:51:08 -07:00
|
|
|
|
|
|
|
repo.cleanup()
|
|
|
|
|
|
|
|
self.mocked_path.return_value.unlink.assert_called_once()
|
|
|
|
|
|
|
|
def test_removes_snapshots(self):
|
2019-04-12 07:53:23 -07:00
|
|
|
repo = Repository("backup")
|
2019-04-12 07:51:08 -07:00
|
|
|
|
|
|
|
repo.cleanup(remove_snapshots=True)
|
|
|
|
|
|
|
|
self.mocked_shutil.rmtree.assert_called_once()
|
|
|
|
|
|
|
|
def test_removes_repo_dir(self):
|
2019-04-12 07:53:23 -07:00
|
|
|
repo = Repository("backup")
|
2019-04-12 07:51:08 -07:00
|
|
|
|
|
|
|
repo.cleanup(remove_repo_dir=True)
|
|
|
|
|
|
|
|
self.mocked_shutil.rmtree.assert_called_once()
|
|
|
|
|
2019-04-11 22:13:14 -07:00
|
|
|
def tearDown(self):
|
|
|
|
self.patched_path.stop()
|
|
|
|
self.patched_r_metadata.stop()
|
|
|
|
self.patched_w_metadata.stop()
|
|
|
|
self.patched_shutil.stop()
|
|
|
|
self.patched_snapshot.stop()
|