test_respawn.py (1477:b93d1acf81bd) test_respawn.py (1596:b7e2d4d92624)
1import re
2import subprocess
3import time
4
5from unit.applications.lang.python import TestApplicationPython
1import re
2import subprocess
3import time
4
5from unit.applications.lang.python import TestApplicationPython
6from conftest import skip_alert
6
7
8class TestRespawn(TestApplicationPython):
9 prerequisites = {'modules': {'python': 'any'}}
10
11 PATTERN_ROUTER = 'unit: router'
12 PATTERN_CONTROLLER = 'unit: controller'
13
7
8
9class TestRespawn(TestApplicationPython):
10 prerequisites = {'modules': {'python': 'any'}}
11
12 PATTERN_ROUTER = 'unit: router'
13 PATTERN_CONTROLLER = 'unit: controller'
14
14 def setUp(self):
15 super().setUp()
15 def setup_method(self):
16 super().setup_method()
16
17
17 self.app_name = "app-" + self.testdir.split('/')[-1]
18 self.app_name = "app-" + self.temp_dir.split('/')[-1]
18
19 self.load('empty', self.app_name)
20
19
20 self.load('empty', self.app_name)
21
21 self.assertIn(
22 'success',
23 self.conf('1', 'applications/' + self.app_name + '/processes')
22 assert 'success' in self.conf(
23 '1', 'applications/' + self.app_name + '/processes'
24 )
25
26 def pid_by_name(self, name):
27 output = subprocess.check_output(['ps', 'ax']).decode()
24 )
25
26 def pid_by_name(self, name):
27 output = subprocess.check_output(['ps', 'ax']).decode()
28 m = re.search('\s*(\d+).*' + name, output)
28 m = re.search(r'\s*(\d+).*' + name, output)
29 return m if m is None else m.group(1)
30
31 def kill_pids(self, *pids):
32 subprocess.call(['kill', '-9'] + list(pids))
33
34 def wait_for_process(self, process):
35 for i in range(50):
36 found = self.pid_by_name(process)
37
38 if found is not None:
39 break
40
41 time.sleep(0.1)
42
43 return found
44
45 def smoke_test(self):
46 for _ in range(5):
29 return m if m is None else m.group(1)
30
31 def kill_pids(self, *pids):
32 subprocess.call(['kill', '-9'] + list(pids))
33
34 def wait_for_process(self, process):
35 for i in range(50):
36 found = self.pid_by_name(process)
37
38 if found is not None:
39 break
40
41 time.sleep(0.1)
42
43 return found
44
45 def smoke_test(self):
46 for _ in range(5):
47 self.assertIn(
48 'success',
49 self.conf('1', 'applications/' + self.app_name + '/processes')
47 assert 'success' in self.conf(
48 '1', 'applications/' + self.app_name + '/processes'
50 )
49 )
51 self.assertEqual(self.get()['status'], 200)
50 assert self.get()['status'] == 200
52
53 # Check if the only one router, controller,
54 # and application processes running.
55
56 output = subprocess.check_output(['ps', 'ax']).decode()
51
52 # Check if the only one router, controller,
53 # and application processes running.
54
55 output = subprocess.check_output(['ps', 'ax']).decode()
57 self.assertEqual(len(re.findall(self.PATTERN_ROUTER, output)), 1)
58 self.assertEqual(len(re.findall(self.PATTERN_CONTROLLER, output)), 1)
59 self.assertEqual(len(re.findall(self.app_name, output)), 1)
56 assert len(re.findall(self.PATTERN_ROUTER, output)) == 1
57 assert len(re.findall(self.PATTERN_CONTROLLER, output)) == 1
58 assert len(re.findall(self.app_name, output)) == 1
60
61 def test_respawn_router(self):
62 pid = self.pid_by_name(self.PATTERN_ROUTER)
63
64 self.kill_pids(pid)
59
60 def test_respawn_router(self):
61 pid = self.pid_by_name(self.PATTERN_ROUTER)
62
63 self.kill_pids(pid)
65 self.skip_alerts.append(r'process %s exited on signal 9' % pid)
64 skip_alert(r'process %s exited on signal 9' % pid)
66
65
67 self.assertIsNotNone(self.wait_for_process(self.PATTERN_ROUTER))
66 assert self.wait_for_process(self.PATTERN_ROUTER) is not None
68
69 self.smoke_test()
70
71 def test_respawn_controller(self):
72 pid = self.pid_by_name(self.PATTERN_CONTROLLER)
73
74 self.kill_pids(pid)
67
68 self.smoke_test()
69
70 def test_respawn_controller(self):
71 pid = self.pid_by_name(self.PATTERN_CONTROLLER)
72
73 self.kill_pids(pid)
75 self.skip_alerts.append(r'process %s exited on signal 9' % pid)
74 skip_alert(r'process %s exited on signal 9' % pid)
76
75
77 self.assertIsNotNone(self.wait_for_process(self.PATTERN_CONTROLLER))
76 assert self.wait_for_process(self.PATTERN_CONTROLLER) is not None
78
77
79 self.assertEqual(self.get()['status'], 200)
78 assert self.get()['status'] == 200
80
81 self.smoke_test()
82
83 def test_respawn_application(self):
84 pid = self.pid_by_name(self.app_name)
85
86 self.kill_pids(pid)
79
80 self.smoke_test()
81
82 def test_respawn_application(self):
83 pid = self.pid_by_name(self.app_name)
84
85 self.kill_pids(pid)
87 self.skip_alerts.append(r'process %s exited on signal 9' % pid)
86 skip_alert(r'process %s exited on signal 9' % pid)
88
87
89 self.assertIsNotNone(self.wait_for_process(self.app_name))
88 assert self.wait_for_process(self.app_name) is not None
90
91 self.smoke_test()
89
90 self.smoke_test()
92
93
94if __name__ == '__main__':
95 TestRespawn.main()