Skip to content

Commit 1d36e6d

Browse files
authored
[SAI-PTF] Skip test when hit expected error from sai api (#1697)
Why When hit the expected error like SAI_STATUS_NOT_SUPPORTED = -2, then skip the test. related to issue #1610 How Add conditional exeption handler when return from sai api invocation Test: Test on brcm platform on case sainexthop.tunnelVrfTest Signed-off-by: richardyu-ms <[email protected]> Signed-off-by: richardyu-ms <[email protected]>
1 parent 9ece32e commit 1d36e6d

File tree

5 files changed

+23
-56
lines changed

5 files changed

+23
-56
lines changed

meta/gensairpc.pl

-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
[ 'verbose|v', 'Print more details', { default => 0 } ],
9191
[ 'mandatory-attrs', 'Make mandatory attributes obligatory in sai_adapter.py', { default => 0 } ],
9292
[ 'dev-utils:s', 'Generate additional development utils within the generated code. Additional options: [=log,zero]', { default => 0 } ],
93-
[ 'skip_error:s', 'Skip test on specified error code. Additional options: [=-2]', { default => 0 } ],
9493
[ 'adapter_logger', 'Enable the logger in sai_adapter, it will log all the method invocation.', { default => 0} ],
9594
[ 'attr-header', 'Generate additional header of attributes definitions (including object types)', { default => 0 } ],
9695
[ 'help|h', 'Print this help', { shortcircuit => 1 } ],
@@ -116,7 +115,6 @@
116115
my $clean = $args->clean_meta;
117116
my $mandatory_attrs = $args->mandatory_attrs;
118117
my $dev_utils = ( $args->dev_utils ne q{} ? $args->dev_utils : 1 );
119-
my $skip_error = ( $args->skip_error ne q{} ? $args->skip_error : 1 );
120118
my $adapter_logger = ( $args->adapter_logger ne q{} ? $args->adapter_logger : 1 );
121119
my $attr_header = $args->attr_header;
122120

@@ -163,7 +161,6 @@
163161
dbg => $dbg,
164162
mandatory_attrs => $mandatory_attrs,
165163
dev_utils => $dev_utils,
166-
skip_error => $skip_error,
167164
adapter_logger => $adapter_logger,
168165
templates_dir => $templates_dir
169166
};

meta/rpc/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ Optional arguments are useful for development purposes.
1919
| `-d` `--dump` | Dump all data to the file. **Should be used during development**. |
2020
| `--mandatory-attrs` | Make mandatory attributes obligatory in *sai\_adapter.py*. It removes `=None` from attributes, which are passed as arguments into python functions. Can be useful for debugging purposes, but since most of attributes are **optionally** mandatory, this is not as useful as it could be. |
2121
| `--dev-utils[=STR]` | Generate additional development utils within the generated code. Additional options: [=log,zero]. Useful for tests development and debugging. The generated code **should not** be committed. |
22-
| `--skip_error[=STR]` | Skip test on specified error code. Additional options: [=-2]. The generated code **should not** be committed. |
2322
| `--adapter_logger` | Enable the logger in sai_adapter, it will log all the method invocation. |
2423
| `-h` `--help` | Print the help. |
2524

meta/templates/sai_adapter.py.tt

