Add a script to serve the Redfish mockup files

This patch is adding a small script at tools/mockup_server.py that can
be used to create a HTTP server to serve the Redfish mockup files. This
enables people to test the library without the need of having a real
hardware.

The patch also documents the use of the script.

Change-Id: I6ab21ebba27590f9eefcb7ce79620a9bc4ecec5a
This commit is contained in:
Lucas Alvares Gomes
2017-02-16 17:01:44 +00:00
parent 3d8196ab1f
commit b7d2165eb9
2 changed files with 114 additions and 2 deletions

View File

@@ -73,5 +73,26 @@ To use sushy in a project:
# Get a list of allowed boot source target values
print(sys_inst.get_allowed_system_boot_source_values())
.. TODO: Get current boot source device, enabled and mode
.. TODO: Document how to setup the Redfish mockup server
Running the mockup server
-------------------------
Sushy ships with a small script at ``tools/mockup_server.py``
that creates a HTTP server to serve any of the `Redfish mockups
<https://www.dmtf.org/standards/redfish>`_. This enables users to test
the library without having a real hardware.
To run it, do:
#. Download the .zip containing the Redfish mockups files from
https://www.dmtf.org/standards/redfish, for example::
wget https://www.dmtf.org/sites/default/files/standards/documents/DSP2043_1.0.0.zip
#. Extract it somewhere in the file-system::
unzip DSP2043_1.0.0.zip -d <output-path>
#. Now run the ``mockup_server.py`` script::
python sushy/tools/mockup_server.py -m <output-path>/DSP2043-server

91
tools/mockup_server.py Executable file
View File

@@ -0,0 +1,91 @@
#!/usr/bin/env python
#
# Copyright 2017 Red Hat, 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.
import argparse
import os
import sys
from six.moves import BaseHTTPServer
REDFISH_MOCKUP_FILES = None
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
REDFISH_SUBURI = '/redfish/v1'
def _log_request(self, method):
global REQUEST_NUMBER
print(self.headers)
content_length = int(self.headers.get('content-length', 0))
if content_length > 0:
print('Data: %s\n' % self.rfile.read(content_length))
def do_GET(self):
self._log_request('GET')
path = self.path.rstrip('/')
if not path.startswith(self.REDFISH_SUBURI):
self.send_error(404)
return
resource_path = path.replace(self.REDFISH_SUBURI, '').lstrip('/')
fpath = os.path.join(REDFISH_MOCKUP_FILES, resource_path, 'index.json')
if not os.path.exists(fpath):
self.send_error(404, 'Sub-URI %s not found' % resource_path)
return
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
with open(fpath, 'r') as f:
self.wfile.write(f.read().encode('utf-8'))
def do_POST(self):
self._log_request('POST')
self.send_response(204)
self.end_headers()
def do_PATCH(self):
self._log_request('PATCH')
self.send_response(204)
self.end_headers()
def parse_args():
parser = argparse.ArgumentParser('MockupServer')
parser.add_argument('-p', '--port',
type=int,
default=8000,
help='The port to bind the server to')
parser.add_argument('-m', '--mockup-files',
type=str,
required=True,
help=('The path to the Redfish Mockup files in '
'the filesystem'))
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
if not os.path.exists(args.mockup_files):
print('Mockup files %s not found' % args.mockup_files)
sys.exit(1)
REDFISH_MOCKUP_FILES = os.path.realpath(args.mockup_files)
http_server = BaseHTTPServer.HTTPServer(('', args.port), RequestHandler)
http_server.serve_forever()