unitest-restful: general cleaning

This commit is contained in:
Alessio Sergi 2015-09-22 12:29:29 +02:00
parent 075ba72d79
commit 183328dbd0

View File

@ -3,7 +3,7 @@
#
# Glances - An eye on your system
#
# Copyright (C) 2014 Nicolargo <nicolas@nicolargo.com>
# Copyright (C) 2015 Nicolargo <nicolas@nicolargo.com>
#
# 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
@ -18,37 +18,21 @@
# 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/>.
"""Glances unitary tests suite for the RESTFul API."""
"""Glances unitary tests suite for the RESTful API."""
import sys
import time
import unittest
import shlex
import subprocess
import requests
import json
import time
import unittest
from glances.core.glances_globals import version
import requests
SERVER_PORT = 61234
URL = "http://localhost:%s/api/2" % SERVER_PORT
pid = None
# Global variables
# =================
# Init Glances core
from glances.core.glances_main import GlancesMain
core = GlancesMain()
if not core.is_standalone():
print('ERROR: Glances core should be ran in standalone mode')
sys.exit(1)
# Init Glances stats
from glances.core.glances_stats import GlancesStats
stats = GlancesStats()
# Unitest class
# ==============
print('RESTful API unitary tests for Glances %s' % version)
@ -63,11 +47,10 @@ class TestGlances(unittest.TestCase):
print('\n' + '=' * 78)
def test_000_start_server(self):
"""Start the Glances Web Server"""
print('INFO: [TEST_000] Start the Glances Web Server')
"""Start the Glances Web Server."""
global pid
print('INFO: [TEST_000] Start the Glances Web Server')
cmdline = "python -m glances -w -p %s" % SERVER_PORT
print("Run the Glances Web Server on port %s" % SERVER_PORT)
args = shlex.split(cmdline)
@ -78,21 +61,19 @@ class TestGlances(unittest.TestCase):
self.assertTrue(pid is not None)
def test_001_all(self):
"""All"""
"""All."""
method = "all"
print('INFO: [TEST_001] Get all stats')
print("HTTP RESTFul request: %s/%s" % (URL, method))
print("HTTP RESTful request: %s/%s" % (URL, method))
req = requests.get("%s/%s" % (URL, method))
self.assertTrue(req.ok)
def test_002_pluginslist(self):
"""Plugins list"""
"""Plugins list."""
method = "pluginslist"
print('INFO: [TEST_002] Plugins list')
print("HTTP RESTFul request: %s/%s" % (URL, method))
print("HTTP RESTful request: %s/%s" % (URL, method))
req = requests.get("%s/%s" % (URL, method))
self.assertTrue(req.ok)
@ -100,19 +81,19 @@ class TestGlances(unittest.TestCase):
self.assertIn('cpu', req.json())
def test_003_plugins(self):
"""Plugins"""
"""Plugins."""
method = "pluginslist"
print('INFO: [TEST_003] Plugins')
plist = requests.get("%s/%s" % (URL, method))
for p in plist.json():
print("HTTP RESTFul request: %s/%s" % (URL, p))
print("HTTP RESTful request: %s/%s" % (URL, p))
req = requests.get("%s/%s" % (URL, p))
self.assertTrue(req.ok)
if p in ('uptime', 'now'):
self.assertIsInstance(req.json(), str)
elif p in ('fs', 'monitor', 'percpu', 'sensors', 'alert', 'processlist', 'diskio', 'hddtemp', 'batpercent', 'network'):
elif p in ('fs', 'monitor', 'percpu', 'sensors', 'alert', 'processlist',
'diskio', 'hddtemp', 'batpercent', 'network'):
self.assertIsInstance(req.json(), list)
elif p in ('psutilversion', 'help'):
pass
@ -120,24 +101,22 @@ class TestGlances(unittest.TestCase):
self.assertIsInstance(req.json(), dict)
def test_004_items(self):
"""Items"""
"""Items."""
method = "cpu"
print('INFO: [TEST_004] Items for the CPU method')
ilist = requests.get("%s/%s" % (URL, method))
for i in ilist.json():
print("HTTP RESTFul request: %s/%s/%s" % (URL, method, i))
print("HTTP RESTful request: %s/%s/%s" % (URL, method, i))
req = requests.get("%s/%s/%s" % (URL, method, i))
self.assertTrue(req.ok)
self.assertIsInstance(req.json(), dict)
self.assertIsInstance(req.json()[i], float)
def test_005_values(self):
"""Valuess"""
"""Values."""
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))
@ -145,60 +124,55 @@ class TestGlances(unittest.TestCase):
self.assertIsInstance(req.json(), dict)
def test_006_all_limits(self):
"""All"""
"""All limits."""
method = "all/limits"
print('INFO: [TEST_006] Get all limits')
print("HTTP RESTFul request: %s/%s" % (URL, method))
print("HTTP RESTful request: %s/%s" % (URL, method))
req = requests.get("%s/%s" % (URL, method))
self.assertTrue(req.ok)
self.assertIsInstance(req.json(), dict)
def test_007_all_views(self):
"""All"""
"""All views."""
method = "all/views"
print('INFO: [TEST_007] Get all views')
print("HTTP RESTFul request: %s/%s" % (URL, method))
print("HTTP RESTful request: %s/%s" % (URL, method))
req = requests.get("%s/%s" % (URL, method))
self.assertTrue(req.ok)
self.assertIsInstance(req.json(), dict)
def test_008_plugins_limits(self):
"""Plugins limits"""
"""Plugins limits."""
method = "pluginslist"
print('INFO: [TEST_008] Plugins limits')
plist = requests.get("%s/%s" % (URL, method))
for p in plist.json():
print("HTTP RESTFul request: %s/%s/limits" % (URL, p))
print("HTTP RESTful request: %s/%s/limits" % (URL, p))
req = requests.get("%s/%s/limits" % (URL, p))
self.assertTrue(req.ok)
self.assertIsInstance(req.json(), dict)
def test_009_plugins_views(self):
"""Plugins views"""
"""Plugins views."""
method = "pluginslist"
print('INFO: [TEST_009] Plugins views')
plist = requests.get("%s/%s" % (URL, method))
for p in plist.json():
print("HTTP RESTFul request: %s/%s/views" % (URL, p))
print("HTTP RESTful request: %s/%s/views" % (URL, p))
req = requests.get("%s/%s/views" % (URL, p))
self.assertTrue(req.ok)
self.assertIsInstance(req.json(), dict)
def test_999_stop_server(self):
"""Stop the Glances Web Server"""
"""Stop the Glances Web Server."""
print('INFO: [TEST_999] Stop the Glances Web Server')
print("Stop the Glances Web Server")
pid.terminate()
print("Please wait...")
time.sleep(1)
self.assertTrue(True)