Completed
Push — master ( e61e99...a0b053 )
by Nicolas
01:23
created

TestGlances.test_013_gpu()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
#
4
# Glances - An eye on your system
5
#
6
# Copyright (C) 2017 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."""
22
23
import sys
24
import time
25
import unittest
26
27
# Global variables
28
# =================
29
30
# Init Glances core
31
from glances.main import GlancesMain
32
core = GlancesMain()
33
if not core.is_standalone():
34
    print('ERROR: Glances core should be ran in standalone mode')
35
    sys.exit(1)
36
37
# Init Glances stats
38
from glances.stats import GlancesStats
39
stats = GlancesStats()
40
41
from glances import __version__
42
from glances.globals import WINDOWS, LINUX
43
from glances.outputs.glances_bars import Bar
44
45
# Unitest class
46
# ==============
47
print('Unitary tests for Glances %s' % __version__)
48
49
50
class TestGlances(unittest.TestCase):
51
    """Test Glances class."""
52
53
    def setUp(self):
54
        """The function is called *every time* before test_*."""
55
        print('\n' + '=' * 78)
56
57
    def test_000_update(self):
58
        """Update stats (mandatory step for all the stats).
59
60
        The update is made twice (for rate computation).
61
        """
62
        print('INFO: [TEST_000] Test the stats update function')
63
        try:
64
            stats.update()
65
        except Exception as e:
66
            print('ERROR: Stats update failed: %s' % e)
67
            self.assertTrue(False)
68
        time.sleep(1)
69
        try:
70
            stats.update()
71
        except Exception as e:
72
            print('ERROR: Stats update failed: %s' % e)
73
            self.assertTrue(False)
74
75
        self.assertTrue(True)
76
77
    def test_001_plugins(self):
78
        """Check mandatory plugins."""
79
        plugins_to_check = ['system', 'cpu', 'load', 'mem', 'memswap', 'network', 'diskio', 'fs', 'irq']
80
        print('INFO: [TEST_001] Check the mandatory plugins list: %s' % ', '.join(plugins_to_check))
81
        plugins_list = stats.getAllPlugins()
82
        for plugin in plugins_to_check:
83
            self.assertTrue(plugin in plugins_list)
84
85
    def test_002_system(self):
86
        """Check SYSTEM plugin."""
87
        stats_to_check = ['hostname', 'os_name']
88
        print('INFO: [TEST_002] Check SYSTEM stats: %s' % ', '.join(stats_to_check))
89
        stats_grab = stats.get_plugin('system').get_raw()
90
        for stat in stats_to_check:
91
            # Check that the key exist
92
            self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat)
93
        print('INFO: SYSTEM stats: %s' % stats_grab)
94
95
    def test_003_cpu(self):
96
        """Check CPU plugin."""
97
        stats_to_check = ['system', 'user', 'idle']
98
        print('INFO: [TEST_003] Check mandatory CPU stats: %s' % ', '.join(stats_to_check))
99
        stats_grab = stats.get_plugin('cpu').get_raw()
100
        for stat in stats_to_check:
101
            # Check that the key exist
102
            self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat)
103
            # Check that % is > 0 and < 100
104
            self.assertGreaterEqual(stats_grab[stat], 0)
105
            self.assertLessEqual(stats_grab[stat], 100)
106
        print('INFO: CPU stats: %s' % stats_grab)
107
108
    @unittest.skipIf(WINDOWS, "Load average not available on Windows")
109
    def test_004_load(self):
110
        """Check LOAD plugin."""
111
        stats_to_check = ['cpucore', 'min1', 'min5', 'min15']
112
        print('INFO: [TEST_004] Check LOAD stats: %s' % ', '.join(stats_to_check))
113
        stats_grab = stats.get_plugin('load').get_raw()
114
        for stat in stats_to_check:
115
            # Check that the key exist
116
            self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat)
117
            # Check that % is > 0
118
            self.assertGreaterEqual(stats_grab[stat], 0)
119
        print('INFO: LOAD stats: %s' % stats_grab)
120
121
    def test_005_mem(self):
122
        """Check MEM plugin."""
123
        stats_to_check = ['available', 'used', 'free', 'total']
