Last active
April 22, 2020 19:58
-
-
Save juanje/76d76df1a45cabf7a8e3b5cd8a622baf to your computer and use it in GitHub Desktop.
Smoke tests for a web server with TestInfra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
If you sum the times (setup + call) it seems like the final ranking (from the fastest to the slower test) would be:
test_website_5
test_website_2
test_website_1
test_website_4
test_website_3
Being the third, by far, test the slower.