1 # Copyright 2012 OpenStack Foundation
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
19 from oslo_log import log as logging
21 LOG = logging.getLogger(__name__)
25 """Simple wrapper for classes that should only have a single instance."""
29 if cls not in instances:
30 instances[cls] = cls()
35 def find_test_caller():
36 """Find the caller class and test name.
38 Because we know that the interesting things that call us are
39 test_* methods, and various kinds of setUp / tearDown, we
40 can look through the call stack to find appropriate methods,
41 and the class we were in when those were called.
45 frame = inspect.currentframe()
47 # Start climbing the ladder until we hit a good method
51 name = frame.f_code.co_name
53 if re.search("^(test_|setUp|tearDown)", name):
55 if 'self' in frame.f_locals:
56 cname = frame.f_locals['self'].__class__.__name__
57 if 'cls' in frame.f_locals:
58 cname = frame.f_locals['cls'].__name__
59 caller_name = cname + ":" + name
61 elif re.search("^_run_cleanup", name):
68 if 'self' in frame.f_locals:
69 cname = frame.f_locals['self'].__class__.__name__
70 if 'cls' in frame.f_locals:
71 cname = frame.f_locals['cls'].__name__
73 # the fact that we are running cleanups is indicated pretty
74 # deep in the stack, so if we see that we want to just
75 # start looking for a real class name, and declare victory
77 if is_cleanup and cname:
78 if not re.search("^RunTest", cname):
79 caller_name = cname + ":_run_cleanups"
83 # prevents frame leaks
85 if caller_name is None:
86 LOG.debug("Sane call name not found in %s" % names)