Added some tests with Testinfra

This commit is contained in:
Werner Dijkerman 2019-04-16 20:36:31 +02:00
parent d20608b770
commit fa7a5e6b91
4 changed files with 60 additions and 30 deletions

View File

@ -9,25 +9,17 @@ lint:
platforms: platforms:
- name: wazuh_manager_bionic - name: wazuh_manager_bionic
image: ubuntu:bionic image: ubuntu:bionic
groups:
- wazuh_manager
- name: wazuh_manager_xenial - name: wazuh_manager_xenial
image: ubuntu:xenial image: solita/ubuntu-systemd:xenial
groups: privileged: True
- wazuh_manager command: /sbin/init
- name: wazuh_manager_trusty - name: wazuh_manager_trusty
image: ubuntu:trusty image: ubuntu:trusty
groups:
- wazuh_manager
- name: wazuh_manager_centos6 - name: wazuh_manager_centos6
image: centos:6 image: centos:6
groups:
- wazuh_manager
- name: wazuh_manager_centos7 - name: wazuh_manager_centos7
image: milcom/centos7-systemd image: milcom/centos7-systemd
privileged: True privileged: True
groups:
- wazuh_manager
provisioner: provisioner:
name: ansible name: ansible
env: env:

View File

@ -1,6 +1,6 @@
--- ---
- name: Converge - name: Converge
hosts: wazuh_manager hosts: all
roles: roles:
- role: wazuh/ansible-wazuh-manager - role: wazuh/ansible-wazuh-manager

View File

@ -1,4 +1,5 @@
import os import os
import pytest
import testinfra.utils.ansible_runner import testinfra.utils.ansible_runner
@ -6,25 +7,62 @@ testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
def test_hosts_file(host): def get_wazuh_version():
f = host.file('/etc/hosts') """This return the version of Wazuh."""
return "3.8"
assert f.exists
assert f.user == 'root'
assert f.group == 'root'
def test_filebeat_is_installed(host): def test_wazuh_packages_are_installed(host):
package = host.package("filebeat") """Test if the main packages are installed."""
assert package.is_installed manager = host.package("wazuh-manager")
assert package.version.startswith("6") api = host.package("wazuh-api")
distribution = host.system_info.distribution.lower()
if distribution == 'centos':
if host.system_info.release == "7":
assert manager.is_installed
assert manager.version.startswith(get_wazuh_version())
assert api.is_installed
assert api.version.startswith(get_wazuh_version())
elif host.system_info.release.startswith("6"):
assert manager.is_installed
assert manager.version.startswith(get_wazuh_version())
elif distribution == 'ubuntu':
assert manager.is_installed
assert manager.version.startswith(get_wazuh_version())
def test_filebeat_service_enabled(host): def test_wazuh_services_are_running(host):
service = host.service('filebeat') """Test if the services are enabled and running.
assert service.is_enabled
When assert commands are commented, this means that the service command has a
wrong exit code: https://github.com/wazuh/wazuh-ansible/issues/107
"""
manager = host.service("wazuh-manager")
api = host.service("wazuh-api")
def test_filebeat_config_file_present(host): distribution = host.system_info.distribution.lower()
config_file = host.file('/etc/filebeat/filebeat.yml') if distribution == 'centos':
assert config_file.is_file # assert manager.is_running
assert manager.is_enabled
# assert not api.is_running
assert not api.is_enabled
elif distribution == 'ubuntu':
# assert manager.is_running
assert manager.is_enabled
# assert api.is_running
assert api.is_enabled
@pytest.mark.parametrize("wazuh_file, wazuh_owner, wazuh_group, wazuh_mode", [
("/var/ossec/etc/sslmanager.cert", "root", "root", 0o640),
("/var/ossec/etc/sslmanager.key", "root", "root", 0o640),
("/var/ossec/etc/rules/local_rules.xml", "root", "ossec", 0o640),
("/var/ossec/etc/lists/audit-keys", "root", "ossec", 0o640),
])
def test_wazuh_files(host, wazuh_file, wazuh_owner, wazuh_group, wazuh_mode):
"""Test if Wazuh related files exist and have proper owners and mode."""
wazuh_file_host = host.file(wazuh_file)
assert wazuh_file_host.user == wazuh_owner
assert wazuh_file_host.group == wazuh_group
assert wazuh_file_host.mode == wazuh_mode

View File

@ -6,9 +6,9 @@
- ca-certificates - ca-certificates
- gnupg - gnupg
state: present state: present
register: wazuh_manager_https_packages_installed
until: wazuh_manager_https_packages_installed is succeeded
cache_valid_time: 3600 cache_valid_time: 3600
register: wazuh_manager_https_packages_installed
until: wazuh_manager_https_packages_installed is succeeded
- name: Debian/Ubuntu | Installing Wazuh repository key (Ubuntu 14) - name: Debian/Ubuntu | Installing Wazuh repository key (Ubuntu 14)
become: yes become: yes