124
        print('INFO: [TEST_005] Check MEM stats: %s' % ', '.join(stats_to_check))
125
        stats_grab = stats.get_plugin('mem').get_raw()
126
        for stat in stats_to_check:
127
            # Check that the key exist
128
            self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat)
129
            # Check that % is > 0
130
            self.assertGreaterEqual(stats_grab[stat], 0)
131
        print('INFO: MEM stats: %s' % stats_grab)
132
133
    def test_006_swap(self):
134
        """Check MEMSWAP plugin."""
135
        stats_to_check = ['used', 'free', 'total']
136
        print('INFO: [TEST_006] Check SWAP stats: %s' % ', '.join(stats_to_check))
137
        stats_grab = stats.get_plugin('memswap').get_raw()
138
        for stat in stats_to_check:
139
            # Check that the key exist
140
            self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat)
141
            # Check that % is > 0
142
            self.assertGreaterEqual(stats_grab[stat], 0)
143
        print('INFO: SWAP stats: %s' % stats_grab)
144
145
    def test_007_network(self):
146
        """Check NETWORK plugin."""
147
        print('INFO: [TEST_007] Check NETWORK stats')
148
        stats_grab = stats.get_plugin('network').get_raw()
149
        self.assertTrue(type(stats_grab) is list, msg='Network stats is not a list')
150
        print('INFO: NETWORK stats: %s' % stats_grab)
151
152
    def test_008_diskio(self):
153
        """Check DISKIO plugin."""
154
        print('INFO: [TEST_008] Check DISKIO stats')
155
        stats_grab = stats.get_plugin('diskio').get_raw()
156
        self.assertTrue(type(stats_grab) is list, msg='DiskIO stats is not a list')
157
        print('INFO: diskio stats: %s' % stats_grab)
158
159
    def test_009_fs(self):
160
        """Check File System plugin."""
161
        # stats_to_check = [ ]
162
        print('INFO: [TEST_009] Check FS stats')
163
        stats_grab = stats.get_plugin('fs').get_raw()
164
        self.assertTrue(type(stats_grab) is list, msg='FileSystem stats is not a list')
165
        print('INFO: FS stats: %s' % stats_grab)
166
167
    def test_010_processes(self):
168
        """Check Process plugin."""
169
        # stats_to_check = [ ]
170
        print('INFO: [TEST_010] Check PROCESS stats')
171
        stats_grab = stats.get_plugin('processcount').get_raw()
172
        # total = stats_grab['total']
173
        self.assertTrue(type(stats_grab) is dict, msg='Process count stats is not a dict')
174
        print('INFO: PROCESS count stats: %s' % stats_grab)
175
        stats_grab = stats.get_plugin('processlist').get_raw()
176
        self.assertTrue(type(stats_grab) is list, msg='Process count stats is not a list')
177
        print('INFO: PROCESS list stats: %s items in the list' % len(stats_grab))
178
        # Check if number of processes in the list equal counter
179
        # self.assertEqual(total, len(stats_grab))
180
181
    def test_011_folders(self):
182
        """Check File System plugin."""
183
        # stats_to_check = [ ]
184
        print('INFO: [TEST_011] Check FOLDER stats')
185
        stats_grab = stats.get_plugin('folders').get_raw()
186
        self.assertTrue(type(stats_grab) is list, msg='Folders stats is not a list')
187
        print('INFO: Folders stats: %s' % stats_grab)
188
189
    def test_012_ip(self):
190
        """Check IP plugin."""
191
        print('INFO: [TEST_012] Check IP stats')
192
        stats_grab = stats.get_plugin('ip').get_raw()
193
        self.assertTrue(type(stats_grab) is dict, msg='IP stats is not a dict')
194
        print('INFO: IP stats: %s' % stats_grab)
195
196
    @unittest.skipIf(not LINUX, "IRQs available only on Linux")
197
    def test_013_irq(self):
198
        """Check IRQ plugin."""
199
        print('INFO: [TEST_013] Check IRQ stats')
200
        stats_grab = stats.get_plugin('irq').get_raw()
201
        self.assertTrue(type(stats_grab) is list, msg='IRQ stats is not a list')
