forked from dimier/python-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_daemon.py
103 lines (81 loc) · 2.71 KB
/
test_daemon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
import os
import sys
import time
import unittest
import test.settings as s
from daemon import Daemon
from daemon.parent_logger import setup_logging
if s.DEBUG:
debug = True
class TDaemon(Daemon):
def __init__(self, *args, **kwargs):
super(TDaemon, self).__init__(*args, **kwargs)
testoutput = open('testing_daemon', 'w')
testoutput.write('inited')
testoutput.close()
def cleanup(self):
testoutput = open('testing_daemon', 'w')
testoutput.write('cleanup')
testoutput.close()
def run(self):
time.sleep(0.4)
testoutput = open('testing_daemon', 'w')
testoutput.write('finished')
testoutput.close()
def control_daemon(action):
os.system(" ".join((sys.executable, __file__, action)))
class TestDaemon(unittest.TestCase):
testoutput = None
def setUp(self):
control_daemon('start')
time.sleep(0.1)
self.testoutput = open('testing_daemon')
def test_daemon_can_start(self):
assert os.path.exists(s.PIDFILE)
assert self.testoutput.read() == 'inited'
def test_daemon_can_stop(self):
control_daemon('stop')
time.sleep(0.11)
assert os.path.exists(s.PIDFILE) is False
assert self.testoutput.read() == 'cleanup'
def test_daemon_can_finish(self):
time.sleep(0.6)
assert os.path.exists(s.PIDFILE) is False
assert self.testoutput.read() == 'finished'
def test_daemon_can_restart(self):
assert os.path.exists(s.PIDFILE)
pidfile = open(s.PIDFILE)
pid1 = pidfile.read()
pidfile.close()
control_daemon('restart')
time.sleep(0.1)
assert os.path.exists(s.PIDFILE)
pidfile = open(s.PIDFILE)
pid2 = pidfile.read()
pidfile.close()
assert pid1 != pid2
# unittest+pytest makes these difficult to get output for assertions
# although correct output can be seen with --capture=no
def test_daemon_status_false(self):
control_daemon('stop')
time.sleep(0.1)
control_daemon('status')
def test_daemon_status_true(self):
assert os.path.exists(s.PIDFILE)
control_daemon('status')
def tearDown(self):
self.testoutput.close()
if os.path.exists(s.PIDFILE):
control_daemon('stop')
time.sleep(0.05)
os.system('rm testing_daemon*')
if __name__ == '__main__':
setup_logging(debug, s.LOGFILE)
if len(sys.argv) == 1:
unittest.main()
elif len(sys.argv) == 2:
arg = sys.argv[1]
if arg in ('start', 'stop', 'restart', 'status'):
d = TDaemon(s.PIDFILE, verbose=1, use_cleanup=True)
getattr(d, arg)()