restic-compose-backup/src/tests/tests.py

204 lines
6.6 KiB
Python
Raw Normal View History

2019-04-16 00:57:06 +00:00
import json
import os
2019-04-15 17:07:14 +00:00
import unittest
from unittest import mock
2020-05-27 22:18:04 +00:00
os.environ['RESTIC_REPOSITORY'] = "test"
os.environ['RESTIC_PASSWORD'] = "password"
2019-12-03 08:40:02 +00:00
from restic_compose_backup import utils
from restic_compose_backup.containers import RunningContainers
2019-04-16 00:57:06 +00:00
import fixtures
2019-04-15 17:07:14 +00:00
2019-12-03 08:40:02 +00:00
list_containers_func = 'restic_compose_backup.utils.list_containers'
2019-04-15 17:07:14 +00:00
2019-04-16 18:56:00 +00:00
2019-04-15 17:07:14 +00:00
class ResticBackupTests(unittest.TestCase):
2019-04-16 00:57:06 +00:00
@classmethod
def setUpClass(cls):
2019-11-12 11:39:49 +00:00
"""Set up basic environment variables"""
2020-05-27 22:18:04 +00:00
# os.environ['RESTIC_REPOSITORY'] = "test"
# os.environ['RESTIC_PASSWORD'] = "password"
2019-04-16 00:57:06 +00:00
2019-04-16 18:56:00 +00:00
def createContainers(self):
backup_hash = fixtures.generate_sha256()
os.environ['HOSTNAME'] = backup_hash[:8]
return [
{
'id': backup_hash,
'service': 'backup',
}
]
def test_list_containers(self):
"""Test a basic container list"""
2019-04-16 00:57:06 +00:00
containers = [
{
'service': 'web',
'labels': {
'moo': 1,
},
'mounts': [{
'Source': 'moo',
'Destination': 'moo',
'Type': 'bind',
}]
},
{
'service': 'mysql',
},
{
'service': 'postgres',
},
]
with mock.patch(list_containers_func, fixtures.containers(containers=containers)):
test = utils.list_containers()
2019-04-16 18:56:00 +00:00
def test_running_containers(self):
containers = self.createContainers()
containers += [
{
'service': 'web',
'labels': {
'restic-compose-backup.volumes': True,
2019-04-16 18:56:00 +00:00
'test': 'test',
},
'mounts': [{
'Source': 'test',
'Destination': 'test',
'Type': 'bind',
}]
},
{
'service': 'mysql',
},
{
'service': 'postgres',
},
]
with mock.patch(list_containers_func, fixtures.containers(containers=containers)):
result = RunningContainers()
2019-12-10 16:28:35 +00:00
self.assertEqual(len(result.containers), 4, msg="Three containers expected")
2019-04-16 18:56:00 +00:00
self.assertNotEqual(result.this_container, None, msg="No backup container found")
web_service = result.get_service('web')
2019-04-18 22:13:51 +00:00
self.assertNotEqual(web_service, None)
self.assertEqual(len(web_service.filter_mounts()), 1)
2019-04-16 18:56:00 +00:00
2019-11-26 13:24:42 +00:00
def test_volumes_for_backup(self):
containers = self.createContainers()
containers += [
{
'service': 'web',
'labels': {
2019-12-03 08:40:02 +00:00
'restic-compose-backup.volumes': True,
2019-11-26 13:24:42 +00:00
},
'mounts': [{
'Source': 'test',
'Destination': 'test',
'Type': 'bind',
}]
},
{
'service': 'mysql',
'labels': {
2019-12-03 08:40:02 +00:00
'restic-compose-backup.mysql': True,
2019-11-26 13:24:42 +00:00
},
'mounts': [{
'Source': 'data',
'Destination': 'data',
'Type': 'bind',
}]
},
]
with mock.patch(list_containers_func, fixtures.containers(containers=containers)):
cnt = RunningContainers()
self.assertTrue(len(cnt.containers_for_backup()) == 2)
2019-12-04 21:17:57 +00:00
self.assertEqual(cnt.generate_backup_mounts(), {'test': {'bind': '/volumes/web/test', 'mode': 'ro'}})
2019-11-26 13:24:42 +00:00
2019-04-16 18:56:00 +00:00
def test_include(self):
containers = self.createContainers()
containers += [
{
'service': 'web',
'labels': {
'restic-compose-backup.volumes': True,
2019-12-04 21:17:57 +00:00
'restic-compose-backup.volumes.include': 'media',
2019-04-16 18:56:00 +00:00
},
'mounts': [
{
'Source': '/srv/files/media',
'Destination': '/srv/media',
'Type': 'bind',
},
{
'Source': '/srv/files/stuff',
'Destination': '/srv/stuff',
'Type': 'bind',
},
]
},
]
with mock.patch(list_containers_func, fixtures.containers(containers=containers)):
cnt = RunningContainers()
2019-04-16 20:47:20 +00:00
web_service = cnt.get_service('web')
self.assertNotEqual(web_service, None, msg="Web service not found")
2019-04-17 00:45:01 +00:00
mounts = web_service.filter_mounts()
2019-12-04 21:17:57 +00:00
print(mounts)
2019-04-16 20:47:20 +00:00
self.assertEqual(len(mounts), 1)
2019-04-16 22:09:52 +00:00
self.assertEqual(mounts[0].source, '/srv/files/media')
2019-04-16 18:56:00 +00:00
def test_exclude(self):
2019-04-17 00:45:01 +00:00
containers = self.createContainers()
containers += [
{
'service': 'web',
'labels': {
'restic-compose-backup.volumes': True,
2019-12-04 21:17:57 +00:00
'restic-compose-backup.volumes.exclude': 'stuff',
2019-04-17 00:45:01 +00:00
},
'mounts': [
{
'Source': '/srv/files/media',
'Destination': '/srv/media',
'Type': 'bind',
},
{
'Source': '/srv/files/stuff',
'Destination': '/srv/stuff',
'Type': 'bind',
},
]
},
]
with mock.patch(list_containers_func, fixtures.containers(containers=containers)):
cnt = RunningContainers()
web_service = cnt.get_service('web')
self.assertNotEqual(web_service, None, msg="Web service not found")
mounts = web_service.filter_mounts()
self.assertEqual(len(mounts), 1)
self.assertEqual(mounts[0].source, '/srv/files/media')
def test_find_running_backup_container(self):
containers = self.createContainers()
with mock.patch(list_containers_func, fixtures.containers(containers=containers)):
cnt = RunningContainers()
self.assertFalse(cnt.backup_process_running)
containers += [
{
'service': 'backup_runner',
'labels': {
'restic-compose-backup.process-default': 'True',
},
},
]
with mock.patch(list_containers_func, fixtures.containers(containers=containers)):
cnt = RunningContainers()
self.assertTrue(cnt.backup_process_running)