aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKubi <jean.gaoliang@huawei.com>2017-01-19 07:35:55 +0000
committerGerrit Code Review <gerrit@opnfv.org>2017-01-19 07:35:55 +0000
commitfd1d3c0504c5e5b153b78bd0d5bd8fe3f4bc37d4 (patch)
tree764c4ca7db6b8c589a0ac24d715bc6b369a4233e
parentcd36791a022aefc54eb258d62037274f7065a399 (diff)
parentb56008ed0978632063f6dd9653f9d7a2070e2727 (diff)
Merge "Re-factor Node.py to use better python inbuilt functions"
-rw-r--r--tests/unit/benchmark/contexts/test_node.py4
-rw-r--r--yardstick/benchmark/contexts/node.py56
2 files changed, 37 insertions, 23 deletions
diff --git a/tests/unit/benchmark/contexts/test_node.py b/tests/unit/benchmark/contexts/test_node.py
index de5ba7066..64fe4a566 100644
--- a/tests/unit/benchmark/contexts/test_node.py
+++ b/tests/unit/benchmark/contexts/test_node.py
@@ -42,7 +42,7 @@ class NodeContextTestCase(unittest.TestCase):
'file': self._get_file_abspath("error_file")
}
- self.assertRaises(SystemExit, self.test_context.init, attrs)
+ self.assertRaises(IOError, self.test_context.init, attrs)
def test_successful_init(self):
@@ -100,7 +100,7 @@ class NodeContextTestCase(unittest.TestCase):
attr_name = 'node1.foo'
- self.assertRaises(SystemExit, self.test_context._get_server, attr_name)
+ self.assertRaises(ValueError, self.test_context._get_server, attr_name)
def test__get_server_found(self):
diff --git a/yardstick/benchmark/contexts/node.py b/yardstick/benchmark/contexts/node.py
index e02a71669..9242e2727 100644
--- a/yardstick/benchmark/contexts/node.py
+++ b/yardstick/benchmark/contexts/node.py
@@ -8,10 +8,11 @@
##############################################################################
from __future__ import absolute_import
-import sys
+import logging
+import errno
import os
+import collections
import yaml
-import logging
from yardstick.benchmark.contexts.base import Context
from yardstick.definitions import YARDSTICK_ROOT_PATH
@@ -33,20 +34,28 @@ class NodeContext(Context):
self.baremetals = []
super(self.__class__, self).__init__()
+ def read_config_file(self):
+ """Read from config file"""
+
+ with open(self.file_path) as stream:
+ LOG.info("Parsing pod file: %s", self.file_path)
+ cfg = yaml.load(stream)
+ return cfg
+
def init(self, attrs):
"""initializes itself from the supplied arguments"""
self.name = attrs["name"]
self.file_path = attrs.get("file", "pod.yaml")
- if not os.path.exists(self.file_path):
- self.file_path = os.path.join(YARDSTICK_ROOT_PATH, self.file_path)
-
- LOG.info("Parsing pod file: %s", self.file_path)
try:
- with open(self.file_path) as stream:
- cfg = yaml.load(stream)
+ cfg = self.read_config_file()
except IOError as ioerror:
- sys.exit(ioerror)
+ if ioerror.errno == errno.ENOENT:
+ self.file_path = \
+ os.path.join(YARDSTICK_ROOT_PATH, self.file_path)
+ cfg = self.read_config_file()
+ else:
+ raise
self.nodes.extend(cfg["nodes"])
self.controllers.extend([node for node in cfg["nodes"]
@@ -72,23 +81,28 @@ class NodeContext(Context):
"""lookup server info by name from context
attr_name: a name for a server listed in nodes config file
"""
- if type(attr_name) is dict:
+ if isinstance(attr_name, collections.Mapping):
return None
if self.name != attr_name.split(".")[1]:
return None
node_name = attr_name.split(".")[0]
- nodes = [n for n in self.nodes
- if n["name"] == node_name]
- if len(nodes) == 0:
+ matching_nodes = (n for n in self.nodes if n["name"] == node_name)
+
+ try:
+ # A clone is created in order to avoid affecting the
+ # original one.
+ node = dict(next(matching_nodes))
+ except StopIteration:
return None
- elif len(nodes) > 1:
- LOG.error("Duplicate nodes!!!")
- LOG.error("Nodes: %r", nodes)
- sys.exit(-1)
-
- # A clone is created in order to avoid affecting the
- # original one.
- node = dict(nodes[0])
+
+ try:
+ duplicate = next(matching_nodes)
+ except StopIteration:
+ pass
+ else:
+ raise ValueError("Duplicate nodes!!! Nodes: %s %s",
+ (matching_nodes, duplicate))
+
node["name"] = attr_name
return node