Bugfix:can not run a test suite if not under yardstick root path
[yardstick.git] / yardstick / benchmark / contexts / base.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from __future__ import absolute_import
10 import abc
11 import six
12
13 import yardstick.common.utils as utils
14
15
16 @six.add_metaclass(abc.ABCMeta)
17 class Context(object):
18     '''Class that represents a context in the logical model'''
19     list = []
20
21     def __init__(self):
22         Context.list.append(self)
23
24     @abc.abstractmethod
25     def init(self, attrs):
26         "Initiate context."
27
28     @staticmethod
29     def get_cls(context_type):
30         '''Return class of specified type.'''
31         for context in utils.itersubclasses(Context):
32             if context_type == context.__context_type__:
33                 return context
34         raise RuntimeError("No such context_type %s" % context_type)
35
36     @staticmethod
37     def get(context_type):
38         """Returns instance of a context for context type.
39         """
40         return Context.get_cls(context_type)()
41
42     @abc.abstractmethod
43     def deploy(self):
44         '''Deploy context.'''
45
46     @abc.abstractmethod
47     def undeploy(self):
48         '''Undeploy context.'''
49
50     @abc.abstractmethod
51     def _get_server(self, attr_name):
52         '''get server info by name from context
53         '''
54
55     @staticmethod
56     def get_server(attr_name):
57         '''lookup server info by name from context
58         attr_name: either a name for a server created by yardstick or a dict
59         with attribute name mapping when using external heat templates
60         '''
61         server = None
62         for context in Context.list:
63             server = context._get_server(attr_name)
64             if server is not None:
65                 break
66
67         if server is None:
68             raise ValueError("context not found for server '%r'" %
69                              attr_name)
70
71         return server