glances/unitest-restful.py

259 lines
8.6 KiB
Python
Raw Normal View History

2014-07-09 11:50:25 +00: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 11:50:25 +00:00
#
# SPDX-License-Identifier: LGPL-3.0-only
2014-07-09 11:50:25 +00:00
#
2015-09-22 10:29:29 +00:00
"""Glances unitary tests suite for the RESTful API."""
2014-07-09 11:50:25 +00:00
2021-08-25 11:07:44 +00:00
import os
2014-07-09 11:50:25 +00:00
import shlex
import subprocess
2015-09-22 10:29:29 +00:00
import time
import numbers
2015-09-22 10:29:29 +00:00
import unittest
2021-09-04 14:46:20 +00:00
from glances import __version__
2021-08-28 08:56:04 +00:00
from glances.globals import text_type
2014-07-09 11:50:25 +00:00
2015-09-22 10:29:29 +00:00
import requests
2014-07-09 11:50:25 +00:00
SERVER_PORT = 61234
2018-08-05 15:29:08 +00:00
API_VERSION = 3
2018-08-05 15:08:05 +00:00
URL = "http://localhost:{}/api/{}".format(SERVER_PORT, API_VERSION)
2014-07-09 11:50:25 +00:00
pid = None
# Unitest class
# ==============
print('RESTful API unitary tests for Glances %s' % __version__)
2014-07-09 11:50:25 +00: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 13:09:06 +00:00
def http_get(self, url, deflate=False):
"""Make the request"""
2018-02-25 13:09:06 +00:00
if deflate:
2017-11-09 20:32:07 +00:00
ret = requests.get(url,
stream=True,
2018-02-25 13:09:06 +00:00
headers={'Accept-encoding': 'deflate'})
2017-11-09 20:32:07 +00:00
else:
ret = requests.get(url,
headers={'Accept-encoding': 'identity'})
return ret
2014-07-09 11:50:25 +00:00
def test_000_start_server(self):
2015-09-22 10:29:29 +00:00
"""Start the Glances Web Server."""
2014-07-09 11:50:25 +00:00
global pid
2015-09-22 10:29:29 +00:00
print('INFO: [TEST_000] Start the Glances Web Server')
2021-08-25 11:07:44 +00:00
if os.path.isfile('./venv/bin/python'):
cmdline = "./venv/bin/python"
else:
cmdline = "python"
2022-05-25 14:01:06 +00:00
cmdline += " -m glances -B localhost -w -p %s" % SERVER_PORT
2014-07-09 11:50:25 +00: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 11:50:25 +00:00
self.assertTrue(pid is not None)
def test_001_all(self):
2015-09-22 10:29:29 +00:00
"""All."""
2014-07-09 11:50:25 +00:00
method = "all"
print('INFO: [TEST_001] Get all stats')
2015-09-22 10:29:29 +00:00
print("HTTP RESTful request: %s/%s" % (URL, method))
2017-11-09 20:32:07 +00:00
req = self.http_get("%s/%s" % (URL, method))
self.assertTrue(req.ok)
2018-02-25 13:09:06 +00:00
def test_001a_all_deflate(self):
2017-11-09 20:32:07 +00:00
"""All."""
method = "all"
2018-02-25 13:09:06 +00:00
print('INFO: [TEST_001a] Get all stats (with Deflate compression)')
2017-11-09 20:32:07 +00:00
print("HTTP RESTful request: %s/%s" % (URL, method))
2018-02-25 13:09:06 +00:00
req = self.http_get("%s/%s" % (URL, method), deflate=True)
2014-07-09 11:50:25 +00:00
self.assertTrue(req.ok)
2018-02-25 13:09:06 +00:00
self.assertTrue(req.headers['Content-Encoding'] == 'deflate')
2014-07-09 11:50:25 +00:00
def test_002_pluginslist(self):
2015-09-22 10:29:29 +00:00
"""Plugins list."""
2014-07-09 11:50:25 +00:00
method = "pluginslist"
print('INFO: [TEST_002] Plugins list')
2015-09-22 10:29:29 +00:00
print("HTTP RESTful request: %s/%s" % (URL, method))
2017-11-09 20:32:07 +00:00
req = self.http_get("%s/%s" % (URL, method))
2014-07-09 11:50:25 +00:00
self.assertTrue(req.ok)
2015-09-22 10:00:00 +00:00
self.assertIsInstance(req.json(), list)
2014-07-12 16:02:31 +00:00
self.assertIn('cpu', req.json())
2014-07-09 11:50:25 +00:00
def test_003_plugins(self):
2015-09-22 10:29:29 +00:00
"""Plugins."""
method = "pluginslist"
2014-07-09 11:50:25 +00:00
print('INFO: [TEST_003] Plugins')
2017-11-09 20:32:07 +00:00
plist = self.http_get("%s/%s" % (URL, method))
2014-07-09 11:50:25 +00:00
2014-07-12 16:02:31 +00:00
for p in plist.json():
2015-09-22 10:29:29 +00:00
print("HTTP RESTful request: %s/%s" % (URL, p))
2017-11-09 20:32:07 +00:00
req = self.http_get("%s/%s" % (URL, p))
2014-07-09 11:50:25 +00:00
self.assertTrue(req.ok)
if p in ('uptime', 'now'):
self.assertIsInstance(req.json(), text_type)
elif p in ('fs', 'percpu', 'sensors', 'alert', 'processlist', 'diskio',
'hddtemp', 'batpercent', 'network', 'folders', 'amps', 'ports',
'irq', 'wifi', 'gpu'):
2015-09-22 10:00:00 +00:00
self.assertIsInstance(req.json(), list)
2014-07-09 11:50:25 +00:00
elif p in ('psutilversion', 'help'):
pass
else:
2015-09-22 10:00:00 +00:00
self.assertIsInstance(req.json(), dict)
2014-07-09 11:50:25 +00:00
def test_004_items(self):
2015-09-22 10:29:29 +00:00
"""Items."""
2014-07-09 11:50:25 +00:00
method = "cpu"
print('INFO: [TEST_004] Items for the CPU method')
2017-11-09 20:32:07 +00:00
ilist = self.http_get("%s/%s" % (URL, method))
2014-07-09 11:50:25 +00:00
2014-07-12 16:02:31 +00:00
for i in ilist.json():
2015-09-22 10:29:29 +00:00
print("HTTP RESTful request: %s/%s/%s" % (URL, method, i))
2017-11-09 20:32:07 +00:00
req = self.http_get("%s/%s/%s" % (URL, method, i))
2014-07-09 11:50:25 +00:00
self.assertTrue(req.ok)
2015-09-22 10:00:00 +00:00
self.assertIsInstance(req.json(), dict)
print(req.json()[i])
self.assertIsInstance(req.json()[i], numbers.Number)
2014-07-09 11:50:25 +00:00
def test_005_values(self):
2015-09-22 10:29:29 +00:00
"""Values."""
2014-07-09 11:50:25 +00:00
method = "processlist"
print('INFO: [TEST_005] Item=Value for the PROCESSLIST method')
print("%s/%s/pid/0" % (URL, method))
2017-11-09 20:32:07 +00:00
req = self.http_get("%s/%s/pid/0" % (URL, method))
2014-07-09 11:50:25 +00:00
self.assertTrue(req.ok)
2015-09-22 10:00:00 +00:00
self.assertIsInstance(req.json(), dict)
2014-07-09 11:50:25 +00:00
def test_006_all_limits(self):
2015-09-22 10:29:29 +00:00
"""All limits."""
method = "all/limits"
print('INFO: [TEST_006] Get all limits')
2015-09-22 10:29:29 +00:00
print("HTTP RESTful request: %s/%s" % (URL, method))
2017-11-09 20:32:07 +00:00
req = self.http_get("%s/%s" % (URL, method))
self.assertTrue(req.ok)
2015-09-22 10:00:00 +00:00
self.assertIsInstance(req.json(), dict)
def test_007_all_views(self):
2015-09-22 10:29:29 +00:00
"""All views."""
method = "all/views"
print('INFO: [TEST_007] Get all views')
2015-09-22 10:29:29 +00:00
print("HTTP RESTful request: %s/%s" % (URL, method))
2017-11-09 20:32:07 +00:00
req = self.http_get("%s/%s" % (URL, method))
self.assertTrue(req.ok)
2015-09-22 10:00:00 +00:00
self.assertIsInstance(req.json(), dict)
def test_008_plugins_limits(self):
2015-09-22 10:29:29 +00:00
"""Plugins limits."""
method = "pluginslist"
print('INFO: [TEST_008] Plugins limits')
2017-11-09 20:32:07 +00:00
plist = self.http_get("%s/%s" % (URL, method))
for p in plist.json():
2015-09-22 10:29:29 +00:00
print("HTTP RESTful request: %s/%s/limits" % (URL, p))
2017-11-09 20:32:07 +00:00
req = self.http_get("%s/%s/limits" % (URL, p))
self.assertTrue(req.ok)
2015-09-22 10:00:00 +00:00
self.assertIsInstance(req.json(), dict)
def test_009_plugins_views(self):
2015-09-22 10:29:29 +00:00
"""Plugins views."""
method = "pluginslist"
print('INFO: [TEST_009] Plugins views')
2017-11-09 20:32:07 +00:00
plist = self.http_get("%s/%s" % (URL, method))
for p in plist.json():
2015-09-22 10:29:29 +00:00
print("HTTP RESTful request: %s/%s/views" % (URL, p))
2017-11-09 20:32:07 +00:00
req = self.http_get("%s/%s/views" % (URL, p))
self.assertTrue(req.ok)
2015-09-22 10:00:00 +00: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 20:32:07 +00: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 20:32:07 +00: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 20:29:17 +00:00
self.assertTrue(len(req.json()['user']) > 1)
print("HTTP RESTful request: %s/cpu/system/%s" % (URL, method))
2017-11-09 20:32:07 +00:00
req = self.http_get("%s/cpu/system/%s" % (URL, method))
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 20:32:07 +00:00
req = self.http_get("%s/cpu/system/%s/3" % (URL, method))
self.assertIsInstance(req.json(), dict)
self.assertIsInstance(req.json()['system'], list)
2016-09-25 20:29:17 +00:00
self.assertTrue(len(req.json()['system']) > 1)
2019-01-24 20:43:50 +00: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)
self.assertEqual(req.text, "Active")
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)
2014-07-09 11:50:25 +00:00
def test_999_stop_server(self):
2015-09-22 10:29:29 +00:00
"""Stop the Glances Web Server."""
2014-07-09 11:50:25 +00: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 11:50:25 +00:00
if __name__ == '__main__':
unittest.main()