Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions tests/common/helpers/platform_api/scripts/platform_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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__':
Expand Down
16 changes: 10 additions & 6 deletions tests/platform_tests/api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down