Implement rpc framework and get_interface unittest

This commit is contained in:
yaowei 2015-12-24 11:34:40 +08:00
parent eba5e1df83
commit e6b0fbf365
13 changed files with 41 additions and 60 deletions

View File

@ -17,13 +17,11 @@ import os
import sys
from SocketServer import ThreadingMixIn
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
from stetho.common import log
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
LISTEN_PROTOCOL = 'tcp'
LISTEN_ADDR = '0.0.0.0'
LISTEN_PORT = 9698
@ -38,9 +36,8 @@ def main():
endpoint = (LISTEN_ADDR, LISTEN_PORT)
server = AsyncJSONRPCServer(endpoint)
server.register_multicall_functions()
s_utils.register_api(server, agent_api)
agent_utils.register_api(server, agent_api)
server.serve_forever()
LOG.info("Agent listening in %s:%s" % endpoint)
if __name__ == '__main__':

View File

@ -18,16 +18,11 @@ import time
import shlex
import subprocess
from threading import Timer
from stetho.common import log
from stetho.common import resource
log = log.get_logger('/var/log/stetho/stetho-agent.log')
from stetho.agent.common import resource
def execute(cmd, shell=False, root=False, timeout=10):
try:
log.debug(cmd)
subproc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=shell)
timer = Timer(timeout, lambda proc: proc.kill(), [subproc])
timer.start()
@ -42,9 +37,12 @@ def execute(cmd, shell=False, root=False, timeout=10):
def get_interface(interface):
"""Support Centos standard physical interface,
such as eth0.
"""
cmd = ['ifconfig', interface]
stdcode, stdout = execute(cmd)
if stdcode == 0:
try:
stdcode, stdout = execute(cmd)
inf = resource.Interface(interface)
pattern = r'<([A-Z]+)'
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.ether = stdout[3][6:23]
return stdcode, '', inf.make_dict()
else:
return stdcode, stdout.pop(), None
except Exception as e:
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

View File

@ -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

View File

@ -34,6 +34,11 @@ class TestUtils(unittest.TestCase):
root = '/tmp/%s'
return root % filename
if __name__ == '__main__':
unittest.main()
def test_make_response(self):
para = dict()
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)

View File

@ -14,15 +14,11 @@
# under the License.
import unittest
import logging
from stetho.common import log
from stetho.agent import api
class TestLog(unittest.TestCase):
def test_get_logger(self):
log_test = log.get_logger(filename='test')
self.assertEqual(type(log_test), type(logging.getLogger()))
if __name__ == '__main__':
unittest.main()
class TestApi(unittest.TestCase):
def test_get_interface(self):
interface = 'eth0'
result = api.get_interface(interface)
self.assertEqual(result['code'], 0)