瀏覽代碼

AMBARI-9507. RU - Prepare Namenode fails on Ubuntu12 due to path error with import (alejandro)

Alejandro Fernandez 10 年之前
父節點
當前提交
b654355970

+ 7 - 5
ambari-server/src/main/resources/custom_actions/scripts/ru_execute_tasks.py

@@ -19,15 +19,14 @@ limitations under the License.
 Ambari Agent
 
 """
-
+import re
 import os
 import json
 import socket
-import time
 
 from resource_management.libraries.script import Script
 from resource_management.libraries.functions.default import default
-from resource_management.core.shell import call
+from resource_management.core import shell
 from resource_management.core.exceptions import Fail
 from resource_management.core.logger import Logger
 from ambari_agent.FileCache import FileCache
@@ -127,7 +126,8 @@ class ExecuteUpgradeTasks(Script):
 
           # Notice that the script_path is now the fully qualified path, and the
           # same command-#.json file is used.
-          command_params = ["python",
+          # Also, the python wrapper is used, since it sets up the correct environment variables
+          command_params = ["/usr/bin/ambari-python-wrap",
                             script_path,
                             task.function,
                             self.command_data_file,
@@ -137,10 +137,12 @@ class ExecuteUpgradeTasks(Script):
                             Script.get_tmp_dir()]
 
           task.command = " ".join(command_params)
+          # Replace redundant whitespace to make the unit tests easier to validate
+          task.command = re.sub("\s+", " ", task.command).strip()
 
         if task.command:
           task.command = replace_variables(task.command, host_name, version)
-          code, out = call(task.command)
+          code, out = shell.call(task.command)
           Logger.info("Command: %s\nCode: %s, Out: %s" % (task.command, str(code), str(out)))
           if code != 0:
             raise Fail(out)

+ 104 - 0
ambari-server/src/test/python/custom_actions/test_ru_execute_tasks.py

@@ -0,0 +1,104 @@
+# !/usr/bin/env python
+
+'''
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+'''
+
+# Python Imports
+import os
+import json
+
+from mock.mock import patch
+from mock.mock import MagicMock
+
+# Module imports
+from stacks.utils.RMFTestCase import *
+from resource_management import Script, ConfigDictionary
+from resource_management.libraries.functions.default import default
+from resource_management.core.logger import Logger
+from ambari_agent.AmbariConfig import AmbariConfig
+from ambari_commons.os_check import OSCheck
+
+
+def fake_call(command):
+  """
+  Instead of shell.call, call a command whose output equals the command.
+  :param command: Command that will be echoed.
+  :return: Returns a tuple of (process output code, output)
+  """
+  return (0, command)
+
+
+class TestRUExecuteTasks(RMFTestCase):
+  CUSTOM_ACTIONS_DIR = os.path.join(os.getcwd(), "../resources/custom_actions/")
+
+  @patch.object(Logger, "info")
+  @patch.object(Logger, "error")
+  @patch.object(OSCheck, "get_os_type")
+  def setUp(self, os_type_mock, error_mock, info_mock):
+    # Must patch the logger and get_os_type function.
+    os_type_mock.return_value = "redhat"
+
+    Logger.logger = MagicMock()
+
+    # Import the class under test. This is done here as opposed to the rest of the imports because the get_os_type()
+    # method needs to be patched first.
+    from ru_execute_tasks import ExecuteUpgradeTasks
+    global ExecuteUpgradeTasks
+
+  def tearDown(self):
+    Logger.logger = None
+
+  @patch("resource_management.core.shell.call")
+  @patch("os.path.exists")
+  @patch.object(AmbariConfig, "getConfigFile")
+  @patch.object(Script, 'get_tmp_dir')
+  @patch.object(Script, 'get_config')
+  def test_execution(self, get_config_mock, get_tmp_dir_mock, get_config_file_mock, os_path_exists_mock, call_mock):
+    # Mock the config objects
+    json_file_path = os.path.join(self.CUSTOM_ACTIONS_DIR, "ru_execute_tasks_namenode_prepare.json")
+    self.assertTrue(os.path.isfile(json_file_path))
+    with open(json_file_path, "r") as json_file:
+      json_payload = json.load(json_file)
+
+    config_dict = ConfigDictionary(json_payload)
+
+    get_config_mock.return_value = config_dict
+    get_tmp_dir_mock.return_value = "/tmp"
+
+    ambari_agent_ini_file_path = os.path.join(os.getcwd(), "../../../../ambari-agent/conf/unix/ambari-agent.ini")
+    self.assertTrue(os.path.isfile(ambari_agent_ini_file_path))
+    get_config_file_mock.return_value = ambari_agent_ini_file_path
+
+    # Mock os calls
+    os_path_exists_mock.return_value = True
+    call_mock.side_effect = fake_call   # echo the command
+
+    # Ensure that the json file was actually read.
+    stack_name = default("/hostLevelParams/stack_name", None)
+    stack_version = default("/hostLevelParams/stack_version", None)
+    service_package_folder = default('/roleParams/service_package_folder', None)
+
+    self.assertEqual(stack_name, "HDP")
+    self.assertEqual(stack_version, 2.2)
+    self.assertEqual(service_package_folder, "common-services/HDFS/2.1.0.2.0/package")
+
+    # Begin the test
+    ru_execute = ExecuteUpgradeTasks()
+    ru_execute.actionexecute(None)
+
+    call_mock.assert_called_with("/usr/bin/ambari-python-wrap /var/lib/ambari-agent/cache/common-services/HDFS/2.1.0.2.0/package/scripts/namenode.py prepare_rolling_upgrade /tmp")

文件差異過大導致無法顯示
+ 165 - 0
ambari-server/src/test/resources/custom_actions/ru_execute_tasks_namenode_prepare.json


部分文件因文件數量過多而無法顯示