Skip to content

Instantly share code, notes, and snippets.

@juanje
Last active Apr 22, 2020
Embed
What would you like to do?
Smoke tests for a web server with TestInfra
import requests
def test_website_1(host):
"""Check if the website is recheable from outside the managed host,
using the standar Python's library Requests.
It also check for the website content.
"""
host_ip = host.interface("eth0").addresses[0]
result = requests.get(f'http://{host_ip}')
assert result.status_code == 200
assert 'Hello world!' in result.text
def test_website_2(host):
"""Check if the website is recheable from the managed host,
using Testinfra's module Addr that check for remote addresses and ports.
With this method you can't check for the website content.
"""
localhost = host.addr('localhost')
web_port = 80
assert localhost.port(web_port).is_reachable
def test_website_3(host):
"""Check if the website is recheable from inside the managed host,
using Ansible.
With this method you can't check for the website content.
"""
response = host.ansible('uri', 'url=http://localhost', check=False)
assert response['status'] == 200
assert 'OK' in response['msg']
def test_website_4(host):
"""Check if the web server is running and is serving the right content.
Also, it checks if the right port is listening.
"""
web_port = 80
webserver = host.service('nginx')
local_port = host.socket(f'tcp://0.0.0.0:{web_port}')
index_html = host.file('/var/www/html/index.nginx-debian.html')
assert webserver.is_running
assert local_port.is_listening
assert index_html.exists
assert 'Hello world!' in index_html.content_string
def test_website_5(host):
"""Check if the website is recheable from inside the managed host,
using 'curl'.
You need to have the command curl installed at that host to be able
to run this tests.
"""
result = host.check_output('curl -s http://localhost')
assert 'Hello world!' in result
#
@juanje
Copy link
Author

juanje commented Apr 13, 2020

I wrote this for using it with molecule to tests Ansible plays and roles, but it can be used to smoke test any web-like installation.
I didn't find any good example to do it, so I wrote this one. It's far from perfect, but it's a start to do more advance stuff.

There you can see 4 different methods for checking if a specific contend has been deployed and is being served through a web server. All have their uses, pros and cons. As additional info, I did some basic benchmarking and this was the result:

3.13s call     test_website_3
2.18s call     test_website_4
0.94s call     test_website_1
0.48s call     test_website_2
0.26s call     test_website_5

0.67s setup    test_website_3
0.61s setup    test_website_2
0.57s setup    test_website_5
0.55s setup    test_website_4
0.53s setup    test_website_1

If you sum the times (setup + call) it seems like the final ranking (from the fastest to the slower test) would be:

  1. test_website_5
  2. test_website_2
  3. test_website_1
  4. test_website_4
  5. test_website_3

Being the third, by far, test the slower.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment