glances/unitest-restful.py

210 lines
7.2 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
#
2017-01-09 14:09:17 +03:00
# Copyright (C) 2017 Nicolargo <nicolas@nicolargo.com>
2014-07-09 15:50:25 +04:00
#
# Glances is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Glances is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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
import numbers
2015-09-22 13:29:29 +03:00
import unittest
2014-07-09 15:50:25 +04:00
from glances import __version__
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
URL = "http://localhost:%s/api/2" % SERVER_PORT
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)
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-09-22 13:29:29 +03:00
print('INFO: [TEST_000] Start the Glances Web Server')
cmdline = "python -m glances -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)
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))
req = requests.get("%s/%s" % (URL, method))
2014-07-09 15:50:25 +04:00
self.assertTrue(req.ok)
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))
2014-07-09 15:50:25 +04:00
req = requests.get("%s/%s" % (URL, method))
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')
plist = requests.get("%s/%s" % (URL, method))
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))
2014-07-09 15:50:25 +04:00
req = requests.get("%s/%s" % (URL, p))
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 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')
ilist = requests.get("%s/%s" % (URL, method))
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))
2014-07-09 15:50:25 +04:00
req = requests.get("%s/%s/%s" % (URL, method, i))
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))
req = requests.get("%s/%s/pid/0" % (URL, method))
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))
req = requests.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))
req = requests.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')
plist = requests.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))
req = requests.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')
plist = requests.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))
req = requests.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))
req = requests.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))
req = requests.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))
req = requests.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))
req = requests.get("%s/cpu/system/%s/3" % (URL, method))
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)
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)
if __name__ == '__main__':
unittest.main()