Testing OpenPanel
Note: this document assumes you are running the development packages that will eventually turn into 1.1.
Preparation
For testing, you will need:
# apt-get install openpanel-mod-stub openpanel-api-python
Plus any other modules you would like to test (although right now, only Stub, Domain and PostfixCourier ship testing code).
Running tests
To test:
# PYTHONPATH=/var/openpanel/api/python/package python -m OpenPanel.test.run | grep -v DEBUG INFO:openpanel-test:Prefix=pekkqv-, Username=nosxdi, Password=LMUHZp INFO:openpanel-test:testing module Domain INFO:openpanel-test:testing module PostfixCourier INFO:openpanel-test:testing module Stub INFO:openpanel-test:all tests passed!
The test runner uses the Python logging framework to produce output; by default it outputs all levels.
If one or more tests fail, output might look more like this:
INFO:openpanel-test:Prefix=sfpelf-, Username=xtpdxd, Password=DRDNGo INFO:openpanel-test:testing module Domain INFO:openpanel-test:testing module PostfixCourier INFO:openpanel-test:testing module Stub ERROR:Stub:test stub-just-for-docs failed: this failure inserted intentionally CRITICAL:openpanel-test:1 tests failed
Writing tests
To demonstrate test writing, we are going to write one simple test for Storpel.module. This howto assumes you have a working Storpel.module (note that this requires making an /etc/init.d/storpeld that returns true).
Tests are contained in a file called tests/test.py in the module directory (/var/openpanel/modules/Storpel.module). We're going to write a simple test that verifies that creation of a Storpel causes the right file to pop up in /etc/storpels.
Storpels live inside Domains. The testing code for Domain conveniently creates an object that we can use for our tests too.
Let's write some code:
import os
# we expect a Domain to work with, so let's depend on the module
requires = ['Domain']
# the test runner will call this function, and pass some useful context to us
def test(ctx):
# make sure Storpel is even loaded
try:
ctx.conn.rpc.classinfo(classid='Storpel')
except CoreException:
ctx.fail("storpel-class-not-found", 'Storpel class not found, skipping tests')
return
# create a Storpel object and dig the UUID out of the response
storpeluuid = ctx.conn.rpc.create(
classid="Storpel",
objectid=ctx.domain,
parentid=ctx.domainuuid
)['body']['data']['objid']
ctx.logger.debug('created Storpel %s (%s)' % (ctx.domain, storpeluuid))
# check /etc/storpels
if os.path.exists("/etc/storpels/"+ctx.domain+".storpel"):
ctx.logger.debug('file in /etc/storpels created correctly')
else:
ctx.fail('storpel-no-file', 'file in /etc/storpels not created')
def cleanup(ctx):
pass
This code shows the basic concept. Use ctx.conn to talk to the OpenPanel core; use ctx.logger to log at various levels; use ctx.fail to note failing tests. The Domain tests store the domainname we can use in ctx.domain.
Output:
INFO:openpanel-test:testing module Storpel DEBUG:Storpel:created Storpel pitabp-dom1.example.com (2ca017a4-5c32-401a-ccd0-b7065c14b87f) DEBUG:Storpel:file in /etc/storpels created correctly
Improvements, left as exercises to the reader:
- check contents of the file in /etc/storpel
- delete the object when we're done with it (remember that it will go away during Domain cleanup - so this short version does not in fact leave garbage behind)
- ..