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