Files
@ 8f33126c7555
Branch filter:
Location: majic-ansible-roles/roles/wsgi_website/tests/test_parameters_paste_req.py
8f33126c7555
9.5 KiB
text/x-python
MAR-119: Updated all test playbooks to mark apt cache update as always idempotent (since it does not play role in actual role idempotence).
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | import re
import time
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
'.molecule/ansible_inventory').get_hosts('all')
def test_website_group(Group):
"""
Tests if website group has been created correctly.
"""
group = Group('web-parameters-paste-req')
assert group.exists
assert group.gid == 5002
def test_website_admin_user(User):
"""
Tests if website administrator user has been created correctly.
"""
user = User('admin-parameters-paste-req')
assert user.exists
assert user.uid == 5002
assert user.group == 'web-parameters-paste-req'
assert user.groups == ['web-parameters-paste-req']
assert user.shell == '/bin/bash'
assert user.home == '/var/www/parameters-paste-req'
def test_website_admin_home(File, Sudo):
"""
Tests if permissions on website admin home directory are correct.
"""
home = File('/var/www/parameters-paste-req')
assert home.is_directory
assert home.user == 'admin-parameters-paste-req'
assert home.group == 'web-parameters-paste-req'
assert home.mode == 0o750
def test_home_profile_directory(File, Sudo):
"""
Tests if profile directory has been set-up correctly for the website
administrator/application user.
"""
with Sudo():
directory = File('/var/www/parameters-paste-req/.profile.d')
assert directory.is_directory
assert directory.user == 'admin-parameters-paste-req'
assert directory.group == 'web-parameters-paste-req'
assert directory.mode == 0o750
def test_virtualenv_profile_configuration(File, Sudo):
"""
Tests if profile configuration file for auto-activation of virtual
environment has been deployed correctly.
"""
with Sudo():
config = File('/var/www/parameters-paste-req/.profile.d/virtualenv.sh')
assert config.is_file
assert config.user == 'root'
assert config.group == 'web-parameters-paste-req'
assert config.mode == 0o640
def test_profile_configuration(Command):
"""
Tests if profile configuration is behaving correctly (setting appropriate
vars via login shell).
"""
env = Command("sudo -i -u admin-parameters-paste-req printenv VIRTUAL_ENV MY_ENV_VAR")
assert env.stdout == "/var/www/parameters-paste-req/virtualenv"
def test_website_application_user(Command, Sudo, User):
"""
Tests if website application user has been created correctly.
"""
user = User('web-parameters-paste-req')
assert user.exists
assert user.uid == 998
assert user.group == 'web-parameters-paste-req'
assert user.groups == ['web-parameters-paste-req']
assert user.shell == '/bin/sh'
assert user.home == '/var/www/parameters-paste-req'
with Sudo():
umask = Command("su -l web-parameters-paste-req -c 'bash -c umask'")
assert umask.stdout == '0007'
def test_nginx_user(User):
"""
Tests if web server user has been added to website group.
"""
user = User('www-data')
assert 'web-parameters-paste-req' in user.groups
def test_forward_file(File, Sudo):
"""
Tests if the forward file has correct permissions and content.
"""
with Sudo():
config = File('/var/www/parameters-paste-req/.forward')
assert config.is_file
assert config.user == 'root'
assert config.group == 'web-parameters-paste-req'
assert config.mode == 0o640
assert config.content == "root"
def test_mail_forwarding(Command, File, Sudo):
"""
Tests if mail forwarding works as expected.
"""
send = Command('swaks --suppress-data --to web-parameters-paste-req@localhost')
assert send.rc == 0
message_id = re.search('Ok: queued as (.*)', send.stdout).group(1)
# Sleep for a couple of seconds so the mail can get delivered.
time.sleep(5)
with Sudo():
mail_log = File('/var/log/mail.log')
# First extract message ID of forwarded mail.
pattern = "%s: to=<web-parameters-paste-req@localhost>.*status=sent \(forwarded as ([^)]*)\)" % message_id
message_id = re.search(pattern, mail_log.content).group(1)
# Now try to determine where the forward ended-up at.
pattern = "%s: to=<vagrant@wsgi-website>, orig_to=<web-parameters-paste-req@localhost>.*status=sent" % message_id
assert re.search(pattern, mail_log.content) is not None
def test_python_virtualenv_created(File, Sudo):
"""
Tests if Python virtual environment has been created correctly.
"""
with Sudo():
virtualenv = File("/var/www/parameters-paste-req/virtualenv")
assert virtualenv.is_directory
assert virtualenv.user == 'admin-parameters-paste-req'
assert virtualenv.group == 'web-parameters-paste-req'
assert virtualenv.mode == 0o2750
virtualenv_activate = File("/var/www/parameters-paste-req/virtualenv/bin/activate")
assert virtualenv_activate.is_file
assert virtualenv_activate.user == 'admin-parameters-paste-req'
assert virtualenv_activate.group == 'web-parameters-paste-req'
assert virtualenv_activate.mode == 0o644
def test_python_virtualenv_project_directory_config(File, Sudo):
"""
Tests if project directory configuration within virtualenv is set-up
correctly.
"""
with Sudo():
project = File("/var/www/parameters-paste-req/virtualenv/.project")
assert project.is_file
assert project.user == 'admin-parameters-paste-req'
assert project.group == 'web-parameters-paste-req'
assert project.mode == 0o640
def test_python_virtualenv_wrapper_script(Command, File, Sudo):
"""
Tests if Python virtualenv wrapper script is functioning correctly.
"""
with Sudo():
wrapper = File("/var/www/parameters-paste-req/virtualenv/bin/exec")
assert wrapper.is_file
assert wrapper.user == 'admin-parameters-paste-req'
assert wrapper.group == 'web-parameters-paste-req'
assert wrapper.mode == 0o750
command = Command("sudo -u admin-parameters-paste-req /var/www/parameters-paste-req/virtualenv/bin/exec python -c 'import gunicorn'")
assert command.rc == 0
def test_virtualenv_packages(Command):
"""
Tests if correct packages are installed in virtualenv.
"""
packages = Command("sudo -u admin-parameters-paste-req /var/www/parameters-paste-req/virtualenv/bin/pip freeze")
assert sorted(packages.stdout.lower().split("\n")) == sorted("""Flask==0.12.2
Jinja2==2.9.6
MarkupSafe==1.0
Paste==2.0.3
PasteDeploy==1.5.2
PasteScript==2.0.2
Werkzeug==0.12.2
argparse==1.2.1
click==6.7
futures==3.1.0
gunicorn==19.7.0
itsdangerous==0.24
six==1.10.0
wsgiref==0.1.2""".lower().split("\n"))
def test_wsgi_requirements_upgrade_checks(File, Sudo):
"""
Tests if Python requirements files for upgrade checks are set-up correctly.
"""
with Sudo():
directory = File('/etc/pip_check_requirements_upgrades/parameters-paste-req')
assert directory.is_directory
assert directory.user == 'root'
assert directory.group == 'pipreqcheck'
assert directory.mode == 0o750
config = File('/etc/pip_check_requirements_upgrades/parameters-paste-req/wsgi_requirements.in')
assert config.is_file
assert config.user == 'root'
assert config.group == 'pipreqcheck'
assert config.mode == 0o640
assert config.content == "gunicorn\nfutures"
config = File('/etc/pip_check_requirements_upgrades/parameters-paste-req/wsgi_requirements.txt')
assert config.is_file
assert config.user == 'root'
assert config.group == 'pipreqcheck'
assert config.mode == 0o640
assert config.content == "futures==3.1.0\ngunicorn==19.7.0"
def test_gunicorn_requirements_installation_file(File, Sudo):
"""
Tests if requirements file for installing Gunicorn has been deployed
correctly.
"""
with Sudo():
requirements = File('/var/www/parameters-paste-req/.wsgi_requirements.txt')
assert requirements.is_file
assert requirements.user == 'admin-parameters-paste-req'
assert requirements.group == 'web-parameters-paste-req'
assert requirements.mode == 0o640
assert requirements.content == "futures==3.1.0\ngunicorn==19.7.0"
def test_index_page(Command):
"""
Tests if index page is served correctly. This covers:
- Basic WSGI application operation.
- Handling of environment variables.
- Handling of proxy headers.
"""
page = Command('curl -H "Accept-Encoding: plain" https://parameters-paste-req/')
assert page.rc == 0
assert "This is the WSGI application at parameters-paste-req." in page.stdout
assert "Requested URL was: https://parameters-paste-req/" in page.stdout
assert "MY_ENV_VAR: None" in page.stdout
assert "Accept-Encoding: plain" in page.stdout
def test_static_file_serving(Command):
"""
Tests serving of static files.
"""
page = Command('curl https://parameters-paste-req/static/static_file.txt')
assert page.rc == 0
assert "This is the WSGI application at parameters-paste-req." in page.stdout
assert "Requested URL was: https://parameters-paste-req/static/static_file.txt" in page.stdout
page = Command('curl https://parameters-paste-req/media/media_file.txt')
assert page.rc == 0
assert "This is the WSGI application at parameters-paste-req." in page.stdout
assert "Requested URL was: https://parameters-paste-req/media/media_file.txt" in page.stdout
|