mirror of
https://github.com/ZettaIO/restic-compose-backup.git
synced 2025-09-27 22:25:24 +00:00
Move tests into src
This commit is contained in:
225
src/tests/container.json
Normal file
225
src/tests/container.json
Normal file
@@ -0,0 +1,225 @@
|
||||
{
|
||||
"Id": "0bd20acb61c051ae44e5e064a63c83bfd4fe12449ac917fde2dc9507dc45faac",
|
||||
"Created": "2019-04-17T19:43:40.599952Z",
|
||||
"Path": "sh",
|
||||
"Args": [],
|
||||
"State": {
|
||||
"Status": "running",
|
||||
"Running": true,
|
||||
"Paused": false,
|
||||
"Restarting": false,
|
||||
"OOMKilled": false,
|
||||
"Dead": false,
|
||||
"Pid": 15081,
|
||||
"ExitCode": 0,
|
||||
"Error": "",
|
||||
"StartedAt": "2019-04-17T19:43:41.4367357Z",
|
||||
"FinishedAt": "0001-01-01T00:00:00Z"
|
||||
},
|
||||
"Image": "sha256:90b8647e0feab8d2fce2d80098b2bf2f83ef7e88b7d5909a9962b4d24de07ebe",
|
||||
"ResolvConfPath": "/var/lib/docker/containers/0bd20acb61c051ae44e5e064a63c83bfd4fe12449ac917fde2dc9507dc45faac/resolv.conf",
|
||||
"HostnamePath": "/var/lib/docker/containers/0bd20acb61c051ae44e5e064a63c83bfd4fe12449ac917fde2dc9507dc45faac/hostname",
|
||||
"HostsPath": "/var/lib/docker/containers/0bd20acb61c051ae44e5e064a63c83bfd4fe12449ac917fde2dc9507dc45faac/hosts",
|
||||
"LogPath": "/var/lib/docker/containers/0bd20acb61c051ae44e5e064a63c83bfd4fe12449ac917fde2dc9507dc45faac/0bd20acb61c051ae44e5e064a63c83bfd4fe12449ac917fde2dc9507dc45faac-json.log",
|
||||
"Name": "/restic-volume-backup_backup_run_536c1bd7e6ab",
|
||||
"RestartCount": 0,
|
||||
"Driver": "overlay2",
|
||||
"Platform": "linux",
|
||||
"MountLabel": "",
|
||||
"ProcessLabel": "",
|
||||
"AppArmorProfile": "",
|
||||
"ExecIDs": null,
|
||||
"HostConfig": {
|
||||
"Binds": [
|
||||
"/Users/einarforselv/Documents/projects/contraz/restic-volume-backup:/restic-volume-backup:rw",
|
||||
"/var/run/docker.sock:/tmp/docker.sock:ro"
|
||||
],
|
||||
"ContainerIDFile": "",
|
||||
"LogConfig": {
|
||||
"Type": "json-file",
|
||||
"Config": {}
|
||||
},
|
||||
"NetworkMode": "restic-volume-backup_default",
|
||||
"PortBindings": {},
|
||||
"RestartPolicy": {
|
||||
"Name": "",
|
||||
"MaximumRetryCount": 0
|
||||
},
|
||||
"AutoRemove": false,
|
||||
"VolumeDriver": "",
|
||||
"VolumesFrom": [],
|
||||
"CapAdd": null,
|
||||
"CapDrop": null,
|
||||
"Dns": null,
|
||||
"DnsOptions": null,
|
||||
"DnsSearch": null,
|
||||
"ExtraHosts": null,
|
||||
"GroupAdd": null,
|
||||
"IpcMode": "shareable",
|
||||
"Cgroup": "",
|
||||
"Links": null,
|
||||
"OomScoreAdj": 0,
|
||||
"PidMode": "",
|
||||
"Privileged": false,
|
||||
"PublishAllPorts": false,
|
||||
"ReadonlyRootfs": false,
|
||||
"SecurityOpt": null,
|
||||
"UTSMode": "",
|
||||
"UsernsMode": "",
|
||||
"ShmSize": 67108864,
|
||||
"Runtime": "runc",
|
||||
"ConsoleSize": [
|
||||
0,
|
||||
0
|
||||
],
|
||||
"Isolation": "",
|
||||
"CpuShares": 0,
|
||||
"Memory": 0,
|
||||
"NanoCpus": 0,
|
||||
"CgroupParent": "",
|
||||
"BlkioWeight": 0,
|
||||
"BlkioWeightDevice": null,
|
||||
"BlkioDeviceReadBps": null,
|
||||
"BlkioDeviceWriteBps": null,
|
||||
"BlkioDeviceReadIOps": null,
|
||||
"BlkioDeviceWriteIOps": null,
|
||||
"CpuPeriod": 0,
|
||||
"CpuQuota": 0,
|
||||
"CpuRealtimePeriod": 0,
|
||||
"CpuRealtimeRuntime": 0,
|
||||
"CpusetCpus": "",
|
||||
"CpusetMems": "",
|
||||
"Devices": null,
|
||||
"DeviceCgroupRules": null,
|
||||
"DiskQuota": 0,
|
||||
"KernelMemory": 0,
|
||||
"MemoryReservation": 0,
|
||||
"MemorySwap": 0,
|
||||
"MemorySwappiness": null,
|
||||
"OomKillDisable": false,
|
||||
"PidsLimit": 0,
|
||||
"Ulimits": null,
|
||||
"CpuCount": 0,
|
||||
"CpuPercent": 0,
|
||||
"IOMaximumIOps": 0,
|
||||
"IOMaximumBandwidth": 0,
|
||||
"MaskedPaths": [
|
||||
"/proc/asound",
|
||||
"/proc/acpi",
|
||||
"/proc/kcore",
|
||||
"/proc/keys",
|
||||
"/proc/latency_stats",
|
||||
"/proc/timer_list",
|
||||
"/proc/timer_stats",
|
||||
"/proc/sched_debug",
|
||||
"/proc/scsi",
|
||||
"/sys/firmware"
|
||||
],
|
||||
"ReadonlyPaths": [
|
||||
"/proc/bus",
|
||||
"/proc/fs",
|
||||
"/proc/irq",
|
||||
"/proc/sys",
|
||||
"/proc/sysrq-trigger"
|
||||
]
|
||||
},
|
||||
"GraphDriver": {
|
||||
"Data": {
|
||||
"LowerDir": "/var/lib/docker/overlay2/9e61d3da917e78eff11e7ca52227713a5ab490272df2645e7f4a72d03b88e170-init/diff:/var/lib/docker/overlay2/73a00eb3c2f6fd67c1b391313e58afe642fc118099b607fabba5632124d87787/diff:/var/lib/docker/overlay2/47c76761e1fcec1148be971e86505540b491462292dbe92fd8648387b81ea949/diff:/var/lib/docker/overlay2/f6bf6d8dc65bd7ac64ebaa478025aea834611447a7c6ecea7c72091445d8abd5/diff:/var/lib/docker/overlay2/6f7810e639455c207edcd04ea1f79d6be6b9a849ceb73d37b962bb8e33f6c363/diff:/var/lib/docker/overlay2/2a66846241f2d8d18422d1c36a6367c4e83055958ed4655a66bc5d5b6cae2937/diff:/var/lib/docker/overlay2/6241fe000d335f1140f6979b66fd025128e906886d0fe10484cbdd112fd4e5e7/diff",
|
||||
"MergedDir": "/var/lib/docker/overlay2/9e61d3da917e78eff11e7ca52227713a5ab490272df2645e7f4a72d03b88e170/merged",
|
||||
"UpperDir": "/var/lib/docker/overlay2/9e61d3da917e78eff11e7ca52227713a5ab490272df2645e7f4a72d03b88e170/diff",
|
||||
"WorkDir": "/var/lib/docker/overlay2/9e61d3da917e78eff11e7ca52227713a5ab490272df2645e7f4a72d03b88e170/work"
|
||||
},
|
||||
"Name": "overlay2"
|
||||
},
|
||||
"Mounts": [{
|
||||
"Type": "bind",
|
||||
"Source": "/Users/einarforselv/Documents/projects/contraz/restic-volume-backup",
|
||||
"Destination": "/restic-volume-backup",
|
||||
"Mode": "rw",
|
||||
"RW": true,
|
||||
"Propagation": "rprivate"
|
||||
},
|
||||
{
|
||||
"Type": "bind",
|
||||
"Source": "/var/run/docker.sock",
|
||||
"Destination": "/tmp/docker.sock",
|
||||
"Mode": "ro",
|
||||
"RW": false,
|
||||
"Propagation": "rprivate"
|
||||
}
|
||||
],
|
||||
"Config": {
|
||||
"Hostname": "0bd20acb61c0",
|
||||
"Domainname": "",
|
||||
"User": "",
|
||||
"AttachStdin": true,
|
||||
"AttachStdout": true,
|
||||
"AttachStderr": true,
|
||||
"Tty": true,
|
||||
"OpenStdin": true,
|
||||
"StdinOnce": true,
|
||||
"Env": [
|
||||
"DOCKER_BASE_URL=unix://tmp/docker.sock",
|
||||
"RESTIC_REPOSITORY=/tmp/backup",
|
||||
"RESTIC_PASSWORD=password",
|
||||
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
||||
],
|
||||
"Cmd": [
|
||||
"sh"
|
||||
],
|
||||
"Image": "restic-volume-backup_backup",
|
||||
"Volumes": {
|
||||
"/restic-volume-backup": {},
|
||||
"/tmp/docker.sock": {}
|
||||
},
|
||||
"WorkingDir": "/restic-volume-backup",
|
||||
"Entrypoint": null,
|
||||
"OnBuild": null,
|
||||
"Labels": {
|
||||
"com.docker.compose.oneoff": "True",
|
||||
"com.docker.compose.project": "restic-volume-backup",
|
||||
"com.docker.compose.service": "backup",
|
||||
"com.docker.compose.slug": "536c1bd7e6aba9e5efccfb7cf7197292657aed3cf38a57f175dab293c932267",
|
||||
"com.docker.compose.version": "1.23.2"
|
||||
}
|
||||
},
|
||||
"NetworkSettings": {
|
||||
"Bridge": "",
|
||||
"SandboxID": "f8784b0eb57951bdca00da1a3ad91077a0b94c331e89045811b6eac15d46fc42",
|
||||
"HairpinMode": false,
|
||||
"LinkLocalIPv6Address": "",
|
||||
"LinkLocalIPv6PrefixLen": 0,
|
||||
"Ports": {},
|
||||
"SandboxKey": "/var/run/docker/netns/f8784b0eb579",
|
||||
"SecondaryIPAddresses": null,
|
||||
"SecondaryIPv6Addresses": null,
|
||||
"EndpointID": "",
|
||||
"Gateway": "",
|
||||
"GlobalIPv6Address": "",
|
||||
"GlobalIPv6PrefixLen": 0,
|
||||
"IPAddress": "",
|
||||
"IPPrefixLen": 0,
|
||||
"IPv6Gateway": "",
|
||||
"MacAddress": "",
|
||||
"Networks": {
|
||||
"restic-volume-backup_default": {
|
||||
"IPAMConfig": null,
|
||||
"Links": null,
|
||||
"Aliases": [
|
||||
"0bd20acb61c0"
|
||||
],
|
||||
"NetworkID": "98886e14fddc2b3d31d15fbbfc3ea1b339626374ba2ff6c2dad406b1617ee09c",
|
||||
"EndpointID": "b53f795306063d732775c73599481a192858958ccf0eff3f02434bba1e86d86f",
|
||||
"Gateway": "172.22.0.1",
|
||||
"IPAddress": "172.22.0.3",
|
||||
"IPPrefixLen": 16,
|
||||
"IPv6Gateway": "",
|
||||
"GlobalIPv6Address": "",
|
||||
"GlobalIPv6PrefixLen": 0,
|
||||
"MacAddress": "02:42:ac:16:00:03",
|
||||
"DriverOpts": null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
54
src/tests/fixtures.py
Normal file
54
src/tests/fixtures.py
Normal file
@@ -0,0 +1,54 @@
|
||||
"""Generate test fixtures"""
|
||||
from datetime import datetime
|
||||
import hashlib
|
||||
import string
|
||||
import random
|
||||
|
||||
|
||||
def generate_sha256():
|
||||
"""Generate a unique sha256"""
|
||||
h = hashlib.sha256()
|
||||
h.update(str(datetime.now().timestamp()).encode())
|
||||
return h.hexdigest()
|
||||
|
||||
|
||||
def containers(project="default", containers=[]):
|
||||
"""
|
||||
Args:
|
||||
project (str): Name of the compose project
|
||||
containers (dict):
|
||||
{
|
||||
'containers: [
|
||||
'id': 'something'
|
||||
'service': 'service_name',
|
||||
'mounts: [{
|
||||
'Source': '/home/user/stuff',
|
||||
'Destination': '/srv/stuff',
|
||||
'Type': 'bind' / 'volume'
|
||||
}],
|
||||
]
|
||||
}
|
||||
"""
|
||||
def wrapper(*args, **kwargs):
|
||||
return [
|
||||
{
|
||||
'Id': container.get('id', generate_sha256()),
|
||||
'Name': container.get('service') + '_' + ''.join(random.choice(string.ascii_lowercase) for i in range(16)),
|
||||
'Config': {
|
||||
'Image': 'restic-compose-backup_backup',
|
||||
'Labels': {
|
||||
'com.docker.compose.oneoff': 'False',
|
||||
'com.docker.compose.project': project,
|
||||
'com.docker.compose.service': container['service'],
|
||||
**container.get('labels', {}),
|
||||
},
|
||||
},
|
||||
'Mounts': container.get('mounts', []),
|
||||
'State': {
|
||||
"Status": "running",
|
||||
"Running": True,
|
||||
}
|
||||
}
|
||||
for container in containers]
|
||||
|
||||
return wrapper
|
2
src/tests/requirements.txt
Normal file
2
src/tests/requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
pytest==4.3.1
|
||||
tox
|
200
src/tests/tests.py
Normal file
200
src/tests/tests.py
Normal file
@@ -0,0 +1,200 @@
|
||||
import json
|
||||
import os
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from restic_compose_backup import utils
|
||||
from restic_compose_backup.containers import RunningContainers
|
||||
import fixtures
|
||||
|
||||
list_containers_func = 'restic_compose_backup.utils.list_containers'
|
||||
|
||||
|
||||
class ResticBackupTests(unittest.TestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
"""Set up basic environment variables"""
|
||||
os.environ['RESTIC_REPOSITORY'] = "test"
|
||||
os.environ['RESTIC_PASSWORD'] = "password"
|
||||
|
||||
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"""
|
||||
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()
|
||||
|
||||
def test_running_containers(self):
|
||||
containers = self.createContainers()
|
||||
containers += [
|
||||
{
|
||||
'service': 'web',
|
||||
'labels': {
|
||||
'restic-compose-backup.volumes': True,
|
||||
'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()
|
||||
self.assertEqual(len(result.containers), 3, msg="Three containers expected")
|
||||
self.assertNotEqual(result.this_container, None, msg="No backup container found")
|
||||
web_service = result.get_service('web')
|
||||
self.assertNotEqual(web_service, None)
|
||||
self.assertEqual(len(web_service.filter_mounts()), 1)
|
||||
|
||||
def test_volumes_for_backup(self):
|
||||
containers = self.createContainers()
|
||||
containers += [
|
||||
{
|
||||
'service': 'web',
|
||||
'labels': {
|
||||
'restic-compose-backup.volumes': True,
|
||||
},
|
||||
'mounts': [{
|
||||
'Source': 'test',
|
||||
'Destination': 'test',
|
||||
'Type': 'bind',
|
||||
}]
|
||||
},
|
||||
{
|
||||
'service': 'mysql',
|
||||
'labels': {
|
||||
'restic-compose-backup.mysql': True,
|
||||
},
|
||||
'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)
|
||||
self.assertEqual(cnt.generate_backup_mounts(), {'test': {'bind': '/volumes/web/test', 'mode': 'ro'}})
|
||||
|
||||
def test_include(self):
|
||||
containers = self.createContainers()
|
||||
containers += [
|
||||
{
|
||||
'service': 'web',
|
||||
'labels': {
|
||||
'restic-compose-backup.volumes': True,
|
||||
'restic-compose-backup.volumes.include': 'media',
|
||||
},
|
||||
'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()
|
||||
print(mounts)
|
||||
self.assertEqual(len(mounts), 1)
|
||||
self.assertEqual(mounts[0].source, '/srv/files/media')
|
||||
|
||||
def test_exclude(self):
|
||||
containers = self.createContainers()
|
||||
containers += [
|
||||
{
|
||||
'service': 'web',
|
||||
'labels': {
|
||||
'restic-compose-backup.volumes': True,
|
||||
'restic-compose-backup.volumes.exclude': 'stuff',
|
||||
},
|
||||
'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.backup_process': 'True',
|
||||
},
|
||||
},
|
||||
]
|
||||
with mock.patch(list_containers_func, fixtures.containers(containers=containers)):
|
||||
cnt = RunningContainers()
|
||||
self.assertTrue(cnt.backup_process_running)
|
Reference in New Issue
Block a user