summaryrefslogtreecommitdiffhomepage
path: root/test/test_respawn.py
blob: 2d01cf3bae811108b802b656e7d4290f1d2ca24e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import re
import subprocess
import time

import pytest
from unit.applications.lang.python import TestApplicationPython


class TestRespawn(TestApplicationPython):
    prerequisites = {'modules': {'python': 'any'}}

    PATTERN_ROUTER = 'unit: router'
    PATTERN_CONTROLLER = 'unit: controller'

    @pytest.fixture(autouse=True)
    def setup_method_fixture(self, temp_dir):
        self.app_name = f'app-{temp_dir.split("/")[-1]}'

        self.load('empty', self.app_name)

        assert 'success' in self.conf(
            '1', f'applications/{self.app_name}/processes'
        )

    def pid_by_name(self, name, ppid):
        output = subprocess.check_output(['ps', 'ax', '-O', 'ppid']).decode()
        m = re.search(fr'\s*(\d+)\s*{ppid}.*{name}', output)
        return None if m is None else m.group(1)

    def kill_pids(self, *pids):
        subprocess.call(['kill', '-9', *pids])

    def wait_for_process(self, process, unit_pid):
        for _ in range(50):
            found = self.pid_by_name(process, unit_pid)

            if found is not None:
                break

            time.sleep(0.1)

        return found

    def find_proc(self, name, ppid, ps_output):
        return re.findall(fr'{ppid}.*{name}', ps_output)

    def smoke_test(self, unit_pid):
        for _ in range(10):
            r = self.conf('1', f'applications/{self.app_name}/processes')

            if 'success' in r:
                break

            time.sleep(0.1)

        assert 'success' in r
        assert self.get()['status'] == 200

        # Check if the only one router, controller,
        # and application processes running.

        out = subprocess.check_output(['ps', 'ax', '-O', 'ppid']).decode()
        assert len(self.find_proc(self.PATTERN_ROUTER, unit_pid, out)) == 1
        assert len(self.find_proc(self.PATTERN_CONTROLLER, unit_pid, out)) == 1
        assert len(self.find_proc(self.app_name, unit_pid, out)) == 1

    def test_respawn_router(self, skip_alert, unit_pid, skip_fds_check):
        skip_fds_check(router=True)
        pid = self.pid_by_name(self.PATTERN_ROUTER, unit_pid)

        self.kill_pids(pid)
        skip_alert(fr'process {pid} exited on signal 9')

        assert self.wait_for_process(self.PATTERN_ROUTER, unit_pid) is not None

        self.smoke_test(unit_pid)

    def test_respawn_controller(self, skip_alert, unit_pid, skip_fds_check):
        skip_fds_check(controller=True)
        pid = self.pid_by_name(self.PATTERN_CONTROLLER, unit_pid)

        self.kill_pids(pid)
        skip_alert(fr'process {pid} exited on signal 9')

        assert (
            self.wait_for_process(self.PATTERN_CONTROLLER, unit_pid) is not None
        )

        assert self.get()['status'] == 200

        self.smoke_test(unit_pid)

    def test_respawn_application(self, skip_alert, unit_pid):
        pid = self.pid_by_name(self.app_name, unit_pid)

        self.kill_pids(pid)
        skip_alert(fr'process {pid} exited on signal 9')

        assert self.wait_for_process(self.app_name, unit_pid) is not None

        self.smoke_test(unit_pid)