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 84db5fbba5a..dc463c1c4d3 100644 --- a/tests/common/helpers/platform_api/scripts/platform_api_server.py +++ b/tests/common/helpers/platform_api/scripts/platform_api_server.py @@ -2,10 +2,14 @@ import inspect import json import os +import sys import syslog -from io import BytesIO -from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler +# TODO: Clean this up once we no longer need to support Python 2 +if sys.version_info.major == 3: + from http.server import HTTPServer, BaseHTTPRequestHandler +else: + from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler import sonic_platform @@ -66,7 +70,13 @@ def do_platform_api(self): obj = platform while len(path) != 1: _dir = path.pop() - args = inspect.getargspec(getattr(obj, 'get_' + _dir)).args + + # TODO: Clean this up once we no longer need to support Python 2 + if sys.version_info.major == 3: + args = inspect.getfullargspec(getattr(obj, 'get_' + _dir)).args + else: + args = inspect.getargspec(getattr(obj, 'get_' + _dir)).args + if 'index' in args: _idx = int(path.pop()) obj = getattr(obj, 'get_' + _dir)(_idx) @@ -83,10 +93,7 @@ def do_platform_api(self): except NotImplementedError as e: syslog.syslog(syslog.LOG_WARNING, "API '{}' not implemented".format(api)) - response = BytesIO() - response.write(json.dumps({'res': res}, default=obj_serialize)) - - self.wfile.write(response.getvalue()) + self.wfile.write(json.dumps({'res': res}, default=obj_serialize).encode('utf-8')) if __name__ == '__main__': diff --git a/tests/platform_tests/api/conftest.py b/tests/platform_tests/api/conftest.py index cc2b641c69f..9c8fc0203e8 100644 --- a/tests/platform_tests/api/conftest.py +++ b/tests/platform_tests/api/conftest.py @@ -20,13 +20,17 @@ def start_platform_api_service(duthost, localhost): timeout=5, module_ignore_errors=True) if 'exception' in res: + # TODO: Remove this check once we no longer need to support Python 2 + res = duthost.command('docker exec -i pmon python3 -c "import sonic_platform"', module_ignore_errors=True) + py3_platform_api_available = not res['failed'] + supervisor_conf = [ - "[program:platform_api_server]", - "command=/usr/bin/python /opt/platform_api_server.py --port {}".format(SERVER_PORT), - "autostart=True", - "autorestart=True", - "stdout_logfile=syslog", - "stderr_logfile=syslog", + '[program:platform_api_server]', + 'command=/usr/bin/python{} /opt/platform_api_server.py --port {}'.format('3' if py3_platform_api_available else '2', SERVER_PORT), + 'autostart=True', + 'autorestart=True', + 'stdout_logfile=syslog', + 'stderr_logfile=syslog', ] dest_path = os.path.join(os.sep, 'tmp', 'platform_api_server.conf') pmon_path = os.path.join(os.sep, 'etc', 'supervisor', 'conf.d', 'platform_api_server.conf')