diff options
author | Klement Sekera <ksekera@cisco.com> | 2017-01-25 08:00:40 +0100 |
---|---|---|
committer | Damjan Marion <dmarion.lists@gmail.com> | 2017-01-25 15:57:14 +0000 |
commit | e0545ef85ab136a51c42319935834d08a35eaa90 (patch) | |
tree | 4ccb026e7b6cfbbdd6e76a0dd35e0e5fae1e7f07 /test | |
parent | d2c97d988b2fbc28f0905d1826b428967d09348a (diff) |
make test: elegantly handle expected API failures
Allow writing simple code to expect API failure when needed:
with self.vapi.expect_negative_api_retval():
do_api_call() # expected to return negative retval in response
Change-Id: Id58d91d9ce38d20ad6ff7a43a6897e79ffbd23bf
Signed-off-by: Klement Sekera <ksekera@cisco.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/test_bfd.py | 9 | ||||
-rw-r--r-- | test/vpp_papi_provider.py | 46 |
2 files changed, 42 insertions, 13 deletions
diff --git a/test/test_bfd.py b/test/test_bfd.py index b56df3398d5..d047b5a3b99 100644 --- a/test/test_bfd.py +++ b/test/test_bfd.py @@ -42,14 +42,11 @@ class BFDAPITestCase(VppTestCase): """ create the same BFD session twice (negative case) """ session = VppBFDUDPSession(self, self.pg0, self.pg0.remote_ip4) session.add_vpp_config() - try: + + with self.vapi.expect_negative_api_retval(): session.add_vpp_config() - except: - session.remove_vpp_config() - return + session.remove_vpp_config() - raise Exception("Expected failure while adding duplicate " - "configuration") def test_add_two(self): """ create two BFD sessions """ diff --git a/test/vpp_papi_provider.py b/test/vpp_papi_provider.py index 901ea2744cb..4f2cea8b788 100644 --- a/test/vpp_papi_provider.py +++ b/test/vpp_papi_provider.py @@ -32,15 +32,17 @@ class VppPapiProvider(object): """VPP-api provider using vpp-papi @property hook: hook object providing before and after api/cli hooks - - """ + _zero, _negative = range(2) + def __init__(self, name, shm_prefix, test_class): self.hook = Hook("vpp-papi-provider") self.name = name self.shm_prefix = shm_prefix self.test_class = test_class + self._expect_api_retval = self._zero + self._expect_stack = [] jsonfiles = [] install_dir = os.getenv('VPP_TEST_INSTALL_PATH') @@ -51,6 +53,24 @@ class VppPapiProvider(object): self.papi = VPP(jsonfiles) self._events = deque() + def __enter__(self): + return self + + def expect_negative_api_retval(self): + """ Expect API failure """ + self._expect_stack.append(self._expect_api_retval) + self._expect_api_retval = self._negative + return self + + def expect_zero_api_retval(self): + """ Expect API success """ + self._expect_stack.append(self._expect_api_retval) + self._expect_api_retval = self._zero + return self + + def __exit__(self, exc_type, exc_value, traceback): + self._expect_api_retval = self._expect_stack.pop() + def register_hook(self, hook): """Replace hook registration with new hook @@ -113,11 +133,23 @@ class VppPapiProvider(object): """ self.hook.before_api(api_fn.__name__, api_args) reply = api_fn(**api_args) - if hasattr(reply, 'retval') and reply.retval != expected_retval: - msg = "API call failed, expected retval == %d, got %s" % ( - expected_retval, repr(reply)) - self.test_class.logger.info(msg) - raise Exception(msg) + if self._expect_api_retval == self._negative: + if hasattr(reply, 'retval') and reply.retval >= 0: + msg = "API call passed unexpectedly: expected negative "\ + "return value instead of %d in %s" % \ + (reply.retval, repr(reply)) + self.test_class.logger.info(msg) + raise Exception(msg) + elif self._expect_api_retval == self._zero: + if hasattr(reply, 'retval') and reply.retval != expected_retval: + msg = "API call failed, expected zero return value instead "\ + "of %d in %s" % (expected_retval, repr(reply)) + self.test_class.logger.info(msg) + raise Exception(msg) + else: + raise Exception("Internal error, unexpected value for " + "self._expect_api_retval %s" % + self._expect_api_retval) self.hook.after_api(api_fn.__name__, api_args) return reply |