glances/unitest-xmlrpc.py

197 lines
5.6 KiB
Python
Raw Normal View History

2014-07-12 20:02:31 +04:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Glances - An eye on your system
#
2015-09-21 20:39:39 +03:00
# Copyright (C) 2015 Nicolargo <nicolas@nicolargo.com>
2014-07-12 20:02:31 +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-21 20:39:39 +03:00
"""Glances unitary tests suite for the XML-RPC API."""
2014-07-12 20:02:31 +04:00
2015-09-21 20:39:39 +03:00
import json
2014-07-12 20:02:31 +04:00
import shlex
import subprocess
2015-09-21 20:39:39 +03:00
import time
import unittest
2014-07-12 20:02:31 +04:00
from glances import __version__
from glances.compat import ServerProxy
2015-09-21 20:39:39 +03:00
2014-07-12 20:02:31 +04:00
SERVER_PORT = 61234
URL = "http://localhost:%s" % SERVER_PORT
pid = None
2015-09-21 20:39:39 +03:00
# Init the XML-RPC client
2014-10-03 00:13:48 +04:00
client = ServerProxy(URL)
2014-07-12 20:02:31 +04:00
# Unitest class
# ==============
print('XML-RPC API unitary tests for Glances %s' % __version__)
2015-09-21 20:39:39 +03:00
2014-07-12 20:02:31 +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-21 20:39:39 +03:00
"""Start the Glances Web Server."""
2014-07-12 20:02:31 +04:00
global pid
2015-09-21 20:39:39 +03:00
print('INFO: [TEST_000] Start the Glances Web Server')
cmdline = "python -m glances -s -p %s" % SERVER_PORT
2014-07-12 20:02:31 +04:00
print("Run the Glances Server on port %s" % SERVER_PORT)
args = shlex.split(cmdline)
pid = subprocess.Popen(args)
print("Please wait...")
time.sleep(1)
2014-07-12 20:02:31 +04:00
self.assertTrue(pid is not None)
def test_001_all(self):
2015-09-21 20:39:39 +03:00
"""All."""
2014-07-12 20:02:31 +04:00
method = "getAll()"
print('INFO: [TEST_001] Connection test')
2015-09-21 20:39:39 +03:00
print("XML-RPC request: %s" % method)
2014-07-12 20:02:31 +04:00
req = json.loads(client.getAll())
2015-09-21 19:49:49 +03:00
self.assertIsInstance(req, dict)
2014-07-12 20:02:31 +04:00
def test_002_pluginslist(self):
2015-09-21 20:39:39 +03:00
"""Plugins list."""
2014-07-12 20:02:31 +04:00
method = "getAllPlugins()"
print('INFO: [TEST_002] Get plugins list')
2015-09-21 20:39:39 +03:00
print("XML-RPC request: %s" % method)
2014-07-12 20:02:31 +04:00
req = json.loads(client.getAllPlugins())
2015-09-21 19:49:49 +03:00
self.assertIsInstance(req, list)
2014-07-12 20:02:31 +04:00
def test_003_system(self):
2015-09-21 20:39:39 +03:00
"""System."""
2014-07-12 20:02:31 +04:00
method = "getSystem()"
print('INFO: [TEST_003] Method: %s' % method)
req = json.loads(client.getSystem())
2015-09-21 19:49:49 +03:00
self.assertIsInstance(req, dict)
2014-07-12 20:02:31 +04:00
def test_004_cpu(self):
2015-09-21 20:39:39 +03:00
"""CPU."""
2014-07-12 20:02:31 +04:00
method = "getCpu(), getPerCpu(), getLoad() and getCore()"
print('INFO: [TEST_004] Method: %s' % method)
req = json.loads(client.getCpu())
2015-09-21 19:49:49 +03:00
self.assertIsInstance(req, dict)
2014-07-12 20:02:31 +04:00
req = json.loads(client.getPerCpu())
2015-09-21 19:49:49 +03:00
self.assertIsInstance(req, list)
2014-07-12 20:02:31 +04:00
req = json.loads(client.getLoad())
2015-09-21 19:49:49 +03:00
self.assertIsInstance(req, dict)
2014-07-12 20:02:31 +04:00
req = json.loads(client.getCore())
2015-09-21 19:49:49 +03:00
self.assertIsInstance(req, dict)
2014-07-12 20:02:31 +04:00
def test_005_mem(self):
2015-09-21 20:39:39 +03:00
"""MEM."""
2014-07-12 20:02:31 +04:00
method = "getMem() and getMemSwap()"
print('INFO: [TEST_005] Method: %s' % method)
req = json.loads(client.getMem())
2015-09-21 19:49:49 +03:00
self.assertIsInstance(req, dict)
2014-07-12 20:02:31 +04:00
req = json.loads(client.getMemSwap())
2015-09-21 19:49:49 +03:00
self.assertIsInstance(req, dict)
2014-07-12 20:02:31 +04:00
def test_006_net(self):
2015-09-21 20:39:39 +03:00
"""NETWORK."""
2014-07-12 20:02:31 +04:00
method = "getNetwork()"
print('INFO: [TEST_006] Method: %s' % method)
req = json.loads(client.getNetwork())
2015-09-21 19:49:49 +03:00
self.assertIsInstance(req, list)
2014-07-12 20:02:31 +04:00
def test_007_disk(self):
2015-09-21 20:39:39 +03:00
"""DISK."""
method = "getFs(), getFolders() and getDiskIO()"
2014-07-12 20:02:31 +04:00
print('INFO: [TEST_007] Method: %s' % method)
req = json.loads(client.getFs())
2015-09-21 19:49:49 +03:00
self.assertIsInstance(req, list)
2014-07-12 20:02:31 +04:00
req = json.loads(client.getFolders())
self.assertIsInstance(req, list)
2014-07-12 20:02:31 +04:00
req = json.loads(client.getDiskIO())
2015-09-21 19:49:49 +03:00
self.assertIsInstance(req, list)
2014-07-12 20:02:31 +04:00
def test_008_sensors(self):
2015-09-21 20:39:39 +03:00
"""SENSORS."""
2014-07-12 20:02:31 +04:00
method = "getSensors()"
print('INFO: [TEST_008] Method: %s' % method)
req = json.loads(client.getSensors())
2015-09-21 19:49:49 +03:00
self.assertIsInstance(req, list)
2014-07-12 20:02:31 +04:00
def test_009_process(self):
2015-09-21 20:39:39 +03:00
"""PROCESS."""
2014-07-12 20:02:31 +04:00
method = "getProcessCount() and getProcessList()"
print('INFO: [TEST_009] Method: %s' % method)
req = json.loads(client.getProcessCount())
2015-09-21 19:49:49 +03:00
self.assertIsInstance(req, dict)
2014-07-12 20:02:31 +04:00
req = json.loads(client.getProcessList())
2015-09-21 19:49:49 +03:00
self.assertIsInstance(req, list)
2014-07-12 20:02:31 +04:00
def test_010_all_limits(self):
2015-09-21 20:39:39 +03:00
"""All limits."""
method = "getAllLimits()"
print('INFO: [TEST_010] Method: %s' % method)
req = json.loads(client.getAllLimits())
2015-09-21 19:49:49 +03:00
self.assertIsInstance(req, dict)
self.assertIsInstance(req['cpu'], dict)
def test_011_all_views(self):
2015-09-21 20:39:39 +03:00
"""All views."""
method = "getAllViews()"
print('INFO: [TEST_011] Method: %s' % method)
req = json.loads(client.getAllViews())
2015-09-21 19:49:49 +03:00
self.assertIsInstance(req, dict)
self.assertIsInstance(req['cpu'], dict)
2016-08-19 20:26:23 +03:00
def test_012_irq(self):
"""IRQS"""
2016-08-19 20:26:23 +03:00
method = "getIrqs()"
print('INFO: [TEST_012] Method: %s' % method)
req = json.loads(client.getIrq())
self.assertIsInstance(req, list)
2014-07-12 20:02:31 +04:00
def test_999_stop_server(self):
2015-09-21 20:39:39 +03:00
"""Stop the Glances Web Server."""
2014-07-12 20:02:31 +04:00
print('INFO: [TEST_999] Stop the Glances Server')
print("Stop the Glances Server")
pid.terminate()
time.sleep(1)
self.assertTrue(True)
if __name__ == '__main__':
unittest.main()