Replace goss tests with testinfra

This commit is contained in:
Dieter Verhelst 2019-03-12 10:33:14 +01:00
parent b4695d7403
commit 97faa7429c
65 changed files with 163 additions and 1281 deletions

View file

@ -0,0 +1,2 @@
[flake8]
ignore = E501

View file

@ -0,0 +1,6 @@
def test_borg_binary(host):
borg = host.file("/usr/local/bin/borg")
assert borg.exists
assert borg.user == "root"
assert borg.group == "root"
assert borg.mode == 0o755

View file

@ -1,9 +0,0 @@
# Molecule managed
---
file:
/usr/local/bin/borg:
exists: true
owner: root
group: root
mode: "0755"

View file

@ -0,0 +1,45 @@
import os
import pytest
import re
from testinfra.utils.ansible_runner import AnsibleRunner
testinfra_hosts = ["ansible://all:!borgbackup_servers"]
def test_log(host):
logfile = host.file("/var/log/borg-backup.log")
assert logfile.contains("Backup succeeded")
assert logfile.user == "root"
assert logfile.group == "root"
# to do read inventory variable : export BORG_PASSPHRASE="{{ borgbackup_passphrase }}"
def test_passphrase(host):
pfile = host.file("/root/.borg.passphrase")
assert pfile.contains("BORG_PASSPHRASE=")
assert pfile.user == "root"
assert pfile.group == "root"
@pytest.mark.parametrize('server', AnsibleRunner(os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('borgbackup_servers'))
def test_sshconfig(host, server):
sshconf = host.file("/root/.ssh/config")
assert sshconf.contains(" ANSIBLE MANAGED BLOCK %s " % server)
assert sshconf.contains("Host %s" % server)
assert sshconf.user == "root"
assert sshconf.group == "root"
def test_scriptfile(host):
script = host.file("/usr/local/bin/borg-backup")
assert script.user == "root"
assert script.group == "root"
assert script.mode == 0o744
@pytest.mark.parametrize('server', AnsibleRunner(os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('borgbackup_servers'))
def test_list_backups(host, server):
command = host.run("bash /usr/local/bin/borg-backup list")
assert command.rc == 0
assert command.stderr == ''
assert re.match("Archives on %s :\n[0-9]{8}-[0-9]{4}.*" % server, command.stdout) is not None

View file

@ -1,38 +0,0 @@
# Molecule managed
---
file:
/var/log/borg-backup.log:
exists: true
owner: root
group: root
contains:
- "Backup succeeded"
/root/.borg.passphrase:
exists: true
owner: root
group: root
contains:
- 'export BORG_PASSPHRASE="{{ borgbackup_passphrase }}"'
/root/.ssh/config:
exists: true
owner: root
group: root
contains:
{% for item in borgbackup_servers %}
- 'ANSIBLE MANAGED BLOCK {{ item.fqdn }}'
{% endfor %}
/usr/local/bin/borg-backup:
exists: true
owner: root
group: root
mode: "0744"
command:
bash /usr/local/bin/borg-backup list:
exit-status: 0
stdout:
{% for item in borgbackup_servers %}
- "Archives on {{ item.fqdn }}"
{% endfor %}
- "/[[:digit:]]{8}-[[:digit:]]{4}.*/"

View file

@ -0,0 +1,9 @@
testinfra_hosts = ["ansible://folders"]
def test_include_exclude(host):
script = host.file("/usr/local/bin/borg-backup")
assert script.contains("/var/cache")
assert script.contains("--exclude '/var/cache/apt'")
assert script.user == "root"
assert script.group == "root"

View file

@ -0,0 +1,24 @@
import os
import pytest
from testinfra.utils.ansible_runner import AnsibleRunner
testinfra_hosts = ["ansible://all:!borgbackup_servers"]
def test_client_sample_file(host):
sample = host.file("/root/sample.txt")
assert sample.is_file
@pytest.mark.parametrize('server', AnsibleRunner(os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('borgbackup_servers'))
def test_client_dir(host, server):
command = host.run("diff -s /root/sample.txt /root/restore/%s/root/sample.txt" % server)
assert command.rc == 0
assert "Files /root/sample.txt and /root/restore/%s/root/sample.txt are identical" % server in command.stdout
@pytest.mark.parametrize('server', AnsibleRunner(os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('borgbackup_servers'))
def test_client_verify(host, server):
vcommand = host.run("/root/restore.sh verify")
assert vcommand.rc == 0
assert vcommand.stdout.rstrip("verifying on %s" % server)

View file

@ -1,18 +0,0 @@
---
file:
/root/sample.txt:
exists: true
command:
{% for item in borgbackup_servers %}
diff -s /root/sample.txt /root/restore/{{ item.fqdn }}/root/sample.txt:
exit-status: 0
stdout:
- "Files /root/sample.txt and /root/restore/{{ item.fqdn }}/root/sample.txt are identical"
{% endfor %}
/root/restore.sh verify:
exit-status: 0
stdout:
{% for item in borgbackup_servers %}
- "verifying on {{ item.fqdn }}"
{% endfor %}

View file

@ -0,0 +1,16 @@
testinfra_hosts = ["ansible://lamp"]
def test_lamp_default(host):
script = host.file("/usr/local/bin/borg-backup")
assert script.contains("/usr/sbin/automysqlbackup")
assert script.contains("/var/lib/automysqlbackup")
assert script.contains("/var/www")
assert script.user == "root"
assert script.group == "root"
def test_lamp_automysqlbackup(host):
backup_dir = host.file("/var/lib/automysqlbackup/daily")
assert backup_dir.exists
assert backup_dir.is_directory

View file

@ -0,0 +1,18 @@
import os
import pytest
from testinfra.utils.ansible_runner import AnsibleRunner
testinfra_hosts = ["ansible://borgbackup_management"]
# to do read inventory variable : export BORG_PASSPHRASE="{{ borgbackup_passphrase }}"
@pytest.mark.parametrize('client', AnsibleRunner(os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all:!borgbackup_management:!borgbackup_servers'))
def test_prune_script(host, client):
prune = host.file("/root/prune.sh")
assert prune.user == "root"
assert prune.group == "root"
assert prune.mode == 0o700
assert prune.contains("/usr/local/bin/borg prune")
assert prune.contains("export BORG_PASSPHRASE=")
assert prune.contains("Host: %s" % client)
assert prune.contains(":/var/backup/repos/%s" % client)

View file

@ -0,0 +1,23 @@
import os
import pytest
from testinfra.utils.ansible_runner import AnsibleRunner
testinfra_hosts = ["ansible://borgbackup_servers"]
def test_client_parent_dir(host):
parentdir = host.file("/var/backup/repos")
assert parentdir.is_directory
@pytest.mark.parametrize('client', AnsibleRunner(os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all:!borgbackup_servers'))
def test_client_dir(host, client):
clientdir = host.file("/var/backup/repos/%s" % client)
assert clientdir.is_directory
@pytest.mark.parametrize('client', AnsibleRunner(os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all:!borgbackup_servers'))
def test_ssh_client_conf(host, client):
sshconf = host.file("/var/backup/.ssh/authorized_keys")
assert sshconf.is_file
assert sshconf.contains("%s;borg serve" % client)

View file

@ -1,28 +0,0 @@
# Molecule managed
---
file:
{% for item in borgbackup_servers %}
{% if item.fqdn == inventory_hostname %}
{{ item.home }}:
exists: true
owner: "{{ item.user }}"
group: "{{ item.user }}"
{% for host in groups.all|difference(groups.borgbackup_servers) %}
{{ item.home }}/repos/{{ host }}:
exists: true
{% endfor %}
{% endif %}
{% endfor %}
{% for item in borgbackup_servers %}
{% if item.fqdn == inventory_hostname %}
{{ item.home }}/.ssh/authorized_keys:
exists: true
owner: "{{ item.user }}"
group: "{{ item.user }}"
contains:
{% for host in groups.all|difference(groups.borgbackup_servers) %}
- "{{ host }};borg serve"
{% endfor %}
{% endif %}
{% endfor %}