ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dmitriu...@apache.org
Subject ambari git commit: AMBARI-12647. Agent errors out during cache invalidation if cache directories are missing (dlysnichenko)
Date Mon, 17 Aug 2015 17:00:47 GMT
Repository: ambari
Updated Branches:
  refs/heads/branch-2.1 6960cae78 -> 1e8822b6b


AMBARI-12647. Agent errors out during cache invalidation if cache directories are missing
(dlysnichenko)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/1e8822b6
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/1e8822b6
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/1e8822b6

Branch: refs/heads/branch-2.1
Commit: 1e8822b6baf62565b59b49f43d68ea70e0d1654d
Parents: 6960cae
Author: Lisnichenko Dmitro <dlysnichenko@hortonworks.com>
Authored: Mon Aug 17 20:01:00 2015 +0300
Committer: Lisnichenko Dmitro <dlysnichenko@hortonworks.com>
Committed: Mon Aug 17 20:01:00 2015 +0300

----------------------------------------------------------------------
 .../src/main/python/ambari_agent/FileCache.py   |  9 +++++----
 .../test/python/ambari_agent/TestFileCache.py   | 21 +++++++++++++++++++-
 .../test/python/ambari_agent/TestLiveStatus.py  |  4 +++-
 ambari-agent/src/test/python/unitTests.py       | 17 +++++++++-------
 4 files changed, 38 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/1e8822b6/ambari-agent/src/main/python/ambari_agent/FileCache.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/ambari_agent/FileCache.py b/ambari-agent/src/main/python/ambari_agent/FileCache.py
index 641c9c4..1a683a1 100644
--- a/ambari-agent/src/main/python/ambari_agent/FileCache.py
+++ b/ambari-agent/src/main/python/ambari_agent/FileCache.py
@@ -235,10 +235,11 @@ class FileCache():
     """
     logger.debug("Invalidating directory {0}".format(directory))
     try:
-      if os.path.isfile(directory): # It would be a strange situation
-        os.unlink(directory)
-      elif os.path.isdir(directory):
-        shutil.rmtree(directory)
+      if os.path.exists(directory):
+        if os.path.isfile(directory): # It would be a strange situation
+          os.unlink(directory)
+        elif os.path.isdir(directory):
+          shutil.rmtree(directory)
       # create directory itself and any parent directories
       os.makedirs(directory)
     except Exception, err:

http://git-wip-us.apache.org/repos/asf/ambari/blob/1e8822b6/ambari-agent/src/test/python/ambari_agent/TestFileCache.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/ambari_agent/TestFileCache.py b/ambari-agent/src/test/python/ambari_agent/TestFileCache.py
index 4ead0d2..e2aadc6 100644
--- a/ambari-agent/src/test/python/ambari_agent/TestFileCache.py
+++ b/ambari-agent/src/test/python/ambari_agent/TestFileCache.py
@@ -291,17 +291,20 @@ class TestFileCache(TestCase):
         self.fail('Unexpected exception thrown:' + str(e))
 
 
+  @patch("os.path.exists")
   @patch("os.path.isfile")
   @patch("os.path.isdir")
   @patch("os.unlink")
   @patch("shutil.rmtree")
   @patch("os.makedirs")
   def test_invalidate_directory(self, makedirs_mock, rmtree_mock,
-                                unlink_mock, isdir_mock, isfile_mock):
+                                unlink_mock, isdir_mock, isfile_mock,
+                                exists_mock):
     fileCache = FileCache(self.config)
     # Test execution flow if path points to file
     isfile_mock.return_value = True
     isdir_mock.return_value = False
+    exists_mock.return_value = True
 
     fileCache.invalidate_directory("dummy-dir")
 
@@ -316,6 +319,7 @@ class TestFileCache(TestCase):
     # Test execution flow if path points to dir
     isfile_mock.return_value = False
     isdir_mock.return_value = True
+    exists_mock.return_value = True
 
     fileCache.invalidate_directory("dummy-dir")
 
@@ -327,6 +331,21 @@ class TestFileCache(TestCase):
     rmtree_mock.reset_mock()
     makedirs_mock.reset_mock()
 
+    # Test execution flow if path points nowhere
+    isfile_mock.return_value = False
+    isdir_mock.return_value = False
+    exists_mock.return_value = False
+
+    fileCache.invalidate_directory("dummy-dir")
+
+    self.assertFalse(unlink_mock.called)
+    self.assertFalse(rmtree_mock.called)
+    self.assertTrue(makedirs_mock.called)
+
+    unlink_mock.reset_mock()
+    rmtree_mock.reset_mock()
+    makedirs_mock.reset_mock()
+
     # Test exception handling
     makedirs_mock.side_effect = self.exc_side_effect
     try:

http://git-wip-us.apache.org/repos/asf/ambari/blob/1e8822b6/ambari-agent/src/test/python/ambari_agent/TestLiveStatus.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/ambari_agent/TestLiveStatus.py b/ambari-agent/src/test/python/ambari_agent/TestLiveStatus.py
index e9bb776..6a94583 100644
--- a/ambari-agent/src/test/python/ambari_agent/TestLiveStatus.py
+++ b/ambari-agent/src/test/python/ambari_agent/TestLiveStatus.py
@@ -144,9 +144,11 @@ class TestLiveStatus(TestCase):
     # enable stdout
     sys.stdout = sys.__stdout__
 
+  @patch("os.path.isdir")
   @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))
   @patch.object(ActualConfigHandler.ActualConfigHandler, "read_actual_component")
-  def test_build(self, read_actual_component_mock):
+  def test_build(self, read_actual_component_mock, isdir_mock):
+    isdir_mock.return_value = False
     for component in LiveStatus.COMPONENTS:
       config = AmbariConfig().getConfig()
       config.set('agent', 'prefix', "ambari_agent" + os.sep + "dummy_files")

http://git-wip-us.apache.org/repos/asf/ambari/blob/1e8822b6/ambari-agent/src/test/python/unitTests.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/unitTests.py b/ambari-agent/src/test/python/unitTests.py
index 83b5a0f..e9ceb2e 100644
--- a/ambari-agent/src/test/python/unitTests.py
+++ b/ambari-agent/src/test/python/unitTests.py
@@ -25,6 +25,9 @@ python unitTests.py
 python unitTests.py NameOfFile.py
 python unitTests.py NameOfFileWithoutExtension  (this will append .* to the end, so it can
match other file names too)
 
+prepend _ to test file name(s) and run "python unitTests.py": execute only
+  test files whose name begins with _ (useful for quick debug)
+
 SETUP:
 To run in Linux from command line,
 cd to this same directory. Then make sure PYTHONPATH is correct.
@@ -38,6 +41,7 @@ $(pwd)/ambari-agent/src/test/python/resource_management:
 $(pwd)/ambari-common/src/main/python/ambari_jinja2
 """
 
