1 | #!/usr/bin/env python |
||
2 | # |
||
3 | # Glances - An eye on your system |
||
4 | # |
||
5 | # SPDX-FileCopyrightText: 2022 Nicolas Hennion <[email protected]> |
||
6 | # |
||
7 | # SPDX-License-Identifier: LGPL-3.0-only |
||
8 | # |
||
9 | |||
10 | """Glances unitary tests suite for the XML-RPC API.""" |
||
11 | |||
12 | import json |
||
13 | import os |
||
14 | import shlex |
||
15 | import subprocess |
||
16 | import time |
||
17 | import unittest |
||
18 | |||
19 | from glances import __version__ |
||
20 | from glances.globals import ServerProxy |
||
21 | |||
22 | SERVER_PORT = 61234 |
||
23 | URL = f"http://localhost:{SERVER_PORT}" |
||
24 | pid = None |
||
25 | |||
26 | # Init the XML-RPC client |
||
27 | client = ServerProxy(URL) |
||
28 | |||
29 | # Unitest class |
||
30 | # ============== |
||
31 | print(f'XML-RPC API unitary tests for Glances {__version__}') |
||
32 | |||
33 | |||
34 | class TestGlances(unittest.TestCase): |
||
35 | """Test Glances class.""" |
||
36 | |||
37 | def setUp(self): |
||
38 | """The function is called *every time* before test_*.""" |
||
39 | print('\n' + '=' * 78) |
||
40 | |||
41 | View Code Duplication | def test_000_start_server(self): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
42 | """Start the Glances Web Server.""" |
||
43 | global pid |
||
44 | |||
45 | print('INFO: [TEST_000] Start the Glances Web Server') |
||
46 | if os.path.isfile('./venv/bin/python'): |
||
47 | cmdline = "./venv/bin/python" |
||
48 | else: |
||
49 | cmdline = "python" |
||
50 | cmdline += f" -m glances -B localhost -s -p {SERVER_PORT}" |
||
51 | print(f"Run the Glances Server on port {SERVER_PORT}") |
||
52 | args = shlex.split(cmdline) |
||
53 | pid = subprocess.Popen(args) |
||
54 | print("Please wait...") |
||
55 | time.sleep(1) |
||
56 | |||
57 | self.assertTrue(pid is not None) |
||
58 | |||
59 | def test_001_all(self): |
||
60 | """All.""" |
||
61 | method = "getAll()" |
||
62 | print('INFO: [TEST_001] Connection test') |
||
63 | print(f"XML-RPC request: {method}") |
||
64 | req = json.loads(client.getAll()) |
||
65 | |||
66 | self.assertIsInstance(req, dict) |
||
67 | |||
68 | def test_002_pluginslist(self): |
||
69 | """Plugins list.""" |
||
70 | method = "getAllPlugins()" |
||
71 | print('INFO: [TEST_002] Get plugins list') |
||
72 | print(f"XML-RPC request: {method}") |
||
73 | req = json.loads(client.getAllPlugins()) |
||
74 | |||
75 | self.assertIsInstance(req, list) |
||
76 | |||
77 | def test_003_system(self): |
||
78 | """System.""" |
||
79 | method = "getSystem()" |
||
80 | print(f'INFO: [TEST_003] Method: {method}') |
||
81 | req = json.loads(client.getSystem()) |
||
82 | |||
83 | self.assertIsInstance(req, dict) |
||
84 | |||
85 | def test_004_cpu(self): |
||
86 | """CPU.""" |
||
87 | method = "getCpu(), getPerCpu(), getLoad() and getCore()" |
||
88 | print(f'INFO: [TEST_004] Method: {method}') |
||
89 | |||
90 | req = json.loads(client.getCpu()) |
||
91 | self.assertIsInstance(req, dict) |
||
92 | |||
93 | req = json.loads(client.getPerCpu()) |
||
94 | self.assertIsInstance(req, list) |
||
95 | |||
96 | req = json.loads(client.getLoad()) |
||
97 | self.assertIsInstance(req, dict) |
||
98 | |||
99 | req = json.loads(client.getCore()) |
||
100 | self.assertIsInstance(req, dict) |
||
101 | |||
102 | def test_005_mem(self): |
||
103 | """MEM.""" |
||
104 | method = "getMem() and getMemSwap()" |
||
105 | print(f'INFO: [TEST_005] Method: {method}') |
||
106 | |||
107 | req = json.loads(client.getMem()) |
||
108 | self.assertIsInstance(req, dict) |
||
109 | |||
110 | req = json.loads(client.getMemSwap()) |
||
111 | self.assertIsInstance(req, dict) |
||
112 | |||
113 | def test_006_net(self): |
||
114 | """NETWORK.""" |
||
115 | method = "getNetwork()" |
||
116 | print(f'INFO: [TEST_006] Method: {method}') |
||
117 | |||
118 | req = json.loads(client.getNetwork()) |
||
119 | self.assertIsInstance(req, list) |
||
120 | |||
121 | def test_007_disk(self): |
||
122 | """DISK.""" |
||
123 | method = "getFs(), getFolders() and getDiskIO()" |
||
124 | print(f'INFO: [TEST_007] Method: {method}') |
||
125 | |||
126 | req = json.loads(client.getFs()) |
||
127 | self.assertIsInstance(req, list) |
||
128 | |||
129 | req = json.loads(client.getFolders()) |
||
130 | self.assertIsInstance(req, list) |
||
131 | |||
132 | req = json.loads(client.getDiskIO()) |
||
133 | self.assertIsInstance(req, list) |
||
134 | |||
135 | def test_008_sensors(self): |
||
136 | """SENSORS.""" |
||
137 | method = "getSensors()" |
||
138 | print(f'INFO: [TEST_008] Method: {method}') |
||
139 | |||
140 | req = json.loads(client.getSensors()) |
||
141 | self.assertIsInstance(req, list) |
||
142 | |||
143 | def test_009_process(self): |
||
144 | """PROCESS.""" |
||
145 | method = "getProcessCount() and getProcessList()" |
||
146 | print(f'INFO: [TEST_009] Method: {method}') |
||
147 | |||
148 | req = json.loads(client.getProcessCount()) |
||
149 | self.assertIsInstance(req, dict) |
||
150 | |||
151 | req = json.loads(client.getProcessList()) |
||
152 | self.assertIsInstance(req, list) |
||
153 | |||
154 | def test_010_all_limits(self): |
||
155 | """All limits.""" |
||
156 | method = "getAllLimits()" |
||
157 | print(f'INFO: [TEST_010] Method: {method}') |
||
158 | |||
159 | req = json.loads(client.getAllLimits()) |
||
160 | self.assertIsInstance(req, dict) |
||
161 | self.assertIsInstance(req['cpu'], dict) |
||
162 | |||
163 | def test_011_all_views(self): |
||
164 | """All views.""" |
||
165 | method = "getAllViews()" |
||
166 | print(f'INFO: [TEST_011] Method: {method}') |
||
167 | |||
168 | req = json.loads(client.getAllViews()) |
||
169 | self.assertIsInstance(req, dict) |
||
170 | self.assertIsInstance(req['cpu'], dict) |
||
171 | |||
172 | def test_012_irq(self): |
||
173 | """IRQS""" |
||
174 | method = "getIrqs()" |
||
175 | print(f'INFO: [TEST_012] Method: {method}') |
||
176 | req = json.loads(client.getIrq()) |
||
177 | self.assertIsInstance(req, list) |
||
178 | |||
179 | def test_013_plugin_views(self): |
||
180 | """Plugin views.""" |
||
181 | method = "getViewsCpu()" |
||
182 | print(f'INFO: [TEST_013] Method: {method}') |
||
183 | |||
184 | req = json.loads(client.getViewsCpu()) |
||
185 | self.assertIsInstance(req, dict) |
||
186 | |||
187 | def test_999_stop_server(self): |
||
188 | """Stop the Glances Web Server.""" |
||
189 | print('INFO: [TEST_999] Stop the Glances Server') |
||
190 | |||
191 | print("Stop the Glances Server") |
||
192 | pid.terminate() |
||
193 | time.sleep(1) |
||
194 | |||
195 | self.assertTrue(True) |
||
196 | |||
197 | |||
198 | if __name__ == '__main__': |
||
199 | unittest.main() |
||
200 |