Remove singleton class in FakeServerManager class
The current fake NFVO server design is based on a singleton class pattern which restricts the instantiation of a class to one server object. Multi-tenant functional test case environment requires multiple fake NFVO servers(one server per tenant). This patch removes the SingletonMixin class and modifies the FakeServerManager class to instantiate multiple server class objects. Implement: blueprint multi-tenant-policy Change-Id: Ia83aad3a8255ea6ee8cddc01e6a75c7e0e7945b8
This commit is contained in:
parent
f55ed58501
commit
1cb068c74a
@ -26,74 +26,8 @@ from oslo_log import log as logging
|
|||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class SingletonMixin:
|
def PrepareRequestHandler(manager):
|
||||||
"""Mixin class to make your class a Singleton class."""
|
class DummyRequestHandler(http.server.CGIHTTPRequestHandler):
|
||||||
|
|
||||||
_instance = None
|
|
||||||
_rlock = threading.RLock()
|
|
||||||
_inside_instance = False
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def get_instance(cls, *args, **kwargs):
|
|
||||||
"""Get *the* instance of the class, constructed when needed using(kw)args.
|
|
||||||
|
|
||||||
Return the instance of the class. If it did not yet exist, create
|
|
||||||
it by calling the "constructor" with whatever arguments and keyword
|
|
||||||
arguments provided.
|
|
||||||
|
|
||||||
This routine is thread-safe. It uses the *double-checked locking*
|
|
||||||
design pattern ``https://en.wikipedia.org/wiki/Double-checked_locking``
|
|
||||||
for this.
|
|
||||||
|
|
||||||
:param args: Used for constructing the instance, when not performed
|
|
||||||
yet.
|
|
||||||
:param kwargs: Used for constructing the instance, when not
|
|
||||||
perfored yet.
|
|
||||||
:return: An instance of the class.
|
|
||||||
"""
|
|
||||||
if cls._instance is not None:
|
|
||||||
return cls._instance
|
|
||||||
with cls._rlock:
|
|
||||||
# re-check, perhaps it was created in the mean time...
|
|
||||||
if cls._instance is None:
|
|
||||||
cls._inside_instance = True
|
|
||||||
try:
|
|
||||||
cls._instance = cls(*args, **kwargs)
|
|
||||||
finally:
|
|
||||||
cls._inside_instance = False
|
|
||||||
return cls._instance
|
|
||||||
|
|
||||||
def __new__(cls, *args, **kwargs):
|
|
||||||
"""Raise Exception when not called from the :func:``instance``
|
|
||||||
|
|
||||||
Class method.
|
|
||||||
This method raises RuntimeError when not called from the
|
|
||||||
instance class method.
|
|
||||||
|
|
||||||
:param args: Arguments eventually passed to
|
|
||||||
:func:``__init__``_.
|
|
||||||
:param kwargs: Keyword arguments eventually passed to
|
|
||||||
:func:``__init__``_
|
|
||||||
:return: the created instance.
|
|
||||||
"""
|
|
||||||
if cls is SingletonMixin:
|
|
||||||
raise TypeError(
|
|
||||||
"Attempt to instantiate\
|
|
||||||
mixin class {}".format(cls.__qualname__)
|
|
||||||
)
|
|
||||||
|
|
||||||
if cls._instance is None:
|
|
||||||
with cls._rlock:
|
|
||||||
if cls._instance is None and cls._inside_instance:
|
|
||||||
return super().__new__(cls, *args, **kwargs)
|
|
||||||
|
|
||||||
raise RuntimeError(
|
|
||||||
"Attempt to create a {}\
|
|
||||||
instance outside of instance()".format(cls.__qualname__)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class DummyRequestHander(http.server.CGIHTTPRequestHandler):
|
|
||||||
"""HTTP request handler for dummy server."""
|
"""HTTP request handler for dummy server."""
|
||||||
|
|
||||||
def __init__(self, request, client_address, server):
|
def __init__(self, request, client_address, server):
|
||||||
@ -106,14 +40,12 @@ class DummyRequestHander(http.server.CGIHTTPRequestHandler):
|
|||||||
Return:
|
Return:
|
||||||
True/False
|
True/False
|
||||||
"""
|
"""
|
||||||
manager = FakeServerManager.get_instance()
|
|
||||||
func_uri_list = manager._methods[self.command]
|
func_uri_list = manager._methods[self.command]
|
||||||
for objChkUrl in func_uri_list:
|
for objChkUrl in func_uri_list:
|
||||||
# Check which requested path is in our list.
|
# Check which requested path is in our list.
|
||||||
LOG.debug('path for check:%s' % objChkUrl)
|
LOG.debug('path for check:%s' % objChkUrl)
|
||||||
if(self.path.startswith(objChkUrl)):
|
if(self.path.startswith(objChkUrl)):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _returned_callback(self, path, mock_info):
|
def _returned_callback(self, path, mock_info):
|
||||||
@ -148,13 +80,15 @@ class DummyRequestHander(http.server.CGIHTTPRequestHandler):
|
|||||||
if len(response_body_str) > 0:
|
if len(response_body_str) > 0:
|
||||||
self.wfile.write(response_body_str)
|
self.wfile.write(response_body_str)
|
||||||
|
|
||||||
FakeServerManager.get_instance().add_history(path, RequestHistory(
|
manager.add_history(path, RequestHistory(
|
||||||
status_code=status_code,
|
status_code=status_code,
|
||||||
request_headers=request_headers,
|
request_headers=request_headers,
|
||||||
request_body=request_body,
|
request_body=request_body,
|
||||||
response_headers=copy.deepcopy(mock_headers),
|
response_headers=copy.deepcopy(mock_headers),
|
||||||
response_body=copy.deepcopy(mock_body))
|
response_body=copy.deepcopy(mock_body)))
|
||||||
)
|
|
||||||
|
if mock_info.get('content') is None:
|
||||||
|
self.end_headers()
|
||||||
|
|
||||||
def _parse_request_body(self):
|
def _parse_request_body(self):
|
||||||
if 'content-length' not in self.headers:
|
if 'content-length' not in self.headers:
|
||||||
@ -201,9 +135,7 @@ class DummyRequestHander(http.server.CGIHTTPRequestHandler):
|
|||||||
|
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
"""Process GET request"""
|
"""Process GET request"""
|
||||||
LOG.debug(
|
LOG.debug('[Start] %s.%s()' % (self.__class__.__name__,
|
||||||
'[Start] %s.%s()' %
|
|
||||||
(self.__class__.__name__,
|
|
||||||
inspect.currentframe().f_code.co_name))
|
inspect.currentframe().f_code.co_name))
|
||||||
|
|
||||||
# Check URI in request.
|
# Check URI in request.
|
||||||
@ -211,7 +143,7 @@ class DummyRequestHander(http.server.CGIHTTPRequestHandler):
|
|||||||
# Request is registered in our list.
|
# Request is registered in our list.
|
||||||
tplUri = urlparse(self.path)
|
tplUri = urlparse(self.path)
|
||||||
self._returned_callback(tplUri.path,
|
self._returned_callback(tplUri.path,
|
||||||
FakeServerManager.get_instance()._funcs_gets[tplUri.path])
|
manager._funcs_gets[tplUri.path])
|
||||||
else:
|
else:
|
||||||
# Unregistered URI is requested
|
# Unregistered URI is requested
|
||||||
LOG.debug('GET Recv. Unknown URL: "%s"' % self.path)
|
LOG.debug('GET Recv. Unknown URL: "%s"' % self.path)
|
||||||
@ -234,7 +166,7 @@ class DummyRequestHander(http.server.CGIHTTPRequestHandler):
|
|||||||
# Request is registered in our list.
|
# Request is registered in our list.
|
||||||
tplUri = urlparse(self.path)
|
tplUri = urlparse(self.path)
|
||||||
self._returned_callback(tplUri.path,
|
self._returned_callback(tplUri.path,
|
||||||
FakeServerManager.get_instance()._funcs_posts[tplUri.path])
|
manager._funcs_posts[tplUri.path])
|
||||||
else:
|
else:
|
||||||
# Unregistered URI is requested
|
# Unregistered URI is requested
|
||||||
LOG.debug('POST Recv. Unknown URL: "%s"' % self.path)
|
LOG.debug('POST Recv. Unknown URL: "%s"' % self.path)
|
||||||
@ -249,6 +181,8 @@ class DummyRequestHander(http.server.CGIHTTPRequestHandler):
|
|||||||
def do_PUT(self):
|
def do_PUT(self):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
return DummyRequestHandler
|
||||||
|
|
||||||
|
|
||||||
class RequestHistory:
|
class RequestHistory:
|
||||||
"""Storage class for storing requested data(Maybe POSTed datas)."""
|
"""Storage class for storing requested data(Maybe POSTed datas)."""
|
||||||
@ -268,7 +202,7 @@ class RequestHistory:
|
|||||||
self.response_body = response_body
|
self.response_body = response_body
|
||||||
|
|
||||||
|
|
||||||
class FakeServerManager(SingletonMixin):
|
class FakeServerManager(object):
|
||||||
"""Manager class to manage dummy server setting and control"""
|
"""Manager class to manage dummy server setting and control"""
|
||||||
|
|
||||||
SERVER_PORT = 9990
|
SERVER_PORT = 9990
|
||||||
@ -347,7 +281,6 @@ class FakeServerManager(SingletonMixin):
|
|||||||
path (str): URI path
|
path (str): URI path
|
||||||
history (RequestHistory): Storage container for each request.
|
history (RequestHistory): Storage container for each request.
|
||||||
"""
|
"""
|
||||||
with self._rlock:
|
|
||||||
if path in self._history:
|
if path in self._history:
|
||||||
self._history[path].append(history)
|
self._history[path].append(history)
|
||||||
else:
|
else:
|
||||||
@ -359,7 +292,6 @@ class FakeServerManager(SingletonMixin):
|
|||||||
Args:
|
Args:
|
||||||
path (str): URI path
|
path (str): URI path
|
||||||
"""
|
"""
|
||||||
with self._rlock:
|
|
||||||
if not path:
|
if not path:
|
||||||
self._history = {}
|
self._history = {}
|
||||||
return
|
return
|
||||||
@ -399,8 +331,9 @@ class FakeServerManager(SingletonMixin):
|
|||||||
inspect.currentframe().f_code.co_name))
|
inspect.currentframe().f_code.co_name))
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
RequestHandler = PrepareRequestHandler(self)
|
||||||
self.objHttpd = http.server.HTTPServer(
|
self.objHttpd = http.server.HTTPServer(
|
||||||
(address, port), DummyRequestHander)
|
(address, port), RequestHandler)
|
||||||
except OSError:
|
except OSError:
|
||||||
time.sleep(self.SERVER_INVOKE_CHECK_INTERVAL)
|
time.sleep(self.SERVER_INVOKE_CHECK_INTERVAL)
|
||||||
continue
|
continue
|
||||||
|
@ -38,7 +38,7 @@ VNF_DELETE_COMPLETION_WAIT = 60
|
|||||||
VNF_HEAL_TIMEOUT = 600
|
VNF_HEAL_TIMEOUT = 600
|
||||||
VNF_LCM_DONE_TIMEOUT = 1200
|
VNF_LCM_DONE_TIMEOUT = 1200
|
||||||
RETRY_WAIT_TIME = 5
|
RETRY_WAIT_TIME = 5
|
||||||
FAKE_SERVER_MANAGER = FakeServerManager.get_instance()
|
FAKE_SERVER_MANAGER = FakeServerManager()
|
||||||
FAKE_SERVER_PORT = 9990
|
FAKE_SERVER_PORT = 9990
|
||||||
MOCK_NOTIFY_CALLBACK_URL = '/notification/callback'
|
MOCK_NOTIFY_CALLBACK_URL = '/notification/callback'
|
||||||
UUID_RE = r'\w{8}-\w{4}-\w{4}-\w{4}-\w{12}'
|
UUID_RE = r'\w{8}-\w{4}-\w{4}-\w{4}-\w{12}'
|
||||||
|
@ -33,7 +33,7 @@ from tacker.tests.functional.sol_v2 import utils
|
|||||||
from tacker.tests import utils as base_utils
|
from tacker.tests import utils as base_utils
|
||||||
from tacker import version
|
from tacker import version
|
||||||
|
|
||||||
FAKE_SERVER_MANAGER = FakeServerManager.get_instance()
|
FAKE_SERVER_MANAGER = FakeServerManager()
|
||||||
MOCK_NOTIFY_CALLBACK_URL = '/notification/callback'
|
MOCK_NOTIFY_CALLBACK_URL = '/notification/callback'
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
Loading…
Reference in New Issue
Block a user