Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
welelay committed Feb 4, 2016
1 parent 1a14069 commit 5c31d60
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions superlance/tests/crashmail_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from superlance.compat import StringIO
import re

class CrashMailTests(unittest.TestCase):
def _getTargetClass(self):
Expand Down Expand Up @@ -29,7 +30,7 @@ def _makeOnePopulated(self, programs, any, response=None):
return prog

def test_runforever_not_process_state_exited(self):
programs = {'foo':0, 'bar':0, 'baz_01':0 }
programs = []
any = None
prog = self._makeOnePopulated(programs, any)
prog.stdin.write('eventname:PROCESS_STATE len:0\n')
Expand All @@ -38,7 +39,7 @@ def test_runforever_not_process_state_exited(self):
self.assertEqual(prog.stderr.getvalue(), 'non-exited event\n')

def test_runforever_expected_exit(self):
programs = ['foo']
programs = [re.compile('foo')]
any = None
prog = self._makeOnePopulated(programs, any)
payload=('expected:1 processname:foo groupname:bar '
Expand All @@ -51,7 +52,7 @@ def test_runforever_expected_exit(self):
self.assertEqual(prog.stderr.getvalue(), 'expected exit\n')

def test_runforever_unexpected_exit(self):
programs = ['foo']
programs = [re.compile('bar:foo')]
any = None
prog = self._makeOnePopulated(programs, any)
payload=('expected:0 processname:foo groupname:bar '
Expand All @@ -63,20 +64,53 @@ def test_runforever_unexpected_exit(self):
prog.runforever(test=True)
output = prog.stderr.getvalue()
lines = output.split('\n')
self.assertEqual(lines[0], 'unexpected exit, mailing')
self.assertEqual(lines[1], 'Mailed:')
self.assertEqual(lines[2], '')
self.assertEqual(lines[3], 'To: [email protected]')
self.assertTrue('Subject: [foo]: foo crashed at' in lines[4])
self.assertEqual(lines[5], '')

self.assertEqual(lines[1], 'unexpected exit, mailing')
self.assertEqual(lines[2], 'Mailed:')
self.assertEqual(lines[3], '')
self.assertEqual(lines[4], 'To: [email protected]')
self.assertTrue('Subject: [foo]: foo crashed at' in lines[5])
self.assertEqual(lines[6], '')
self.assertTrue(
'Process foo in group bar exited unexpectedly' in lines[6])
'Process foo in group bar exited unexpectedly' in lines[7])
import os
f = open(os.path.join(self.tempdir, 'email.log'), 'r')
mail = f.read()
f.close()
self.assertTrue(
'Process foo in group bar exited unexpectedly' in mail)

def test_runforever_unexpected_exit_group(self):
programs = [re.compile('bar:*')]
any = None
prog = self._makeOnePopulated(programs, any)
payload=('expected:0 processname:foo groupname:bar '
'from_state:RUNNING pid:1')
prog.stdin.write(
'eventname:PROCESS_STATE_EXITED len:%s\n' % len(payload))
prog.stdin.write(payload)
prog.stdin.seek(0)
prog.runforever(test=True)
output = prog.stderr.getvalue()
lines = output.split('\n')

self.assertEqual(lines[1], 'unexpected exit, mailing')

def test_runforever_unexpected_exit_ignored(self):
programs = [re.compile('notfoo')]
any = None
prog = self._makeOnePopulated(programs, any)
payload=('expected:0 processname:foo groupname:bar '
'from_state:RUNNING pid:1')
prog.stdin.write(
'eventname:PROCESS_STATE_EXITED len:%s\n' % len(payload))
prog.stdin.write(payload)
prog.stdin.seek(0)
prog.runforever(test=True)
output = prog.stderr.getvalue()
lines = output.split('\n')

self.assertTrue('ignoring [foo]: foo crashed ' in lines[1])

if __name__ == '__main__':
unittest.main()

0 comments on commit 5c31d60

Please sign in to comment.