Guest User

Untitled

a guest
May 3rd, 2019
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.41 KB | None | 0 0
  1. #-------------------------------------------------------------------------------
  2. # Name: hazard
  3. # Purpose:
  4. #
  5. # Author: 40011
  6. #
  7. # Created: 04/30/2019
  8. # Copyright: (c) 40011 2019
  9. # Licence: <your licence>
  10. #-------------------------------------------------------------------------------
  11. import os
  12. import time
  13. import shutil
  14. import glob
  15.  
  16. import logging as Star_logger
  17. import subprocess
  18. import re
  19. import pwd
  20.  
  21.  
  22.  
  23. if __name__ == "__main__":
  24. import sys
  25. sys.path.append("/home/qa/sandisk/executable")
  26.  
  27. from Star_FW.StarUtil.Core import Core
  28.  
  29. class hazard(Core):
  30.  
  31. def __init__(self, dictJson):
  32. Core.__init__(self, globals())
  33. #pdb.set_trace()
  34. Star_logger.info("initializing variables")
  35. self.currentDir = os.getcwd()
  36. self.setJsonDict(dictJson)
  37. self.timeInMin = str(self.getValue_userDefine("timeInMinutes"))
  38. self.timeInSec = str(int(self.timeInMin) * 60)
  39. self.loopIntervalTime = str(self.getValue_userDefine("loopTimeInMinutes"))
  40. self.appDir = str(self.getValue_userDefine("applicationDir"))
  41. self.testInstanceId = str(self.dictJson[0][0]['testinstanceid'])
  42. self.testLogsDir = os.path.join(self.appDir, "logs")
  43. self.networkTestResultDir = str(self.dictJson[0][0]['networkresultdir'])
  44. self.machinePassword = str(self.getValue_userDefine("machinePassword"))
  45. self.machineUser = str(self.getValue_userDefine("machineUser"))
  46. self.mountDir = "/mnt/datastore"
  47. self.networkMountFullPath = ""
  48. self.networkWinPathToMount = ""
  49.  
  50.  
  51. def mountDatastore(self):
  52. re1='(.)' # Any Single Character 1
  53. re2='(.)' # Any Single Character 2
  54. re3='((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?![\\d])' # IPv4 IP Address 1
  55. re4='(.)' # Any Single Character 3
  56. re5='((?:[a-z][a-z]*[0-9]+[a-z0-9]*))' # Alphanum 1
  57. rg = re.compile(re1+re2+re3+re4+re5,re.IGNORECASE|re.DOTALL)
  58. match = rg.search(self.networkTestResultDir)
  59. Star_logger.info("extracting datastore path from network path")
  60. self.networkWinPathToMount = match.group(0)
  61. Star_logger.info("Path to mount is "+self.networkWinPathToMount)
  62. mountAppendPath = self.networkTestResultDir.replace(self.networkWinPathToMount,'')
  63. mountAppendPath = mountAppendPath.replace('\\', '/')
  64. self.networkMountFullPath = self.mountDir + mountAppendPath
  65. Star_logger.info("Linux based mount path is " + self.networkMountFullPath)
  66.  
  67. self.networkWinPathToMount = self.networkWinPathToMount.replace('\\','/')
  68.  
  69. uid = str(pwd.getpwnam(self.machineUser).pw_uid)
  70. gid = str(pwd.getpwnam(self.machineUser).pw_gid)
  71.  
  72. if not(os.path.exists(self.mountDir)):
  73. os.system('echo %s|sudo -S %s' % (self.machinePassword, "mkdir " + self.mountDir))
  74.  
  75. if (os.path.ismount(self.mountDir)):
  76. cmdUnmount = "umount -l " + self.mountDir
  77. os.system('echo %s|sudo -S %s' % (self.machinePassword, cmdUnmount))
  78.  
  79.  
  80. cmdMount = "mount -t cifs -o soft " + self.networkWinPathToMount + " " + self.mountDir + " -o username=pssd,password=Pw4p1x12,uid="+uid+",gid="+gid
  81. Star_logger.info("mount command is " + cmdMount)
  82. os.system('echo %s|sudo -S %s' % (self.machinePassword, cmdMount))
  83. Star_logger.info("datastore path is mounted")
  84.  
  85.  
  86. def copyLogs(self):
  87. if (os.path.exists(self.testLogsDir)):
  88. shutil.copytree(self.testLogsDir, self.networkMountFullPath)
  89.  
  90.  
  91. def checkFileOutput(self):
  92. if os.path.exists("/tmp/errorFilename.txt"):
  93. os.system('echo %s|sudo -S %s' % (self.machinePassword, r"rm /tmp/errorFilename.txt"))
  94. ## os.remove("/tmp/errorFilename.txt")
  95. if not os.path.exists(self.testLogsDir):
  96. Star_logger.error("Failed, test not launched properly or logs not created")
  97. assert False, "Failed, test not launched properly or logs not created"
  98. os.chdir(self.testLogsDir)
  99. fileNameArray = glob.glob('mgerror*')
  100. errorFileName = fileNameArray[0]
  101. cmd = "cp " + errorFileName + " /tmp/errorFilename.txt"
  102. os.system('echo %s|sudo -S %s' % (self.machinePassword, cmd))
  103. ## os.system(cmd)
  104. cmd = "dos2unix " + "/tmp/errorFilename.txt"
  105. os.system('echo %s|sudo -S %s' % (self.machinePassword, cmd))
  106. ## os.system(cmd)
  107. #if 'Error Log End Time' in open(errorFileName).read():
  108. if 'Error Log End Time' in open("/tmp/errorFilename.txt").read():
  109. os.chdir(self.currentDir)
  110. return True
  111. else:
  112. os.chdir(self.currentDir)
  113. return False
  114.  
  115.  
  116. def ToolExecution(self):
  117. Star_logger.info("starting network mount")
  118. self.mountDatastore()
  119.  
  120. #remove the log folders of previous runs starting with name 'HAZARD'
  121. if os.path.exists(self.appDir):
  122. Star_logger.info("remove test logs if already existing")
  123. os.system('echo %s|sudo -S %s' % (self.machinePassword, "rm -fR "+self.appDir+"/HAZARD*"))
  124.  
  125. cmd_makeExecutable = "chmod +x -R " + self.appDir
  126. os.system(cmd_makeExecutable)
  127.  
  128.  
  129. cmd_runHazard = self.appDir + "/hazard -Cidsys -Csysstalled -HsystemCrits=50 -ocat3=2 -l16 -t"+self.testTimeInHrs+" -ossd="+self.numOfSSDs+" -d HAZARD_"+self.modelName+"_"+self.numOfSSDs+"SSD_"+self.monthDate+" "+self.machineIP
  130. Star_logger.info("command to launch hazard tool is " + cmd_runHazard)
  131. pidApp = os.system('echo %s|sudo -S %s' % (self.machinePassword, cmd_runHazard))
  132. Star_logger.info("Hazard Test started")
  133.  
  134. Star_logger.info("sleeping for 5 seconds")
  135. time.sleep(5)
  136.  
  137. timeout = time.time() + int(self.timeInSec)
  138. loopingTime = int(self.loopIntervalTime) * 60
  139. Star_logger.info("loop monitoring time in seconds is "+str(loopingTime))
  140. #loopingTime = 30
  141. if (loopingTime >= int(self.timeInSec)):
  142. Star_logger.error("loop interval time cannot be greater than total run time")
  143. assert False, "Loop interval time should be less than total run time"
  144.  
  145. time.sleep(5)
  146. Star_logger.info("hazard started")
  147. processPid = subprocess.check_output(["ps -eaf | grep -i 'hazard' | grep -i 'Csysstalled' | awk '{print $2}'"], shell=True)
  148. processPid = processPid.strip()
  149. Star_logger.info("PID of the hazard tool is "+ processPid)
  150. #processPid = os.system("ps -eaf | grep -i 'nodrivers' | grep -i 'ld-2.12.so' | awk '{print $2}'")
  151.  
  152. if (processPid == None) or (processPid=="") or (processPid == 0):
  153. Star_logger.error("Hazard PID not found, tool not running")
  154. assert False, "Hazard app not started from command line"
  155.  
  156. Star_logger.info("monitoring starts here")
  157. poll = ""
  158. while True:
  159. #apply the logic for monitoring here-------------------------------------
  160. try:
  161. Star_logger.info("checking for process pid")
  162. poll = (subprocess.check_output(["ps -eaf | grep -i 'hazard' | grep -i 'Csysstalled' | awk '{print $2}'"], shell=True)).strip()
  163. except Exception as e:
  164. poll = ""
  165. if (poll == ""):
  166. Star_logger.info("check output error file if no pid found")
  167. if(self.checkFileOutput() == False):
  168. Star_logger.error("failed as process closed before proper termination")
  169. assert False, "Process closed before proper termination"
  170. if time.time() > timeout:
  171. Star_logger.info("coming out of monitoring loop when time is exceeded")
  172. break
  173.  
  174. time.sleep(loopingTime)
  175.  
  176. time.sleep(60)
  177.  
  178. Star_logger.info("finally checking for output logs")
  179. if(self.checkFileOutput() == False):
  180. Star_logger.error("failed logs not proper after test completion")
  181. assert False, "logs not generated properly after completion"
  182. else:
  183. if os.path.exists("/tmp/errorFilename.txt"):
  184. os.system('echo %s|sudo -S %s' % (self.machinePassword, r"rm /tmp/errorFilename.txt"))
  185. ## os.remove("/tmp/errorFilename.txt")
  186. os.chdir(self.testLogsDir)
  187. fileNameArray = glob.glob('mgerror*')
  188. errorFileName = fileNameArray[0]
  189. cmd = "cp " + errorFileName + " /tmp/errorFilename.txt"
  190. os.system('echo %s|sudo -S %s' % (self.machinePassword, cmd))
  191. ## os.system(cmd)
  192. cmd = "dos2unix " + "/tmp/errorFilename.txt"
  193. os.system('echo %s|sudo -S %s' % (self.machinePassword, cmd))
  194. ## os.system(cmd)
  195. if 'Total Errors = 0' in open("/tmp/errorFilename.txt").read():
  196. Star_logger.info("Test passed")
  197. else:
  198. Star_logger.error("failed because of errors in log file")
  199. assert False, "errors encountered in test log file"
  200.  
  201. Star_logger.info("Copying logs to datastore")
  202. self.copyLogs()
  203. Star_logger.info("Unmounting datastore path")
  204. os.system('echo %s|sudo -S %s' % (self.machinePassword, "umount " + self.mountDir)) #unmount the datastore at end of test
  205.  
  206.  
  207. def run(self):
  208. self.ToolExecution()
  209. return 0, "Pass"
Add Comment
Please, Sign In to add comment