unitest.py 10.1 KB
Newer Older
N
Nicolargo 已提交
1 2 3 4 5
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Glances - An eye on your system
#
6
# Copyright (C) 2016 Nicolargo <nicolas@nicolargo.com>
N
Nicolargo 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19
#
# 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/>.
A
PEP 257  
Alessio Sergi 已提交
20 21

"""Glances unitary tests suite."""
N
Nicolargo 已提交
22

A
Alessio Sergi 已提交
23 24
import sys
import time
N
Nicolargo 已提交
25 26 27
import unittest

# Global variables
A
PEP 257  
Alessio Sergi 已提交
28
# =================
N
Nicolargo 已提交
29 30

# Init Glances core
31
from glances.main import GlancesMain
N
Nicolargo 已提交
32 33 34 35 36 37
core = GlancesMain()
if not core.is_standalone():
    print('ERROR: Glances core should be ran in standalone mode')
    sys.exit(1)

# Init Glances stats
38
from glances.stats import GlancesStats
N
Nicolargo 已提交
39 40
stats = GlancesStats()

41
from glances import __version__
A
angelopoerio 已提交
42
from glances.globals import WINDOWS, LINUX
43
from glances.outputs.glances_bars import Bar
A
Alessio Sergi 已提交
44

N
Nicolargo 已提交
45
# Unitest class
A
PEP 257  
Alessio Sergi 已提交
46
# ==============
47
print('Unitary tests for Glances %s' % __version__)
48

A
PEP 257  
Alessio Sergi 已提交
49

A
Alessio Sergi 已提交
50
class TestGlances(unittest.TestCase):
A
PEP 257  
Alessio Sergi 已提交
51
    """Test Glances class."""
N
Nicolargo 已提交
52 53

    def setUp(self):
A
PEP 257  
Alessio Sergi 已提交
54
        """The function is called *every time* before test_*."""
A
Alessio Sergi 已提交
55
        print('\n' + '=' * 78)
N
Nicolargo 已提交
56 57

    def test_000_update(self):
A
PEP 257  
Alessio Sergi 已提交
58
        """Update stats (mandatory step for all the stats).
N
Nicolargo 已提交
59

A
PEP 257  
Alessio Sergi 已提交
60 61
        The update is made twice (for rate computation).
        """
N
Nicolargo 已提交
62 63 64
        print('INFO: [TEST_000] Test the stats update function')
        try:
            stats.update()
65
        except Exception as e:
A
Alessio Sergi 已提交
66
            print('ERROR: Stats update failed: %s' % e)
N
Nicolargo 已提交
67 68 69 70
            self.assertTrue(False)
        time.sleep(1)
        try:
            stats.update()
71
        except Exception as e:
A
Alessio Sergi 已提交
72
            print('ERROR: Stats update failed: %s' % e)
N
Nicolargo 已提交
73
            self.assertTrue(False)
A
PEP 257  
Alessio Sergi 已提交
74

N
Nicolargo 已提交
75 76 77
        self.assertTrue(True)

    def test_001_plugins(self):
A
PEP 257  
Alessio Sergi 已提交
78
        """Check mandatory plugins."""
A
angelopoerio 已提交
79
        plugins_to_check = ['system', 'cpu', 'load', 'mem', 'memswap', 'network', 'diskio', 'fs', 'irq']
A
Alessio Sergi 已提交
80 81 82 83
        print('INFO: [TEST_001] Check the mandatory plugins list: %s' % ', '.join(plugins_to_check))
        plugins_list = stats.getAllPlugins()
        for plugin in plugins_to_check:
            self.assertTrue(plugin in plugins_list)
N
Nicolargo 已提交
84 85

    def test_002_cpu(self):
A
PEP 257  
Alessio Sergi 已提交
86
        """Check SYSTEM plugin."""
A
Alessio Sergi 已提交
87
        stats_to_check = ['hostname', 'os_name']
N
Nicolargo 已提交
88 89
        print('INFO: [TEST_002] Check SYSTEM stats: %s' % ', '.join(stats_to_check))
        stats_grab = stats.get_plugin('system').get_raw()
A
Alessio Sergi 已提交
90
        for stat in stats_to_check:
N
Nicolargo 已提交
91
            # Check that the key exist
A
Alessio Sergi 已提交
92
            self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat)
N
Nicolargo 已提交
93 94 95
        print('INFO: SYSTEM stats: %s' % stats_grab)

    def test_003_cpu(self):
A
PEP 257  
Alessio Sergi 已提交
96
        """Check CPU plugin."""
A
Alessio Sergi 已提交
97
        stats_to_check = ['system', 'user', 'idle']
N
Nicolargo 已提交
98 99
        print('INFO: [TEST_003] Check mandatory CPU stats: %s' % ', '.join(stats_to_check))
        stats_grab = stats.get_plugin('cpu').get_raw()
