Parcourir la source

AMBARI-12514. THP changes / package issues (and possibly others) aren’t detected with a ‘Re-run’ action.(vbrodetskyi)

Vitaly Brodetskyi il y a 10 ans
Parent
commit
cc03263ee6

+ 21 - 1
ambari-server/src/main/resources/custom_actions/scripts/check_host.py

@@ -21,6 +21,7 @@ Ambari Agent
 """
 
 import os
+import re
 import subprocess
 import socket
 import getpass
@@ -42,6 +43,7 @@ CHECK_HOST_RESOLUTION = "host_resolution_check"
 CHECK_LAST_AGENT_ENV = "last_agent_env_check"
 CHECK_INSTALLED_PACKAGES = "installed_packages"
 CHECK_EXISTING_REPOS = "existing_repos"
+CHECK_TRANSPARENT_HUGE_PAGE = "transparentHugePage"
 
 DB_MYSQL = "mysql"
 DB_ORACLE = "oracle"
@@ -59,6 +61,8 @@ JDBC_DRIVER_SYMLINK_POSTGRESQL = "postgres-jdbc-driver.jar"
 JDBC_DRIVER_SYMLINK_MSSQL = "sqljdbc4.jar"
 JDBC_AUTH_SYMLINK_MSSQL = "sqljdbc_auth.dll"
 
+THP_FILE = "/sys/kernel/mm/redhat_transparent_hugepage/enabled"
+
 class CheckHost(Script):
   # Packages that are used to find repos (then repos are used to find other packages)
   PACKAGES = [
@@ -146,7 +150,23 @@ class CheckHost(Script):
         print "There was an unknown error while checking installed packages and existing repositories: " + str(exception)
         structured_output[CHECK_INSTALLED_PACKAGES] = {"exit_code" : 1, "message": str(exception)}
         structured_output[CHECK_EXISTING_REPOS] = {"exit_code" : 1, "message": str(exception)}
-        
+
+    # Here we are checking transparent huge page if CHECK_TRANSPARENT_HUGE_PAGE is in check_execute_list
+    if CHECK_TRANSPARENT_HUGE_PAGE in check_execute_list:
+      try :
+        # This file exist only on redhat 6
+        thp_regex = "\[(.+)\]"
+        if os.path.isfile(THP_FILE):
+          with open(THP_FILE) as f:
+            file_content = f.read()
+            structured_output[CHECK_TRANSPARENT_HUGE_PAGE] = {"exit_code" : 0, "message": str(re.search(thp_regex,
+                                                                                            file_content).groups()[0])}
+        else:
+          structured_output[CHECK_TRANSPARENT_HUGE_PAGE] = {"exit_code" : 0, "message": ""}
+      except Exception, exception :
+        print "There was an unknown error while getting transparent huge page data: " + str(exception)
+        structured_output[CHECK_TRANSPARENT_HUGE_PAGE] = {"exit_code" : 1, "message": str(exception)}
+
     # this is necessary for HostCleanup to know later what were the results.
     self.reportFileHandler.writeHostChecksCustomActionsFile(structured_output)
     

+ 40 - 1
ambari-server/src/test/python/custom_actions/TestCheckHost.py

@@ -287,4 +287,43 @@ class TestCheckHost(TestCase):
     checkHost.actionexecute(None)
 
     #ensure the correct response is returned
-    put_structured_out_mock.assert_called_with({'last_agent_env_check': {'message': 'test exception', 'exit_code': 1}})
+    put_structured_out_mock.assert_called_with({'last_agent_env_check': {'message': 'test exception', 'exit_code': 1}})
+
+
+  @patch("resource_management.libraries.script.Script.put_structured_out")
+  @patch.object(Script, 'get_tmp_dir')
+  @patch.object(Script, 'get_config')
+  @patch("os.path.isfile")
+  @patch('__builtin__.open')
+  def testTransparentHugePage(self, open_mock, os_path_isfile_mock, mock_config, get_tmp_dir_mock, structured_out_mock):
+    context_manager_mock = MagicMock()
+    open_mock.return_value = context_manager_mock
+    file_mock = MagicMock()
+    file_mock.read.return_value = "[never] always"
+    enter_mock = MagicMock()
+    enter_mock.return_value = file_mock
+    enter_mock = MagicMock()
+    enter_mock.return_value = file_mock
+    exit_mock  = MagicMock()
+    setattr( context_manager_mock, '__enter__', enter_mock )
+    setattr( context_manager_mock, '__exit__', exit_mock )
+    os_path_isfile_mock.return_value = True
+    get_tmp_dir_mock.return_value = "/tmp"
+    mock_config.return_value = {"commandParams" : {"check_execute_list" : "transparentHugePage"}}
+
+    checkHost = CheckHost()
+    checkHost.actionexecute(None)
+
+    self.assertEquals(structured_out_mock.call_args[0][0], {'transparentHugePage' : {'message': 'never', 'exit_code': 0}})
+
+    # case 2, file not exists
+    os_path_isfile_mock.return_value = False
+    checkHost.actionexecute(None)
+
+    self.assertEquals(structured_out_mock.call_args[0][0], {'transparentHugePage' : {'message': '', 'exit_code': 0}})
+
+
+
+
+
+