glances/unitest-xmlrpc.py

201 lines
5.5 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
#
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <nicolas@nicolargo.com>
2014-07-12 20:02:31 +04:00
#
# SPDX-License-Identifier: LGPL-3.0-only
2014-07-12 20:02:31 +04:00
#
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
2021-08-25 14:07:44 +03:00
import os
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
2021-09-04 17:46:20 +03:00
from glances import __version__
2021-08-28 11:56:04 +03:00
from glances.globals 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')
2021-08-25 14:07:44 +03:00
if os.path.isfile('./venv/bin/python'):
cmdline = "./venv/bin/python"
else:
cmdline = "python"
2022-05-25 17:01:06 +03:00
cmdline += " -m glances -B localhost -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)
2017-05-14 12:46:23 +03:00
def test_013_plugin_views(self):
"""Plugin views."""
method = "getViewsCpu()"
print('INFO: [TEST_013] Method: %s' % method)
req = json.loads(client.getViewsCpu())
self.assertIsInstance(req, dict)
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)
2017-05-14 12:47:38 +03:00
2014-07-12 20:02:31 +04:00
if __name__ == '__main__':
unittest.main()