Debian Codegraph @

tests/u/test_psutil.py

raw
1 #!/usr/bin/env python3
2 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
3 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 import json
19 import os
20
21 from psutil import Process
22
23 from cylc.flow.scripts.psutil import _psutil
24
25
26 def test_psutil_basic():
27 """It should return measurements."""
28 # obtain memory reading
29 ret = _psutil('[["virtual_memory"]]')
30
31 # we asked for one thing so we should get one response
32 assert len(ret) == 1
33
34 # the result should be dict-like and serialise to json
35 mem = ret[0]
36 dict(mem)
37 json.dumps(mem)
38
39 for key in ('total', 'available', 'used', 'free'):
40 # it should have multiple fields
41 assert key in mem
42 # all of which should be integers
43 assert isinstance(mem[key], int)
44
45
46 def test_psutil_object():
47 """It should call object methods."""
48 # obtain the commandline for this test process
49 ret = _psutil(f'[["Process.cmdline", {os.getpid()}]]')
50
51 assert ret[0] == Process().cmdline()