202
        print('INFO: IRQ stats: %s' % stats_grab)
203
204
    @unittest.skipIf(not LINUX, "GPU available only on Linux")
205
    def test_013_gpu(self):
206
        """Check GPU plugin."""
207
        print('INFO: [TEST_014] Check GPU stats')
208
        stats_grab = stats.get_plugin('gpu').get_raw()
209
        self.assertTrue(type(stats_grab) is list, msg='GPU stats is not a list')
210
        print('INFO: GPU stats: %s' % stats_grab)
211
212
    def test_095_methods(self):
213
        """Test mandatories methods"""
214
        print('INFO: [TEST_095] Mandatories methods')
215
        mandatories_methods = ['reset', 'update']
216
        plugins_list = stats.getAllPlugins()
217
        for plugin in plugins_list:
218
            for method in mandatories_methods:
219
                self.assertTrue(hasattr(stats.get_plugin(plugin), method),
220
                                msg='{} has no method {}()'.format(plugin, method))
221
222
    def test_096_views(self):
223
        """Test get_views method"""
224
        print('INFO: [TEST_096] Test views')
225
        plugins_list = stats.getAllPlugins()
226
        for plugin in plugins_list:
227
            stats_grab = stats.get_plugin(plugin).get_raw()
228
            views_grab = stats.get_plugin(plugin).get_views()
229
            self.assertTrue(type(views_grab) is dict,
230
                            msg='{} view is not a dict'.format(plugin))
231
232
    def test_097_attribute(self):
233
        """Test GlancesAttribute classe"""
234
        print('INFO: [TEST_097] Test attribute')
235
        # GlancesAttribute
236
        from glances.attribute import GlancesAttribute
237
        a = GlancesAttribute('a', description='ad', history_max_size=3)
238
        self.assertEqual(a.name, 'a')
239
        self.assertEqual(a.description, 'ad')
240
        a.description = 'adn'
241
        self.assertEqual(a.description, 'adn')
242
        a.value = 1
243
        a.value = 2
244
        self.assertEqual(len(a.history), 2)
245
        a.value = 3
246
        self.assertEqual(len(a.history), 3)
247
        a.value = 4
248
        # Check if history_max_size=3 is OK
249
        self.assertEqual(len(a.history), 3)
250
        self.assertEqual(a.history_size(), 3)
251
        self.assertEqual(a.history_len(), 3)
252
        self.assertEqual(a.history_value()[1], 4)
253
        self.assertEqual(a.history_mean(nb=3), 4.5)
254
255
    def test_098_history(self):
256
        """Test GlancesHistory classe"""
257
        print('INFO: [TEST_098] Test history')
258
        # GlancesHistory
259
        from glances.history import GlancesHistory
260
        h = GlancesHistory()
261
        h.add('a', 1)
262
        h.add('a', 2)
263
        h.add('a', 3)
264
        h.add('b', 10)
265
        h.add('b', 20)
266
        h.add('b', 30)
267
        self.assertEqual(len(h.get()), 2)
268
        self.assertEqual(len(h.get()['a']), 3)
269
        h.reset()
270
        self.assertEqual(len(h.get()), 2)
271
        self.assertEqual(len(h.get()['a']), 0)
272
273
    def test_099_output_bars_must_be_between_0_and_100_percent(self):
274
        """Test quick look plugin.
275
276
        > bar.min_value
277
        0
278
        > bar.max_value
279
        100
280
        > bar.percent = -1
281
        > bar.percent
282
        0
283
        > bar.percent = 101
284
        > bar.percent
285
        100
286
        """
287
        print('INFO: [TEST_099] Test progress bar')
288
        bar = Bar(size=1)
289
        bar.percent = -1
290
        self.assertLessEqual(bar.percent, bar.min_value)
291
        bar.percent = 101
292
        self.assertGreaterEqual(bar.percent, bar.max_value)
293
294
    def test_999_the_end(self):
295
        """Free all the stats"""
296
        print('INFO: [TEST_999] Free the stats')
297
        stats.end()
298
        self.assertTrue(True)
299
300
if __name__ == '__main__':
301
    unittest.main()
302