+import re
 import unittest
 import fnmatch
 from os.path import isdir
@@ -55,8 +59,6 @@ if get_platform() == PLATFORM_WINDOWS:
 else:
   IGNORE_FOLDERS = ["resource_management_windows"]
 
-TEST_MASK = '[Tt]est*.py'
-
 class TestAgent(unittest.TestSuite):
   def run(self, result):
     run = unittest.TestSuite.run
@@ -80,7 +82,7 @@ def get_test_files(path, mask=None, recursive=True):
   """
   # Must convert mask so it can match a file
   if mask and mask != "" and not mask.endswith("*"):
-    mask = mask + "*"
+    mask=mask+"*"
 
   file_list = []
   directory_items = os.listdir(path)
@@ -89,9 +91,10 @@ def get_test_files(path, mask=None, recursive=True):
     add_to_pythonpath = False
     p = os.path.join(path, item)
     if os.path.isfile(p):
-      if fnmatch.fnmatch(item, mask):
-        add_to_pythonpath = True
-        file_list.append(item)
+      if mask is not None and fnmatch.fnmatch(item, mask) or \
+        mask is None and re.search(r"^_?[Tt]est.*\.py$", item):
+          add_to_pythonpath = True
+          file_list.append(item)
     elif os.path.isdir(p)and p not in IGNORE_FOLDERS:
       if recursive:
         file_list.extend(get_test_files(p, mask=mask))
@@ -102,7 +105,7 @@ def get_test_files(path, mask=None, recursive=True):
 
 
 def all_tests_suite(custom_test_mask):
-  test_mask = custom_test_mask if custom_test_mask else TEST_MASK
+  test_mask = custom_test_mask if custom_test_mask else None
 
   src_dir = os.getcwd()
   files_list = get_test_files(src_dir, mask=test_mask)


Mime
View raw message