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