/usr/lib/python2.7/dist-packages/dtest/resource.py is in python-dtest 0.5.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | # Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
==============
Test Resources
==============
This module contains the Resource class, along with a number of other
functions, which together provide the functionality for providing test
resources. A test resource is simply a discrete object which is
required by a given test function or method; it could be a temporary
file containing configuration for a component, or a client object for
accessing a server, or virtually anything that would ordinarily be set
up in a test fixture. Resources which are not modified (which are not
"dirty") may be reused by following tests, subject to threading
constraints.
This file does not contain the @require() decorator, which is defined
in the dtest.test module.
"""
import functools
import sys
from eventlet import semaphore
class ResourceObjectMeta(type):
"""
ResourceObjectMeta class, which provides some utility class
methods for the ResourceObject class without polluting its
namespace.
"""
def resource(cls, obj):
"""
Retrieve the resource associated with a ResourceObject
``obj``.
"""
return obj.__res_resource__
def obj(cls, obj):
"""
Retrieve the actual object associated with a ResourceObject
``obj``.
"""
return obj.__res_obj__
def dirty(cls, obj, new=None):
"""
Retrieve the dirty flag associated with a ResourceObject
``obj``. If ``new`` is provided, specifies a new value for
the dirty flag.
"""
# Save the current value
old = obj.__res_dirty__
# Update it
if new is not None:
obj.__res_dirty__ = new
return old
def makedirty(cls, obj, new):
"""
Changes the 'makedirty' flag associated with a ResourceObject
``obj``. Returns its previous value.
"""
# Save the current value
old = obj.__res_makedirty__
# Update it
obj.__res_makedirty__ = new
return old
class ResourceObject(object):
"""
ResourceObject class, which acts as a binding between a Resource
and the actual resource object. Acts as a transparent proxy, but
calling any of the dirty methods or setting or deleting any
attributes causes the resource to be marked as dirty. Use the
cleanaccess() function as a context manager to allow accesses that
would normally dirty the resource.
"""
__metaclass__ = ResourceObjectMeta
def __init__(self, resource, resobj, dirtymeths):
"""
Create a ResourceObject. The ``resource`` is an instance of
Resource, and ``resobj`` is the actual object to delegate
accesses to. The ``dirtymeths`` is a list of methods which,
when called, cause the object to become dirty.
"""
# We use __res_*__ to avoid conflicting with attributes on the
# resource object
self.__res_resource__ = resource
self.__res_obj__ = resobj
self.__res_dirtymeths__ = set(dirtymeths)
self.__res_dirty__ = False
self.__res_makedirty__ = True
def __getattribute__(self, name):
"""
Retrieves an attribute from the resource object. If the
attribute is a dirty method, it will be wrapped with a
function which will set the dirty attribute on the resource.
"""
# Short-cut the __res_*__ attributes...
if name.startswith('__res_') and name.endswith('__'):
return super(ResourceObject, self).__getattribute__(name)
# First, get the attribute from the resource object
attr = getattr(self.__res_obj__, name)
# If it's a callable, and listed in dirtymeths, wrap it
if callable(attr) and name in self.__res_dirtymeths__:
@functools.wraps(attr)
def wrapper(*args, **kwargs):
# Mark it dirty
if self.__res_makedirty__:
self.__res_dirty__ = True
# Call the function
return attr(*args, **kwargs)
# Return our wrapper
return wrapper
return attr
def __setattr__(self, name, value):
"""
Sets an attribute on the resource object. Sets the dirty
attribute on the resource.
"""
# Short-cut the __res_*__ attributes...
if name.startswith('__res_') and name.endswith('__'):
return super(ResourceObject, self).__setattr__(name, value)
# OK, mark resource dirty
if self.__res_makedirty__:
self.__res_dirty__ = True
# Delegate to the resource object
return setattr(self.__res_obj__, name, value)
def __delattr__(self, name):
"""
Deletes an attribute on the resource object. Sets the dirty
attribute on the resource.
"""
# Short-cut the __res_*__ attributes...
if name.startswith('__res_') and name.endswith('__'):
return super(ResourceObject, self).__delattr__(name)
# OK, mark resource dirty
if self.__res_makedirty__:
self.__res_dirty__ = True
# Delegate to the resource object
return delattr(self.__res_obj__, name)
class CleanContext(object):
"""
Context manager returned by cleanaccess(). Temporarily disables
dirty detection for resource objects.
"""
def __init__(self, objs):
"""
Initialize the CleanContext. Saves the list of objects for
which temporary dirty detection must be disabled.
"""
self.objs = objs
self.makedirty = {}
def __enter__(self):
"""
Enter the context. Turns off ``makedirty`` for all objects,
saving the original values for later restoration.
"""
for obj in self.objs:
self.makedirty[id(obj)] = ResourceObject.makedirty(obj, False)
# Return the list of objects
return obj
def __exit__(self, exc_type, exc_value, exc_tb):
"""
Exit the context. Restores the ``makedirty`` for all objects.
"""
for obj in self.objs:
ResourceObject.makedirty(obj, self.makedirty[id(obj)])
def cleanaccess(*objs):
"""
Allows access to the objects given as arguments without affecting
their ``dirty`` flags.
"""
return CleanContext(objs)
def dirty(*objs):
"""
Marks the objects given as arguments as dirty.
"""
for obj in objs:
ResourceObject.dirty(obj, True)
def clean(*objs):
"""
Marks the objects given as arguments as clean.
"""
for obj in objs:
ResourceObject.dirty(obj, False)
def getobject(obj):
"""
The resources system uses proxy objects, so it can catch actions
which make a resource "dirty". Sometimes, this interferes with
the action of the base resource object, for instance, when the
base resource object implements one of the special methods. This
function provides a means of accessing the base resource object
directly, given a wrapped resource object ``obj``.
"""
return ResourceObject.obj(obj)
class Resource(object):
"""
Resource class, which describes test resources. To define a
resource, extend this class and implement the setUp() method and,
optionally, the tearDown() method. Subclasses may also specify
alternate values for the ``oneshot`` and ``dirtymeths`` class
attributes: if ``oneshot`` is True, every acquired resource will
only be used once; and ``dirtymeths`` should be a list giving the
name of methods which, when called, cause the resource object to
be considered dirty.
"""
# If set to True, resource will never be used more than once
oneshot = False
dirtymeths = []
def __init__(self, *args, **kwargs):
"""
Initialize a Resource. This saves the arguments, which will
later be passed to setUp().
"""
# Build a key from the arguments
key = [self.__class__]
key += [str(a) for a in args]
key += ['%s=%s' % (k, kwargs[k]) for k in sorted(kwargs.keys())]
self.key = tuple(key)
# Save the arguments
self.args = args
self.kwargs = kwargs
def acquire(self):
"""
Acquire a resource object. This calls the setUp() method and
binds its return result to this resource object using the
ResourceObject class.
"""
# Build and return the actual resource object
obj = self.setUp(*self.args, **self.kwargs)
# Build our proxy object
return ResourceObject(self, obj, self.dirtymeths)
def release(self, obj, msgs, status=None, force=False):
"""
Release a resource object. If the object cannot be reused, or
if ``force`` is True, the tearDown() method will be called.
Returns True if the object may be reused, otherwise returns
False.
"""
# Do we need to release the object?
if force or self.oneshot or ResourceObject.dirty(obj):
try:
self.tearDown(ResourceObject.obj(obj), status)
except:
# In the event of an error releasing a resource, save
# a message documenting the failure
msgs.append((self, sys.exc_info()))
return False
return True
def setUp(self, *args, **kwargs):
"""
Sets up and returns the resource. Must be implemented by all
subclasses.
"""
raise NotImplementedError("%s.%s.setUp() is not implemented" %
(self.__class__.__module__,
self.__class__.__name__))
def tearDown(self, obj, status):
"""
Tears down a resource allocated by setUp(). This is optional;
implement it only if you need to release resources, such as
open files.
Note that the test's status will be passed as the ``status``
argument, except in the case of cached resources being cleaned
up after all tests have finished running. This status value
can be used to ensure that debugging resources, such as
temporary files, are preserved in the event of a failure.
"""
pass
class ResourceManager(object):
"""
ResourceManager class, which manages a pool of resources.
"""
def __init__(self):
"""
Initializes the resource pool.
"""
self._pool_lock = semaphore.Semaphore()
self._pool = {}
# Need a place to store error messages
self._messages = []
def _get_pool(self, res):
"""
Retrieve the pool corresponding to the resource ``res``. This
method must be called with the pool lock held.
"""
# Make sure we have the pool...
if res.key not in self._pool:
self._pool[res.key] = []
# Return the resource pool
return self._pool[res.key]
def acquire(self, res):
"""
Acquire a resource object corresponding to the resource
``res``.
"""
# Hold the lock while we're accessing the resource pool
with self._pool_lock:
pool = self._get_pool(res)
# Do we have an available resource?
if len(pool) > 0:
return pool.pop(0)
# OK, create a new resource object
return res.acquire()
def release(self, obj, status=None):
"""
Release a resource object ``obj``. If the object is dirty, it
will be discarded; otherwise, it will be added to the resource
pool for later reuse.
"""
# Get the resource
res = ResourceObject.resource(obj)
# Let the resource do any cleaning up it needs to do...
if not res.release(obj, self._messages, status=status):
# It was dirty, so we got rid of it
return
# OK, we're going to add it back to the resource pool, so grab
# the lock
with self._pool_lock:
# Get the pool
pool = self._get_pool(res)
# Append resource object to the end of the pool, so we
# have FIFO-style reuse
pool.append(obj)
def release_all(self):
"""
Release all resources in the resource pool.
"""
# Force-release all resource objects
with self._pool_lock:
for objlist in self._pool.values():
for obj in objlist:
res = ResourceObject.resource(obj)
res.release(obj, self._messages, force=True)
# Clear the pool
self._pool = {}
def collect(self, resources):
"""
Collects the resources identified by the ``resources``
dictionary and yields them as a dictionary to the caller. The
resources allocated are then released when the generator
continues. The generator's send() method should be called
with the test status, which will then be passed to the
resource tearDown() methods.
"""
# Set up the resources we need...
objects = {}
for key, res in resources.items():
objects[key] = self.acquire(res)
# Yield the resource dictionary and get the test status
status = yield objects
# Now, release the resources we used
for obj in objects.values():
self.release(obj, status=status)
@property
def messages(self):
"""
Retrieve all error messages accumulated while releasing
resources. Clears the message list.
"""
# Get the current messages
msgs = self._messages
# Clear the list
self._messages = []
return msgs
|