A
Alessio Sergi 已提交
100
        for stat in stats_to_check:
N
Nicolargo 已提交
101
            # Check that the key exist
A
Alessio Sergi 已提交
102
            self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat)
N
Nicolargo 已提交
103
            # Check that % is > 0 and < 100
A
Alessio Sergi 已提交
104 105
            self.assertGreaterEqual(stats_grab[stat], 0)
            self.assertLessEqual(stats_grab[stat], 100)
N
Nicolargo 已提交
106 107
        print('INFO: CPU stats: %s' % stats_grab)

N
nicolargo 已提交
108
    @unittest.skipIf(WINDOWS, "Load average not available on Windows")
N
Nicolargo 已提交
109
    def test_004_load(self):
A
PEP 257  
Alessio Sergi 已提交
110
        """Check LOAD plugin."""
A
Alessio Sergi 已提交
111
        stats_to_check = ['cpucore', 'min1', 'min5', 'min15']
N
Nicolargo 已提交
112 113
        print('INFO: [TEST_004] Check LOAD stats: %s' % ', '.join(stats_to_check))
        stats_grab = stats.get_plugin('load').get_raw()
A
Alessio Sergi 已提交
114
        for stat in stats_to_check:
N
Nicolargo 已提交
115
            # Check that the key exist
A
Alessio Sergi 已提交
116
            self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat)
N
Nicolargo 已提交
117
            # Check that % is > 0
A
Alessio Sergi 已提交
118
            self.assertGreaterEqual(stats_grab[stat], 0)
N
Nicolargo 已提交
119 120 121
        print('INFO: LOAD stats: %s' % stats_grab)

    def test_005_mem(self):
A
PEP 257  
Alessio Sergi 已提交
122
        """Check MEM plugin."""
A
Alessio Sergi 已提交
123
        stats_to_check = ['available', 'used', 'free', 'total']
N
Nicolargo 已提交
124 125
        print('INFO: [TEST_005] Check MEM stats: %s' % ', '.join(stats_to_check))
        stats_grab = stats.get_plugin('mem').get_raw()
A
Alessio Sergi 已提交
126
        for stat in stats_to_check:
N
Nicolargo 已提交
127
            # Check that the key exist
A
Alessio Sergi 已提交
128
            self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat)
N
Nicolargo 已提交
129
            # Check that % is > 0
A
Alessio Sergi 已提交
130
            self.assertGreaterEqual(stats_grab[stat], 0)
N
Nicolargo 已提交
131 132 133
        print('INFO: MEM stats: %s' % stats_grab)

    def test_006_swap(self):
A
PEP 257  
Alessio Sergi 已提交
134
        """Check MEMSWAP plugin."""
A
Alessio Sergi 已提交
135
        stats_to_check = ['used', 'free', 'total']
N
Nicolargo 已提交
136 137
        print('INFO: [TEST_006] Check SWAP stats: %s' % ', '.join(stats_to_check))
        stats_grab = stats.get_plugin('memswap').get_raw()
A
Alessio Sergi 已提交
138
        for stat in stats_to_check:
N
Nicolargo 已提交
139
            # Check that the key exist
A
Alessio Sergi 已提交
140
            self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat)
N
Nicolargo 已提交
141
            # Check that % is > 0
A
Alessio Sergi 已提交
142
            self.assertGreaterEqual(stats_grab[stat], 0)
N
Nicolargo 已提交
143 144 145
        print('INFO: SWAP stats: %s' % stats_grab)

    def test_007_network(self):
A
PEP 257  
Alessio Sergi 已提交
146
        """Check NETWORK plugin."""
N
Nicolargo 已提交
147 148 149 150 151 152
        print('INFO: [TEST_007] Check NETWORK stats')
        stats_grab = stats.get_plugin('network').get_raw()
        self.assertTrue(type(stats_grab) is list, msg='Network stats is not a list')
        print('INFO: NETWORK stats: %s' % stats_grab)

    def test_008_diskio(self):
A
PEP 257  
Alessio Sergi 已提交
153
        """Check DISKIO plugin."""
154
        print('INFO: [TEST_008] Check DISKIO stats')
N
Nicolargo 已提交
155 156 157 158 159
        stats_grab = stats.get_plugin('diskio').get_raw()
        self.assertTrue(type(stats_grab) is list, msg='DiskIO stats is not a list')
        print('INFO: diskio stats: %s' % stats_grab)

    def test_009_fs(self):
A
PEP 257  
Alessio Sergi 已提交
160
        """Check File System plugin."""
A
Alessio Sergi 已提交
161
        # stats_to_check = [ ]
N
Nicolargo 已提交
162 163 164 165 166 167
        print('INFO: [TEST_009] Check FS stats')
        stats_grab = stats.get_plugin('fs').get_raw()
        self.assertTrue(type(stats_grab) is list, msg='FileSystem stats is not a list')
        print('INFO: FS stats: %s' % stats_grab)

    def test_010_processes(self):
