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