diff --git a/tests/common/helpers/platform_api/scripts/platform_api_server.py b/tests/common/helpers/platform_api/scripts/platform_api_server.py index 02a86b26f28..4c94a55854d 100644 --- a/tests/common/helpers/platform_api/scripts/platform_api_server.py +++ b/tests/common/helpers/platform_api/scripts/platform_api_server.py @@ -1,14 +1,32 @@ -import json import argparse import inspect -import sonic_platform +import json +import os +import syslog +from io import BytesIO from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler -from io import BytesIO + +import sonic_platform + +SYSLOG_IDENTIFIER = os.path.basename(__file__) platform = sonic_platform.platform.Platform() +def obj_serialize(obj): + ''' JSON serializer for objects not serializable by default json library code + We simply return a dictionary containing the object's class and module + ''' + syslog.syslog(syslog.LOG_WARNING, 'Unserializable object: {}.{}'.format(obj.__module__, obj.__class__.__name__)) + + data = { + '__class__': obj.__class__.__name__, + '__module__': obj.__module__ + } + return data + + class PlatformAPITestService(BaseHTTPRequestHandler): ''' Handles HTTP POST requests and translated them into platform API call. The expected URL path format is the following: @@ -61,14 +79,19 @@ def do_platform_api(self): res = getattr(obj, api)(*args) response = BytesIO() - response.write(json.dumps({'res': res})) + response.write(json.dumps({'res': res}, default=obj_serialize)) self.wfile.write(response.getvalue()) if __name__ == '__main__': parser = argparse.ArgumentParser() - parser.add_argument('-p', '--port', type=int, help='port to listent to', required=True) + parser.add_argument('-p', '--port', type=int, help='port to listen to', required=True) args = parser.parse_args() + + syslog.openlog(SYSLOG_IDENTIFIER) + httpd = HTTPServer(('', args.port), PlatformAPITestService) httpd.serve_forever() + + syslog.closelog()