A
PEP 257  
Alessio Sergi 已提交
168
        """Check Process plugin."""
A
Alessio Sergi 已提交
169
        # stats_to_check = [ ]
N
Nicolargo 已提交
170 171
        print('INFO: [TEST_010] Check PROCESS stats')
        stats_grab = stats.get_plugin('processcount').get_raw()
A
Alessio Sergi 已提交
172
        # total = stats_grab['total']
N
Nicolargo 已提交
173 174 175 176 177 178
        self.assertTrue(type(stats_grab) is dict, msg='Process count stats is not a dict')
        print('INFO: PROCESS count stats: %s' % stats_grab)
        stats_grab = stats.get_plugin('processlist').get_raw()
        self.assertTrue(type(stats_grab) is list, msg='Process count stats is not a list')
        print('INFO: PROCESS list stats: %s items in the list' % len(stats_grab))
        # Check if number of processes in the list equal counter
179
        # self.assertEqual(total, len(stats_grab))
N
Nicolargo 已提交
180

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    def test_011_folders(self):
        """Check File System plugin."""
        # stats_to_check = [ ]
        print('INFO: [TEST_011] Check FOLDER stats')
        stats_grab = stats.get_plugin('folders').get_raw()
        self.assertTrue(type(stats_grab) is list, msg='Folders stats is not a list')
        print('INFO: Folders stats: %s' % stats_grab)

    def test_012_ip(self):
        """Check IP plugin."""
        print('INFO: [TEST_012] Check IP stats')
        stats_grab = stats.get_plugin('ip').get_raw()
        self.assertTrue(type(stats_grab) is dict, msg='IP stats is not a dict')
        print('INFO: IP stats: %s' % stats_grab)

A
angelopoerio 已提交
196
    @unittest.skipIf(not LINUX, "IRQs available only on Linux")
A
angelopoerio 已提交
197 198 199 200 201 202 203
    def test_013_irq(self):
        """Check IRQ plugin."""
        print('INFO: [TEST_013] Check IRQ stats')
        stats_grab = stats.get_plugin('irq').get_raw()
        self.assertTrue(type(stats_grab) is list, msg='IRQ stats is not a list')
        print('INFO: IRQ stats: %s' % stats_grab)

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    def test_097_attribute(self):
        """Test GlancesAttribute classe"""
        print('INFO: [TEST_097] Test attribute')
        # GlancesAttribute
        from glances.attribute import GlancesAttribute
        a = GlancesAttribute('a', description='ad', history_max_size=3)
        self.assertEqual(a.name, 'a')
        self.assertEqual(a.description, 'ad')
        a.description = 'adn'
        self.assertEqual(a.description, 'adn')
        a.value = 1
        a.value = 2
        self.assertEqual(len(a.history), 2)
        a.value = 3
        self.assertEqual(len(a.history), 3)
        a.value = 4
        # Check if history_max_size=3 is OK
        self.assertEqual(len(a.history), 3)
        self.assertEqual(a.history_size(), 3)
        self.assertEqual(a.history_len(), 3)
        self.assertEqual(a.history_value()[1], 4)
        self.assertEqual(a.history_mean(nb=3), 4.5)

    def test_098_history(self):
        """Test GlancesHistory classe"""
        print('INFO: [TEST_098] Test history')
        # GlancesHistory
        from glances.history import GlancesHistory
        h = GlancesHistory()
        h.add('a', 1)
        h.add('a', 2)
        h.add('a', 3)
        h.add('b', 10)
        h.add('b', 20)
        h.add('b', 30)
        self.assertEqual(len(h.get()), 2)
        self.assertEqual(len(h.get()['a']), 3)
        h.reset()
        self.assertEqual(len(h.get()), 2)
        self.assertEqual(len(h.get()['a']), 0)

245
    def test_099_output_bars_must_be_between_0_and_100_percent(self):
246 247 248 249 250 251 252 253 254 255 256 257 258
        """Test quick look plugin.

        > bar.min_value
        0
        > bar.max_value
        100
        > bar.percent = -1
        > bar.percent
        0
        > bar.percent = 101
        > bar.percent
        100
        """
259
        print('INFO: [TEST_099] Test progress bar')
260
        bar = Bar(size=1)
261 262 263 264
        bar.percent = -1
        self.assertLessEqual(bar.percent, bar.min_value)
        bar.percent = 101
        self.assertGreaterEqual(bar.percent, bar.max_value)
265

266 267 268 269 270 271
    def test_999_the_end(self):
        """Free all the stats"""
        print('INFO: [TEST_999] Free the stats')
        stats.end()
        self.assertTrue(True)

N
Nicolargo 已提交
272 273
if __name__ == '__main__':
    unittest.main()