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