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 RESTful API.""" |
||
12 | |||
13 | import shlex |
||
14 | import subprocess |
||
15 | import time |
||
16 | import numbers |
||
17 | import unittest |
||
18 | |||
19 | from glances import __version__ |
||
20 | from glances.compat import text_type |
||
21 | |||
22 | import requests |
||
23 | |||
24 | SERVER_PORT = 61234 |
||
25 | API_VERSION = 3 |
||
26 | URL = "http://localhost:{}/api/{}".format(SERVER_PORT, API_VERSION) |
||
27 | pid = None |
||
28 | |||
29 | # Unitest class |
||
30 | # ============== |
||
31 | print('RESTful 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 | def http_get(self, url, deflate=False): |
||
42 | """Make the request""" |
||
43 | if deflate: |
||
44 | ret = requests.get(url, |
||
45 | stream=True, |
||
46 | headers={'Accept-encoding': 'deflate'}) |
||
47 | else: |
||
48 | ret = requests.get(url, |
||
49 | headers={'Accept-encoding': 'identity'}) |
||
50 | return ret |
||
51 | |||
52 | View Code Duplication | def test_000_start_server(self): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
53 | """Start the Glances Web Server.""" |
||
54 | global pid |
||
55 | |||
56 | print('INFO: [TEST_000] Start the Glances Web Server') |
||
57 | cmdline = "python -m glances -B localhost -w -p %s" % SERVER_PORT |
||
58 | print("Run the Glances Web Server on port %s" % SERVER_PORT) |
||
59 | args = shlex.split(cmdline) |
||
60 | pid = subprocess.Popen(args) |
||
61 | print("Please wait 5 seconds...") |
||
62 | time.sleep(5) |
||
63 | |||
64 | self.assertTrue(pid is not None) |
||
65 | |||
66 | def test_001_all(self): |
||
67 | """All.""" |
||
68 | method = "all" |
||
69 | print('INFO: [TEST_001] Get all stats') |
||
70 | print("HTTP RESTful request: %s/%s" % (URL, method)) |
||
71 | req = self.http_get("%s/%s" % (URL, method)) |
||
72 | |||
73 | self.assertTrue(req.ok) |
||
74 | |||
75 | def test_001a_all_deflate(self): |
||
76 | """All.""" |
||
77 | method = "all" |
||
78 | print('INFO: [TEST_001a] Get all stats (with Deflate compression)') |
||
79 | print("HTTP RESTful request: %s/%s" % (URL, method)) |
||
80 | req = self.http_get("%s/%s" % (URL, method), deflate=True) |
||
81 | |||
82 | self.assertTrue(req.ok) |
||
83 | self.assertTrue(req.headers['Content-Encoding'] == 'deflate') |
||
84 | |||
85 | def test_002_pluginslist(self): |
||
86 | """Plugins list.""" |
||
87 | method = "pluginslist" |
||
88 | print('INFO: [TEST_002] Plugins list') |
||
89 | print("HTTP RESTful request: %s/%s" % (URL, method)) |
||
90 | req = self.http_get("%s/%s" % (URL, method)) |
||
91 | |||
92 | self.assertTrue(req.ok) |
||
93 | self.assertIsInstance(req.json(), list) |
||
94 | self.assertIn('cpu', req.json()) |
||
95 | |||
96 | def test_003_plugins(self): |
||
97 | """Plugins.""" |
||
98 | method = "pluginslist" |
||
99 | print('INFO: [TEST_003] Plugins') |
||
100 | plist = self.http_get("%s/%s" % (URL, method)) |
||
101 | |||
102 | for p in plist.json(): |
||
103 | print("HTTP RESTful request: %s/%s" % (URL, p)) |
||
104 | req = self.http_get("%s/%s" % (URL, p)) |
||
105 | self.assertTrue(req.ok) |
||
106 | if p in ('uptime', 'now'): |
||
107 | self.assertIsInstance(req.json(), text_type) |
||
108 | elif p in ('fs', 'percpu', 'sensors', 'alert', 'processlist', 'diskio', |
||
109 | 'hddtemp', 'batpercent', 'network', 'folders', 'amps', 'ports', |
||
110 | 'irq', 'wifi', 'gpu'): |
||
111 | self.assertIsInstance(req.json(), list) |
||
112 | elif p in ('psutilversion', 'help'): |
||
113 | pass |
||
114 | else: |
||
115 | self.assertIsInstance(req.json(), dict) |
||
116 | |||
117 | def test_004_items(self): |
||
118 | """Items.""" |
||
119 | method = "cpu" |
||
120 | print('INFO: [TEST_004] Items for the CPU method') |
||
121 | ilist = self.http_get("%s/%s" % (URL, method)) |
||
122 | |||
123 | for i in ilist.json(): |
||
124 | print("HTTP RESTful request: %s/%s/%s" % (URL, method, i)) |
||
125 | req = self.http_get("%s/%s/%s" % (URL, method, i)) |
||
126 | self.assertTrue(req.ok) |
||
127 | self.assertIsInstance(req.json(), dict) |
||
128 | print(req.json()[i]) |
||
129 | self.assertIsInstance(req.json()[i], numbers.Number) |
||
130 | |||
131 | def test_005_values(self): |
||
132 | """Values.""" |
||
133 | method = "processlist" |
||
134 | print('INFO: [TEST_005] Item=Value for the PROCESSLIST method') |
||
135 | print("%s/%s/pid/0" % (URL, method)) |
||
136 | req = self.http_get("%s/%s/pid/0" % (URL, method)) |
||
137 | |||
138 | self.assertTrue(req.ok) |
||
139 | self.assertIsInstance(req.json(), dict) |
||
140 | |||
141 | def test_006_all_limits(self): |
||
142 | """All limits.""" |
||
143 | method = "all/limits" |
||
144 | print('INFO: [TEST_006] Get all limits') |
||
145 | print("HTTP RESTful request: %s/%s" % (URL, method)) |
||
146 | req = self.http_get("%s/%s" % (URL, method)) |
||
147 | |||
148 | self.assertTrue(req.ok) |
||
149 | self.assertIsInstance(req.json(), dict) |
||
150 | |||
151 | def test_007_all_views(self): |
||
152 | """All views.""" |
||
153 | method = "all/views" |
||
154 | print('INFO: [TEST_007] Get all views') |
||
155 | print("HTTP RESTful request: %s/%s" % (URL, method)) |
||
156 | req = self.http_get("%s/%s" % (URL, method)) |
||
157 | |||
158 | self.assertTrue(req.ok) |
||
159 | self.assertIsInstance(req.json(), dict) |
||
160 | |||
161 | def test_008_plugins_limits(self): |
||
162 | """Plugins limits.""" |
||
163 | method = "pluginslist" |
||
164 | print('INFO: [TEST_008] Plugins limits') |
||
165 | plist = self.http_get("%s/%s" % (URL, method)) |
||
166 | |||
167 | for p in plist.json(): |
||
168 | print("HTTP RESTful request: %s/%s/limits" % (URL, p)) |
||
169 | req = self.http_get("%s/%s/limits" % (URL, p)) |
||
170 | self.assertTrue(req.ok) |
||
171 | self.assertIsInstance(req.json(), dict) |
||
172 | |||
173 | def test_009_plugins_views(self): |
||
174 | """Plugins views.""" |
||
175 | method = "pluginslist" |
||
176 | print('INFO: [TEST_009] Plugins views') |
||
177 | plist = self.http_get("%s/%s" % (URL, method)) |
||
178 | |||
179 | for p in plist.json(): |
||
180 | print("HTTP RESTful request: %s/%s/views" % (URL, p)) |
||
181 | req = self.http_get("%s/%s/views" % (URL, p)) |
||
182 | self.assertTrue(req.ok) |
||
183 | self.assertIsInstance(req.json(), dict) |
||
184 | |||
185 | def test_010_history(self): |
||
186 | """History.""" |
||
187 | method = "history" |
||
188 | print('INFO: [TEST_010] History') |
||
189 | print("HTTP RESTful request: %s/cpu/%s" % (URL, method)) |
||
190 | req = self.http_get("%s/cpu/%s" % (URL, method)) |
||
191 | self.assertIsInstance(req.json(), dict) |
||
192 | self.assertIsInstance(req.json()['user'], list) |
||
193 | self.assertTrue(len(req.json()['user']) > 0) |
||
194 | print("HTTP RESTful request: %s/cpu/%s/3" % (URL, method)) |
||
195 | req = self.http_get("%s/cpu/%s/3" % (URL, method)) |
||
196 | self.assertIsInstance(req.json(), dict) |
||
197 | self.assertIsInstance(req.json()['user'], list) |
||
198 | self.assertTrue(len(req.json()['user']) > 1) |
||
199 | print("HTTP RESTful request: %s/cpu/system/%s" % (URL, method)) |
||
200 | req = self.http_get("%s/cpu/system/%s" % (URL, method)) |
||
201 | self.assertIsInstance(req.json(), dict) |
||
202 | self.assertIsInstance(req.json()['system'], list) |
||
203 | self.assertTrue(len(req.json()['system']) > 0) |
||
204 | print("HTTP RESTful request: %s/cpu/system/%s/3" % (URL, method)) |
||
205 | req = self.http_get("%s/cpu/system/%s/3" % (URL, method)) |
||
206 | self.assertIsInstance(req.json(), dict) |
||
207 | self.assertIsInstance(req.json()['system'], list) |
||
208 | self.assertTrue(len(req.json()['system']) > 1) |
||
209 | |||
210 | def test_011_issue1401(self): |
||
211 | """Check issue #1401.""" |
||
212 | method = "network/interface_name" |
||
213 | print('INFO: [TEST_011] Issue #1401') |
||
214 | req = self.http_get("%s/%s" % (URL, method)) |
||
215 | self.assertTrue(req.ok) |
||
216 | self.assertIsInstance(req.json(), dict) |
||
217 | self.assertIsInstance(req.json()['interface_name'], list) |
||
218 | |||
219 | def test_999_stop_server(self): |
||
220 | """Stop the Glances Web Server.""" |
||
221 | print('INFO: [TEST_999] Stop the Glances Web Server') |
||
222 | |||
223 | print("Stop the Glances Web Server") |
||
224 | pid.terminate() |
||
225 | time.sleep(1) |
||
226 | |||
227 | self.assertTrue(True) |
||
228 | |||
229 | |||
230 | if __name__ == '__main__': |
||
231 | unittest.main() |
||
232 |