1 | #!/usr/bin/env python |
||
2 | # -*- coding: utf-8 -*- |
||
3 | # |
||
4 | # Glances - An eye on your system |
||
5 | # |
||
6 | # Copyright (C) 2019 Nicolargo <[email protected]> |
||
7 | # |
||
8 | # Glances is free software; you can redistribute it and/or modify |
||
9 | # it under the terms of the GNU Lesser General Public License as published by |
||
10 | # the Free Software Foundation, either version 3 of the License, or |
||
11 | # (at your option) any later version. |
||
12 | # |
||
13 | # Glances is distributed in the hope that it will be useful, |
||
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | # GNU Lesser General Public License for more details. |
||
17 | # |
||
18 | # You should have received a copy of the GNU Lesser General Public License |
||
19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
20 | |||
21 | """Glances unitary tests suite for the RESTful API.""" |
||
22 | |||
23 | import shlex |
||
24 | import subprocess |
||
25 | import time |
||
26 | import numbers |
||
27 | import unittest |
||
28 | |||
29 | from glances import __version__ |
||
30 | from glances.compat import text_type |
||
31 | |||
32 | import requests |
||
33 | |||
34 | SERVER_PORT = 61234 |
||
35 | API_VERSION = 3 |
||
36 | URL = "http://localhost:{}/api/{}".format(SERVER_PORT, API_VERSION) |
||
37 | pid = None |
||
38 | |||
39 | # Unitest class |
||
40 | # ============== |
||
41 | print('RESTful API unitary tests for Glances %s' % __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 | def http_get(self, url, deflate=False): |
||
52 | """Make the request""" |
||
53 | if deflate: |
||
54 | ret = requests.get(url, |
||
55 | stream=True, |
||
56 | headers={'Accept-encoding': 'deflate'}) |
||
57 | else: |
||
58 | ret = requests.get(url, |
||
59 | headers={'Accept-encoding': 'identity'}) |
||
60 | return ret |
||
61 | |||
62 | View Code Duplication | def test_000_start_server(self): |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
63 | """Start the Glances Web Server.""" |
||
64 | global pid |
||
65 | |||
66 | print('INFO: [TEST_000] Start the Glances Web Server') |
||
67 | cmdline = "python -m glances -w -p %s" % SERVER_PORT |
||
68 | print("Run the Glances Web Server on port %s" % SERVER_PORT) |
||
69 | args = shlex.split(cmdline) |
||
70 | pid = subprocess.Popen(args) |
||
71 | print("Please wait 5 seconds...") |
||
72 | time.sleep(5) |
||
73 | |||
74 | self.assertTrue(pid is not None) |
||
75 | |||
76 | def test_001_all(self): |
||
77 | """All.""" |
||
78 | method = "all" |
||
79 | print('INFO: [TEST_001] Get all stats') |
||
80 | print("HTTP RESTful request: %s/%s" % (URL, method)) |
||
81 | req = self.http_get("%s/%s" % (URL, method)) |
||
82 | |||
83 | self.assertTrue(req.ok) |
||
84 | |||
85 | def test_001a_all_deflate(self): |
||
86 | """All.""" |
||
87 | method = "all" |
||
88 | print('INFO: [TEST_001a] Get all stats (with Deflate compression)') |
||
89 | print("HTTP RESTful request: %s/%s" % (URL, method)) |
||
90 | req = self.http_get("%s/%s" % (URL, method), deflate=True) |
||
91 | |||
92 | self.assertTrue(req.ok) |
||
93 | self.assertTrue(req.headers['Content-Encoding'] == 'deflate') |
||
94 | |||
95 | def test_002_pluginslist(self): |
||
96 | """Plugins list.""" |
||
97 | method = "pluginslist" |
||
98 | print('INFO: [TEST_002] Plugins list') |
||
99 | print("HTTP RESTful request: %s/%s" % (URL, method)) |
||
100 | req = self.http_get("%s/%s" % (URL, method)) |
||
101 | |||
102 | self.assertTrue(req.ok) |
||
103 | self.assertIsInstance(req.json(), list) |
||
104 | self.assertIn('cpu', req.json()) |
||
105 | |||
106 | def test_003_plugins(self): |
||
107 | """Plugins.""" |
||
108 | method = "pluginslist" |
||
109 | print('INFO: [TEST_003] Plugins') |
||
110 | plist = self.http_get("%s/%s" % (URL, method)) |
||
111 | |||
112 | for p in plist.json(): |
||
113 | print("HTTP RESTful request: %s/%s" % (URL, p)) |
||
114 | req = self.http_get("%s/%s" % (URL, p)) |
||
115 | self.assertTrue(req.ok) |
||
116 | if p in ('uptime', 'now'): |
||
117 | self.assertIsInstance(req.json(), text_type) |
||
118 | elif p in ('fs', 'percpu', 'sensors', 'alert', 'processlist', 'diskio', |
||
119 | 'hddtemp', 'batpercent', 'network', 'folders', 'amps', 'ports', |
||
120 | 'irq', 'wifi', 'gpu'): |
||
121 | self.assertIsInstance(req.json(), list) |
||
122 | elif p in ('psutilversion', 'help'): |
||
123 | pass |
||
124 | else: |
||
125 | self.assertIsInstance(req.json(), dict) |
||
126 | |||
127 | def test_004_items(self): |
||
128 | """Items.""" |
||
129 | method = "cpu" |
||
130 | print('INFO: [TEST_004] Items for the CPU method') |
||
131 | ilist = self.http_get("%s/%s" % (URL, method)) |
||
132 | |||
133 | for i in ilist.json(): |
||
134 | print("HTTP RESTful request: %s/%s/%s" % (URL, method, i)) |
||
135 | req = self.http_get("%s/%s/%s" % (URL, method, i)) |
||
136 | self.assertTrue(req.ok) |
||
137 | self.assertIsInstance(req.json(), dict) |
||
138 | print(req.json()[i]) |
||
139 | self.assertIsInstance(req.json()[i], numbers.Number) |
||
140 | |||
141 | def test_005_values(self): |
||
142 | """Values.""" |
||
143 | method = "processlist" |
||
144 | print('INFO: [TEST_005] Item=Value for the PROCESSLIST method') |
||
145 | print("%s/%s/pid/0" % (URL, method)) |
||
146 | req = self.http_get("%s/%s/pid/0" % (URL, method)) |
||
147 | |||
148 | self.assertTrue(req.ok) |
||
149 | self.assertIsInstance(req.json(), dict) |
||
150 | |||
151 | def test_006_all_limits(self): |
||
152 | """All limits.""" |
||
153 | method = "all/limits" |
||
154 | print('INFO: [TEST_006] Get all limits') |
||
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_007_all_views(self): |
||
162 | """All views.""" |
||
163 | method = "all/views" |
||
164 | print('INFO: [TEST_007] Get all views') |
||
165 | print("HTTP RESTful request: %s/%s" % (URL, method)) |
||
166 | req = self.http_get("%s/%s" % (URL, method)) |
||
167 | |||
168 | self.assertTrue(req.ok) |
||
169 | self.assertIsInstance(req.json(), dict) |
||
170 | |||
171 | def test_008_plugins_limits(self): |
||
172 | """Plugins limits.""" |
||
173 | method = "pluginslist" |
||
174 | print('INFO: [TEST_008] Plugins limits') |
||
175 | plist = self.http_get("%s/%s" % (URL, method)) |
||
176 | |||
177 | for p in plist.json(): |
||
178 | print("HTTP RESTful request: %s/%s/limits" % (URL, p)) |
||
179 | req = self.http_get("%s/%s/limits" % (URL, p)) |
||
180 | self.assertTrue(req.ok) |
||
181 | self.assertIsInstance(req.json(), dict) |
||
182 | |||
183 | def test_009_plugins_views(self): |
||
184 | """Plugins views.""" |
||
185 | method = "pluginslist" |
||
186 | print('INFO: [TEST_009] Plugins views') |
||
187 | plist = self.http_get("%s/%s" % (URL, method)) |
||
188 | |||
189 | for p in plist.json(): |
||
190 | print("HTTP RESTful request: %s/%s/views" % (URL, p)) |
||
191 | req = self.http_get("%s/%s/views" % (URL, p)) |
||
192 | self.assertTrue(req.ok) |
||
193 | self.assertIsInstance(req.json(), dict) |
||
194 | |||
195 | def test_010_history(self): |
||
196 | """History.""" |
||
197 | method = "history" |
||
198 | print('INFO: [TEST_010] History') |
||
199 | print("HTTP RESTful request: %s/cpu/%s" % (URL, method)) |
||
200 | req = self.http_get("%s/cpu/%s" % (URL, method)) |
||
201 | self.assertIsInstance(req.json(), dict) |
||
202 | self.assertIsInstance(req.json()['user'], list) |
||
203 | self.assertTrue(len(req.json()['user']) > 0) |
||
204 | print("HTTP RESTful request: %s/cpu/%s/3" % (URL, method)) |
||
205 | req = self.http_get("%s/cpu/%s/3" % (URL, method)) |
||
206 | self.assertIsInstance(req.json(), dict) |
||
207 | self.assertIsInstance(req.json()['user'], list) |
||
208 | self.assertTrue(len(req.json()['user']) > 1) |
||
209 | print("HTTP RESTful request: %s/cpu/system/%s" % (URL, method)) |
||
210 | req = self.http_get("%s/cpu/system/%s" % (URL, method)) |
||
211 | self.assertIsInstance(req.json(), dict) |
||
212 | self.assertIsInstance(req.json()['system'], list) |
||
213 | self.assertTrue(len(req.json()['system']) > 0) |
||
214 | print("HTTP RESTful request: %s/cpu/system/%s/3" % (URL, method)) |
||
215 | req = self.http_get("%s/cpu/system/%s/3" % (URL, method)) |
||
216 | self.assertIsInstance(req.json(), dict) |
||
217 | self.assertIsInstance(req.json()['system'], list) |
||
218 | self.assertTrue(len(req.json()['system']) > 1) |
||
219 | |||
220 | def test_011_issue1401(self): |
||
221 | """Check issue #1401.""" |
||
222 | method = "network/interface_name" |
||
223 | print('INFO: [TEST_011] Issue #1401') |
||
224 | req = self.http_get("%s/%s" % (URL, method)) |
||
225 | self.assertTrue(req.ok) |
||
226 | self.assertIsInstance(req.json(), dict) |
||
227 | self.assertIsInstance(req.json()['interface_name'], list) |
||
228 | |||
229 | def test_999_stop_server(self): |
||
230 | """Stop the Glances Web Server.""" |
||
231 | print('INFO: [TEST_999] Stop the Glances Web Server') |
||
232 | |||
233 | print("Stop the Glances Web Server") |
||
234 | pid.terminate() |
||
235 | time.sleep(1) |
||
236 | |||
237 | self.assertTrue(True) |
||
238 | |||
239 | |||
240 | if __name__ == '__main__': |
||
241 | unittest.main() |
||
242 |