xref: /unit/test/unit/status.py (revision 2187:ed0ff6e170d0)
1from unit.control import TestControl
2
3
4class Status:
5    _status = None
6    control = TestControl()
7
8    def _check_zeros():
9        assert Status.control.conf_get('/status') == {
10            'connections': {
11                'accepted': 0,
12                'active': 0,
13                'idle': 0,
14                'closed': 0,
15            },
16            'requests': {'total': 0},
17            'applications': {},
18        }
19
20    def init(status=None):
21        Status._status = (
22            status if status is not None else Status.control.conf_get('/status')
23        )
24
25    def diff():
26        def find_diffs(d1, d2):
27            if isinstance(d1, dict) and isinstance(d2, dict):
28                return {
29                    k: find_diffs(d1.get(k, 0), d2.get(k, 0))
30                    for k in d1
31                    if k in d2
32                }
33            else:
34                return d1 - d2
35
36        return find_diffs(Status.control.conf_get('/status'), Status._status)
37
38    def get(path='/'):
39        path = path.split('/')[1:]
40        diff = Status.diff()
41
42        for p in path:
43            diff = diff[p]
44
45        return diff
46