Files
@ 423f330ec482
Branch filter:
Location: majic-ansible-roles/roles/web_server/molecule/default/tests/test_default.py
423f330ec482
6.4 KiB
text/x-python
MAR-152: Switch to using /run/php for storing PHP-FPM unix socket files.
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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | import os
import testinfra.utils.ansible_runner
import pytest
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('parameters-*')
def test_installed_packages(host, php_info):
"""
Tests if the required packages have been installed.
"""
assert host.package('nginx').is_installed
assert host.package('python-setuptools').is_installed
assert host.package('python3-setuptools').is_installed
assert host.package('virtualenv').is_installed
assert host.package('virtualenvwrapper').is_installed
assert host.package(php_info.fpm_package).is_installed
def test_nginx_user(host):
"""
Tests if Nginx user has been set-up correctly to traverse TLS directories.
"""
assert 'ssl-cert' in host.user('www-data').groups
def test_default_tls_configuration_removed(host):
"""
Tests if TLS configuration has been removed from the main (default)
configuration file.
"""
assert 'ssl_protocols' not in host.file('/etc/nginx/nginx.conf').content
def test_nginx_configuration_verification_script(host):
"""
Tests if script used for verifying Nginx configuration is deployed
correctly.
"""
script = host.file('/usr/local/bin/nginx_verify_site.sh')
assert script.is_file
assert script.user == 'root'
assert script.group == 'root'
assert script.mode == 0o755
def test_tls_configuration_file(host):
"""
Tests permissions of TLS configuration file.
"""
config = host.file('/etc/nginx/conf.d/tls.conf')
assert config.is_file
assert config.user == 'root'
assert config.group == 'root'
assert config.mode == 0o644
def test_default_vhost_file(host):
"""
Tests permissions of default vhost configuration file.
"""
config = host.file('/etc/nginx/sites-available/default')
assert config.is_file
assert config.user == 'root'
assert config.group == 'root'
assert config.mode == 0o640
def test_default_website_enabled(host):
"""
Tests if default website has been enabled.
"""
config = host.file('/etc/nginx/sites-enabled/default')
assert config.is_symlink
assert config.linked_to == '/etc/nginx/sites-available/default'
def test_firewall_configuration_file(host):
"""
Tests if firewall configuration file has been deployed correctly.
"""
with host.sudo():
config = host.file('/etc/ferm/conf.d/30-web.conf')
assert config.is_file
assert config.user == 'root'
assert config.group == 'root'
assert config.mode == 0o640
def test_default_debian_index_removed(host):
"""
Tests if default HTML pages provided by debian are removed.
"""
with host.sudo():
assert not host.file('/var/www/html').exists
def test_default_vhost_root_directory(host):
"""
Tests if the default vhost root directory exists.
"""
directory = host.file('/var/www/default')
assert directory.is_directory
assert directory.user == 'root'
assert directory.group == 'www-data'
assert directory.mode == 0o750
def test_default_vhost_index_page_file(host):
"""
Tests permissions of default vhost index page.
"""
with host.sudo():
page = host.file('/var/www/default/index.html')
assert page.is_file
assert page.user == 'root'
assert page.group == 'www-data'
assert page.mode == 0o640
def test_services(host, php_info):
"""
Tests if services are enabled at boot and running.
"""
service = host.service('nginx')
assert service.is_enabled
assert service.is_running
service = host.service(php_info.fpm_service)
assert service.is_enabled
assert service.is_running
def test_sockets(host):
"""
Tests if web server is listening on correct ports.
"""
assert host.socket("tcp://80").is_listening
assert host.socket("tcp://443").is_listening
@pytest.mark.parametrize("application_type, tmpfiles_d_path",
[("wsgi", "/etc/tmpfiles.d/wsgi.conf"),
("php", "/etc/tmpfiles.d/php7.0-fpm.conf")])
def test_socket_directories(host, application_type, tmpfiles_d_path):
"""
Tests if directories containing sockets for WSGI and PHP apps are created
correctly.
"""
socket_directory = "/run/%s" % application_type
tmpfiles_d_content = "d /run/%s/ 0750 root www-data - -" % application_type
directory = host.file(socket_directory)
assert directory.is_directory
assert directory.user == 'root'
assert directory.group == 'www-data'
assert directory.mode == 0o750
config = host.file(tmpfiles_d_path)
assert config.is_file
assert config.user == 'root'
assert config.group == 'root'
assert config.mode == 0o644
assert config.content == tmpfiles_d_content
def test_php_fpm_service_overrides(host, php_info):
"""
Tests if overrides for PHP-FPM service are deployed correctly.
"""
directory = host.file('/etc/systemd/system/%s.service.d' % php_info.fpm_service)
assert directory.is_directory
assert directory.user == 'root'
assert directory.group == 'root'
assert directory.mode == 0o755
config = host.file('/etc/systemd/system/%s.service.d/umask.conf' % php_info.fpm_service)
assert config.is_file
assert config.user == 'root'
assert config.group == 'root'
assert config.mode == 0o644
def test_php_timezone_configuration(host, php_info):
"""
Tests if PHP timezone configuration has been set correctly.
"""
server_timezone = host.file("/etc/timezone").content.strip()
config = host.file('%s/cli/conf.d/30-timezone.ini' % php_info.base_config_dir)
assert config.is_file
assert config.user == 'root'
assert config.group == 'root'
assert config.mode == 0o644
config = host.file('%s/fpm/conf.d/30-timezone.ini' % php_info.base_config_dir)
assert config.is_file
assert config.user == 'root'
assert config.group == 'root'
assert config.mode == 0o644
timezone = host.run("php --php-ini %s -r %s", "%s/cli/php.ini" % php_info.base_config_dir, "echo ini_get('date.timezone');")
assert timezone.rc == 0
assert timezone.stdout == server_timezone
timezone = host.run("php --php-ini %s -r %s", "%s/fpm/php.ini" % php_info.base_config_dir, "echo ini_get('date.timezone');")
assert timezone.rc == 0
assert timezone.stdout == server_timezone
|