2018-10-01 02:30:04 +02:00
|
|
|
import json
|
|
|
|
import os
|
2018-10-04 07:36:25 +02:00
|
|
|
import shutil
|
2018-10-01 02:30:04 +02:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2018-10-04 07:36:25 +02:00
|
|
|
import tempfile
|
2018-10-01 02:30:04 +02:00
|
|
|
|
|
|
|
|
2018-10-04 07:36:25 +02:00
|
|
|
def generate_configuration(config_path, repository_path):
|
|
|
|
'''
|
|
|
|
Generate borgmatic configuration into a file at the config path, and update the defaults so as
|
|
|
|
to work for testing (including injecting the given repository path).
|
|
|
|
'''
|
2018-10-15 18:37:26 +02:00
|
|
|
subprocess.check_call(
|
|
|
|
'generate-borgmatic-config --destination {}'.format(config_path).split(' ')
|
|
|
|
)
|
2018-10-01 02:30:04 +02:00
|
|
|
config = (
|
2018-10-04 07:36:25 +02:00
|
|
|
open(config_path)
|
2018-10-01 02:30:04 +02:00
|
|
|
.read()
|
2018-10-04 07:36:25 +02:00
|
|
|
.replace('user@backupserver:sourcehostname.borg', repository_path)
|
2018-10-15 18:30:04 +02:00
|
|
|
.replace('- /home', '- {}'.format(config_path))
|
2018-10-04 07:36:25 +02:00
|
|
|
.replace('- /etc', '')
|
2018-10-01 02:30:04 +02:00
|
|
|
.replace('- /var/log/syslog*', '')
|
|
|
|
)
|
2018-10-04 07:36:25 +02:00
|
|
|
config_file = open(config_path, 'w')
|
2018-10-01 02:30:04 +02:00
|
|
|
config_file.write(config)
|
|
|
|
config_file.close()
|
|
|
|
|
|
|
|
|
|
|
|
def test_borgmatic_command():
|
|
|
|
# Create a Borg repository.
|
2018-10-04 07:36:25 +02:00
|
|
|
temporary_directory = tempfile.mkdtemp()
|
|
|
|
repository_path = os.path.join(temporary_directory, 'test.borg')
|
2018-10-01 02:30:04 +02:00
|
|
|
|
2018-10-04 07:36:25 +02:00
|
|
|
try:
|
|
|
|
subprocess.check_call(
|
2018-10-15 18:30:04 +02:00
|
|
|
'borg init --encryption repokey {}'.format(repository_path).split(' '),
|
2018-10-04 07:36:25 +02:00
|
|
|
env={'BORG_PASSPHRASE': '', **os.environ},
|
|
|
|
)
|
2018-10-01 02:30:04 +02:00
|
|
|
|
2018-10-04 07:36:25 +02:00
|
|
|
config_path = os.path.join(temporary_directory, 'test.yaml')
|
|
|
|
generate_configuration(config_path, repository_path)
|
|
|
|
|
|
|
|
# Run borgmatic to generate a backup archive, and then list it to make sure it exists.
|
2018-10-15 18:30:04 +02:00
|
|
|
subprocess.check_call('borgmatic --config {}'.format(config_path).split(' '))
|
2018-10-04 07:36:25 +02:00
|
|
|
output = subprocess.check_output(
|
2018-10-15 18:37:26 +02:00
|
|
|
'borgmatic --config {} --list --json'.format(config_path).split(' ')
|
|
|
|
).decode(sys.stdout.encoding)
|
2018-10-04 07:36:25 +02:00
|
|
|
parsed_output = json.loads(output)
|
2018-10-01 02:30:04 +02:00
|
|
|
|
2018-10-04 07:36:25 +02:00
|
|
|
assert len(parsed_output) == 1
|
|
|
|
assert len(parsed_output[0]['archives']) == 1
|
2018-10-13 22:09:12 +02:00
|
|
|
|
|
|
|
# Also exercise the info flag.
|
|
|
|
output = subprocess.check_output(
|
2018-10-15 18:37:26 +02:00
|
|
|
'borgmatic --config {} --info --json'.format(config_path).split(' ')
|
|
|
|
).decode(sys.stdout.encoding)
|
2018-10-13 22:09:12 +02:00
|
|
|
parsed_output = json.loads(output)
|
|
|
|
|
|
|
|
assert len(parsed_output) == 1
|
|
|
|
assert 'repository' in parsed_output[0]
|
2018-10-04 07:36:25 +02:00
|
|
|
finally:
|
|
|
|
shutil.rmtree(temporary_directory)
|