+22-3
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ client.[% function.thrift_name %](
191191
[%- BLOCK catch_exception -%]
192192
except sai_thrift_exception as e:
193193
status = e.status
194+
if SKIP_TEST_ON_EXPECTED_ERROR and status in EXPECTED_ERROR_CODE:
195+
reason = "SkipTest on expected error. [% function.thrift_name %] with errorcode: {} error: {}".format(
196+
status, e)
197+
print(reason)
198+
testutils.skipped_test_count=1
199+
raise SkipTest(reason)
194200
if CATCH_EXCEPTIONS:
195201
[%- IF function.operation == 'stats' %]
196202
pass
@@ -409,6 +415,12 @@ client.[% function.thrift_name %](
409415
[%- BLOCK return_from_empty_function -%]
410416
global status
411417
status = SAI_STATUS_NOT_SUPPORTED
418+
if SKIP_TEST_ON_EXPECTED_ERROR and status in EXPECTED_ERROR_CODE:
419+
reason = "SkipTest on expected error. [% function.name %] with errorcode: {} error: {}".format(
420+
status, e)
421+
print(reason)
422+
testutils.skipped_test_count=1
423+
raise SkipTest(reason)
412424

413425
if CATCH_EXCEPTIONS:
414426
[%- IF function.operation == 'create' AND NOT function.rpc_return.is_list %]
@@ -431,7 +443,6 @@ client.[% function.thrift_name %](
431443

432444

433445
[%- PROCESS decorate_method IF dev_utils -%]
434-
[%- PROCESS decorate_skip_test_on_error IF skip_error -%]
435446
[%- PROCESS decorate_invocation_logger IF adapter_logger -%]
436447
[%- PROCESS function_header %]
437448
[%- PROCESS function_docstring %]
@@ -489,9 +500,11 @@ Thrift SAI interface basic tests
489500
# pylint: disable=too-many-return-statements,line-too-long,invalid-name
490501

491502
[%- PROCESS dev_utils_imports IF dev_utils -%]
492-
[%- PROCESS skip_test_on_error_imports IF skip_error -%]
493503
[%- PROCESS invocation_logger_imports IF adapter_logger -%]
494504

505+
from unittest import SkipTest
506+
from ptf import testutils
507+
495508
from sai_thrift.ttypes import *
496509
from sai_thrift.sai_headers import *
497510

@@ -503,10 +516,16 @@ from sai_thrift.sai_headers import *
503516
# In order to catch exceptions and get error codes
504517
# in the application, it should be disabled.
505518
CATCH_EXCEPTIONS = True
519+
# Expected error code
520+
# Used with SKIP_TEST_ON_EXPECTED_ERROR
521+
# For some expected errors in test
522+
# Like SAI_STATUS_NOT_SUPPORTED = -2
523+
EXPECTED_ERROR_CODE = [-2]
524+
# Skip test when hitting an expected error
525+
SKIP_TEST_ON_EXPECTED_ERROR = True
506526
status = 0
507527

508528
[%- PROCESS dev_utils IF dev_utils -%]
509-
[%- PROCESS skip_error IF skip_error -%]
510529
[%- PROCESS invocation_logger IF adapter_logger -%]
511530

512531
[%- FOREACH api IN apis.keys.sort -%]

meta/templates/sai_adapter_utils.tt

-48
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@
44
from __future__ import print_function
55
from collections import defaultdict
66
from collections import Counter
7-
[% IF NOT skip_error %]from functools import wraps[% END %]
8-
[% END -%]
9-
10-
[%- BLOCK skip_test_on_error_imports %]
117
from functools import wraps
12-
from unittest import SkipTest
13-
from ptf import testutils
148
[% END -%]
159

1610
[%- BLOCK invocation_logger_imports %]
@@ -29,13 +23,6 @@ import logging
2923
[% PROCESS instance_counter %]
3024
[% END -%]
3125

32-
[%- BLOCK skip_error %]
33-
34-
# skip_error
35-
36-
[% PROCESS skip_test_on_error %]
37-
[% END -%]
38-
3926
[%- BLOCK invocation_logger %]
4027

4128
# invocation_logger
@@ -186,38 +173,6 @@ instance_counter.removed = defaultdict(Counter)
186173
instance_counter.not_removed = defaultdict(Counter)
187174
[%- END -%]
188175

189-
[%- BLOCK skip_test_on_error -%]
190-
def skip_test_on_error(errorcode=[-2]):
191-
def skip_test_on_error_decorator(func):
192-
"""
193-
Decorator for skip the test when error happened.
194-
Args:
195-
errorcode: a list of the error code that test will be skipped.
196-
"""
197-
198-
@wraps(func)
199-
def decorated(*args, **kwargs):
200-
"""
201-
Check the return value and check if the status is the error code
202-
on which the test should be skipped.
203-
204-
Args:
205-
args(List): original args
206-
kwargs(Dict): original kwargs
207-
Returns:
208-
retval(Any): the original return value
209-
"""
210-
retval = func(*args, **kwargs)
211-
global status
212-
if status in errorcode:
213-
reason = "SkipTest: {} with errorcode: {}".format(func.__name__, status)
214-
print(reason)
215-
testutils.skipped_test_count=1
216-
raise SkipTest(reason)
217-
return retval
218-
return decorated
219-
return skip_test_on_error_decorator
220-
[%- END -%]
221176

222177
[%- BLOCK invocation_logger_func -%]
223178
def invocation_logger(func):
@@ -259,9 +214,6 @@ def invocation_logger(func):
259214
@instance_counter("[% function.object %]", "[% function.operation %]"[% IF dev_utils.match('log') %], log=True[% END %][% IF dev_utils.match('zero') %], zero=True[% END %])
260215
[%- END -%]
261216

262-
[%- BLOCK decorate_skip_test_on_error %]
263-
@skip_test_on_error([% IF skip_error %]errorcode=[[% skip_error %]][% END %])
264-
[%- END -%]
265217

266218
[%- BLOCK decorate_invocation_logger %]
267219
@invocation_logger

ptf/utest/TemplateTest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212
Class use to test the template.
1313
Run command under folder 'meta'
14-
perl -Irpc gensairpc.pl --adapter_logger --skip_error=-2
14+
perl -Irpc gensairpc.pl --adapter_logger
1515
copy sai_adaptor to ./test/saithriftv2/gen-py/sai
1616
cd ./test/saithriftv2
1717
sudo python3 setup.py install

0 commit comments

Comments
 (0)