test_usr1.py (1770:e4ece1ff4413) test_usr1.py (1850:839024ce4a6a)
1import os
1import os
2from subprocess import call
2import signal
3
4from unit.applications.lang.python import TestApplicationPython
3
4from unit.applications.lang.python import TestApplicationPython
5from unit.log import Log
5from unit.utils import waitforfiles
6
7
8class TestUSR1(TestApplicationPython):
9 prerequisites = {'modules': {'python': 'any'}}
10
6from unit.utils import waitforfiles
7
8
9class TestUSR1(TestApplicationPython):
10 prerequisites = {'modules': {'python': 'any'}}
11
11 def test_usr1_access_log(self, temp_dir):
12 def test_usr1_access_log(self, temp_dir, unit_pid):
12 self.load('empty')
13
14 log = 'access.log'
15 log_new = 'new.log'
16 log_path = temp_dir + '/' + log
17
18 assert 'success' in self.conf(
19 '"' + log_path + '"', 'access_log'

--- 6 unchanged lines hidden (view full) ---

26 assert self.get()['status'] == 200
27
28 assert (
29 self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "-" "-"', log_new)
30 is not None
31 ), 'rename new'
32 assert not os.path.isfile(log_path), 'rename old'
33
13 self.load('empty')
14
15 log = 'access.log'
16 log_new = 'new.log'
17 log_path = temp_dir + '/' + log
18
19 assert 'success' in self.conf(
20 '"' + log_path + '"', 'access_log'

--- 6 unchanged lines hidden (view full) ---

27 assert self.get()['status'] == 200
28
29 assert (
30 self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "-" "-"', log_new)
31 is not None
32 ), 'rename new'
33 assert not os.path.isfile(log_path), 'rename old'
34
34 with open(temp_dir + '/unit.pid', 'r') as f:
35 pid = f.read().rstrip()
35 os.kill(unit_pid, signal.SIGUSR1)
36
36
37 call(['kill', '-s', 'USR1', pid])
38
39 assert waitforfiles(log_path), 'reopen'
40
41 assert self.get(url='/usr1')['status'] == 200
42
43 assert (
44 self.wait_for_record(r'"GET /usr1 HTTP/1.1" 200 0 "-" "-"', log)
45 is not None
46 ), 'reopen 2'
47 assert self.search_in_log(r'/usr1', log_new) is None, 'rename new 2'
48
37 assert waitforfiles(log_path), 'reopen'
38
39 assert self.get(url='/usr1')['status'] == 200
40
41 assert (
42 self.wait_for_record(r'"GET /usr1 HTTP/1.1" 200 0 "-" "-"', log)
43 is not None
44 ), 'reopen 2'
45 assert self.search_in_log(r'/usr1', log_new) is None, 'rename new 2'
46
49 def test_usr1_unit_log(self, temp_dir):
47 def test_usr1_unit_log(self, temp_dir, unit_pid):
50 self.load('log_body')
51
52 log_new = 'new.log'
53 log_path = temp_dir + '/unit.log'
54 log_path_new = temp_dir + '/' + log_new
55
56 os.rename(log_path, log_path_new)
57
48 self.load('log_body')
49
50 log_new = 'new.log'
51 log_path = temp_dir + '/unit.log'
52 log_path_new = temp_dir + '/' + log_new
53
54 os.rename(log_path, log_path_new)
55
58 body = 'body_for_a_log_new'
59 assert self.post(body=body)['status'] == 200
56 Log.swap(log_new)
60
57
61 assert self.wait_for_record(body, log_new) is not None, 'rename new'
62 assert not os.path.isfile(log_path), 'rename old'
58 try:
59 body = 'body_for_a_log_new\n'
60 assert self.post(body=body)['status'] == 200
63
61
64 with open(temp_dir + '/unit.pid', 'r') as f:
65 pid = f.read().rstrip()
62 assert (
63 self.wait_for_record(body, log_new) is not None
64 ), 'rename new'
65 assert not os.path.isfile(log_path), 'rename old'
66
66
67 call(['kill', '-s', 'USR1', pid])
67 os.kill(unit_pid, signal.SIGUSR1)
68
68
69 assert waitforfiles(log_path), 'reopen'
69 assert waitforfiles(log_path), 'reopen'
70
70
71 body = 'body_for_a_log_unit'
72 assert self.post(body=body)['status'] == 200
71 body = 'body_for_a_log_unit\n'
72 assert self.post(body=body)['status'] == 200
73
73
74 assert self.wait_for_record(body) is not None, 'rename new'
75 assert self.search_in_log(body, log_new) is None, 'rename new 2'
74 assert self.wait_for_record(body) is not None, 'rename new'
75 assert self.search_in_log(body, log_new) is None, 'rename new 2'
76
76
77 # merge two log files into unit.log to check alerts
77 finally:
78 # merge two log files into unit.log to check alerts
78
79
79 with open(log_path, 'w') as unit_log, open(
80 log_path_new, 'r'
81 ) as unit_log_new:
82 unit_log.write(unit_log_new.read())
80 with open(log_path, 'r', errors='ignore') as unit_log:
81 log = unit_log.read()
82
83 with open(log_path, 'w') as unit_log, open(
84 log_path_new, 'r', errors='ignore'
85 ) as unit_log_new:
86 unit_log.write(unit_log_new.read())
87 unit_log.write(log)
88
89 Log.swap(log_new)