1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
# |
4
|
|
|
# Glances - An eye on your system |
5
|
|
|
# |
6
|
|
|
# Copyright (C) 2019 Nicolargo <[email protected]> |
7
|
|
|
# |
8
|
|
|
# Glances is free software; you can redistribute it and/or modify |
9
|
|
|
# it under the terms of the GNU Lesser General Public License as published by |
10
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
# (at your option) any later version. |
12
|
|
|
# |
13
|
|
|
# Glances is distributed in the hope that it will be useful, |
14
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
# GNU Lesser General Public License for more details. |
17
|
|
|
# |
18
|
|
|
# You should have received a copy of the GNU Lesser General Public License |
19
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
|
21
|
|
|
"""Glances unitary tests suite for the XML-RPC API.""" |
22
|
|
|
|
23
|
|
|
import json |
24
|
|
|
import shlex |
25
|
|
|
import subprocess |
26
|
|
|
import time |
27
|
|
|
import unittest |
28
|
|
|
|
29
|
|
|
from glances import __version__ |
30
|
|
|
from glances.compat import ServerProxy |
31
|
|
|
|
32
|
|
|
SERVER_PORT = 61234 |
33
|
|
|
URL = "http://localhost:%s" % SERVER_PORT |
34
|
|
|
pid = None |
35
|
|
|
|
36
|
|
|
# Init the XML-RPC client |
37
|
|
|
client = ServerProxy(URL) |
38
|
|
|
|
39
|
|
|
# Unitest class |
40
|
|
|
# ============== |
41
|
|
|
print('XML-RPC API unitary tests for Glances %s' % __version__) |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
class TestGlances(unittest.TestCase): |
45
|
|
|
"""Test Glances class.""" |
46
|
|
|
|
47
|
|
|
def setUp(self): |
48
|
|
|
"""The function is called *every time* before test_*.""" |
49
|
|
|
print('\n' + '=' * 78) |
50
|
|
|
|
51
|
|
View Code Duplication |
def test_000_start_server(self): |
|
|
|
|
52
|
|
|
"""Start the Glances Web Server.""" |
53
|
|
|
global pid |
54
|
|
|
|
55
|
|
|
print('INFO: [TEST_000] Start the Glances Web Server') |
56
|
|
|
cmdline = "python -m glances -s -p %s" % SERVER_PORT |
57
|
|
|
print("Run the Glances Server on port %s" % SERVER_PORT) |
58
|
|
|
args = shlex.split(cmdline) |
59
|
|
|
pid = subprocess.Popen(args) |
60
|
|
|
print("Please wait...") |
61
|
|
|
time.sleep(1) |
62
|
|
|
|
63
|
|
|
self.assertTrue(pid is not None) |
64
|
|
|
|
65
|
|
|
def test_001_all(self): |
66
|
|
|
"""All.""" |
67
|
|
|
method = "getAll()" |
68
|
|
|
print('INFO: [TEST_001] Connection test') |
69
|
|
|
print("XML-RPC request: %s" % method) |
70
|
|
|
req = json.loads(client.getAll()) |
71
|
|
|
|
72
|
|
|
self.assertIsInstance(req, dict) |
73
|
|
|
|
74
|
|
|
def test_002_pluginslist(self): |
75
|
|
|
"""Plugins list.""" |
76
|
|
|
method = "getAllPlugins()" |
77
|
|
|
print('INFO: [TEST_002] Get plugins list') |
78
|
|
|
print("XML-RPC request: %s" % method) |
79
|
|
|
req = json.loads(client.getAllPlugins()) |
80
|
|
|
|
81
|
|
|
self.assertIsInstance(req, list) |
82
|
|
|
|
83
|
|
|
def test_003_system(self): |
84
|
|
|
"""System.""" |
85
|
|
|
method = "getSystem()" |
86
|
|
|
print('INFO: [TEST_003] Method: %s' % method) |
87
|
|
|
req = json.loads(client.getSystem()) |
88
|
|
|
|
89
|
|
|
self.assertIsInstance(req, dict) |
90
|
|
|
|
91
|
|
|
def test_004_cpu(self): |
92
|
|
|
"""CPU.""" |
93
|
|
|
method = "getCpu(), getPerCpu(), getLoad() and getCore()" |
94
|
|
|
print('INFO: [TEST_004] Method: %s' % method) |
95
|
|
|
|
96
|
|
|
req = json.loads(client.getCpu()) |
97
|
|
|
self.assertIsInstance(req, dict) |
98
|
|
|
|
99
|
|
|
req = json.loads(client.getPerCpu()) |
100
|
|
|
self.assertIsInstance(req, list) |
101
|
|
|
|
102
|
|
|
req = json.loads(client.getLoad()) |
103
|
|
|
self.assertIsInstance(req, dict) |
104
|
|
|
|
105
|
|
|
req = json.loads(client.getCore()) |
106
|
|
|
self.assertIsInstance(req, dict) |
107
|
|
|
|
108
|
|
|
def test_005_mem(self): |
109
|
|
|
"""MEM.""" |
110
|
|
|
method = "getMem() and getMemSwap()" |
111
|
|
|
print('INFO: [TEST_005] Method: %s' % method) |
112
|
|
|
|
113
|
|
|
req = json.loads(client.getMem()) |
114
|
|
|
self.assertIsInstance(req, dict) |
115
|
|
|
|
116
|
|
|
req = json.loads(client.getMemSwap()) |
117
|
|
|
self.assertIsInstance(req, dict) |
118
|
|
|
|
119
|
|
|
def test_006_net(self): |
120
|
|
|
"""NETWORK.""" |
121
|
|
|
method = "getNetwork()" |
122
|
|
|
print('INFO: [TEST_006] Method: %s' % method) |
123
|
|
|
|
124
|
|
|
req = json.loads(client.getNetwork()) |
125
|
|
|
self.assertIsInstance(req, list) |
126
|
|
|
|
127
|
|
|
def test_007_disk(self): |
128
|
|
|
"""DISK.""" |
129
|
|
|
method = "getFs(), getFolders() and getDiskIO()" |
130
|
|
|
print('INFO: [TEST_007] Method: %s' % method) |
131
|
|
|
|
132
|
|
|
req = json.loads(client.getFs()) |
133
|
|
|
self.assertIsInstance(req, list) |
134
|
|
|
|
135
|
|
|
req = json.loads(client.getFolders()) |
136
|
|
|
self.assertIsInstance(req, list) |
137
|
|
|
|
138
|
|
|
req = json.loads(client.getDiskIO()) |
139
|
|
|
self.assertIsInstance(req, list) |
140
|
|
|
|
141
|
|
|
def test_008_sensors(self): |
142
|
|
|
"""SENSORS.""" |
143
|
|
|
method = "getSensors()" |
144
|
|
|
print('INFO: [TEST_008] Method: %s' % method) |
145
|
|
|
|
146
|
|
|
req = json.loads(client.getSensors()) |
147
|
|
|
self.assertIsInstance(req, list) |
148
|
|
|
|
149
|
|
|
def test_009_process(self): |
150
|
|
|
"""PROCESS.""" |
151
|
|
|
method = "getProcessCount() and getProcessList()" |
152
|
|
|
print('INFO: [TEST_009] Method: %s' % method) |
153
|
|
|
|
154
|
|
|
req = json.loads(client.getProcessCount()) |
155
|
|
|
self.assertIsInstance(req, dict) |
156
|
|
|
|
157
|
|
|
req = json.loads(client.getProcessList()) |
158
|
|
|
self.assertIsInstance(req, list) |
159
|
|
|
|
160
|
|
|
def test_010_all_limits(self): |
161
|
|
|
"""All limits.""" |
162
|
|
|
method = "getAllLimits()" |
163
|
|
|
print('INFO: [TEST_010] Method: %s' % method) |
164
|
|
|
|
165
|
|
|
req = json.loads(client.getAllLimits()) |
166
|
|
|
self.assertIsInstance(req, dict) |
167
|
|
|
self.assertIsInstance(req['cpu'], dict) |
168
|
|
|
|
169
|
|
|
def test_011_all_views(self): |
170
|
|
|
"""All views.""" |
171
|
|
|
method = "getAllViews()" |
172
|
|
|
print('INFO: [TEST_011] Method: %s' % method) |
173
|
|
|
|
174
|
|
|
req = json.loads(client.getAllViews()) |
175
|
|
|
self.assertIsInstance(req, dict) |
176
|
|
|
self.assertIsInstance(req['cpu'], dict) |
177
|
|
|
|
178
|
|
|
def test_012_irq(self): |
179
|
|
|
"""IRQS""" |
180
|
|
|
method = "getIrqs()" |
181
|
|
|
print('INFO: [TEST_012] Method: %s' % method) |
182
|
|
|
req = json.loads(client.getIrq()) |
183
|
|
|
self.assertIsInstance(req, list) |
184
|
|
|
|
185
|
|
|
def test_013_plugin_views(self): |
186
|
|
|
"""Plugin views.""" |
187
|
|
|
method = "getViewsCpu()" |
188
|
|
|
print('INFO: [TEST_013] Method: %s' % method) |
189
|
|
|
|
190
|
|
|
req = json.loads(client.getViewsCpu()) |
191
|
|
|
self.assertIsInstance(req, dict) |
192
|
|
|
|
193
|
|
|
def test_999_stop_server(self): |
194
|
|
|
"""Stop the Glances Web Server.""" |
195
|
|
|
print('INFO: [TEST_999] Stop the Glances Server') |
196
|
|
|
|
197
|
|
|
print("Stop the Glances Server") |
198
|
|
|
pid.terminate() |
199
|
|
|
time.sleep(1) |
200
|
|
|
|
201
|
|
|
self.assertTrue(True) |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
if __name__ == '__main__': |
205
|
|
|
unittest.main() |
206
|
|
|
|