Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
蜕变的菜鸟
glances
提交
b31d824f
G
glances
项目概览
蜕变的菜鸟
/
glances
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
glances
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
b31d824f
编写于
7月 09, 2014
作者:
N
Nicolas Hennion
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add unitest script for the RESTFul API
上级
682390e9
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
186 addition
and
6 deletion
+186
-6
glances/core/glances_main.py
glances/core/glances_main.py
+10
-6
unitest-restful.py
unitest-restful.py
+176
-0
未找到文件。
glances/core/glances_main.py
浏览文件 @
b31d824f
...
...
@@ -82,7 +82,7 @@ class GlancesMain(object):
help
=
_
(
'connect to a Glances server by IPv4/IPv6 address or hostname'
))
parser
.
add_argument
(
'-s'
,
'--server'
,
action
=
'store_true'
,
default
=
False
,
dest
=
'server'
,
help
=
_
(
'run Glances in server mode'
))
parser
.
add_argument
(
'-p'
,
'--port'
,
default
=
self
.
server_port
,
type
=
int
,
dest
=
'port'
,
parser
.
add_argument
(
'-p'
,
'--port'
,
default
=
None
,
type
=
int
,
dest
=
'port'
,
help
=
_
(
'define the client/server TCP port [default: {0}]'
).
format
(
self
.
server_port
))
parser
.
add_argument
(
'-B'
,
'--bind'
,
default
=
'0.0.0.0'
,
dest
=
'bind_address'
,
help
=
_
(
'bind server to the given IPv4/IPv6 address or hostname'
))
...
...
@@ -128,12 +128,16 @@ class GlancesMain(object):
from
logging
import
DEBUG
logger
.
setLevel
(
DEBUG
)
# In web server mode, default:
# - refresh time: 5 sec
# - host port: 61208
# Client/server Port
if
args
.
port
is
None
:
if
args
.
webserver
:
args
.
port
=
self
.
web_server_port
else
:
args
.
port
=
self
.
server_port
# In web server mode, defaul refresh time: 5 sec
if
args
.
webserver
:
args
.
time
=
5
args
.
port
=
self
.
web_server_port
args
.
time
=
5
# Server or client login/password
args
.
username
=
self
.
username
...
...
unitest-restful.py
0 → 100755
浏览文件 @
b31d824f
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Glances - An eye on your system
#
# Copyright (C) 2014 Nicolargo <nicolas@nicolargo.com>
#
# 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/>.
"""Glances unitary tests suite for the RESTFul API."""
import
gettext
import
locale
import
sys
import
time
import
unittest
import
shlex
import
subprocess
import
requests
import
json
import
types
from
glances.core.glances_globals
import
(
appname
,
is_linux
,
version
)
SERVER_PORT
=
61234
URL
=
"http://localhost:%s/api/2"
%
SERVER_PORT
pid
=
None
# Global variables
# =================
# Unitary test is only available from a GNU/Linus machine
if
not
is_linux
:
print
(
'ERROR: RESTFul API unitaries tests should be ran on GNU/Linux operating system'
)
sys
.
exit
(
2
)
else
:
print
(
'Unitary tests for {0} {1}'
.
format
(
appname
,
version
))
# Import local settings
from
glances.core.glances_globals
import
gettext_domain
,
locale_dir
locale
.
setlocale
(
locale
.
LC_ALL
,
''
)
gettext
.
install
(
gettext_domain
,
locale_dir
)
# Init Glances core
from
glances.core.glances_main
import
GlancesMain
core
=
GlancesMain
()
if
not
core
.
is_standalone
():
print
(
'ERROR: Glances core should be ran in standalone mode'
)
sys
.
exit
(
1
)
# Init Glances stats
from
glances.core.glances_stats
import
GlancesStats
stats
=
GlancesStats
()
# Unitest class
# ==============
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
):
"""Start the Glances Web Server"""
print
(
'INFO: [TEST_000] Start the Glances Web Server'
)
global
pid
cmdline
=
"/usr/bin/python -m glances -w -p %s"
%
SERVER_PORT
print
(
"Run the Glances Web Server on port %s"
%
SERVER_PORT
)
args
=
shlex
.
split
(
cmdline
)
pid
=
subprocess
.
Popen
(
args
)
print
(
"Please wait..."
)
time
.
sleep
(
1
)
self
.
assertTrue
(
pid
is
not
None
)
def
test_001_all
(
self
):
"""All"""
method
=
"all"
print
(
'INFO: [TEST_001] Connection test'
)
print
(
"HTTP RESTFul request: %s/%s"
%
(
URL
,
method
))
req
=
requests
.
get
(
"%s/%s"
%
(
URL
,
method
))
self
.
assertTrue
(
req
.
ok
)
def
test_002_pluginslist
(
self
):
"""Plugins list"""
method
=
"pluginslist"
print
(
'INFO: [TEST_002] Plugins list'
)
print
(
"HTTP RESTFul request: %s/%s"
%
(
URL
,
method
))
req
=
requests
.
get
(
"%s/%s"
%
(
URL
,
method
))
self
.
assertTrue
(
req
.
ok
)
self
.
assertIsInstance
(
req
.
json
,
types
.
ListType
)
self
.
assertIn
(
'cpu'
,
req
.
json
)
def
test_003_plugins
(
self
):
"""Plugins"""
method
=
"pluginslist"
print
(
'INFO: [TEST_003] Plugins'
)
plist
=
requests
.
get
(
"%s/%s"
%
(
URL
,
method
))
for
p
in
plist
.
json
:
print
(
"HTTP RESTFul request: %s/%s"
%
(
URL
,
p
))
req
=
requests
.
get
(
"%s/%s"
%
(
URL
,
p
))
self
.
assertTrue
(
req
.
ok
)
if
p
in
(
'uptime'
,
'now'
):
self
.
assertIsInstance
(
req
.
json
,
types
.
UnicodeType
)
elif
p
in
(
'fs'
,
'monitor'
,
'percpu'
,
'sensors'
,
'alert'
,
'processlist'
,
'diskio'
,
'hddtemp'
,
'batpercent'
,
'network'
):
self
.
assertIsInstance
(
req
.
json
,
types
.
ListType
)
elif
p
in
(
'psutilversion'
,
'help'
):
pass
else
:
self
.
assertIsInstance
(
req
.
json
,
types
.
DictType
)
def
test_004_items
(
self
):
"""Items"""
method
=
"cpu"
print
(
'INFO: [TEST_004] Items for the CPU method'
)
ilist
=
requests
.
get
(
"%s/%s"
%
(
URL
,
method
))
for
i
in
ilist
.
json
:
print
(
"HTTP RESTFul request: %s/%s/%s"
%
(
URL
,
method
,
i
))
req
=
requests
.
get
(
"%s/%s/%s"
%
(
URL
,
method
,
i
))
self
.
assertTrue
(
req
.
ok
)
self
.
assertIsInstance
(
req
.
json
,
types
.
DictType
)
self
.
assertIsInstance
(
req
.
json
[
i
],
types
.
FloatType
)
def
test_005_values
(
self
):
"""Valuess"""
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
)
self
.
assertIsInstance
(
req
.
json
,
types
.
DictType
)
def
test_999_stop_server
(
self
):
"""Stop the Glances Web Server"""
print
(
'INFO: [TEST_999] Stop the Glances Web Server'
)
print
(
"Stop the Glances Web Server"
)
pid
.
terminate
()
print
(
"Please wait..."
)
time
.
sleep
(
1
)
self
.
assertTrue
(
True
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录