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 | |||
19 | from glances import __version__ |
||
20 | from glances.compat import ServerProxy |
||
21 | |||
22 | SERVER_PORT = 61234 |
||
23 | URL = "http://localhost:%s" % SERVER_PORT |
||
24 | pid = None |
||
25 | |||
26 | # Init the XML-RPC client |
||
27 | client = ServerProxy(URL) |
||
28 | |||
29 | # Unitest class |
||
30 | # ============== |
||
31 | print('XML-RPC API unitary tests for Glances %s' % __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 | cmdline = "python -m glances -B localhost -s -p %s" % SERVER_PORT |
||
47 | print("Run the Glances Server on port %s" % SERVER_PORT) |
||
48 | args = shlex.split(cmdline) |
||
49 | pid = subprocess.Popen(args) |
||
50 | print("Please wait...") |
||
51 | time.sleep(1) |
||
52 | |||
53 | self.assertTrue(pid is not None) |
||
54 | |||
55 | def test_001_all(self): |
||
56 | """All.""" |
||
57 | method = "getAll()" |
||
58 | print('INFO: [TEST_001] Connection test') |
||
59 | print("XML-RPC request: %s" % method) |
||
60 | req = json.loads(client.getAll()) |
||
61 | |||
62 | self.assertIsInstance(req, dict) |
||
63 | |||
64 | def test_002_pluginslist(self): |
||
65 | """Plugins list.""" |
||
66 | method = "getAllPlugins()" |
||
67 | print('INFO: [TEST_002] Get plugins list') |
||
68 | print("XML-RPC request: %s" % method) |
||
69 | req = json.loads(client.getAllPlugins()) |
||
70 | |||
71 | self.assertIsInstance(req, list) |
||
72 | |||
73 | def test_003_system(self): |
||
74 | """System.""" |
||
75 | method = "getSystem()" |
||
76 | print('INFO: [TEST_003] Method: %s' % method) |
||
77 | req = json.loads(client.getSystem()) |
||
78 | |||
79 | self.assertIsInstance(req, dict) |
||
80 | |||
81 | def test_004_cpu(self): |
||
82 | """CPU.""" |
||
83 | method = "getCpu(), getPerCpu(), getLoad() and getCore()" |
||
84 | print('INFO: [TEST_004] Method: %s' % method) |
||
85 | |||
86 | req = json.loads(client.getCpu()) |
||
87 | self.assertIsInstance(req, dict) |
||
88 | |||
89 | req = json.loads(client.getPerCpu()) |
||
90 | self.assertIsInstance(req, list) |
||
91 | |||
92 | req = json.loads(client.getLoad()) |
||
93 | self.assertIsInstance(req, dict) |
||
94 | |||
95 | req = json.loads(client.getCore()) |
||
96 | self.assertIsInstance(req, dict) |
||
97 | |||
98 | def test_005_mem(self): |
||
99 | """MEM.""" |
||
100 | method = "getMem() and getMemSwap()" |
||
101 | print('INFO: [TEST_005] Method: %s' % method) |
||
102 | |||
103 | req = json.loads(client.getMem()) |
||
104 | self.assertIsInstance(req, dict) |
||
105 | |||
106 | req = json.loads(client.getMemSwap()) |
||
107 | self.assertIsInstance(req, dict) |
||
108 | |||
109 | def test_006_net(self): |
||
110 | """NETWORK.""" |
||
111 | method = "getNetwork()" |
||
112 | print('INFO: [TEST_006] Method: %s' % method) |
||
113 | |||
114 | req = json.loads(client.getNetwork()) |
||
115 | self.assertIsInstance(req, list) |
||
116 | |||
117 | def test_007_disk(self): |
||
118 | """DISK.""" |
||
119 | method = "getFs(), getFolders() and getDiskIO()" |
||
120 | print('INFO: [TEST_007] Method: %s' % method) |
||
121 | |||
122 | req = json.loads(client.getFs()) |
||
123 | self.assertIsInstance(req, list) |
||
124 | |||
125 | req = json.loads(client.getFolders()) |
||
126 | self.assertIsInstance(req, list) |
||
127 | |||
128 | req = json.loads(client.getDiskIO()) |
||
129 | self.assertIsInstance(req, list) |
||
130 | |||
131 | def test_008_sensors(self): |
||
132 | """SENSORS.""" |
||
133 | method = "getSensors()" |
||
134 | print('INFO: [TEST_008] Method: %s' % method) |
||
135 | |||
136 | req = json.loads(client.getSensors()) |
||
137 | self.assertIsInstance(req, list) |
||
138 | |||
139 | def test_009_process(self): |
||
140 | """PROCESS.""" |
||
141 | method = "getProcessCount() and getProcessList()" |
||
142 | print('INFO: [TEST_009] Method: %s' % method) |
||
143 | |||
144 | req = json.loads(client.getProcessCount()) |
||
145 | self.assertIsInstance(req, dict) |
||
146 | |||
147 | req = json.loads(client.getProcessList()) |
||
148 | self.assertIsInstance(req, list) |
||
149 | |||
150 | def test_010_all_limits(self): |
||
151 | """All limits.""" |
||
152 | method = "getAllLimits()" |
||
153 | print('INFO: [TEST_010] Method: %s' % method) |
||
154 | |||
155 | req = json.loads(client.getAllLimits()) |
||
156 | self.assertIsInstance(req, dict) |
||
157 | self.assertIsInstance(req['cpu'], dict) |
||
158 | |||
159 | def test_011_all_views(self): |
||
160 | """All views.""" |
||
161 | method = "getAllViews()" |
||
162 | print('INFO: [TEST_011] Method: %s' % method) |
||
163 | |||
164 | req = json.loads(client.getAllViews()) |
||
165 | self.assertIsInstance(req, dict) |
||
166 | self.assertIsInstance(req['cpu'], dict) |
||
167 | |||
168 | def test_012_irq(self): |
||
169 | """IRQS""" |
||
170 | method = "getIrqs()" |
||
171 | print('INFO: [TEST_012] Method: %s' % method) |
||
172 | req = json.loads(client.getIrq()) |
||
173 | self.assertIsInstance(req, list) |
||
174 | |||
175 | def test_013_plugin_views(self): |
||
176 | """Plugin views.""" |
||
177 | method = "getViewsCpu()" |
||
178 | print('INFO: [TEST_013] Method: %s' % method) |
||
179 | |||
180 | req = json.loads(client.getViewsCpu()) |
||
181 | self.assertIsInstance(req, dict) |
||
182 | |||
183 | def test_999_stop_server(self): |
||
184 | """Stop the Glances Web Server.""" |
||
185 | print('INFO: [TEST_999] Stop the Glances Server') |
||
186 | |||
187 | print("Stop the Glances Server") |
||
188 | pid.terminate() |
||
189 | time.sleep(1) |
||
190 | |||
191 | self.assertTrue(True) |
||
192 | |||
193 | |||
194 | if __name__ == '__main__': |
||
195 | unittest.main() |
||
196 |