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

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.pytest_cache/
__pycache__/
pytestdebug.log

View File

@ -15,4 +15,4 @@ script:
- molecule test --scenario-name lamp
- molecule test --scenario-name mgt
- molecule test --scenario-name multiple
- molecule test --scenario-name clients
# - molecule test --scenario-name clients

View File

@ -81,6 +81,4 @@ provisioner:
scenario:
name: clients
verifier:
name: goss
lint:
name: yamllint
name: testinfra

1
molecule/clients/tests Symbolic link
View File

@ -0,0 +1 @@
../generic_files/tests

View File

@ -1 +0,0 @@
../../generic_files/tests/test_all.yml

View File

@ -1 +0,0 @@
../../generic_files/tests/test_client.yml

View File

@ -1 +0,0 @@
../../generic_files/tests/test_client_restore.yml

View File

@ -1 +0,0 @@
../../generic_files/tests/test_server.yml

View File

@ -1,154 +0,0 @@
---
# This is an example playbook to execute goss tests.
# Tests need distributed to the appropriate ansible host/groups
# prior to execution by `goss validate`.
- name: Verify all
hosts: all
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
molecule_scenario_name: clients
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_all.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_all.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"
- name: Verify clients
hosts: all:!borgbackup_servers
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
molecule_scenario_name: clients
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_client*.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_client*.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"
- name: Verify backup servers
hosts: borgbackup_servers
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
molecule_scenario_name: clients
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_server*.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_server*.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"

View File

@ -1 +0,0 @@
../generic_files/Dockerfile.j2

View File

@ -1,26 +0,0 @@
*******
Docker driver installation guide
*******
Requirements
============
* General molecule dependencies (see https://molecule.readthedocs.io/en/latest/installation.html)
* Docker Engine
* docker-py
* docker
Install
=======
Ansible < 2.6
.. code-block:: bash
$ sudo pip install docker-py
Ansible >= 2.6
.. code-block:: bash
$ sudo pip install docker

View File

@ -1,51 +0,0 @@
---
driver:
name: docker
lint:
name: yamllint
platforms:
- name: ${MOLECULE_SCENARIO_NAME}-cds
hostname: ${MOLECULE_SCENARIO_NAME}-cds
image: debian:stretch
networks:
- name: ${MOLECULE_SCENARIO_NAME}-borg
- name: ${MOLECULE_SCENARIO_NAME}-sds
hostname: ${MOLECULE_SCENARIO_NAME}-sds
image: debian:stretch
networks:
- name: ${MOLECULE_SCENARIO_NAME}-borg
groups:
- borgbackup_servers
provisioner:
name: ansible
inventory:
group_vars:
all:
borgbackup_appendonly: true
borgbackup_servers:
- fqdn: ${MOLECULE_SCENARIO_NAME}-sds
user: borgds
type: normal
home: /var/backup/
pool: repos
options: ""
borgbackup_include:
- "/etc"
- "/root"
- "/var/log"
- "/home"
host_vars:
${MOLECULE_SCENARIO_NAME}-cds:
borgbackup_passphrase: soo6Uabiex
borgbackup_pre_commands:
- "touch /var/log/pre_command"
borgbackup_post_commands:
- "touch /var/log/post_command"
lint:
name: ansible-lint
scenario:
name: commands
verifier:
name: goss
lint:
name: yamllint

View File

@ -1 +0,0 @@
../generic_files/playbook.yml

View File

@ -1 +0,0 @@
../generic_files/prepare.yml

View File

@ -1 +0,0 @@
../generic_files/restore.sh.j2

View File

@ -1 +0,0 @@
../generic_files/side_effect.yml

View File

@ -1 +0,0 @@
../../generic_files/tests/test_all.yml

View File

@ -1 +0,0 @@
../../generic_files/tests/test_client.yml

View File

@ -1,19 +0,0 @@
# Molecule managed
---
file:
/var/log/post_command:
exists: true
owner: root
group: root
/var/log/pre_command:
exists: true
owner: root
group: root
/usr/local/bin/borg-backup:
exists: true
owner: root
group: root
contains:
- "/var/log/post_command"
- "/var/log/pre_command"

View File

@ -1 +0,0 @@
../../generic_files/tests/test_client_restore.yml

View File

@ -1 +0,0 @@
../../generic_files/tests/test_server.yml

View File

@ -1,151 +0,0 @@
---
# This is an example playbook to execute goss tests.
# Tests need distributed to the appropriate ansible host/groups
# prior to execution by `goss validate`.
- name: Verify all
hosts: all
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_all.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_all.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"
- name: Verify clients
hosts: all:!borgbackup_servers
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_client*.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_client*.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"
- name: Verify backup servers
hosts: borgbackup_servers
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_server*.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_server*.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"

View File

@ -9,6 +9,8 @@ platforms:
image: debian:stretch
networks:
- name: ${MOLECULE_SCENARIO_NAME}-borg
groups:
- folders
- name: ${MOLECULE_SCENARIO_NAME}-sds
hostname: ${MOLECULE_SCENARIO_NAME}-sds
image: debian:stretch
@ -45,6 +47,6 @@ provisioner:
scenario:
name: folders
verifier:
name: goss
name: testinfra
lint:
name: yamllint
name: flake8

1
molecule/folders/tests Symbolic link
View File

@ -0,0 +1 @@
../generic_files/tests

View File

@ -1 +0,0 @@
../../generic_files/tests/test_all.yml

View File

@ -1 +0,0 @@
../../generic_files/tests/test_client.yml

View File

@ -1,11 +0,0 @@
# Molecule managed
---
file:
/usr/local/bin/borg-backup:
exists: true
owner: root
group: root
contains:
- "/var/cache"
- "--exclude '/var/cache/apt'"

View File

@ -1 +0,0 @@
../../generic_files/tests/test_client_restore.yml

View File

@ -1 +0,0 @@
../../generic_files/tests/test_server.yml

View File

@ -1,151 +0,0 @@
---
# This is an example playbook to execute goss tests.
# Tests need distributed to the appropriate ansible host/groups
# prior to execution by `goss validate`.
- name: Verify all
hosts: all
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_all.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_all.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"
- name: Verify clients
hosts: all:!borgbackup_servers
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_client*.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_client*.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"
- name: Verify backup servers
hosts: borgbackup_servers
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_server*.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_server*.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"

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 %}

