Merge "Ensure that at least one handler is available"
authorRoss Brattain <ross.b.brattain@intel.com>
Tue, 3 Oct 2017 17:56:25 +0000 (17:56 +0000)
committerGerrit Code Review <gerrit@opnfv.org>
Tue, 3 Oct 2017 17:56:25 +0000 (17:56 +0000)
tests/unit/common/test_utils.py
yardstick/__init__.py
yardstick/common/constants.py
yardstick/common/utils.py

index 923ec4a..42b75d1 100644 (file)
@@ -17,6 +17,7 @@ import unittest
 from copy import deepcopy
 from itertools import product, chain
 
+import errno
 import mock
 from six.moves import configparser
 
@@ -780,6 +781,21 @@ class RemoveFileTestCase(unittest.TestCase):
 
 class TestUtils(unittest.TestCase):
 
+    @mock.patch('yardstick.common.utils.os.makedirs')
+    def test_makedirs(self, *_):
+        self.assertIsNone(utils.makedirs('a/b/c/d'))
+
+    @mock.patch('yardstick.common.utils.os.makedirs')
+    def test_makedirs_exists(self, mock_os_makedirs):
+        mock_os_makedirs.side_effect = OSError(errno.EEXIST, 'exists')
+        self.assertIsNone(utils.makedirs('a/b/c/d'))
+
+    @mock.patch('yardstick.common.utils.os.makedirs')
+    def test_makedirs_busy(self, mock_os_makedirs):
+        mock_os_makedirs.side_effect = OSError(errno.EBUSY, 'busy')
+        with self.assertRaises(OSError):
+            utils.makedirs('a/b/c/d')
+
     @mock.patch('yardstick.common.utils.jsonify')
     def test_result_handler(self, mock_jsonify):
         mock_jsonify.return_value = 432
index b673e7c..f95b0a9 100644 (file)
 from __future__ import absolute_import
 import logging
 import os
+import errno
 
+# this module must only import other modules that do
+# not require loggers to be created, so this cannot
+# include yardstick.common.utils
 from yardstick.common import constants
-from yardstick.common import utils as yardstick_utils
 
-yardstick_utils.makedirs(constants.LOG_DIR)
+try:
+    # do not use yardstick.common.utils.makedirs
+    # since yardstick.common.utils creates a logger
+    # and so it cannot be imported before this code
+    os.makedirs(constants.LOG_DIR)
+except OSError as e:
+    if e.errno != errno.EEXIST:
+        raise
+
 LOG_FILE = os.path.join(constants.LOG_DIR, 'yardstick.log')
 LOG_FORMATTER = '%(asctime)s [%(levelname)s] %(name)s %(filename)s:%(lineno)d %(message)s'
 
@@ -34,6 +45,7 @@ def _init_logging():
         _LOG_STREAM_HDLR.setLevel(logging.DEBUG)
     else:
         _LOG_STREAM_HDLR.setLevel(logging.INFO)
+
     # don't append to log file, clobber
     _LOG_FILE_HDLR.setFormatter(_LOG_FORMATTER)
     _LOG_FILE_HDLR.setLevel(logging.DEBUG)
index b416f42..32ed746 100644 (file)
@@ -8,11 +8,16 @@
 ##############################################################################
 from __future__ import absolute_import
 import os
+import errno
+
 from functools import reduce
 
 import pkg_resources
 
-from yardstick.common.utils import parse_yaml
+# this module must only import other modules that do
+# not require loggers to be created, so this cannot
+# include yardstick.common.utils
+from yardstick.common.yaml_loader import yaml_load
 
 dirname = os.path.dirname
 abspath = os.path.abspath
@@ -29,7 +34,19 @@ def get_param(key, default=''):
 
     # don't re-parse yaml for each lookup
     if not CONF:
-        CONF.update(parse_yaml(conf_file))
+        # do not use yardstick.common.utils.parse_yaml
+        # since yardstick.common.utils creates a logger
+        # and so it cannot be imported before this code
+        try:
+            with open(conf_file) as f:
+                value = yaml_load(f)
+        except IOError:
+            pass
+        except OSError as e:
+            if e.errno != errno.EEXIST:
+                raise
+        else:
+            CONF.update(value)
     try:
         return reduce(lambda a, b: a[b], key.split('.'), CONF)
     except KeyError:
index 68c9ed6..6ac99a5 100644 (file)
@@ -37,7 +37,6 @@ from oslo_utils import importutils
 from oslo_serialization import jsonutils
 
 import yardstick
-from yardstick.common.yaml_loader import yaml_load
 
 logger = logging.getLogger(__name__)
 logger.setLevel(logging.DEBUG)
@@ -94,19 +93,6 @@ def import_modules_from_package(package):
                 logger.exception("unable to import %s", module_name)
 
 
-def parse_yaml(file_path):
-    try:
-        with open(file_path) as f:
-            value = yaml_load(f)
-    except IOError:
-        return {}
-    except OSError as e:
-        if e.errno != errno.EEXIST:
-            raise
-    else:
-        return value
-
-
 def makedirs(d):
     try:
         os.makedirs(d)