Skip to content

Commit

Permalink
Merge pull request #65 from DMTF/Fix55-Task-Handling
Browse files Browse the repository at this point in the history
Added method to poll tasks for tests using PATCH, POST, and DELETE
  • Loading branch information
mraineri authored Sep 15, 2023
2 parents 938a1e0 + 9c14538 commit 35c43b0
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 67 deletions.
37 changes: 18 additions & 19 deletions redfish_protocol_validator/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def add_account_via_patch(sut: SystemUnderTest, session, user, role, password,
if role:
payload['RoleId'] = role
headers = utils.get_etag_header(sut, session, uri)
response = session.patch(sut.rhost + uri, json=payload, headers=headers)
response = sut.patch(uri, json=payload, headers=headers, session=session)
sut.add_response(uri, response, resource_type=ResourceType.MANAGER_ACCOUNT,
request_type=request_type)
success = response.status_code == requests.codes.OK
Expand All @@ -166,8 +166,7 @@ def add_account_via_patch(sut: SystemUnderTest, session, user, role, password,
if 'Enabled' in data and data['Enabled'] is False:
headers = utils.get_etag_header(sut, session, uri)
payload = {'Enabled': True}
session.patch(sut.rhost + uri, json=payload,
headers=headers)
sut.patch(uri, json=payload, headers=headers, session=session)
sut.add_response(uri, response,
resource_type=ResourceType.MANAGER_ACCOUNT,
request_type=request_type)
Expand Down Expand Up @@ -199,7 +198,7 @@ def add_account(sut: SystemUnderTest, session,
payload = {'UserName': user, 'Password': password}
if role:
payload['RoleId'] = role
response = session.post(sut.rhost + sut.accounts_uri, json=payload)
response = sut.post(sut.accounts_uri, json=payload, session=session)
sut.add_response(sut.accounts_uri, response, request_type=request_type)

new_acct_uri = None
Expand Down Expand Up @@ -229,8 +228,8 @@ def patch_account(sut: SystemUnderTest, session, acct_uri,
pwd = new_password(sut)
payload = {'Password': pwd, 'BogusProp': 'foo'}
headers = utils.get_etag_header(sut, session, acct_uri)
response = session.patch(sut.rhost + acct_uri, json=payload,
headers=headers)
response = sut.patch(acct_uri, json=payload, headers=headers,
session=session)
if response.ok:
new_pwd = pwd
sut.add_response(acct_uri, response,
Expand All @@ -239,8 +238,8 @@ def patch_account(sut: SystemUnderTest, session, acct_uri,
# patch a single property that can never be updated
payload = {'BogusProp': 'foo'}
headers = utils.get_etag_header(sut, session, acct_uri)
response = session.patch(sut.rhost + acct_uri, json=payload,
headers=headers)
response = sut.patch(acct_uri, json=payload, headers=headers,
session=session)
sut.add_response(acct_uri, response,
resource_type=ResourceType.MANAGER_ACCOUNT,
request_type=RequestType.PATCH_BAD_PROP)
Expand All @@ -249,8 +248,8 @@ def patch_account(sut: SystemUnderTest, session, acct_uri,
else acct_uri + '/')
payload = {'@odata.id': odata_id}
headers = utils.get_etag_header(sut, session, acct_uri)
response = session.patch(sut.rhost + acct_uri, json=payload,
headers=headers)
response = sut.patch(acct_uri, json=payload, headers=headers,
session=session)
sut.add_response(acct_uri, response,
resource_type=ResourceType.MANAGER_ACCOUNT,
request_type=RequestType.PATCH_ODATA_PROPS)
Expand All @@ -260,8 +259,8 @@ def patch_account(sut: SystemUnderTest, session, acct_uri,
pwd = new_password(sut)
payload = {'Password': pwd}
headers = utils.get_etag_header(sut, session, acct_uri)
response = session.patch(sut.rhost + acct_uri, json=payload,
headers=headers)
response = sut.patch(acct_uri, json=payload, headers=headers,
session=session)
if response.ok:
new_pwd = pwd
sut.add_response(acct_uri, response,
Expand All @@ -273,8 +272,8 @@ def patch_account(sut: SystemUnderTest, session, acct_uri,
payload = {'Password': pwd}
new_headers = utils.get_etag_header(sut, session, acct_uri)
bad_headers = {'If-Match': new_headers['If-Match'] + 'foobar'}
r = session.patch(sut.rhost + acct_uri, json=payload,
headers=bad_headers)
r = sut.patch(acct_uri, json=payload, headers=bad_headers,
session=session)
if r.ok:
new_pwd = pwd
sut.add_response(acct_uri, r,
Expand All @@ -293,8 +292,8 @@ def delete_account_via_patch(sut: SystemUnderTest, session, user, acct_uri,
if data.get('Enabled', False):
payload['Enabled'] = False
headers = utils.get_etag_header(sut, session, acct_uri)
response = session.patch(sut.rhost + acct_uri, json=payload,
headers=headers)
response = sut.patch(acct_uri, json=payload, headers=headers,
session=session)
sut.add_response(acct_uri, response,
resource_type=ResourceType.MANAGER_ACCOUNT,
request_type=request_type)
Expand All @@ -318,7 +317,7 @@ def delete_account(sut: SystemUnderTest, session, user, acct_uri,
delete_account_via_patch(sut, session, user, acct_uri,
request_type=request_type)
return
response = session.delete(sut.rhost + acct_uri)
response = sut.delete(acct_uri, session=session)
sut.add_response(acct_uri, response,
resource_type=ResourceType.MANAGER_ACCOUNT,
request_type=request_type)
Expand All @@ -340,8 +339,8 @@ def password_change_required(sut: SystemUnderTest, session, user, password,
if not account_enabled:
payload['Enabled'] = True
headers = {'If-Match': etag} if etag else {}
response = session.patch(sut.rhost + uri, json=payload,
headers=headers)
response = sut.patch(uri, json=payload, headers=headers,
session=session)
sut.add_response(uri, response,
resource_type=ResourceType.MANAGER_ACCOUNT)
if not response.ok:
Expand Down
6 changes: 2 additions & 4 deletions redfish_protocol_validator/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,7 @@ def patch_session(sut: SystemUnderTest, session_uri):
"""PATCH a session; should fail, sessions are not updatable"""
payload = {'UserName': 'pRoToVAl'}
headers = utils.get_etag_header(sut, sut.session, session_uri)
response = sut.session.patch(sut.rhost + session_uri, json=payload,
headers=headers)
response = sut.patch(session_uri, json=payload, headers=headers)
sut.add_response(session_uri, response,
request_type=RequestType.PATCH_RO_RESOURCE)

Expand All @@ -281,8 +280,7 @@ def patch_collection(sut: SystemUnderTest, collection_uri):
"""PATCH a collection; should fail, collections are not updatable"""
payload = {'Name': 'My Collection'}
headers = utils.get_etag_header(sut, sut.session, collection_uri)
response = sut.session.patch(sut.rhost + collection_uri, json=payload,
headers=headers)
response = sut.patch(collection_uri, json=payload, headers=headers)
sut.add_response(collection_uri, response,
request_type=RequestType.PATCH_COLLECTION)

Expand Down
8 changes: 3 additions & 5 deletions redfish_protocol_validator/security_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ def test_session_termination_side_effects(sut: SystemUnderTest):
Assertion.SEC_SESSION_TERMINATION_SIDE_EFFECTS, msg)
elif response.ok:
# delete the session
r = session.delete(sut.rhost + new_session_uri)
r = sut.delete(new_session_uri, session=session)
if r.ok:
# read from the SSE stream
try:
Expand Down Expand Up @@ -952,8 +952,7 @@ def test_priv_predefined_roles_not_modifiable(sut: SystemUnderTest):
# PATCH the test privileges
payload = {'AssignedPrivileges': test_priv}
headers = utils.get_etag_header(sut, sut.session, uri)
response = sut.session.patch(sut.rhost + uri, json=payload,
headers=headers)
response = sut.patch(uri, json=payload, headers=headers)
if response.ok:
msg = ('PATCH request to %s to modify the AssignedPrivileges '
'of predefined role %s succeeded with status %s; '
Expand All @@ -968,8 +967,7 @@ def test_priv_predefined_roles_not_modifiable(sut: SystemUnderTest):
payload = {'AssignedPrivileges': privs}
etag = r.headers.get('ETag')
headers = {'If-Match': etag} if etag else {}
sut.session.patch(sut.rhost + uri, json=payload,
headers=headers)
sut.patch(uri, json=payload, headers=headers)
else:
sut.log(Result.PASS, 'PATCH', response.status_code, uri,
Assertion.SEC_PRIV_PREDEFINED_ROLE_NOT_MODIFIABLE,
Expand Down
24 changes: 10 additions & 14 deletions redfish_protocol_validator/service_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def test_event_service_subscription(sut: SystemUnderTest):
'Protocol': 'Redfish',
'Destination': 'https://192.168.1.50/Destination1'
}
response = sut.session.post(sut.rhost + sut.subscriptions_uri,
json=payload)
response = sut.post(sut.subscriptions_uri, json=payload)
sut.add_response(sut.subscriptions_uri, response,
request_type=RequestType.SUBSCRIPTION)
if response.status_code == requests.codes.CREATED:
Expand All @@ -35,7 +34,7 @@ def test_event_service_subscription(sut: SystemUnderTest):
Assertion.SERV_EVENT_POST_RESP, 'Test passed')
# cleanup
uri = urlparse(location).path
r = sut.session.delete(sut.rhost + uri)
r = sut.delete(uri)
sut.add_response(uri, r, request_type=RequestType.SUBSCRIPTION)
else:
msg = ('Response from event subscription POST request to %s '
Expand Down Expand Up @@ -74,8 +73,7 @@ def test_event_error_on_bad_request(sut: SystemUnderTest):
'Protocol': 'FTP', # invalid EventDestinationProtocol
'Destination': 'https://192.168.1.50/Destination1'
}
response = sut.session.post(sut.rhost + sut.subscriptions_uri,
json=payload)
response = sut.post(sut.subscriptions_uri, json=payload)
sut.add_response(sut.subscriptions_uri, response,
request_type=RequestType.SUBSCRIPTION)
if response.ok:
Expand All @@ -90,7 +88,7 @@ def test_event_error_on_bad_request(sut: SystemUnderTest):
if location:
# cleanup
uri = urlparse(location).path
r = sut.session.delete(sut.rhost + uri)
r = sut.delete(uri)
sut.add_response(uri, r, request_type=RequestType.SUBSCRIPTION)
elif response.status_code == requests.codes.BAD_REQUEST:
sut.log(Result.PASS, response.request.method,
Expand Down Expand Up @@ -126,8 +124,7 @@ def test_event_error_on_mutually_excl_props(sut: SystemUnderTest):
'Base.1.0.GeneralError'
]
}
response = sut.session.post(sut.rhost + sut.subscriptions_uri,
json=payload)
response = sut.post(sut.subscriptions_uri, json=payload)
sut.add_response(sut.subscriptions_uri, response,
request_type=RequestType.SUBSCRIPTION)
if response.ok:
Expand All @@ -143,7 +140,7 @@ def test_event_error_on_mutually_excl_props(sut: SystemUnderTest):
if location:
# cleanup
uri = urlparse(location).path
r = sut.session.delete(sut.rhost + uri)
r = sut.delete(uri)
sut.add_response(uri, r, request_type=RequestType.SUBSCRIPTION)
elif response.status_code == requests.codes.BAD_REQUEST:
sut.log(Result.PASS, response.request.method,
Expand Down Expand Up @@ -215,8 +212,8 @@ def test_ssdp_can_be_disabled(sut: SystemUnderTest):
payload = {'SSDP': {'ProtocolEnabled': False}}
etag = r.headers.get('ETag')
headers = {'If-Match': etag} if etag else {}
r = sut.session.patch(sut.rhost + sut.mgr_net_proto_uri,
json=payload, headers=headers)
r = sut.patch(sut.mgr_net_proto_uri, json=payload, headers=headers)
r = utils.poll_task(sut, r)
if r.ok:
services = utils.discover_ssdp(search_target=SSDP_REDFISH)
uuids = services.keys()
Expand All @@ -235,8 +232,7 @@ def test_ssdp_can_be_disabled(sut: SystemUnderTest):
payload = {'SSDP': {'ProtocolEnabled': True}}
etag = r.headers.get('ETag')
headers = {'If-Match': etag} if etag else {}
sut.session.patch(sut.rhost + sut.mgr_net_proto_uri,
json=payload, headers=headers)
sut.patch(sut.mgr_net_proto_uri, json=payload, headers=headers)
else:
msg = 'Attempt to disable SSDP failed'
sut.log(Result.FAIL, 'PATCH', r.status_code,
Expand Down Expand Up @@ -842,7 +838,7 @@ def test_sse_close_connection_if_event_dest_deleted(
Assertion.SERV_SSE_CLOSE_CONNECTION_IF_EVENT_DEST_DELETED, msg)
return

r = sut.session.delete(sut.rhost + event_dest_uri)
r = sut.delete(event_dest_uri)
if r.ok:
# give the service up to 5 seconds to close the stream
for _ in range(5):
Expand Down
27 changes: 10 additions & 17 deletions redfish_protocol_validator/service_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,12 +848,10 @@ def test_patch_array_element_remove(sut: SystemUnderTest):
}
uri = sut.mgr_net_proto_uri
headers = utils.get_etag_header(sut, sut.session, uri)
response = sut.session.patch(sut.rhost + uri,
json=payload1, headers=headers)
response = sut.patch(uri, json=payload1, headers=headers)
if response.ok:
headers = utils.get_etag_header(sut, sut.session, uri)
response = sut.session.patch(
sut.rhost + uri, json=payload2, headers=headers)
response = sut.patch(uri, json=payload2, headers=headers)
if response.ok:
get_resp = sut.session.get(sut.rhost + uri)
if get_resp.ok:
Expand Down Expand Up @@ -913,12 +911,10 @@ def test_patch_array_element_unchanged(sut: SystemUnderTest):
}
uri = sut.mgr_net_proto_uri
headers = utils.get_etag_header(sut, sut.session, uri)
response = sut.session.patch(sut.rhost + uri,
json=payload1, headers=headers)
response = sut.patch(uri, json=payload1, headers=headers)
if response.ok:
headers = utils.get_etag_header(sut, sut.session, uri)
response = sut.session.patch(
sut.rhost + uri, json=payload2, headers=headers)
response = sut.patch(uri, json=payload2, headers=headers)
if response.ok:
get_resp = sut.session.get(sut.rhost + uri)
if get_resp.ok:
Expand Down Expand Up @@ -986,12 +982,10 @@ def test_patch_array_truncate(sut: SystemUnderTest):
expected_array = ['time-b-b.nist.gov']
uri = sut.mgr_net_proto_uri
headers = utils.get_etag_header(sut, sut.session, uri)
response = sut.session.patch(sut.rhost + uri, json=payload1,
headers=headers)
response = sut.patch(uri, json=payload1, headers=headers)
if response.ok:
headers = utils.get_etag_header(sut, sut.session, uri)
response = sut.session.patch(
sut.rhost + uri, json=payload2, headers=headers)
response = sut.patch(uri, json=payload2, headers=headers)
if response.ok:
get_resp = sut.session.get(sut.rhost + uri)
if get_resp.ok:
Expand Down Expand Up @@ -1045,8 +1039,7 @@ def patch_array_restore(sut: SystemUnderTest, array):
}
}
headers = utils.get_etag_header(sut, sut.session, uri)
response = sut.session.patch(sut.rhost + uri,
json=payload, headers=headers)
response = sut.patch(uri, json=payload, headers=headers)
if not response.ok:
logging.warning('Attempt to PATCH %s to restore the original '
'NTPServers array failed with status %s; PATCH '
Expand Down Expand Up @@ -1124,7 +1117,7 @@ def test_post_create_to_members_prop(sut: SystemUnderTest):
if location and isinstance(location, str):
session_uri = urlparse(location).path
if session_uri:
sut.session.delete(sut.rhost + session_uri)
sut.delete(session_uri)
else:
msg = ('POST to Members property URI %s failed with status %s; '
'extended error: %s' %
Expand Down Expand Up @@ -1228,15 +1221,15 @@ def test_post_create_not_idempotent(sut: SystemUnderTest):
Assertion.REQ_POST_CREATE_NOT_IDEMPOTENT, msg)
# clean-up the created session
if session_uri2 and loc1 != loc2:
sut.session.delete(sut.rhost + session_uri2)
sut.delete(session_uri2)
else:
msg = ('Second POST request to %s failed with status code %s'
% (uri, r2.status_code))
sut.log(Result.WARN, 'POST', r2.status_code, uri,
Assertion.REQ_POST_CREATE_NOT_IDEMPOTENT, msg)
# clean-up the created session
if session_uri1:
sut.session.delete(sut.rhost + session_uri1)
sut.delete(session_uri1)
else:
msg = ('POST request to %s failed with status code %s; unable to test '
'this assertion' % (uri, r1.status_code))
Expand Down
2 changes: 1 addition & 1 deletion redfish_protocol_validator/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def create_session(sut: SystemUnderTest):

def delete_session(sut: SystemUnderTest, session, session_uri,
request_type=RequestType.NORMAL):
response = session.delete(sut.rhost + session_uri)
response = sut.delete(session_uri, session=session)
sut.add_response(session_uri, response, request_type=request_type)
return response

Expand Down
Loading

0 comments on commit 35c43b0

Please sign in to comment.