148 lines
4.3 KiB
Python
148 lines
4.3 KiB
Python
"""Shared things for the tests"""
|
|
|
|
import json
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
|
|
def redfish_json_path(file_name):
|
|
"""
|
|
Get the path to a json file based on the location of this test file -
|
|
basicaly ./redfish_json/file.json
|
|
"""
|
|
return (
|
|
Path(os.path.dirname(os.path.realpath(__file__)))
|
|
/ "redfish_json"
|
|
/ file_name
|
|
)
|
|
|
|
|
|
def get_test_content(file_name):
|
|
"""Load json from a file"""
|
|
with open(redfish_json_path(file_name), encoding="utf-8") as json_file:
|
|
_data = json_file.read()
|
|
|
|
return _data
|
|
|
|
|
|
def get_test_json(file_name):
|
|
"""Load json from a file"""
|
|
with open(redfish_json_path(file_name), encoding="utf-8") as json_file:
|
|
_data = json_file.read()
|
|
_json = json.loads(_data)
|
|
|
|
return _json
|
|
|
|
|
|
class MockRedfishResponse:
|
|
"""Mock a RedFish response"""
|
|
|
|
json_dict: dict = {
|
|
"@odata.context": "/redfish/v1/$metadata#ServiceRoot.ServiceRoot",
|
|
"@odata.id": "/redfish/v1",
|
|
"@odata.type": "#ServiceRoot.v1_3_0.ServiceRoot",
|
|
"AccountService": {
|
|
"@odata.id": "/redfish/v1/Managers/iDRAC.Embedded.1/AccountService"
|
|
},
|
|
"Chassis": {"@odata.id": "/redfish/v1/Chassis"},
|
|
"Description": "Root Service",
|
|
"EventService": {"@odata.id": "/redfish/v1/EventService"},
|
|
"Fabrics": {"@odata.id": "/redfish/v1/Fabrics"},
|
|
"Id": "RootService",
|
|
"JsonSchemas": {"@odata.id": "/redfish/v1/JSONSchemas"},
|
|
"Links": {"Sessions": {"@odata.id": "/redfish/v1/Sessions"}},
|
|
"Managers": {"@odata.id": "/redfish/v1/Managers"},
|
|
"Name": "Root Service",
|
|
"Oem": {
|
|
"Dell": {
|
|
"@odata.type": "#DellServiceRoot.v1_0_0.ServiceRootSummary",
|
|
"IsBranded": 0,
|
|
"ManagerMACAddress": "54:9F:35:1A:0D:D8",
|
|
"ServiceTag": "JYYZY42",
|
|
}
|
|
},
|
|
"Product": "Integrated Dell Remote Access Controller",
|
|
"ProtocolFeaturesSupported": {
|
|
"ExpandQuery": {
|
|
"ExpandAll": True,
|
|
"Levels": True,
|
|
"Links": True,
|
|
"MaxLevels": 1,
|
|
"NoLinks": True,
|
|
},
|
|
"FilterQuery": True,
|
|
"SelectQuery": True,
|
|
},
|
|
"RedfishVersion": "1.4.0",
|
|
"Registries": {"@odata.id": "/redfish/v1/Registries"},
|
|
"SessionService": {"@odata.id": "/redfish/v1/SessionService"},
|
|
"Systems": {"@odata.id": "/redfish/v1/Systems"},
|
|
"Tasks": {"@odata.id": "/redfish/v1/TaskService"},
|
|
"UpdateService": {"@odata.id": "/redfish/v1/UpdateService"},
|
|
}
|
|
status_code = 200
|
|
|
|
def __init__(self, status_code=200, json_dict=None):
|
|
self.status_code = status_code
|
|
if json_dict is not None:
|
|
self.json_dict = json
|
|
|
|
@property
|
|
def text(self):
|
|
"""Return self.json_dict as a json string"""
|
|
return json.dumps(self.json_dict)
|
|
|
|
def update(self, update_dict):
|
|
"""Update self.json_dict with the specified dict"""
|
|
self.json_dict.update(update_dict)
|
|
|
|
|
|
class MockResponse:
|
|
"""A Mocked requests reponse"""
|
|
|
|
# pylint: disable=invalid-name,too-few-public-methods
|
|
|
|
def __init__(self, status_code=200, ok=True, content="", headers=None):
|
|
self.status_code = status_code
|
|
self._ok = ok
|
|
self.content = content
|
|
self._headers = headers
|
|
|
|
def json(self):
|
|
"""return json from the mocked response"""
|
|
return json.loads(self.content)
|
|
|
|
@property
|
|
def text(self):
|
|
"""return text from the mocked response"""
|
|
return self.content
|
|
|
|
@property
|
|
def ok(self):
|
|
"""return the OK flag."""
|
|
return self._ok
|
|
|
|
@property
|
|
def headers(self):
|
|
"""Return the headers dict"""
|
|
return self._headers
|
|
|
|
|
|
class MockWatchedJobResponse:
|
|
"""Special class for testing watching a job"""
|
|
|
|
# pylint: disable=too-few-public-methods, invalid-name
|
|
|
|
def __init__(self):
|
|
self.accessed = 0
|
|
self._json = get_test_json("api_job_details.json")
|
|
self.status_code = 200
|
|
self.ok = True
|
|
|
|
@property
|
|
def text(self):
|
|
"""Return json, increment % complete each time"""
|
|
self._json["PercentComplete"] = self.accessed * 20
|
|
self.accessed += 1
|
|
return json.dumps(self._json)
|