Bugfix:can not run a test suite if not under yardstick root path
[yardstick.git] / yardstick / benchmark / contexts / node.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
10 from __future__ import absolute_import
11 import sys
12 import os
13 import yaml
14 import logging
15
16 from yardstick.benchmark.contexts.base import Context
17 from yardstick.definitions import YARDSTICK_ROOT_PATH
18
19 LOG = logging.getLogger(__name__)
20
21
22 class NodeContext(Context):
23     '''Class that handle nodes info'''
24
25     __context_type__ = "Node"
26
27     def __init__(self):
28         self.name = None
29         self.file_path = None
30         self.nodes = []
31         self.controllers = []
32         self.computes = []
33         self.baremetals = []
34         super(self.__class__, self).__init__()
35
36     def init(self, attrs):
37         '''initializes itself from the supplied arguments'''
38         self.name = attrs["name"]
39         self.file_path = attrs.get("file", "pod.yaml")
40         if not os.path.exists(self.file_path):
41             self.file_path = os.path.join(YARDSTICK_ROOT_PATH, self.file_path)
42
43         LOG.info("Parsing pod file: %s", self.file_path)
44
45         try:
46             with open(self.file_path) as stream:
47                 cfg = yaml.load(stream)
48         except IOError as ioerror:
49             sys.exit(ioerror)
50
51         self.nodes.extend(cfg["nodes"])
52         self.controllers.extend([node for node in cfg["nodes"]
53                                  if node["role"] == "Controller"])
54         self.computes.extend([node for node in cfg["nodes"]
55                               if node["role"] == "Compute"])
56         self.baremetals.extend([node for node in cfg["nodes"]
57                                 if node["role"] == "Baremetal"])
58         LOG.debug("Nodes: %r", self.nodes)
59         LOG.debug("Controllers: %r", self.controllers)
60         LOG.debug("Computes: %r", self.computes)
61         LOG.debug("BareMetals: %r", self.baremetals)
62
63     def deploy(self):
64         '''don't need to deploy'''
65         pass
66
67     def undeploy(self):
68         '''don't need to undeploy'''
69         pass
70
71     def _get_server(self, attr_name):
72         '''lookup server info by name from context
73         attr_name: a name for a server listed in nodes config file
74         '''
75         if type(attr_name) is dict:
76             return None
77
78         if self.name != attr_name.split(".")[1]:
79             return None
80         node_name = attr_name.split(".")[0]
81         nodes = [n for n in self.nodes
82                  if n["name"] == node_name]
83         if len(nodes) == 0:
84             return None
85         elif len(nodes) > 1:
86             LOG.error("Duplicate nodes!!!")
87             LOG.error("Nodes: %r", nodes)
88             sys.exit(-1)
89
90         # A clone is created in order to avoid affecting the
91         # original one.
92         node = dict(nodes[0])
93         node["name"] = attr_name
94         return node