cfme.utils.rest module¶
Helper functions for tests using REST API.
-
cfme.utils.rest.
assert_response
(rest_obj, success=None, http_status=None, results_num=None, task_wait=600)[source]¶ Asserts that the response HTTP status code and content is as expected.
If specific http_status is not given, we simply check that the status was a successful response code via requests.Response.__bool__()
If response status code is ‘204’, ensures there is no content.
Example of verifying a success response:
assert_response(appliance)
Example of verifying a failure response:
with error.expected('ActiveRecord::RecordNotFound'): collection.action.delete(some_stuff) assert_response(appliance, http_status=404)
Note: For below args, ‘results’ refers to rest_obj.last_response.json()[‘results’]
- Parameters
-- instance of cfme.utils.Appliance (rest_obj) – or cfme.utils.appliance.MiqApi
-- if defined, checks each result in results to ensure that result['success'] (success) – is equal to the value defined here
http_status (int or tuple of int) – we simply verify that the response was a success
results_num (int) –
task_wait (int) – the API to ensure that task has moved to ‘finished’ and wait ‘task_wait’ seconds for that state change to occur
-
cfme.utils.rest.
create_resource
(rest_api, col_name, col_data, col_action='create', substr_search=False)[source]¶ Creates new resource in collection.
-
cfme.utils.rest.
delete_resources_from_collection
(resources, collection=None, not_found=None, num_sec=10, delay=2, check_response=True)[source]¶ Checks that delete from collection works as expected.
-
cfme.utils.rest.
delete_resources_from_detail
(resources, method='POST', num_sec=10, delay=2, check_response=True)[source]¶ Checks that delete from detail works as expected.
-
cfme.utils.rest.
get_vms_in_service
(service)[source]¶ Gets list of vm entities associated with the service.
-
cfme.utils.rest.
query_resource_attributes
(resource, soft_assert=None)[source]¶ Checks that all available attributes/subcollections are really accessible.
-
cfme.utils.rest.
wait_for
(func, func_args=[], func_kwargs={}, *, logger=<TraceLogger cfme (INFO)>, **kwargs)¶ Waits for a certain amount of time for an action to complete Designed to wait for a certain length of time, either linearly in 1 second steps, or exponentially, up to a maximum. Returns the output from the function once it completes successfully, along with the time taken to complete the command.
It tries to use
time.monotonic()
, if it is not present, falls back totime.time()
, but it then is not resistant against system time changes.- Note: If using the expo keyword, the returned elapsed time will be inaccurate
as wait_for does not know the exact time that the function returned correctly, only that it returned correctly at last check.
- Parameters
func – A function to be run
func_args – A list of function arguments to be passed to func
func_kwargs – A dict of function keyword arguments to be passed to func
num_sec – An int describing the number of seconds to wait before timing out.
timeout – Either an int describing the number of seconds to wait before timing out. Or a
timedelta
object. Or a string formatted like1h 10m 5s
. This then sets thenum_sec
variable.expo – A boolean flag toggling exponential delay growth.
message – A string containing a description of func’s operation. If None, defaults to the function’s name.
fail_condition – An object describing the failure condition that should be tested against the output of func. If func() == fail_condition, wait_for continues to wait. Can be a callable which takes the result and returns boolean whether to fail. You can also specify it as a set, that way it checks whether it is present in the iterable.
handle_exception – A boolean controlling the handling of excepetions during func() invocation. If set to True, in cases where func() results in an exception, clobber the exception and treat it as a fail_condition.
delay – An integer describing the number of seconds to delay before trying func() again.
fail_func – A function to be run after every unsuccessful attempt to run func()
quiet – Do not write time report to the log (default False)
very_quiet – Do not log unless there was an error (default False). Implies quiet.
silent_failure – Even if the entire attempt times out, don’t throw a exception.
log_on_loop – Fire off a log.info message indicating we’re still waiting at each iteration of the wait loop
- Returns
A tuple containing the output from func() and a float detailing the total wait time.
- Raises
TimedOutError – If num_sec is exceeded after an unsuccessful func() invocation.