Merge "apexlake: speed unittest, mock time.sleep"
[yardstick.git] / yardstick / benchmark / contexts / base.py
index ae860ac..e362c6a 100644 (file)
@@ -6,6 +6,7 @@
 # which accompanies this distribution, and is available at
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
+from __future__ import absolute_import
 import abc
 import six
 
@@ -14,7 +15,7 @@ import yardstick.common.utils as utils
 
 @six.add_metaclass(abc.ABCMeta)
 class Context(object):
-    '''Class that represents a context in the logical model'''
+    """Class that represents a context in the logical model"""
     list = []
 
     def __init__(self):
@@ -22,11 +23,11 @@ class Context(object):
 
     @abc.abstractmethod
     def init(self, attrs):
-        "Initiate context."
+        """Initiate context."""
 
     @staticmethod
     def get_cls(context_type):
-        '''Return class of specified type.'''
+        """Return class of specified type."""
         for context in utils.itersubclasses(Context):
             if context_type == context.__context_type__:
                 return context
@@ -40,31 +41,49 @@ class Context(object):
 
     @abc.abstractmethod
     def deploy(self):
-        '''Deploy context.'''
+        """Deploy context."""
 
     @abc.abstractmethod
     def undeploy(self):
-        '''Undeploy context.'''
+        """Undeploy context."""
+        self._delete_context()
+
+    def _delete_context(self):
+        Context.list.remove(self)
 
     @abc.abstractmethod
     def _get_server(self, attr_name):
-        '''get server object by name from context
-        '''
+        """get server info by name from context
+        """
+
+    @abc.abstractmethod
+    def _get_network(self, attr_name):
+        """get network info by name from context
+        """
 
     @staticmethod
     def get_server(attr_name):
-        '''lookup server object by name from context
+        """lookup server info by name from context
         attr_name: either a name for a server created by yardstick or a dict
         with attribute name mapping when using external heat templates
-        '''
-        server = None
-        for context in Context.list:
-            server = context._get_server(attr_name)
-            if server is not None:
-                break
+        """
+        servers = (context._get_server(attr_name) for context in Context.list)
+        try:
+            return next(s for s in servers if s)
+        except StopIteration:
+            raise ValueError("context not found for server '%r'" %
+                             attr_name)
 
-        if server is None:
-            raise ValueError("context not found for server '%s'" %
-                             attr_name["name"])
+    @staticmethod
+    def get_network(attr_name):
+        """lookup server info by name from context
+        attr_name: either a name for a server created by yardstick or a dict
+        with attribute name mapping when using external heat templates
+        """
 
-        return server
+        networks = (context._get_network(attr_name) for context in Context.list)
+        try:
+            return next(n for n in networks if n)
+        except StopIteration:
+            raise ValueError("context not found for server '%r'" %
+                             attr_name)