diff --git a/tests/fixtures.py b/tests/fixtures.py index 7280cbb..1470a34 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -1,44 +1,36 @@ """Generate test fixtures""" -def containers(project="default", containers=None): +def containers(project="default", containers=[]): """ Args: project (str): Name of the compose project containers (dict): { 'containers: [ - + 'service': 'service_name', + 'mounts: [{ + 'Source': '/home/user/stuff', + 'Destination': '/srv/stuff', + 'Type': 'bind' / 'volume' + }], ] } """ def wrapper(*args, **kwargs): - return { + return [ + { 'HostConfig': {'NetworkMode': 'restic-volume-backup_default'}, 'Id': '58d550e8f450129fa757820446e4021822a660918a61437e95115d3dc48ddde8', 'Image': 'restic-volume-backup_backup', 'ImageID': 'sha256:4d9a81206af7d65563b85d06be160dc90dc20ade94edcf544261f0e1db4472b3', 'Labels': { - 'com.docker.compose.oneoff': 'True', + 'com.docker.compose.oneoff': 'False', 'com.docker.compose.project': project, - 'com.docker.compose.service': 'backup', + 'com.docker.compose.service': container['service'], + **container.get('labels', {}), }, - 'Mounts': [ - { - 'Destination': '/restic-volume-backup', - 'Mode': 'rw', - 'RW': True, - 'Source': '/Users/einarforselv/Documents/projects/contraz/restic-volume-backup', - 'Type': 'bind', - }, - { - 'Destination': '/tmp/docker.sock', - 'Mode': 'ro', - 'RW': False, - 'Source': '/var/run/docker.sock', - 'Type': 'bind', - }, - ], + 'Mounts': container.get('mounts', []), 'Names': ['/restic-volume-backup_backup_run_58d1699be0d8'], 'NetworkSettings': { 'Networks': { @@ -60,6 +52,7 @@ def containers(project="default", containers=None): } }, 'State': 'running', - }, + } + for container in containers] return wrapper diff --git a/tests/tests.py b/tests/tests.py index 3f1b790..035202d 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -1,15 +1,43 @@ +import json +import os import unittest from unittest import mock from restic_volume_backup import utils +import fixtures +list_containers_func = 'restic_volume_backup.utils.list_containers' -def list_containers(): - return {} - - -@mock.patch('restic_volume_backup.utils.list_containers', list_containers) class ResticBackupTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + """Set up basic enviroment variables""" + os.environ['RESTIC_REPOSITORY'] = "test" + os.environ['RESTIC_PASSWORD'] = "password" + def test_stuff(self): - assert utils.list_containers() == {} + 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() + + # raise ValueError(json.dumps(test, indent=2))