2014-07-09 15:50:25 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Glances - An eye on your system
|
|
|
|
#
|
2022-06-25 16:43:36 +03:00
|
|
|
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <nicolas@nicolargo.com>
|
2014-07-09 15:50:25 +04:00
|
|
|
#
|
2022-06-25 16:43:36 +03:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-only
|
2014-07-09 15:50:25 +04:00
|
|
|
#
|
|
|
|
|
2015-09-22 13:29:29 +03:00
|
|
|
"""Glances unitary tests suite for the RESTful API."""
|
2014-07-09 15:50:25 +04:00
|
|
|
|
|
|
|
import shlex
|
|
|
|
import subprocess
|
2015-09-22 13:29:29 +03:00
|
|
|
import time
|
2016-03-31 14:23:46 +03:00
|
|
|
import numbers
|
2015-09-22 13:29:29 +03:00
|
|
|
import unittest
|
2022-10-31 10:40:46 +03:00
|
|
|
import os
|
2014-07-09 15:50:25 +04:00
|
|
|
|
2016-03-24 13:33:31 +03:00
|
|
|
from glances import __version__
|
2015-11-14 17:15:15 +03:00
|
|
|
from glances.compat import text_type
|
2014-07-09 15:50:25 +04:00
|
|
|
|
2015-09-22 13:29:29 +03:00
|
|
|
import requests
|
|
|
|
|
2014-07-09 15:50:25 +04:00
|
|
|
SERVER_PORT = 61234
|
2018-08-05 18:29:08 +03:00
|
|
|
API_VERSION = 3
|
2018-08-05 18:08:05 +03:00
|
|
|
URL = "http://localhost:{}/api/{}".format(SERVER_PORT, API_VERSION)
|
2014-07-09 15:50:25 +04:00
|
|
|
pid = None
|
|
|
|
|
|
|
|
# Unitest class
|
|
|
|
# ==============
|
2016-03-24 13:33:31 +03:00
|
|
|
print('RESTful API unitary tests for Glances %s' % __version__)
|
2015-09-22 13:12:12 +03:00
|
|
|
|
2014-07-09 15:50:25 +04:00
|
|
|
|
|
|
|
class TestGlances(unittest.TestCase):
|
|
|
|
"""Test Glances class."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""The function is called *every time* before test_*."""
|
|
|
|
print('\n' + '=' * 78)
|
|
|
|
|
2018-02-25 16:09:06 +03:00
|
|
|
def http_get(self, url, deflate=False):
|
2017-11-12 19:23:31 +03:00
|
|
|
"""Make the request"""
|
2018-02-25 16:09:06 +03:00
|
|
|
if deflate:
|
2017-11-09 23:32:07 +03:00
|
|
|
ret = requests.get(url,
|
|
|
|
stream=True,
|
2018-02-25 16:09:06 +03:00
|
|
|
headers={'Accept-encoding': 'deflate'})
|
2017-11-09 23:32:07 +03:00
|
|
|
else:
|
|
|
|
ret = requests.get(url,
|
|
|
|
headers={'Accept-encoding': 'identity'})
|
|
|
|
return ret
|
|
|
|
|
2014-07-09 15:50:25 +04:00
|
|
|
def test_000_start_server(self):
|
2015-09-22 13:29:29 +03:00
|
|
|
"""Start the Glances Web Server."""
|
2014-07-09 15:50:25 +04:00
|
|
|
global pid
|
2015-01-10 00:28:00 +03:00
|
|
|
|
2015-09-22 13:29:29 +03:00
|
|
|
print('INFO: [TEST_000] Start the Glances Web Server')
|
2022-10-31 10:40:46 +03:00
|
|
|
if os.path.isfile("./venv/bin/python"):
|
|
|
|
cmdline = "./venv/bin/python"
|
|
|
|
else:
|
|
|
|
cmdline = "python"
|
|
|
|
cmdline += " -m glances -B localhost -w -p %s" % SERVER_PORT
|
2014-07-09 15:50:25 +04:00
|
|
|
print("Run the Glances Web Server on port %s" % SERVER_PORT)
|
|
|
|
args = shlex.split(cmdline)
|
|
|
|
pid = subprocess.Popen(args)
|
2016-06-27 15:31:56 +03:00
|
|
|
print("Please wait 5 seconds...")
|
|
|
|
time.sleep(5)
|
2015-01-10 00:28:00 +03:00
|
|
|
|
2014-07-09 15:50:25 +04:00
|
|
|
self.assertTrue(pid is not None)
|
|
|
|
|
|
|
|
def test_001_all(self):
|
2015-09-22 13:29:29 +03:00
|
|
|
"""All."""
|
2014-07-09 15:50:25 +04:00
|
|
|
method = "all"
|
2015-01-10 00:28:00 +03:00
|
|
|
print('INFO: [TEST_001] Get all stats')
|
2015-09-22 13:29:29 +03:00
|
|
|
print("HTTP RESTful request: %s/%s" % (URL, method))
|
2017-11-09 23:32:07 +03:00
|
|
|
req = self.http_get("%s/%s" % (URL, method))
|
|
|
|
|
|
|
|
self.assertTrue(req.ok)
|
|
|
|
|
2018-02-25 16:09:06 +03:00
|
|
|
def test_001a_all_deflate(self):
|
2017-11-09 23:32:07 +03:00
|
|
|
"""All."""
|
|
|
|
method = "all"
|
2018-02-25 16:09:06 +03:00
|
|
|
print('INFO: [TEST_001a] Get all stats (with Deflate compression)')
|
2017-11-09 23:32:07 +03:00
|
|
|
print("HTTP RESTful request: %s/%s" % (URL, method))
|
2018-02-25 16:09:06 +03:00
|
|
|
req = self.http_get("%s/%s" % (URL, method), deflate=True)
|
2014-07-09 15:50:25 +04:00
|
|
|
|
|
|
|
self.assertTrue(req.ok)
|
2018-02-25 16:09:06 +03:00
|
|
|
self.assertTrue(req.headers['Content-Encoding'] == 'deflate')
|
2014-07-09 15:50:25 +04:00
|
|
|
|
|
|
|
def test_002_pluginslist(self):
|
2015-09-22 13:29:29 +03:00
|
|
|
"""Plugins list."""
|
2014-07-09 15:50:25 +04:00
|
|
|
method = "pluginslist"
|
|
|
|
print('INFO: [TEST_002] Plugins list')
|
2015-09-22 13:29:29 +03:00
|
|
|
print("HTTP RESTful request: %s/%s" % (URL, method))
|
2017-11-09 23:32:07 +03:00
|
|
|
req = self.http_get("%s/%s" % (URL, method))
|
2014-07-09 15:50:25 +04:00
|
|
|
|
|
|
|
self.assertTrue(req.ok)
|
2015-09-22 13:00:00 +03:00
|
|
|
self.assertIsInstance(req.json(), list)
|
2014-07-12 20:02:31 +04:00
|
|
|
self.assertIn('cpu', req.json())
|
2014-07-09 15:50:25 +04:00
|
|
|
|
|
|
|
def test_003_plugins(self):
|
2015-09-22 13:29:29 +03:00
|
|
|
"""Plugins."""
|
2015-01-10 00:28:00 +03:00
|
|
|
method = "pluginslist"
|
2014-07-09 15:50:25 +04:00
|
|
|
print('INFO: [TEST_003] Plugins')
|
2017-11-09 23:32:07 +03:00
|
|
|
plist = self.http_get("%s/%s" % (URL, method))
|
2014-07-09 15:50:25 +04:00
|
|
|
|
2014-07-12 20:02:31 +04:00
|
|
|
for p in plist.json():
|
2015-09-22 13:29:29 +03:00
|
|
|
print("HTTP RESTful request: %s/%s" % (URL, p))
|
2017-11-09 23:32:07 +03:00
|
|
|
req = self.http_get("%s/%s" % (URL, p))
|
2014-07-09 15:50:25 +04:00
|
|
|
self.assertTrue(req.ok)
|
|
|
|
if p in ('uptime', 'now'):
|
2015-09-24 12:29:39 +03:00
|
|
|
self.assertIsInstance(req.json(), text_type)
|
2016-05-01 22:00:47 +03:00
|
|
|
elif p in ('fs', 'percpu', 'sensors', 'alert', 'processlist', 'diskio',
|
2016-10-21 23:32:39 +03:00
|
|
|
'hddtemp', 'batpercent', 'network', 'folders', 'amps', 'ports',
|
2016-12-17 12:35:09 +03:00
|
|
|
'irq', 'wifi', 'gpu'):
|
2015-09-22 13:00:00 +03:00
|
|
|
self.assertIsInstance(req.json(), list)
|
2014-07-09 15:50:25 +04:00
|
|
|
elif p in ('psutilversion', 'help'):
|
|
|
|
pass
|
|
|
|
else:
|
2015-09-22 13:00:00 +03:00
|
|
|
self.assertIsInstance(req.json(), dict)
|
2014-07-09 15:50:25 +04:00
|
|
|
|
|
|
|
def test_004_items(self):
|
2015-09-22 13:29:29 +03:00
|
|
|
"""Items."""
|
2014-07-09 15:50:25 +04:00
|
|
|
method = "cpu"
|
|
|
|
print('INFO: [TEST_004] Items for the CPU method')
|
2017-11-09 23:32:07 +03:00
|
|
|
ilist = self.http_get("%s/%s" % (URL, method))
|
2014-07-09 15:50:25 +04:00
|
|
|
|
2014-07-12 20:02:31 +04:00
|
|
|
for i in ilist.json():
|
2015-09-22 13:29:29 +03:00
|
|
|
print("HTTP RESTful request: %s/%s/%s" % (URL, method, i))
|
2017-11-09 23:32:07 +03:00
|
|
|
req = self.http_get("%s/%s/%s" % (URL, method, i))
|
2014-07-09 15:50:25 +04:00
|
|
|
self.assertTrue(req.ok)
|
2015-09-22 13:00:00 +03:00
|
|
|
self.assertIsInstance(req.json(), dict)
|
2016-06-04 20:31:25 +03:00
|
|
|
print(req.json()[i])
|
2016-03-31 14:23:46 +03:00
|
|
|
self.assertIsInstance(req.json()[i], numbers.Number)
|
2014-07-09 15:50:25 +04:00
|
|
|
|
|
|
|
def test_005_values(self):
|
2015-09-22 13:29:29 +03:00
|
|
|
"""Values."""
|
2014-07-09 15:50:25 +04:00
|
|
|
method = "processlist"
|
|
|
|
print('INFO: [TEST_005] Item=Value for the PROCESSLIST method')
|
|
|
|
print("%s/%s/pid/0" % (URL, method))
|
2017-11-09 23:32:07 +03:00
|
|
|
req = self.http_get("%s/%s/pid/0" % (URL, method))
|
2014-07-09 15:50:25 +04:00
|
|
|
|
|
|
|
self.assertTrue(req.ok)
|
2015-09-22 13:00:00 +03:00
|
|
|
self.assertIsInstance(req.json(), dict)
|
2014-07-09 15:50:25 +04:00
|
|
|
|
2015-01-10 00:28:00 +03:00
|
|
|
def test_006_all_limits(self):
|
2015-09-22 13:29:29 +03:00
|
|
|
"""All limits."""
|
2015-01-10 00:28:00 +03:00
|
|
|
method = "all/limits"
|
|
|
|
print('INFO: [TEST_006] Get all limits')
|
2015-09-22 13:29:29 +03:00
|
|
|
print("HTTP RESTful request: %s/%s" % (URL, method))
|
2017-11-09 23:32:07 +03:00
|
|
|
req = self.http_get("%s/%s" % (URL, method))
|
2015-01-10 00:28:00 +03:00
|
|
|
|
|
|
|
self.assertTrue(req.ok)
|
2015-09-22 13:00:00 +03:00
|
|
|
self.assertIsInstance(req.json(), dict)
|
2015-01-10 00:28:00 +03:00
|
|
|
|
2015-01-18 20:21:26 +03:00
|
|
|
def test_007_all_views(self):
|
2015-09-22 13:29:29 +03:00
|
|
|
"""All views."""
|
2015-01-18 20:21:26 +03:00
|
|
|
method = "all/views"
|
|
|
|
print('INFO: [TEST_007] Get all views')
|
2015-09-22 13:29:29 +03:00
|
|
|
print("HTTP RESTful request: %s/%s" % (URL, method))
|
2017-11-09 23:32:07 +03:00
|
|
|
req = self.http_get("%s/%s" % (URL, method))
|
2015-01-18 20:21:26 +03:00
|
|
|
|
|
|
|
self.assertTrue(req.ok)
|
2015-09-22 13:00:00 +03:00
|
|
|
self.assertIsInstance(req.json(), dict)
|
2015-01-18 20:21:26 +03:00
|
|
|
|
|
|
|
def test_008_plugins_limits(self):
|
2015-09-22 13:29:29 +03:00
|
|
|
"""Plugins limits."""
|
2015-01-18 20:21:26 +03:00
|
|
|
method = "pluginslist"
|
|
|
|
print('INFO: [TEST_008] Plugins limits')
|
2017-11-09 23:32:07 +03:00
|
|
|
plist = self.http_get("%s/%s" % (URL, method))
|
2015-01-18 20:21:26 +03:00
|
|
|
|
|
|
|
for p in plist.json():
|
2015-09-22 13:29:29 +03:00
|
|
|
print("HTTP RESTful request: %s/%s/limits" % (URL, p))
|
2017-11-09 23:32:07 +03:00
|
|
|
req = self.http_get("%s/%s/limits" % (URL, p))
|
2015-01-18 20:21:26 +03:00
|
|
|
self.assertTrue(req.ok)
|
2015-09-22 13:00:00 +03:00
|
|
|
self.assertIsInstance(req.json(), dict)
|
2015-01-18 20:21:26 +03:00
|
|
|
|
|
|
|
def test_009_plugins_views(self):
|
2015-09-22 13:29:29 +03:00
|
|
|
"""Plugins views."""
|
2015-01-18 20:21:26 +03:00
|
|
|
method = "pluginslist"
|
|
|
|
print('INFO: [TEST_009] Plugins views')
|
2017-11-09 23:32:07 +03:00
|
|
|
plist = self.http_get("%s/%s" % (URL, method))
|
2015-01-18 20:21:26 +03:00
|
|
|
|
|
|
|
for p in plist.json():
|
2015-09-22 13:29:29 +03:00
|
|
|
print("HTTP RESTful request: %s/%s/views" % (URL, p))
|
2017-11-09 23:32:07 +03:00
|
|
|
req = self.http_get("%s/%s/views" % (URL, p))
|
2015-01-18 20:21:26 +03:00
|
|
|
self.assertTrue(req.ok)
|
2015-09-22 13:00:00 +03:00
|
|
|
self.assertIsInstance(req.json(), dict)
|
2015-01-18 20:21:26 +03:00
|
|
|
|
2016-06-04 17:17:00 +03:00
|
|
|
def test_010_history(self):
|
|
|
|
"""History."""
|
|
|
|
method = "history"
|
|
|
|
print('INFO: [TEST_010] History')
|
|
|
|
print("HTTP RESTful request: %s/cpu/%s" % (URL, method))
|
2017-11-09 23:32:07 +03:00
|
|
|
req = self.http_get("%s/cpu/%s" % (URL, method))
|
2016-06-04 17:17:00 +03:00
|
|
|
self.assertIsInstance(req.json(), dict)
|
|
|
|
self.assertIsInstance(req.json()['user'], list)
|
|
|
|
self.assertTrue(len(req.json()['user']) > 0)
|
|
|
|
print("HTTP RESTful request: %s/cpu/%s/3" % (URL, method))
|
2017-11-09 23:32:07 +03:00
|
|
|
req = self.http_get("%s/cpu/%s/3" % (URL, method))
|
2016-06-04 17:17:00 +03:00
|
|
|
self.assertIsInstance(req.json(), dict)
|
|
|
|
self.assertIsInstance(req.json()['user'], list)
|
2016-09-25 23:29:17 +03:00
|
|
|
self.assertTrue(len(req.json()['user']) > 1)
|
2016-06-04 17:17:00 +03:00
|
|
|
print("HTTP RESTful request: %s/cpu/system/%s" % (URL, method))
|
2017-11-09 23:32:07 +03:00
|
|
|
req = self.http_get("%s/cpu/system/%s" % (URL, method))
|
2016-06-04 17:17:00 +03:00
|
|
|
self.assertIsInstance(req.json(), dict)
|
|
|
|
self.assertIsInstance(req.json()['system'], list)
|
|
|
|
self.assertTrue(len(req.json()['system']) > 0)
|
|
|
|
print("HTTP RESTful request: %s/cpu/system/%s/3" % (URL, method))
|
2017-11-09 23:32:07 +03:00
|
|
|
req = self.http_get("%s/cpu/system/%s/3" % (URL, method))
|
2016-06-04 17:17:00 +03:00
|
|
|
self.assertIsInstance(req.json(), dict)
|
|
|
|
self.assertIsInstance(req.json()['system'], list)
|
2016-09-25 23:29:17 +03:00
|
|
|
self.assertTrue(len(req.json()['system']) > 1)
|
2016-06-04 17:17:00 +03:00
|
|
|
|
2019-01-24 23:43:50 +03:00
|
|
|
def test_011_issue1401(self):
|
|
|
|
"""Check issue #1401."""
|
|
|
|
method = "network/interface_name"
|
|
|
|
print('INFO: [TEST_011] Issue #1401')
|
|
|
|
req = self.http_get("%s/%s" % (URL, method))
|
|
|
|
self.assertTrue(req.ok)
|
|
|
|
self.assertIsInstance(req.json(), dict)
|
|
|
|
self.assertIsInstance(req.json()['interface_name'], list)
|
|
|
|
|
2023-03-10 16:35:24 +03:00
|
|
|
def test_012_status(self):
|
|
|
|
"""Check status endpoint."""
|
|
|
|
method = "status"
|
|
|
|
print('INFO: [TEST_012] Status')
|
|
|
|
print("HTTP RESTful request: %s/%s" % (URL, method))
|
|
|
|
req = self.http_get("%s/%s" % (URL, method))
|
|
|
|
|
|
|
|
self.assertTrue(req.ok)
|
|
|
|
self.assertEqual(req.text, "Active")
|
|
|
|
|
2014-07-09 15:50:25 +04:00
|
|
|
def test_999_stop_server(self):
|
2015-09-22 13:29:29 +03:00
|
|
|
"""Stop the Glances Web Server."""
|
2014-07-09 15:50:25 +04:00
|
|
|
print('INFO: [TEST_999] Stop the Glances Web Server')
|
|
|
|
|
|
|
|
print("Stop the Glances Web Server")
|
|
|
|
pid.terminate()
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
self.assertTrue(True)
|
|
|
|
|
2017-11-12 19:23:31 +03:00
|
|
|
|
2014-07-09 15:50:25 +04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|