Call API function and check it's return value. Call the appropriate hooks before and after the API call :param api_fn: API function to call :param api_args: tuple of API function arguments :param expected_retval: Expected return value(s) (Default value = 0) :
(self, api_fn, api_args, expected_retval: int | list[int] = 0)
| 383 | self.vpp.disconnect() |
| 384 | |
| 385 | def api(self, api_fn, api_args, expected_retval: int | list[int] = 0): |
| 386 | """Call API function and check it's return value. |
| 387 | Call the appropriate hooks before and after the API call |
| 388 | |
| 389 | :param api_fn: API function to call |
| 390 | :param api_args: tuple of API function arguments |
| 391 | :param expected_retval: Expected return value(s) (Default value = 0) |
| 392 | :returns: reply from the API |
| 393 | |
| 394 | """ |
| 395 | self.hook.before_api(api_fn.__name__, api_args) |
| 396 | reply = api_fn(**api_args) |
| 397 | |
| 398 | retval = None |
| 399 | if hasattr(reply, "retval"): |
| 400 | retval = reply.retval |
| 401 | elif type(reply) is tuple and hasattr(reply[0], "retval"): |
| 402 | retval = reply[0].retval |
| 403 | |
| 404 | if self._expect_api_retval is self._negative: |
| 405 | if retval is not None and retval >= 0: |
| 406 | msg = ( |
| 407 | "%s(%s) passed unexpectedly: expected negative " |
| 408 | "return value instead of %d in %s" |
| 409 | % ( |
| 410 | api_fn.__name__, |
| 411 | as_fn_signature(api_args), |
| 412 | retval, |
| 413 | reprlib.repr(reply), |
| 414 | ) |
| 415 | ) |
| 416 | self.test_class.logger.info(msg) |
| 417 | raise UnexpectedApiReturnValueError(retval, msg, reply) |
| 418 | elif self._expect_api_retval is None: |
| 419 | if retval is not None and ( |
| 420 | (isinstance(expected_retval, int) and retval != expected_retval) |
| 421 | or (isinstance(expected_retval, list) and retval not in expected_retval) |
| 422 | ): |
| 423 | msg = ( |
| 424 | "%s(%s) failed, expected %d return value instead " |
| 425 | "of %d in %s" |
| 426 | % ( |
| 427 | api_fn.__name__, |
| 428 | as_fn_signature(api_args), |
| 429 | expected_retval, |
| 430 | retval, |
| 431 | reprlib.repr(reply), |
| 432 | ) |
| 433 | ) |
| 434 | self.test_class.logger.info(msg) |
| 435 | raise UnexpectedApiReturnValueError(retval, msg, reply) |
| 436 | elif isinstance(self._expect_api_retval, list): |
| 437 | if retval is not None and retval not in self._expect_api_retval: |
| 438 | msg = f"{api_fn.__name__}{as_fn_signature(api_args)} failed, expected return value in {expected_retval}, got {retval} in {reprlib.repr(reply)}" |
| 439 | self.test_class.logger.info(msg) |
| 440 | raise UnexpectedApiReturnValueError(retval, msg, reply) |
| 441 | else: |
| 442 | raise Exception( |