TestInfra - Testing Your Infrastructure
Write unit tests in Python with Testinfra to test the state of your servers configured by managements tools like Salt, Ansible, Puppet, Chef.
Today TestInfra does not support Terraform but maybe this is something to build as a custom module in our environment
Below is a very simple TestInfra script to verify if apache is running
vi apachecheck.py
def test_apache2_is_installed(Package):
apache2 = Package("apache2")
assert apache2.is_installed
assert apache2.version.startswith("2.2")
Then run with
testinfra -v apachecheck.py
==================================================== test session starts
platform linux2 -- Python 2.7.3, pytest-3.0.1, py-1.4.31, pluggy-0.3.1 -- /usr/bin/python
cachedir: .cache
rootdir: /root, inifile:
plugins: testinfra-1.4.2
collected 1 items
apachecheck.py::test_apache2_is_installed[local] PASSED
================================================== 1 passed in 0.53 seconds
Now edit and change the version number to invalid i.e. 3.2
vi apachecheck.py
testinfra -v apachecheck.py
==================================================== test session starts =====================================================
platform linux2 -- Python 2.7.3, pytest-3.0.1, py-1.4.31, pluggy-0.3.1 -- /usr/bin/python
cachedir: .cache
rootdir: /root, inifile:
plugins: testinfra-1.4.2
collected 1 items
apachecheck.py::test_apache2_is_installed[local] FAILED
========================================================== FAILURES ==========================================================
______________________________________________ test_apache2_is_installed[local] ______________________________________________
Package = <class 'testinfra.modules.base.DebianPackage'>
def test_apache2_is_installed(Package):
apache2 = Package("apache2")
assert apache2.is_installed
> assert apache2.version.startswith("3.2")
E assert False
E + where False = <built-in method startswith of unicode object at 0x2462e40>('3.2')
E + where <built-in method startswith of unicode object at 0x2462e40> = '2.2.22-1ubuntu1.11'.startswith
E + where '2.2.22-1ubuntu1.11' = <package apache2>.version
apachecheck.py:4: AssertionError
================================================== 1 failed in 0.58 seconds ==================================================
That's only a very quick example. Easy to use, quick and worth keeping a note for future reference.
Will need support for Terraform before I'd use this in production
Today TestInfra does not support Terraform but maybe this is something to build as a custom module in our environment
Below is a very simple TestInfra script to verify if apache is running
vi apachecheck.py
def test_apache2_is_installed(Package):
apache2 = Package("apache2")
assert apache2.is_installed
assert apache2.version.startswith("2.2")
Then run with
testinfra -v apachecheck.py
==================================================== test session starts
platform linux2 -- Python 2.7.3, pytest-3.0.1, py-1.4.31, pluggy-0.3.1 -- /usr/bin/python
cachedir: .cache
rootdir: /root, inifile:
plugins: testinfra-1.4.2
collected 1 items
apachecheck.py::test_apache2_is_installed[local] PASSED
================================================== 1 passed in 0.53 seconds
Now edit and change the version number to invalid i.e. 3.2
vi apachecheck.py
testinfra -v apachecheck.py
==================================================== test session starts =====================================================
platform linux2 -- Python 2.7.3, pytest-3.0.1, py-1.4.31, pluggy-0.3.1 -- /usr/bin/python
cachedir: .cache
rootdir: /root, inifile:
plugins: testinfra-1.4.2
collected 1 items
apachecheck.py::test_apache2_is_installed[local] FAILED
========================================================== FAILURES ==========================================================
______________________________________________ test_apache2_is_installed[local] ______________________________________________
Package = <class 'testinfra.modules.base.DebianPackage'>
def test_apache2_is_installed(Package):
apache2 = Package("apache2")
assert apache2.is_installed
> assert apache2.version.startswith("3.2")
E assert False
E + where False = <built-in method startswith of unicode object at 0x2462e40>('3.2')
E + where <built-in method startswith of unicode object at 0x2462e40> = '2.2.22-1ubuntu1.11'.startswith
E + where '2.2.22-1ubuntu1.11' = <package apache2>.version
apachecheck.py:4: AssertionError
================================================== 1 failed in 0.58 seconds ==================================================
That's only a very quick example. Easy to use, quick and worth keeping a note for future reference.
Will need support for Terraform before I'd use this in production
Comments
Post a Comment