Selaa lähdekoodia

AMBARI-863. Fix mvn tests to be able to run the python tests cleanly. (mahadev)

git-svn-id: https://svn.apache.org/repos/asf/incubator/ambari/branches/AMBARI-666@1398613 13f79535-47bb-0310-9956-ffa450edef68
Mahadev Konar 12 vuotta sitten
vanhempi
commit
0f888a1e08

+ 3 - 0
AMBARI-666-CHANGES.txt

@@ -12,6 +12,9 @@ AMBARI-666 branch (unreleased changes)
 
   NEW FEATURES
 
+  AMBARI-863. Fix mvn tests to be able to run the python tests cleanly.
+  (mahadev)
+
   AMBARI-849. Stage planner implementation. (jitendra)
 
   AMBARI-860. Remove code that adds fake stages for testing. (jitendra)

+ 30 - 3
ambari-agent/src/test/python/unitTests.py

@@ -21,7 +21,10 @@ limitations under the License.
 import unittest
 import doctest
 from os.path import dirname, split, isdir
+import logging.handlers
+import logging
 
+LOG_FILE_NAME='tests.log'
 
 
 class TestAgent(unittest.TestSuite):
@@ -44,14 +47,32 @@ def all_tests_suite():
   return TestAgent([suite])
 
 def main():
+
+  logger.info('------------------------------------------------------------------------')
+  logger.info('PYTHON AGENT TESTS')
+  logger.info('------------------------------------------------------------------------')
   parent_dir = lambda x: split(x)[0] if isdir(x) else split(dirname(x))[0]
   src_dir = os.getcwd()
-  agent_dir = parent_dir(parent_dir(parent_dir(src_dir)))
-  path = agent_dir + os.sep + "target/tests.log"
+  target_dir = parent_dir(parent_dir(parent_dir(src_dir))) + os.sep + 'target'
+  if not os.path.exists(target_dir):
+    os.mkdir(target_dir)
+  path = target_dir + os.sep + LOG_FILE_NAME
   file=open(path, "w")
   runner = unittest.TextTestRunner(stream=file)
   suite = all_tests_suite()
-  raise SystemExit(not runner.run(suite).wasSuccessful())
+
+  status = runner.run(suite).wasSuccessful()
+
+  if not status:
+    logger.error('-----------------------------------------------------------------------')
+    logger.error('Python unit tests failed')
+    logger.error('Find detailed logs in ' + path)
+    logger.error('-----------------------------------------------------------------------')
+    exit(1)
+  else:
+    logger.info('------------------------------------------------------------------------')
+    logger.info('Python unit tests finished succesfully')
+    logger.info('------------------------------------------------------------------------')
 
 if __name__ == '__main__':
   import os
@@ -60,4 +81,10 @@ if __name__ == '__main__':
   sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
   sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + os.sep + 'main' + os.sep + 'python')
   sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + os.sep + 'main' + os.sep + 'python' + os.sep + 'ambari_agent')
+  logger = logging.getLogger()
+  logger.setLevel(logging.INFO)
+  formatter = logging.Formatter("[%(levelname)s] %(message)s")
+  consoleLog = logging.StreamHandler(sys.stdout)
+  consoleLog.setFormatter(formatter)
+  logger.addHandler(consoleLog)
   main()

+ 5 - 6
ambari-server/src/main/python/ambari-server-state/Configurator.py

@@ -1,5 +1,3 @@
-from xml.dom.minicompat import NodeList
-from pip.backwardcompat import raw_input
 from Entities import *
 from os.path import exists
 import xml.dom.minidom
@@ -117,8 +115,7 @@ class Configurator:
     fsDefaultName = coreSite[0].getElementsByTagName('fs-default-name')[0].childNodes[0].data
     coreSiteResultStructure.fsDefaultName = fsDefaultName
 
-    hadoopSecurityAuthentication = coreSite[0].getElementsByTagName('hadoop-security-authentication')[0].childNodes[
-                                   0].data
+    hadoopSecurityAuthentication = coreSite[0].getElementsByTagName('hadoop-security-authentication')[0].childNodes[0].data
     coreSiteResultStructure.hadoopSecurityAuthentication = hadoopSecurityAuthentication
 
     #########################Get configuration.coreSite#########################
