2019-04-10 19:23:17 -07:00
|
|
|
"""
|
|
|
|
.. author:: Eric Torres
|
|
|
|
|
|
|
|
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-10 18:17:21 -07:00
|
|
|
@given(
|
|
|
|
lists(
|
|
|
|
text(
|
|
|
|
alphabet=characters(blacklist_characters=UNWANTED_SNAPSHOT_CHARS),
|
|
|
|
min_size=1,
|
|
|
|
),
|
|
|
|
unique=True,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
def test_len(self, snapshots):
|
|
|
|
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))
|
|
|
|
def test_contains(self, name):
|
|
|
|
self.mocked_r_metadata = []
|
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):
|
|
|
|
r = Repository("backup")
|
|
|
|
self.assertListEqual(r.snapshots, [])
|
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-03-28 12:11:52 -07:00
|
|
|
self.assertFalse(repo.empty)
|
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-10 18:17:21 -07:00
|
|
|
self.assertEqual(len(repo), len(snapshots) + 1)
|
|
|
|
self.assertEqual(len(repo.snapshots), len(snapshots) + 1)
|
2019-03-20 00:23:25 -07:00
|
|
|
|
2019-04-10 18:17:21 -07:00
|
|
|
@given(
|
|
|
|
text(
|
|
|
|
alphabet=characters(blacklist_characters=UNWANTED_SNAPSHOT_CHARS),
|
|
|
|
min_size=1,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
def test_contains(self, name):
|
|
|
|
self.mocked_path.return_value.exists.return_value = False
|
2019-03-29 15:38:37 -07:00
|
|
|
repo = Repository("backup")
|
2019-03-18 10:36:30 -07:00
|
|
|
|
2019-04-10 18:17:21 -07:00
|
|
|
repo.create_snapshot(name)
|
|
|
|
self.assertTrue(name in repo)
|
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()
|