Skip to content

Commit

Permalink
Prepare tests structure
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainb committed Apr 24, 2013
1 parent 877509c commit 3f86e88
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[run]
include =
*src/supervision/website/*.py

[report]
# Regexes for lines to exclude from consideration
exclude_lines =
Expand Down
2 changes: 0 additions & 2 deletions src/supervision/website/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@


51 changes: 51 additions & 0 deletions src/supervision/website/tests/emailutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
""" Monkey-patch smtplib so we don't send actual emails.
Thanks to http://www.psychicorigami.com/2007/09/20/monkey-patching-pythons-smtp-lib-for-unit-testing/
Importing this module and smtplib.SMTP will refer to the DummySMTP bellow.
It provides access to any sent messages via emailutils.inbox and the last
SMTP class created via emailutils.smtp.
Example :
import emailutils
def test_some_email():
emailutils.inbox = [] # clear the inbox
# code that does some emailing here
assert len(emailutils.inbox) == 1 # check one email was sent
assert emailutils.inbox[0].to_address == '[email protected]'
"""

smtp = None
inbox = []

class Message(object):

def __init__(self, from_address, to_address, fullmessage):
self.from_address = from_address
self.to_address = to_address
self.fullmessage = fullmessage

class DummySMTP(object):

def __init__(self, address):
self.address = address
global smtp
smtp = self

def login(self, username, password):
self.username = username
self.password = password

def sendmail(self, from_address, to_address, fullmessage):
global inbox
inbox.append(Message(from_address, to_address, fullmessage))
return []

def quit(self):
self.has_quit = True

# this is the actual monkey patch (simply replacing one class with another)
import smtplib
smtplib.SMTP = DummySMTP
104 changes: 104 additions & 0 deletions src/supervision/website/tests/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# http://docs.python.org/2/library/unittest.html

import unittest

import emailutils
from supervision.website import config

# TODO
# monkey patching pythons smtp lib
# http://www.psychicorigami.com/2007/09/20/monkey-patching-pythons-smtp-lib-for-unit-testing/

class TestSetupConfig(unittest.TestCase):

def setUp(self):
# Adapt the module configuration for our tests
config.TIMEOUT = 1000
# etc......

class TestGenerateCheckHostShScript(TestSetupConfig):
# sh script generation
# check all is ok
pass

class TestCheckHostShScript(TestSetupConfig):
# extend setup to delete the reports folder and his content
# sh script generation
# launch sh script to initialize folder structure
# check : the folder exists
# check : sitemonitor.current.status exists and is not empty
# check : sitemonitor.previous.status exists and is empty
# launch sh script
# check : sitemonitor.current.status exists and is not empty
# check : sitemonitor.previous.status exists and is not empty
# check : sitemonitor.previous.status = initial sitemonitor.current.status
pass

class TestReadStatus(TestSetupConfig):
# extend setup to delete the reports folder and his content
# sh script generation
# launch sh script to initialize folder structure

# set custom sitemonitor.current.status & sitemonitor.previous.status
# with all possible configurations : SLOW_THRESHOLD, OK_STATUSES_PER_HOST, ...
# verifier read_status(status_type='current')
# verifier read_status(status_type='previous')

pass

class TestGenerateReports(TestSetupConfig):
# ! test only complete report lines

# extend setup to delete the reports folder and his content
# sh script generation
# launch sh script to initialize folder structure

# set custom sitemonitor.current.status
# generate reports
# check reports txt/html

# set custom sitemonitor.current.status & sitemonitor.previous.status
# generate reports
# check reports txt/html

pass

class TestSendMail(TestSetupConfig):
# extend setup to delete the reports folder and his content
# sh script generation
# launch sh script to initialize folder structure

# set same custom sitemonitor.current.status & sitemonitor.previous.status
# MAIL_SEND = False, MAIL_SEND_ONLY_ON_CHANGE=False
# generate reports
# no mail
# MAIL_SEND = False, MAIL_SEND_ONLY_ON_CHANGE=True
# generate reports
# no mail
# MAIL_SEND = True, MAIL_SEND_ONLY_ON_CHANGE=False
# generate reports
# mail
# MAIL_SEND = True, MAIL_SEND_ONLY_ON_CHANGE=True
# generate reports
# no mail

# set different sitemonitor.current.status & sitemonitor.previous.status
# MAIL_SEND = False, MAIL_SEND_ONLY_ON_CHANGE=False
# generate reports
# no mail
# MAIL_SEND = False, MAIL_SEND_ONLY_ON_CHANGE=True
# generate reports
# no mail
# MAIL_SEND = True, MAIL_SEND_ONLY_ON_CHANGE=False
# generate reports
# mail
# MAIL_SEND = True, MAIL_SEND_ONLY_ON_CHANGE=True
# generate reports
# mail

# test from/to... on the last message

pass

if __name__ == '__main__':
unittest.main()
3 changes: 3 additions & 0 deletions src/supervision/website/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# needed to allow coverage for the python script
import generate_reports
import generate_check_host_sh_script

0 comments on commit 3f86e88

Please sign in to comment.