Browse Source

AMBARI-3337. When invalid jce policy file path is specified, ambari-server setup silently switches over to downloading the file from public repo.(Artem Baranchuk via odiachenko)

Oleksandr Diachenko 12 years ago
parent
commit
bf7972bcc7

+ 17 - 5
ambari-server/src/main/python/ambari-server.py

@@ -1594,11 +1594,23 @@ def install_jce_manualy(args):
   if properties == -1:
     err = "Error getting ambari properties"
     raise FatalException(-1, err)
-  if args.jce_policy and os.path.exists(args.jce_policy):
-    jce_destination = os.path.join(properties[RESOURCES_DIR_PROPERTY], JCE_POLICY_FILENAME)
-    shutil.copy(args.jce_policy, jce_destination)
-    print "JCE policy copied from " + args.jce_policy + " to " + jce_destination
-    return 0
+  if args.jce_policy:
+    if os.path.exists(args.jce_policy):
+      if os.path.isdir(args.jce_policy):
+        err = "JCE Policy path is a directory: " + args.jce_policy
+        raise FatalException(-1, err)
+      jce_destination = os.path.join(properties[RESOURCES_DIR_PROPERTY], JCE_POLICY_FILENAME)
+      try:
+        shutil.copy(args.jce_policy, jce_destination)
+      except Exception, e:
+        err = "Can not copy file {0} to {1} due to: {2}. Please check file " \
+              "permissions and free disk space.".format(args.jce_policy, jce_destination, e.message)
+        raise FatalException(-1, err)
+      print "JCE policy copied from " + args.jce_policy + " to " + jce_destination
+      return 0
+    else:
+      err = "JCE Policy path " + args.jce_policy + " doesn't exists."
+      raise FatalException(-1, err)
   else:
     return 1
 #

+ 26 - 1
ambari-server/src/test/python/TestAmbariServer.py

@@ -1042,9 +1042,10 @@ class TestAmbariServer(TestCase):
   @patch("shutil.copy")
   @patch("os.path.join")
   @patch("os.path.exists")
+  @patch("os.path.isdir")
   @patch.object(ambari_server, "get_ambari_properties")
   def test_install_jce_manualy(self, get_ambari_properties_mock, \
-                               os_path_exists_mock, os_path_join_mock, \
+                               os_path_isdir_mock, os_path_exists_mock, os_path_join_mock, \
                                shutil_copy_mock):
     args = MagicMock()
     args.jce_policy = "somewhere"
@@ -1052,7 +1053,31 @@ class TestAmbariServer(TestCase):
     get_ambari_properties_mock.return_value = p
     p.__getitem__.side_effect = None
     p.__getitem__.return_value = "somewhere"
+
+    # Case when JCE path doesn't exists
+    os_path_exists_mock.return_value = False
+    try:
+      ambari_server.install_jce_manualy(args)
+      self.fail("Should throw exception")
+    except FatalException as fe:
+      # Expected
+      self.assertTrue("JCE Policy path" in fe.reason)
+      pass
+    os_path_exists_mock.reset()
+
+    # Case when JCE is a directory
     os_path_exists_mock.return_value = True
+    os_path_isdir_mock.return_value = True
+    try:
+      ambari_server.install_jce_manualy(args)
+      self.fail("Should throw exception")
+    except FatalException as fe:
+      # Expected
+      self.assertTrue("JCE Policy path is a directory" in fe.reason)
+      pass
+    os_path_isdir_mock.reset()
+
+    os_path_isdir_mock.return_value = False
     os_path_join_mock.return_value = \
       "/var/lib/ambari-server/resources/jce_policy-6.zip"
     ambari_server.install_jce_manualy(args)