xref: /unit/test/test_respawn.py (revision 2073:bc6ad31ce286)
11471Szelenkov@nginx.comimport re
21477Szelenkov@nginx.comimport subprocess
31471Szelenkov@nginx.comimport time
41477Szelenkov@nginx.com
51471Szelenkov@nginx.comfrom unit.applications.lang.python import TestApplicationPython
61730Szelenkov@nginx.comfrom unit.option import option
71471Szelenkov@nginx.com
81471Szelenkov@nginx.com
91471Szelenkov@nginx.comclass TestRespawn(TestApplicationPython):
101471Szelenkov@nginx.com    prerequisites = {'modules': {'python': 'any'}}
111471Szelenkov@nginx.com
121471Szelenkov@nginx.com    PATTERN_ROUTER = 'unit: router'
131471Szelenkov@nginx.com    PATTERN_CONTROLLER = 'unit: controller'
141471Szelenkov@nginx.com
151596Szelenkov@nginx.com    def setup_method(self):
161654Szelenkov@nginx.com        self.app_name = "app-" + option.temp_dir.split('/')[-1]
171471Szelenkov@nginx.com
181471Szelenkov@nginx.com        self.load('empty', self.app_name)
191471Szelenkov@nginx.com
201596Szelenkov@nginx.com        assert 'success' in self.conf(
211596Szelenkov@nginx.com            '1', 'applications/' + self.app_name + '/processes'
221471Szelenkov@nginx.com        )
231471Szelenkov@nginx.com
241769St.nateldemoura@f5.com    def pid_by_name(self, name, ppid):
251769St.nateldemoura@f5.com        output = subprocess.check_output(['ps', 'ax', '-O', 'ppid']).decode()
261769St.nateldemoura@f5.com        m = re.search(r'\s*(\d+)\s*' + str(ppid) + r'.*' + name, output)
271769St.nateldemoura@f5.com        return None if m is None else m.group(1)
281471Szelenkov@nginx.com
291471Szelenkov@nginx.com    def kill_pids(self, *pids):
301471Szelenkov@nginx.com        subprocess.call(['kill', '-9'] + list(pids))
311471Szelenkov@nginx.com
321769St.nateldemoura@f5.com    def wait_for_process(self, process, unit_pid):
331471Szelenkov@nginx.com        for i in range(50):
341769St.nateldemoura@f5.com            found = self.pid_by_name(process, unit_pid)
351471Szelenkov@nginx.com
361471Szelenkov@nginx.com            if found is not None:
371471Szelenkov@nginx.com                break
381471Szelenkov@nginx.com
391471Szelenkov@nginx.com            time.sleep(0.1)
401471Szelenkov@nginx.com
411471Szelenkov@nginx.com        return found
421471Szelenkov@nginx.com
431769St.nateldemoura@f5.com    def find_proc(self, name, ppid, ps_output):
441769St.nateldemoura@f5.com        return re.findall(str(ppid) + r'.*' + name, ps_output)
451769St.nateldemoura@f5.com
461769St.nateldemoura@f5.com    def smoke_test(self, unit_pid):
471906Smax.romanov@nginx.com        for _ in range(10):
481906Smax.romanov@nginx.com            r = self.conf('1', 'applications/' + self.app_name + '/processes')
491906Smax.romanov@nginx.com
501906Smax.romanov@nginx.com            if 'success' in r:
511906Smax.romanov@nginx.com                break
521906Smax.romanov@nginx.com
531906Smax.romanov@nginx.com            time.sleep(0.1)
541906Smax.romanov@nginx.com
551906Smax.romanov@nginx.com        assert 'success' in r
561906Smax.romanov@nginx.com        assert self.get()['status'] == 200
571471Szelenkov@nginx.com
581471Szelenkov@nginx.com        # Check if the only one router, controller,
591471Szelenkov@nginx.com        # and application processes running.
601471Szelenkov@nginx.com
611769St.nateldemoura@f5.com        out = subprocess.check_output(['ps', 'ax', '-O', 'ppid']).decode()
621769St.nateldemoura@f5.com        assert len(self.find_proc(self.PATTERN_ROUTER, unit_pid, out)) == 1
631769St.nateldemoura@f5.com        assert len(self.find_proc(self.PATTERN_CONTROLLER, unit_pid, out)) == 1
641769St.nateldemoura@f5.com        assert len(self.find_proc(self.app_name, unit_pid, out)) == 1
651471Szelenkov@nginx.com
661844Szelenkov@nginx.com    def test_respawn_router(self, skip_alert, unit_pid, skip_fds_check):
671844Szelenkov@nginx.com        skip_fds_check(router=True)
681769St.nateldemoura@f5.com        pid = self.pid_by_name(self.PATTERN_ROUTER, unit_pid)
691471Szelenkov@nginx.com
701471Szelenkov@nginx.com        self.kill_pids(pid)
711596Szelenkov@nginx.com        skip_alert(r'process %s exited on signal 9' % pid)
721471Szelenkov@nginx.com
731769St.nateldemoura@f5.com        assert self.wait_for_process(self.PATTERN_ROUTER, unit_pid) is not None
741471Szelenkov@nginx.com
751769St.nateldemoura@f5.com        self.smoke_test(unit_pid)
761471Szelenkov@nginx.com
771844Szelenkov@nginx.com    def test_respawn_controller(self, skip_alert, unit_pid, skip_fds_check):
781844Szelenkov@nginx.com        skip_fds_check(controller=True)
791769St.nateldemoura@f5.com        pid = self.pid_by_name(self.PATTERN_CONTROLLER, unit_pid)
801471Szelenkov@nginx.com
811471Szelenkov@nginx.com        self.kill_pids(pid)
821596Szelenkov@nginx.com        skip_alert(r'process %s exited on signal 9' % pid)
831471Szelenkov@nginx.com
841848Szelenkov@nginx.com        assert (
85*2073Szelenkov@nginx.com            self.wait_for_process(self.PATTERN_CONTROLLER, unit_pid) is not None
861848Szelenkov@nginx.com        )
871471Szelenkov@nginx.com
881596Szelenkov@nginx.com        assert self.get()['status'] == 200
891471Szelenkov@nginx.com
901769St.nateldemoura@f5.com        self.smoke_test(unit_pid)
911471Szelenkov@nginx.com
921769St.nateldemoura@f5.com    def test_respawn_application(self, skip_alert, unit_pid):
931769St.nateldemoura@f5.com        pid = self.pid_by_name(self.app_name, unit_pid)
941471Szelenkov@nginx.com
951471Szelenkov@nginx.com        self.kill_pids(pid)
961596Szelenkov@nginx.com        skip_alert(r'process %s exited on signal 9' % pid)
971471Szelenkov@nginx.com
981769St.nateldemoura@f5.com        assert self.wait_for_process(self.app_name, unit_pid) is not None
991471Szelenkov@nginx.com
1001769St.nateldemoura@f5.com        self.smoke_test(unit_pid)
101