restic-compose-backup/tests/fixtures.py

55 lines
1.6 KiB
Python
Raw Permalink Normal View History

"""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()
2019-04-16 00:57:06 +00:00
def containers(project="default", containers=[]):
"""
Args:
project (str): Name of the compose project
containers (dict):
{
'containers: [
'id': 'something'
2019-04-16 00:57:06 +00:00
'service': 'service_name',
'mounts: [{
'Source': '/home/user/stuff',
'Destination': '/srv/stuff',
'Type': 'bind' / 'volume'
}],
]
}
"""
def wrapper(*args, **kwargs):
2019-04-16 00:57:06 +00:00
return [
{
'Id': container.get('id', generate_sha256()),
'Name': container.get('service') + '_' + ''.join(random.choice(string.ascii_lowercase) for i in range(16)),
'Config': {
2019-12-03 08:40:02 +00:00
'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', {}),
},
},
2019-04-16 00:57:06 +00:00
'Mounts': container.get('mounts', []),
'State': {
"Status": "running",
"Running": True,
}
2019-04-16 00:57:06 +00:00
}
for container in containers]
return wrapper