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