1import os
2from subprocess import call
3
4from unit.applications.lang.python import TestApplicationPython
5from conftest import waitforfiles
6
7
8class TestUSR1(TestApplicationPython):
9 prerequisites = {'modules': {'python': 'any'}}
10
11 def test_usr1_access_log(self):
12 self.load('empty')
13
14 log = 'access.log'
15 log_new = 'new.log'
15 log_path = self.testdir + '/' + log
16 log_path = self.temp_dir + '/' + log
17
17 self.assertIn(
18 'success',
19 self.conf('"' + log_path + '"', 'access_log'),
20 'access log configure',
21 )
18 assert 'success' in self.conf(
19 '"' + log_path + '"', 'access_log'
20 ), 'access log configure'
21
23 self.assertTrue(self.waitforfiles(log_path), 'open')
22 assert waitforfiles(log_path), 'open'
23
25 os.rename(log_path, self.testdir + '/' + log_new)
24 os.rename(log_path, self.temp_dir + '/' + log_new)
25
27 self.assertEqual(self.get()['status'], 200)
26 assert self.get()['status'] == 200
27
29 self.assertIsNotNone(
30 self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "-" "-"', log_new),
31 'rename new',
32 )
33 self.assertFalse(os.path.isfile(log_path), 'rename old')
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
35 with open(self.testdir + '/unit.pid', 'r') as f:
34 with open(self.temp_dir + '/unit.pid', 'r') as f:
35 pid = f.read().rstrip()
36
37 call(['kill', '-s', 'USR1', pid])
38
40 self.assertTrue(self.waitforfiles(log_path), 'reopen')
39 assert waitforfiles(log_path), 'reopen'
40
42 self.assertEqual(self.get(url='/usr1')['status'], 200)
41 assert self.get(url='/usr1')['status'] == 200
42
43 self.stop()
44
46 self.assertIsNotNone(
47 self.wait_for_record(r'"GET /usr1 HTTP/1.1" 200 0 "-" "-"', log),
48 'reopen 2',
49 )
50 self.assertIsNone(
51 self.search_in_log(r'/usr1', log_new), 'rename new 2'
52 )
45 assert (
46 self.wait_for_record(r'"GET /usr1 HTTP/1.1" 200 0 "-" "-"', log)
47 is not None
48 ), 'reopen 2'
49 assert self.search_in_log(r'/usr1', log_new) is None, 'rename new 2'
50
51 def test_usr1_unit_log(self):
52 self.load('log_body')
53
54 log_new = 'new.log'
58 log_path = self.testdir + '/unit.log'
59 log_path_new = self.testdir + '/' + log_new
55 log_path = self.temp_dir + '/unit.log'
56 log_path_new = self.temp_dir + '/' + log_new
57
58 os.rename(log_path, log_path_new)
59
60 body = 'body_for_a_log_new'
64 self.assertEqual(self.post(body=body)['status'], 200)
61 assert self.post(body=body)['status'] == 200
62
66 self.assertIsNotNone(
67 self.wait_for_record(body, log_new), 'rename new'
68 )
69 self.assertFalse(os.path.isfile(log_path), 'rename old')
63 assert self.wait_for_record(body, log_new) is not None, 'rename new'
64 assert not os.path.isfile(log_path), 'rename old'
65
71 with open(self.testdir + '/unit.pid', 'r') as f:
66 with open(self.temp_dir + '/unit.pid', 'r') as f:
67 pid = f.read().rstrip()
68
69 call(['kill', '-s', 'USR1', pid])
70
76 self.assertTrue(self.waitforfiles(log_path), 'reopen')
71 assert waitforfiles(log_path), 'reopen'
72
73 body = 'body_for_a_log_unit'
79 self.assertEqual(self.post(body=body)['status'], 200)
74 assert self.post(body=body)['status'] == 200
75
76 self.stop()
77
83 self.assertIsNotNone(self.wait_for_record(body), 'rename new')
84 self.assertIsNone(self.search_in_log(body, log_new), 'rename new 2')
78 assert self.wait_for_record(body) is not None, 'rename new'
79 assert self.search_in_log(body, log_new) is None, 'rename new 2'
80
81 # merge two log files into unit.log to check alerts
82
88 with open(log_path, 'w') as unit_log, \
89 open(log_path_new, 'r') as unit_log_new:
83 with open(log_path, 'w') as unit_log, open(
84 log_path_new, 'r'
85 ) as unit_log_new:
86 unit_log.write(unit_log_new.read())
91
92
93if __name__ == '__main__':
94 TestUSR1.main()