Debian Codegraph @

octavia_tempest_plugin/common/decorators.py

raw symbols SCIP
1 # Copyright 2020 Red Hat, Inc. All rights reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14 from functools import wraps
15
16 import testtools
17
18 from oslo_utils import excutils
19 from tempest import config
20 from tempest.lib import exceptions
21
22 CONF = config.CONF
23
24
25 def skip_if_not_implemented(f):
26 """A decorator to raise a skip exception for not implemented features.
27
28 This decorator raises a skipException if the method raises a
29 NotImplemented exception. If "skip_if_not_implemented=False"
30 argument was passed to the method, the NotImplemented exception will
31 be raised.
32
33 @param skip_if_not_implemented: If True (default), raise skipException.
34 """
35 @wraps(f)
36 def wrapper(*func_args, **func_kwargs):
37
38 skip = func_kwargs.pop('skip_if_not_implemented', True)
39 if CONF.loadbalancer_feature_enabled.not_implemented_is_error:
40 skip = False
41 try:
42 return f(*func_args, **func_kwargs)
43 except exceptions.NotImplemented as e:
44 with excutils.save_and_reraise_exception():
45 if not skip:
46 raise
47 message = ("The configured provider driver '{driver}' "
48 "does not support a feature required for this "
49 "test.".format(
50 driver=CONF.load_balancer.provider))
51 if hasattr(e, 'resp_body'):
52 message = e.resp_body.get('faultstring', message)
53 raise testtools.TestCase.skipException(message)
54 return wrapper
55
56
57 def retry_on_port_in_use(start_port, max_retries=3):
58 """Decorator to retry a test function if the specified port is in use.
59
60 This handles cases where a test fails due to a port conflict, typically
61 caused by another service binding the same port on the host. The decorator
62 catches '[Errno 98] Address already in use' errors and retries the test
63 using incrementally higher port numbers.
64
65 The decorated function must accept `port` as its first parameter.
66
67 :param start_port: Initial port to attempt.
68 :param max_retries: Number of retries with incremented port values.
69 """
70 def decorator(func):
71 @wraps(func)
72 def wrapper(self, *args, **kwargs):
73 port = start_port
74 last_exception = None
75
76 for _ in range(max_retries):
77 try:
78 return func(self, port, *args, **kwargs)
79 except exceptions.NotImplemented as e:
80 message = (
81 "The configured provider driver '{driver}' does not "
82 "support a feature required for this test.".format(
83 driver=CONF.load_balancer.provider
84 )
85 )
86 if hasattr(e, 'resp_body'):
87 message = e.resp_body.get('faultstring', message)
88 raise testtools.TestCase.skipException(message)
89
90 except Exception as e:
91 if "Address already in use" in str(e):
92 last_exception = e
93 port += 1
94 else:
95 raise
96
97 raise Exception(f"All port attempts failed after {max_retries} "
98 f"retries. Last error: {last_exception}")
99 return wrapper
100 return decorator