unitest-restful.py 5.9 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Glances - An eye on your system
#
6
# Copyright (C) 2015 Nicolargo <nicolas@nicolargo.com>
7 8 9 10 11 12 13 14 15 16 17 18 19 20
#
# 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/>.

21
"""Glances unitary tests suite for the RESTful API."""
22 23 24

import shlex
import subprocess
25 26
import time
import unittest
27

28 29
from glances.compat import text_type
from glances.globals import version
30

31 32
import requests

33 34 35 36 37 38
SERVER_PORT = 61234
URL = "http://localhost:%s/api/2" % SERVER_PORT
pid = None

# Unitest class
# ==============
39 40
print('RESTful API unitary tests for Glances %s' % version)

41 42 43 44 45 46 47 48 49

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):
50
        """Start the Glances Web Server."""
51
        global pid
52

53
        print('INFO: [TEST_000] Start the Glances Web Server')
54
        cmdline = "python -m glances -w -p %s" % SERVER_PORT
55 56 57 58
        print("Run the Glances Web Server on port %s" % SERVER_PORT)
        args = shlex.split(cmdline)
        pid = subprocess.Popen(args)
        print("Please wait...")
59
        time.sleep(3)
60

61 62 63
        self.assertTrue(pid is not None)

    def test_001_all(self):
64
        """All."""
65
        method = "all"
66
        print('INFO: [TEST_001] Get all stats')
67
        print("HTTP RESTful request: %s/%s" % (URL, method))
68
        req = requests.get("%s/%s" % (URL, method))
69 70 71 72

        self.assertTrue(req.ok)

    def test_002_pluginslist(self):
73
        """Plugins list."""
74 75
        method = "pluginslist"
        print('INFO: [TEST_002] Plugins list')
76
        print("HTTP RESTful request: %s/%s" % (URL, method))
77 78 79
        req = requests.get("%s/%s" % (URL, method))

        self.assertTrue(req.ok)
A
Alessio Sergi 已提交
80
        self.assertIsInstance(req.json(), list)
N
Nicolargo 已提交
81
        self.assertIn('cpu', req.json())
82 83

    def test_003_plugins(self):
84
        """Plugins."""
85
        method = "pluginslist"
86 87 88
        print('INFO: [TEST_003] Plugins')
        plist = requests.get("%s/%s" % (URL, method))

N
Nicolargo 已提交
89
        for p in plist.json():
90
            print("HTTP RESTful request: %s/%s" % (URL, p))
91 92 93
            req = requests.get("%s/%s" % (URL, p))
            self.assertTrue(req.ok)
            if p in ('uptime', 'now'):
94
                self.assertIsInstance(req.json(), text_type)
95
            elif p in ('fs', 'monitor', 'percpu', 'sensors', 'alert', 'processlist',
96
                       'diskio', 'hddtemp', 'batpercent', 'network', 'folders'):
A
Alessio Sergi 已提交
97
                self.assertIsInstance(req.json(), list)
98 99 100
            elif p in ('psutilversion', 'help'):
                pass
            else:
A
Alessio Sergi 已提交
101
                self.assertIsInstance(req.json(), dict)
102 103

    def test_004_items(self):
104
        """Items."""
105 106 107 108
        method = "cpu"
        print('INFO: [TEST_004] Items for the CPU method')
        ilist = requests.get("%s/%s" % (URL, method))

N
Nicolargo 已提交
109
        for i in ilist.json():
110
            print("HTTP RESTful request: %s/%s/%s" % (URL, method, i))
111 112
            req = requests.get("%s/%s/%s" % (URL, method, i))
            self.assertTrue(req.ok)
A
Alessio Sergi 已提交
113 114
            self.assertIsInstance(req.json(), dict)
            self.assertIsInstance(req.json()[i], float)
115 116

    def test_005_values(self):
117
        """Values."""
118 119 120 121 122 123
        method = "processlist"
        print('INFO: [TEST_005] Item=Value for the PROCESSLIST method')
        print("%s/%s/pid/0" % (URL, method))
        req = requests.get("%s/%s/pid/0" % (URL, method))

        self.assertTrue(req.ok)
A
Alessio Sergi 已提交
124
        self.assertIsInstance(req.json(), dict)
125

126
    def test_006_all_limits(self):
127
        """All limits."""
128 129
        method = "all/limits"
        print('INFO: [TEST_006] Get all limits')
130
        print("HTTP RESTful request: %s/%s" % (URL, method))
131 132 133
        req = requests.get("%s/%s" % (URL, method))

        self.assertTrue(req.ok)
A
Alessio Sergi 已提交
134
        self.assertIsInstance(req.json(), dict)
135

136
    def test_007_all_views(self):
137
        """All views."""
138 139
        method = "all/views"
        print('INFO: [TEST_007] Get all views')
140
        print("HTTP RESTful request: %s/%s" % (URL, method))
141 142 143
        req = requests.get("%s/%s" % (URL, method))

        self.assertTrue(req.ok)
A
Alessio Sergi 已提交
144
        self.assertIsInstance(req.json(), dict)
145 146

    def test_008_plugins_limits(self):
147
        """Plugins limits."""
148 149 150 151 152
        method = "pluginslist"
        print('INFO: [TEST_008] Plugins limits')
        plist = requests.get("%s/%s" % (URL, method))

        for p in plist.json():
153
            print("HTTP RESTful request: %s/%s/limits" % (URL, p))
154 155
            req = requests.get("%s/%s/limits" % (URL, p))
            self.assertTrue(req.ok)
A
Alessio Sergi 已提交
156
            self.assertIsInstance(req.json(), dict)
157 158

    def test_009_plugins_views(self):
159
        """Plugins views."""
160 161 162 163 164
        method = "pluginslist"
        print('INFO: [TEST_009] Plugins views')
        plist = requests.get("%s/%s" % (URL, method))

        for p in plist.json():
165
            print("HTTP RESTful request: %s/%s/views" % (URL, p))
166 167
            req = requests.get("%s/%s/views" % (URL, p))
            self.assertTrue(req.ok)
A
Alessio Sergi 已提交
168
            self.assertIsInstance(req.json(), dict)
169

170
    def test_999_stop_server(self):
171
        """Stop the Glances Web Server."""
172 173 174 175 176 177 178 179 180 181
        print('INFO: [TEST_999] Stop the Glances Web Server')

        print("Stop the Glances Web Server")
        pid.terminate()
        time.sleep(1)

        self.assertTrue(True)

if __name__ == '__main__':
    unittest.main()