Files
@ 35140b3eb5b8
Branch filter:
Location: majic-ansible-roles/roles/backup_client/molecule/default/tests/test_default.py
35140b3eb5b8
5.9 KiB
text/x-python
MAR-198: Implement removal of expired backups via backup_client role:
- In addition to expired backup removals, ensure that the backup
does not proceed if previous execution step has failed. This means
that pre-scripts now have to run without errors for the main backup
to kick-in.
- Could not use full _and_ chaining of duply. For example, if we have
A_and_B_and_C (where A/B/C are duply backup commands), if A
fails, B will be skipped, but C will still run because B has ran
without_errors.
- In addition to expired backup removals, ensure that the backup
does not proceed if previous execution step has failed. This means
that pre-scripts now have to run without errors for the main backup
to kick-in.
- Could not use full _and_ chaining of duply. For example, if we have
A_and_B_and_C (where A/B/C are duply backup commands), if A
fails, B will be skipped, but C will still run because B has ran
without_errors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | import os
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('parameters-*')
def test_installed_packages(host):
"""
Tests if the necessary packages are installed.
"""
expected_package_name = "python3-pexpect"
assert host.package(expected_package_name).is_installed
assert host.package('duply').is_installed
assert host.package('duplicity').is_installed
def test_duply_directories(host):
"""
Tests if Duply directories have been set-up correctly.
"""
with host.sudo():
for directory_path in ["/etc/duply",
"/etc/duply/main",
"/etc/duply/main/patterns",
"/etc/duply/main/gnupg",
"/etc/duply/main/ssh",
"/var/cache/duply",
"/var/cache/duply/main"]:
directory = host.file(directory_path)
assert directory.is_directory
assert directory.user == 'root'
assert directory.group == 'root'
assert directory.mode == 0o700
def test_gnupg_private_keys_file(host):
"""
Tests if file containing GnuPG private keys has been created and has correct
permissions.
"""
with host.sudo():
gnupg_private_keys = host.file('/etc/duply/main/private_keys.asc')
assert gnupg_private_keys.is_file
assert gnupg_private_keys.user == 'root'
assert gnupg_private_keys.group == 'root'
assert gnupg_private_keys.mode == 0o600
def test_gnupg_public_keys_file(host):
"""
Tests if file containing additional GnuPG public keys used for encryption
has been created and has correct permissions.
"""
with host.sudo():
gnupg_public_keys = host.file('/etc/duply/main/public_keys.asc')
assert gnupg_public_keys.is_file
assert gnupg_public_keys.user == 'root'
assert gnupg_public_keys.group == 'root'
assert gnupg_public_keys.mode == 0o600
def test_private_ssh_key_file(host):
"""
Tests if the file containing client SSH key used for logging-in into the
backup server has been deployed and has correct permissions.
"""
with host.sudo():
ssh_key = host.file('/etc/duply/main/ssh/identity')
assert ssh_key.is_file
assert ssh_key.user == 'root'
assert ssh_key.group == 'root'
assert ssh_key.mode == 0o600
def test_known_hosts(host):
"""
Tests if the Duply known_hosts file has been deployed and has correct
permissions.
"""
with host.sudo():
known_hosts = host.file('/etc/duply/main/ssh/known_hosts')
assert known_hosts.is_file
assert known_hosts.user == 'root'
assert known_hosts.group == 'root'
assert known_hosts.mode == 0o600
def test_duply_configuration(host):
"""
Tests if Duply configuraiton file has been deployed and has correct file
permissions.
"""
with host.sudo():
duply_configuration = host.file('/etc/duply/main/conf')
assert duply_configuration.is_file
assert duply_configuration.user == 'root'
assert duply_configuration.group == 'root'
assert duply_configuration.mode == 0o600
def test_exclude_file(host):
with host.sudo():
exclude = host.file('/etc/duply/main/exclude')
assert exclude.is_file
assert exclude.user == 'root'
assert exclude.group == 'root'
assert exclude.mode == 0o600
assert exclude.content_string == "- **"
def test_pre_backup_script_directory(host):
with host.sudo():
pre_backup_dir = host.file('/etc/duply/main/pre.d')
assert pre_backup_dir.is_directory
assert pre_backup_dir.user == 'root'
assert pre_backup_dir.group == 'root'
assert pre_backup_dir.mode == 0o700
def test_pre_backup_script(host):
"""
Tests if the script used for running pre-backup handles has been deployed
and has correct permissions.x
"""
with host.sudo():
pre_backup_script = host.file('/etc/duply/main/pre')
assert pre_backup_script.is_file
assert pre_backup_script.user == 'root'
assert pre_backup_script.group == 'root'
assert pre_backup_script.mode == 0o700
def test_cron_entry(host):
"""
Tests if cron job has been correctly set-up for running backups.
"""
cron = host.file('/etc/cron.d/backup')
assert cron.is_file
assert cron.user == 'root'
assert cron.group == 'root'
assert cron.mode == 0o644
assert cron.content_string == "#Ansible: backup\n0 2 * * * root /usr/bin/duply main pre_and_bkp && /usr/bin/duply main post_and_purge --force\n"
def test_duply_include_file(host):
"""
Tests include file existence and permissions.
"""
with host.sudo():
include = host.file('/etc/duply/main/include')
assert include.is_file
assert include.user == 'root'
assert include.group == 'root'
assert include.mode == 0o600
def test_backup_and_restore(host):
"""
Tests a simple backup and restore to a directory. Includes tests for
checking if the pre-backup handles are run correctly.
"""
with host.sudo():
# Remove this file so we can be sure the pre-backup script has been run.
host.ansible("file", "path=/var/lib/pre-backup-test state=absent")
backup_run = host.run('duply main pre_and_bkp')
assert backup_run.rc == 0
assert host.file('/var/lib/pre-backup-test').is_file
# Remove restore directory in order to make sure restore has worked
# correctly.
host.ansible("file", "path=/root/restore state=absent")
restore_run = host.run('duply main restore /root/restore')
assert restore_run.rc == 0
assert host.file('/root/restore').is_directory
|