View File

@ -39,6 +39,6 @@ provisioner:
scenario:
name: lamp
verifier:
name: goss
name: testinfra
lint:
name: yamllint
name: flake8

1
molecule/lamp/tests Symbolic link
View File

@ -0,0 +1 @@
../generic_files/tests

View File

@ -1 +0,0 @@
../../generic_files/tests/test_all.yml

View File

@ -1 +0,0 @@
../../generic_files/tests/test_client.yml

View File

@ -1 +0,0 @@
../../generic_files/tests/test_client_restore.yml

View File

@ -1,17 +0,0 @@
# Molecule managed
---
file:
/etc/hosts:
exists: true
owner: root
group: root
/usr/local/bin/borg-backup:
exists: true
owner: root
group: root
contains:
- "automysqlbackup"
- "/var/www"
/var/lib/automysqlbackup/daily:
exists: true

View File

@ -1 +0,0 @@
../../generic_files/tests/test_server.yml

View File

@ -1,200 +0,0 @@
---
# This is an example playbook to execute goss tests.
# Tests need distributed to the appropriate ansible host/groups
# prior to execution by `goss validate`.
- name: Verify all
hosts: all
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_all.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_all.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"
- name: Verify clients
hosts: all:!borgbackup_servers
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_client*.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_client*.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"
- name: Verify lamp clients
hosts: lamp
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_lamp*.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_lamp*.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"
- name: Verify backup servers
hosts: borgbackup_servers
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_server*.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_server*.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"

View File

@ -71,6 +71,6 @@ provisioner:
scenario:
name: mgt
verifier:
name: goss
name: testinfra
lint:
name: yamllint
name: flake8

1
molecule/mgt/tests Symbolic link
View File

@ -0,0 +1 @@
../generic_files/tests

View File

@ -1 +0,0 @@
../../generic_files/tests/test_all.yml

View File

@ -1 +0,0 @@
../../generic_files/tests/test_client.yml

View File

@ -1 +0,0 @@
../../generic_files/tests/test_client_restore.yml

View File

@ -1,16 +0,0 @@
# Molecule managed
---
file:
/root/prune.sh:
exists: true
owner: root
group: root
mode: "0700"
contains:
{% for host in groups.all|difference(groups.borgbackup_servers) %}
- "Host: {{ host }}"
- "export BORG_PASSPHRASE={{ borgbackup_passphrase }}"
- ":/var/backup/repos/{{ host }}"
{% endfor %}
- "/usr/local/bin/borg prune"

View File

@ -1 +0,0 @@
../../generic_files/tests/test_server.yml

View File

@ -1,200 +0,0 @@
---
# This is an example playbook to execute goss tests.
# Tests need distributed to the appropriate ansible host/groups
# prior to execution by `goss validate`.
- name: Verify all
hosts: all
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_all.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_all.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"
- name: Verify mgt
hosts: borgbackup_management
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_mgt.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_mgt.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"
- name: Verify clients
hosts: all:!borgbackup_servers
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_client*.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_client*.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"
- name: Verify backup servers
hosts: borgbackup_servers
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_server*.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_server*.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"

View File

@ -66,6 +66,6 @@ provisioner:
scenario:
name: multiple
verifier:
name: goss
name: testinfra
lint:
name: yamllint
name: flake8

1
molecule/multiple/tests Symbolic link
View File

@ -0,0 +1 @@
../generic_files/tests

View File

@ -1 +0,0 @@
../../generic_files/tests/test_all.yml

View File

@ -1 +0,0 @@
../../generic_files/tests/test_client.yml

View File

@ -1 +0,0 @@
../../generic_files/tests/test_client_restore.yml

View File

@ -1 +0,0 @@
../../generic_files/tests/test_server.yml

View File

@ -1,151 +0,0 @@
---
# This is an example playbook to execute goss tests.
# Tests need distributed to the appropriate ansible host/groups
# prior to execution by `goss validate`.
- name: Verify all
hosts: all
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_all.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_all.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"
- name: Verify clients
hosts: all:!borgbackup_servers
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_client*.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_client*.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"
- name: Verify backup servers
hosts: borgbackup_servers
become: true
vars:
goss_version: v0.3.2
goss_arch: amd64
goss_dst: /usr/local/bin/goss
goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec
goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}"
goss_test_directory: /tmp
goss_format: documentation
tasks:
- name: Download and install Goss
get_url:
url: "{{ goss_url }}"
dest: "{{ goss_dst }}"
sha256sum: "{{ goss_sha256sum }}"
mode: 0755
register: download_goss
until: download_goss is succeeded
retries: 3
- name: Copy Goss tests to remote
template:
src: "{{ item }}"
dest: "{{ goss_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_server*.yml"
- name: Register test files
shell: "ls {{ goss_test_directory }}/test_server*.yml"
register: test_files
- name: Execute Goss tests
command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
- name: Display details about the Goss results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"
- name: Fail when tests fail
fail:
msg: "Goss failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"