Implement rpc framework and get_interface unittest
This commit is contained in:
parent
eba5e1df83
commit
e6b0fbf365
@ -17,13 +17,11 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
from SocketServer import ThreadingMixIn
|
from SocketServer import ThreadingMixIn
|
||||||
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
|
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
|
||||||
from stetho.common import log
|
|
||||||
from stetho.agent import api as agent_api
|
from stetho.agent import api as agent_api
|
||||||
from stetho.common import utils as s_utils
|
from stetho.agent.common import utils as agent_utils
|
||||||
|
|
||||||
|
|
||||||
# Listening endpoint
|
# Listening endpoint
|
||||||
LISTEN_PROTOCOL = 'tcp'
|
|
||||||
LISTEN_ADDR = '0.0.0.0'
|
LISTEN_ADDR = '0.0.0.0'
|
||||||
LISTEN_PORT = 9698
|
LISTEN_PORT = 9698
|
||||||
|
|
||||||
@ -38,9 +36,8 @@ def main():
|
|||||||
endpoint = (LISTEN_ADDR, LISTEN_PORT)
|
endpoint = (LISTEN_ADDR, LISTEN_PORT)
|
||||||
server = AsyncJSONRPCServer(endpoint)
|
server = AsyncJSONRPCServer(endpoint)
|
||||||
server.register_multicall_functions()
|
server.register_multicall_functions()
|
||||||
s_utils.register_api(server, agent_api)
|
agent_utils.register_api(server, agent_api)
|
||||||
server.serve_forever()
|
server.serve_forever()
|
||||||
LOG.info("Agent listening in %s:%s" % endpoint)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -18,16 +18,11 @@ import time
|
|||||||
import shlex
|
import shlex
|
||||||
import subprocess
|
import subprocess
|
||||||
from threading import Timer
|
from threading import Timer
|
||||||
from stetho.common import log
|
from stetho.agent.common import resource
|
||||||
from stetho.common import resource
|
|
||||||
|
|
||||||
|
|
||||||
log = log.get_logger('/var/log/stetho/stetho-agent.log')
|
|
||||||
|
|
||||||
|
|
||||||
def execute(cmd, shell=False, root=False, timeout=10):
|
def execute(cmd, shell=False, root=False, timeout=10):
|
||||||
try:
|
try:
|
||||||
log.debug(cmd)
|
|
||||||
subproc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=shell)
|
subproc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=shell)
|
||||||
timer = Timer(timeout, lambda proc: proc.kill(), [subproc])
|
timer = Timer(timeout, lambda proc: proc.kill(), [subproc])
|
||||||
timer.start()
|
timer.start()
|
||||||
@ -42,9 +37,12 @@ def execute(cmd, shell=False, root=False, timeout=10):
|
|||||||
|
|
||||||
|
|
||||||
def get_interface(interface):
|
def get_interface(interface):
|
||||||
|
"""Support Centos standard physical interface,
|
||||||
|
such as eth0.
|
||||||
|
"""
|
||||||
cmd = ['ifconfig', interface]
|
cmd = ['ifconfig', interface]
|
||||||
stdcode, stdout = execute(cmd)
|
try:
|
||||||
if stdcode == 0:
|
stdcode, stdout = execute(cmd)
|
||||||
inf = resource.Interface(interface)
|
inf = resource.Interface(interface)
|
||||||
pattern = r'<([A-Z]+)'
|
pattern = r'<([A-Z]+)'
|
||||||
inf.state = re.search(pattern, stdout[0]).groups()[0]
|
inf.state = re.search(pattern, stdout[0]).groups()[0]
|
||||||
@ -53,5 +51,22 @@ def get_interface(interface):
|
|||||||
(inf.inet, inf.netmask, inf.broadcast) = tmp
|
(inf.inet, inf.netmask, inf.broadcast) = tmp
|
||||||
inf.ether = stdout[3][6:23]
|
inf.ether = stdout[3][6:23]
|
||||||
return stdcode, '', inf.make_dict()
|
return stdcode, '', inf.make_dict()
|
||||||
else:
|
except Exception as e:
|
||||||
return stdcode, stdout.pop(), None
|
stdcode = 1
|
||||||
|
message = 'Get interface error.'
|
||||||
|
message = message if len(stdout) == 0 else stdout.pop(0)
|
||||||
|
return stdcode, message, None
|
||||||
|
|
||||||
|
|
||||||
|
def register_api(server, api_obj):
|
||||||
|
methods = dir(api_obj)
|
||||||
|
apis = filter(lambda m: not m.startswith('_'), methods)
|
||||||
|
[server.register_function(getattr(api_obj, api)) for api in apis]
|
||||||
|
|
||||||
|
|
||||||
|
def make_response(code=0, message='', data=dict()):
|
||||||
|
response = dict()
|
||||||
|
response['code'] = code
|
||||||
|
response['message'] = '' if message is None else message
|
||||||
|
response['data'] = dict() if data is None else data
|
||||||
|
return response
|
||||||
|
@ -1,32 +0,0 @@
|
|||||||
# Copyright 2015 UnitedStack, Inc.
|
|
||||||
# All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
# not use this file except in compliance with the License. You may obtain
|
|
||||||
# a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
# License for the specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
|
|
||||||
from json import JSONEncoder
|
|
||||||
from json import JSONDecoder
|
|
||||||
|
|
||||||
|
|
||||||
def register_api(server, api_obj):
|
|
||||||
methods = dir(api_obj)
|
|
||||||
apis = filter(lambda m: not m.startswith('__'), methods)
|
|
||||||
[server.register_function(getattr(api_obj, api)) for api in apis]
|
|
||||||
|
|
||||||
|
|
||||||
def make_response(code=0, message='', data=dict()):
|
|
||||||
response = dict()
|
|
||||||
response['code'] = code
|
|
||||||
response['message'] = message
|
|
||||||
response['data'] = data
|
|
||||||
return response
|
|
@ -34,6 +34,11 @@ class TestUtils(unittest.TestCase):
|
|||||||
root = '/tmp/%s'
|
root = '/tmp/%s'
|
||||||
return root % filename
|
return root % filename
|
||||||
|
|
||||||
|
def test_make_response(self):
|
||||||
if __name__ == '__main__':
|
para = dict()
|
||||||
unittest.main()
|
para['code'] = 0
|
||||||
|
para['message'] = 'Test make response.'
|
||||||
|
para['data'] = dict()
|
||||||
|
result = utils.make_response(para['code'], para['message'],
|
||||||
|
para['data'])
|
||||||
|
self.assertEqual(para, result)
|
@ -14,15 +14,11 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
import logging
|
from stetho.agent import api
|
||||||
|
|
||||||
from stetho.common import log
|
|
||||||
|
|
||||||
|
|
||||||
class TestLog(unittest.TestCase):
|
class TestApi(unittest.TestCase):
|
||||||
def test_get_logger(self):
|
def test_get_interface(self):
|
||||||
log_test = log.get_logger(filename='test')
|
interface = 'eth0'
|
||||||
self.assertEqual(type(log_test), type(logging.getLogger()))
|
result = api.get_interface(interface)
|
||||||
|
self.assertEqual(result['code'], 0)
|
||||||
if __name__ == '__main__':
|
|
||||||
unittest.main()
|
|
Loading…
x
Reference in New Issue
Block a user