xref: /unit/test/test_usr1.py (revision 2073:bc6ad31ce286)
1import os
2import signal
3
4from unit.applications.lang.python import TestApplicationPython
5from unit.log import Log
6from unit.utils import waitforfiles
7
8
9class TestUSR1(TestApplicationPython):
10    prerequisites = {'modules': {'python': 'any'}}
11
12    def test_usr1_access_log(self, temp_dir, unit_pid):
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'
21        ), 'access log configure'
22
23        assert waitforfiles(log_path), 'open'
24
25        os.rename(log_path, temp_dir + '/' + log_new)
26
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
35        os.kill(unit_pid, signal.SIGUSR1)
36
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
47    def test_usr1_unit_log(self, temp_dir, unit_pid):
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
56        Log.swap(log_new)
57
58        try:
59            body = 'body_for_a_log_new\n'
60            assert self.post(body=body)['status'] == 200
61
62            assert self.wait_for_record(body, log_new) is not None, 'rename new'
63            assert not os.path.isfile(log_path), 'rename old'
64
65            os.kill(unit_pid, signal.SIGUSR1)
66
67            assert waitforfiles(log_path), 'reopen'
68
69            body = 'body_for_a_log_unit\n'
70            assert self.post(body=body)['status'] == 200
71
72            assert self.wait_for_record(body) is not None, 'rename new'
73            assert self.search_in_log(body, log_new) is None, 'rename new 2'
74
75        finally:
76            # merge two log files into unit.log to check alerts
77
78            with open(log_path, 'r', errors='ignore') as unit_log:
79                log = unit_log.read()
80
81            with open(log_path, 'w') as unit_log, open(
82                log_path_new, 'r', errors='ignore'
83            ) as unit_log_new:
84                unit_log.write(unit_log_new.read())
85                unit_log.write(log)
86
87            Log.swap(log_new)
88