@@ -137,7 +134,7 @@ class Configurator:
     "Type of how to get paths to configuration files"
     "Configuration types are base on Configuration.CONFIG_INIT_TYPE tuple"
     return int(raw_input("\tInput configuration type:\n" +
-                         "0)Current path contains all required configurationfiles files.\n" +
+                         "0)Current path contains all required configuration files.\n" +
                          "1)Enter path for each conf file manually.\n" +
                          "Choose:"
     )
@@ -164,8 +161,9 @@ class Configurator:
     for service in self.servicesPath.keys():
       path = self.servicesPath[service]
       isExit = exists(path)
+      errorMessage = "File " + path + " doesn't exist! ("+ service+ " service)"
       if not isExit:
-        raise  IOError("File " + path + " doesn't exist!")
+        raise  IOError(errorMessage)
 #      else:
 #        print("File " + path + " exist!")
 
@@ -175,6 +173,7 @@ class Configurator:
     resultFile.write( str(self.configurations) )
     resultFile.flush()
     resultFile.close()
+    print("\t\t Result configuration file( "+self.RESULT_FILE_PATH+") was generate successfully.")
 
 
 

+ 12 - 13
ambari-server/src/main/python/ambari-server-state/Entities.py

@@ -39,7 +39,6 @@ class Configurations(ConvertToXml):
   hosts = ""  #Hosts entity
 
   def __init__(self):
-    super().__init__()
     self.stack = Stack()
     self.hosts = Hosts()
     self.configuration = ConfigurationEntity()
@@ -93,19 +92,19 @@ class Service(ConvertToXml):
   user = ""
   enabled = ""
 
-  def __init__(self, name, ver, comment, user, enabled):
-    self.name = name
-    self.version = ver
-    self.comment = comment
-    self.user = user
-    self.enabled = enabled
-
-  def __init__(self):
-    super().__init__()
+#  def __init__(self, name, ver, comment, user, enabled):
+#    super(self)
+#
+#  def __init__(self, name, ver, comment, user, enabled):
+#    self.name = name
+#    self.version = ver
+#    self.comment = comment
+#    self.user = user
+#    self.enabled = enabled
 
   def __str__(self):
     result = "<service>"
-    result += super().attributesToXml()
+    result += self.attributesToXml()
     result += "</service>"
     return result
 
@@ -191,7 +190,7 @@ class HadoopEnv(ConvertToXml):
 
   def __str__(self):
     result = "<hadoop-env>"
-    result += super().attributesToXml()
+    result += self.attributesToXml()
     result += "</hadoop-env>"
     return result
 
@@ -203,7 +202,7 @@ class CoreSite(ConvertToXml):
 
   def __str__(self):
     result = "<core-site>"
-    result += super().attributesToXml()
+    result += self.attributesToXml()
     result += "</core-site>"
     return result
 

+ 21 - 14
ambari-server/src/main/python/ambari-server.py

@@ -18,7 +18,7 @@ See the License for the specific language governing permissions and
 limitations under the License.
 '''
 
-import argparse
+import optparse
 import sys
 import os
 import signal
@@ -59,6 +59,8 @@ def setup_db(args):
   file = args.init_script_file
   command = SETUP_DB_CMD.format(dbname, username, file)
   retcode, outdata, errdata = run_os_command(command)
+  if not retcode == 0:
+    print errdata
   return retcode
 
 def check_se_down():
@@ -154,25 +156,30 @@ def stop(args):
   
   
 def main():
-  parser = argparse.ArgumentParser()
-  parser.add_argument("action", help="action to perform with ambari server")
-  parser.add_argument('-d', '--postgredbname', default='postgres',
+  parser = optparse.OptionParser(usage="usage: %prog [options] action",)
+  parser.add_option('-d', '--postgredbname', default='postgres',
                       help="Database name in postgresql")
-  parser.add_argument('-u', '--postgreuser', default='postgres',
+  parser.add_option('-u', '--postgreuser', default='postgres',
                       help="User in postgresql to run init scripts")
-  parser.add_argument('-f', '--init-script-file', default='setup_db.sql',
+  parser.add_option('-f', '--init-script-file', default='setup_db.sql',
                       help="File with setup script")
-  args = parser.parse_args()
-  action = args.action
+
+  (options, args) = parser.parse_args()
+  
+  if not len(args) == 1:
+    print parser.print_help()
+    parser.error("Invalid number of arguments")
+	
+  action = args[0]
+
   if action == SETUP_ACTION:
-    setup(args)
+    setup(options)
   elif action == START_ACTION:
-    start(args)
+    start(options)
   elif action == STOP_ACTION:
-    stop(args)
+    stop(options)
   else:
-    print "Incorrect action"
-    sys.exit(1)
+    parser.error("Invalid action")
 
 if __name__ == "__main__":
-  main()
+  main()