xref: /unit/test/test_python_procman.py (revision 1477:b93d1acf81bd)
1import re
2import subprocess
3import time
4import unittest
5
6from unit.applications.lang.python import TestApplicationPython
7
8
9class TestPythonProcman(TestApplicationPython):
10    prerequisites = {'modules': {'python': 'any'}}
11
12    def setUp(self):
13        super().setUp()
14
15        self.app_name = "app-" + self.testdir.split('/')[-1]
16        self.app_proc = 'applications/' + self.app_name + '/processes'
17        self.load('empty', self.app_name)
18
19    def pids_for_process(self):
20        time.sleep(0.2)
21
22        output = subprocess.check_output(['ps', 'ax'])
23
24        pids = set()
25        for m in re.findall('.*' + self.app_name, output.decode()):
26            pids.add(re.search('^\s*(\d+)', m).group(1))
27
28        return pids
29
30    def conf_proc(self, conf, path=None):
31        if path is None:
32            path = self.app_proc
33
34        self.assertIn('success', self.conf(conf, path), 'configure processes')
35
36    def test_python_processes_idle_timeout_zero(self):
37        self.conf_proc({"spare": 0, "max": 2, "idle_timeout": 0})
38
39        self.get()
40        self.assertEqual(len(self.pids_for_process()), 0, 'idle timeout 0')
41
42    def test_python_prefork(self):
43        self.conf_proc('2')
44
45        pids = self.pids_for_process()
46        self.assertEqual(len(pids), 2, 'prefork 2')
47
48        self.get()
49        self.assertSetEqual(self.pids_for_process(), pids, 'prefork still 2')
50
51        self.conf_proc('4')
52
53        pids = self.pids_for_process()
54        self.assertEqual(len(pids), 4, 'prefork 4')
55
56        self.get()
57        self.assertSetEqual(self.pids_for_process(), pids, 'prefork still 4')
58
59        self.stop_all()
60
61    @unittest.skip('not yet')
62    def test_python_prefork_same_processes(self):
63        self.conf_proc('2')
64        pids = self.pids_for_process()
65
66        self.conf_proc('4')
67        pids_new = self.pids_for_process()
68
69        self.assertTrue(pids.issubset(pids_new), 'prefork same processes')
70
71    def test_python_ondemand(self):
72        self.conf_proc({"spare": 0, "max": 8, "idle_timeout": 1})
73
74        self.assertEqual(len(self.pids_for_process()), 0, 'on-demand 0')
75
76        self.get()
77        pids = self.pids_for_process()
78        self.assertEqual(len(pids), 1, 'on-demand 1')
79
80        self.get()
81        self.assertSetEqual(self.pids_for_process(), pids, 'on-demand still 1')
82
83        time.sleep(1)
84
85        self.assertEqual(
86            len(self.pids_for_process()), 0, 'on-demand stop idle'
87        )
88
89        self.stop_all()
90
91    def test_python_scale_updown(self):
92        self.conf_proc({"spare": 2, "max": 8, "idle_timeout": 1})
93
94        pids = self.pids_for_process()
95        self.assertEqual(len(pids), 2, 'updown 2')
96
97        self.get()
98        pids_new = self.pids_for_process()
99        self.assertEqual(len(pids_new), 3, 'updown 3')
100        self.assertTrue(pids.issubset(pids_new), 'updown 3 only 1 new')
101
102        self.get()
103        self.assertSetEqual(
104            self.pids_for_process(), pids_new, 'updown still 3'
105        )
106
107        time.sleep(1)
108
109        pids = self.pids_for_process()
110        self.assertEqual(len(pids), 2, 'updown stop idle')
111
112        self.get()
113        pids_new = self.pids_for_process()
114        self.assertEqual(len(pids_new), 3, 'updown again 3')
115        self.assertTrue(pids.issubset(pids_new), 'updown again 3 only 1 new')
116
117        self.stop_all()
118
119    def test_python_reconfigure(self):
120        self.conf_proc({"spare": 2, "max": 6, "idle_timeout": 1})
121
122        pids = self.pids_for_process()
123        self.assertEqual(len(pids), 2, 'reconf 2')
124
125        self.get()
126        pids_new = self.pids_for_process()
127        self.assertEqual(len(pids_new), 3, 'reconf 3')
128        self.assertTrue(pids.issubset(pids_new), 'reconf 3 only 1 new')
129
130        self.conf_proc('6', self.app_proc + '/spare')
131
132        pids = self.pids_for_process()
133        self.assertEqual(len(pids), 6, 'reconf 6')
134
135        self.get()
136        self.assertSetEqual(self.pids_for_process(), pids, 'reconf still 6')
137
138        self.stop_all()
139
140    def test_python_idle_timeout(self):
141        self.conf_proc({"spare": 0, "max": 6, "idle_timeout": 2})
142
143        self.get()
144        pids = self.pids_for_process()
145        self.assertEqual(len(pids), 1, 'idle timeout 1')
146
147        time.sleep(1)
148
149        self.get()
150
151        time.sleep(1)
152
153        pids_new = self.pids_for_process()
154        self.assertEqual(len(pids_new), 1, 'idle timeout still 1')
155        self.assertSetEqual(
156            self.pids_for_process(), pids, 'idle timeout still 1 same pid'
157        )
158
159        time.sleep(1)
160
161        self.assertEqual(len(self.pids_for_process()), 0, 'idle timed out')
162
163    def test_python_processes_connection_keepalive(self):
164        self.conf_proc({"spare": 0, "max": 6, "idle_timeout": 2})
165
166        (resp, sock) = self.get(
167            headers={'Host': 'localhost', 'Connection': 'keep-alive'},
168            start=True,
169            read_timeout=1,
170        )
171        self.assertEqual(
172            len(self.pids_for_process()), 1, 'keepalive connection 1'
173        )
174
175        time.sleep(2)
176
177        self.assertEqual(
178            len(self.pids_for_process()), 0, 'keepalive connection 0'
179        )
180
181        sock.close()
182
183    def test_python_processes_access(self):
184        self.conf_proc('1')
185
186        path = '/' + self.app_proc
187        self.assertIn('error', self.conf_get(path + '/max'))
188        self.assertIn('error', self.conf_get(path + '/spare'))
189        self.assertIn('error', self.conf_get(path + '/idle_timeout'))
190
191    def test_python_processes_invalid(self):
192        self.assertIn(
193            'error', self.conf({"spare": -1}, self.app_proc), 'negative spare',
194        )
195        self.assertIn(
196            'error', self.conf({"max": -1}, self.app_proc), 'negative max',
197        )
198        self.assertIn(
199            'error',
200            self.conf({"idle_timeout": -1}, self.app_proc),
201            'negative idle_timeout',
202        )
203        self.assertIn(
204            'error',
205            self.conf({"spare": 2}, self.app_proc),
206            'spare gt max default',
207        )
208        self.assertIn(
209            'error',
210            self.conf({"spare": 2, "max": 1}, self.app_proc),
211            'spare gt max',
212        )
213        self.assertIn(
214            'error',
215            self.conf({"spare": 0, "max": 0}, self.app_proc),
216            'max zero',
217        )
218
219    def stop_all(self):
220        self.conf({"listeners": {}, "applications": {}})
221
222        self.assertEqual(len(self.pids_for_process()), 0, 'stop all')
223
224
225if __name__ == '__main__':
226    TestPythonProcman.main()
227