glances/unitest-restful.py

287 lines
9.8 KiB
Python
Raw Normal View History

2014-07-09 15:50:25 +04:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Glances - An eye on your system
#
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <nicolas@nicolargo.com>
2014-07-09 15:50:25 +04: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
2021-08-25 14:07:44 +03:00
import os
2014-07-09 15:50:25 +04:00
import shlex
import subprocess
2015-09-22 13:29:29 +03:00
import time
import numbers
2015-09-22 13:29:29 +03:00
import unittest
2021-09-04 17:46:20 +03:00
2023-12-15 21:06:33 +03:00
from glances.outputs.glances_restful_api import GlancesRestfulApi
from glances import __version__
2021-08-28 11:56:04 +03:00
from glances.globals 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
2023-12-15 21:06:33 +03:00
API_VERSION = GlancesRestfulApi.API_VERSION
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
# ==============
print('RESTful API unitary tests for Glances %s' % __version__)
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)
2023-12-09 12:09:17 +03:00
def http_get(self, url, gzip=False):
"""Make the request"""
2023-12-09 12:09:17 +03:00
if gzip:
2017-11-09 23:32:07 +03:00
ret = requests.get(url,
stream=True,
2023-12-09 12:09:17 +03:00
headers={'Accept-encoding': 'gzip'})
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
print('INFO: [TEST_000] Start the Glances Web Server API')
2021-08-25 14:07:44 +03:00
if os.path.isfile('./venv/bin/python'):
cmdline = "./venv/bin/python"
else:
cmdline = "python"
cmdline += " -m glances -B 0.0.0.0 -w -p %s --disable-webui -C ./conf/glances.conf" % 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)
print("Please wait 5 seconds...")
time.sleep(5)
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"
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)
2023-12-09 12:09:17 +03:00
self.assertTrue(req.json(), dict)
2017-11-09 23:32:07 +03:00
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."""
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)
2023-12-16 16:43:51 +03:00
if p in ('uptime', 'now', 'version', 'psutilversion'):
self.assertIsInstance(req.json(), text_type)
elif p in ('fs', 'percpu', 'sensors', 'alert', 'processlist', 'diskio',
'hddtemp', 'batpercent', 'network', 'folders', 'amps', 'ports',
'irq', 'wifi', 'gpu', 'containers'):
2015-09-22 13:00:00 +03:00
self.assertIsInstance(req.json(), list)
if len(req.json()) > 0:
self.assertIsInstance(req.json()[0], dict)
2014-07-09 15:50:25 +04:00
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)
print(req.json()[i])
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
def test_006_all_limits(self):
2015-09-22 13:29:29 +03:00
"""All limits."""
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))
self.assertTrue(req.ok)
2015-09-22 13:00:00 +03:00
self.assertIsInstance(req.json(), dict)
def test_007_all_views(self):
2015-09-22 13:29:29 +03:00
"""All views."""
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))
self.assertTrue(req.ok)
2015-09-22 13:00:00 +03:00
self.assertIsInstance(req.json(), dict)
def test_008_plugins_limits(self):
2015-09-22 13:29:29 +03:00
"""Plugins limits."""
method = "pluginslist"
print('INFO: [TEST_008] Plugins limits')
2017-11-09 23:32:07 +03:00
plist = self.http_get("%s/%s" % (URL, method))
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))
self.assertTrue(req.ok)
2015-09-22 13:00:00 +03:00
self.assertIsInstance(req.json(), dict)
def test_009_plugins_views(self):
2015-09-22 13:29:29 +03:00
"""Plugins views."""
method = "pluginslist"
print('INFO: [TEST_009] Plugins views')
2017-11-09 23:32:07 +03:00
plist = self.http_get("%s/%s" % (URL, method))
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))
self.assertTrue(req.ok)
2015-09-22 13:00:00 +03:00
self.assertIsInstance(req.json(), dict)
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))
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))
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)
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))
2023-12-09 12:09:17 +03:00
self.assertIsInstance(req.json(), list)
self.assertIsInstance(req.json()[0], list)
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))
2023-12-09 12:09:17 +03:00
self.assertIsInstance(req.json(), list)
self.assertIsInstance(req.json()[0], list)
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)
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)
2023-12-16 16:43:51 +03:00
self.assertEqual(req.json()['version'], __version__)
def test_013_top(self):
"""Values."""
method = "processlist"
request = "%s/%s/top/2" % (URL, method)
print('INFO: [TEST_013] Top nb item of PROCESSLIST')
print(request)
req = self.http_get(request)
self.assertTrue(req.ok)
self.assertIsInstance(req.json(), list)
self.assertEqual(len(req.json()), 2)
def test_014_config(self):
"""Test API request to get Glances configuration."""
method = "config"
print('INFO: [TEST_014] Get config')
req = self.http_get("%s/%s" % (URL, method))
self.assertTrue(req.ok)
self.assertIsInstance(req.json(), dict)
req = self.http_get("%s/%s/global/refresh" % (URL, method))
self.assertTrue(req.ok)
self.assertEqual(req.json(), "2")
2023-12-16 16:43:51 +03:00
def test_015_all_gzip(self):
"""All with Gzip."""
method = "all"
print('INFO: [TEST_015] Get all stats (with GZip compression)')
print("HTTP RESTful request: %s/%s" % (URL, method))
req = self.http_get("%s/%s" % (URL, method), gzip=True)
self.assertTrue(req.ok)
self.assertTrue(req.headers['Content-Encoding'] == 'gzip')
self.assertTrue(req.json(), dict)
def test_016_fields_description(self):
"""Fields description."""
print('INFO: [TEST_016] Get fields description and unit')
print("HTTP RESTful request: %s/cpu/total/description" % URL)
req = self.http_get("%s/cpu/total/description" % URL)
self.assertTrue(req.ok)
self.assertTrue(req.json(), str)
print("HTTP RESTful request: %s/cpu/total/unit" % URL)
req = self.http_get("%s/cpu/total/unit" % URL)
self.assertTrue(req.ok)
self.assertTrue(req.json(), str)
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)
2014-07-09 15:50:25 +04:00
if __name__ == '__main__':
unittest.main()