PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/pytest/avatar-test.py

https://github.com/heca/rextest2
Python | 167 lines | 106 code | 41 blank | 20 comment | 19 complexity | 830819bc46c40d13d4fd933560d50853 MD5 | raw file
  1. #!/usr/local/bin/python
  2. ##
  3. # TODO:
  4. #
  5. ##
  6. #import
  7. import os
  8. import time
  9. import shutil
  10. import subprocess
  11. import getpass
  12. import glob
  13. import autoreport
  14. from multiprocessing import Process
  15. from optparse import OptionParser
  16. import config
  17. #variables
  18. numberOfRuns = 2
  19. numberOfClients = 2
  20. testName = "avatar-test"
  21. #folder config
  22. scriptDir = os.path.abspath(os.getcwd())
  23. rexbinDir = config.rexbinDir
  24. #rexbinDir = os.path.abspath(os.path.join(scriptDir, '../../bin'))
  25. testDir = os.path.abspath(os.path.join(scriptDir, '..'))
  26. logsDir = os.path.abspath(os.path.join(scriptDir, 'logs/avatar-output'))
  27. wiresTempDir = os.path.abspath(os.path.join(scriptDir, 'wireshark_temp'))
  28. tsharkLocation = "c:/Program Files/Wireshark/"
  29. #test chiru or local server (default if no parameter is given)
  30. #js = scriptDir + "/" + "autoConnect.js"
  31. js = scriptDir + "/" + "autoConnectLocal.js"
  32. def main():
  33. operations(numberOfRuns)
  34. movePcapFiles()
  35. cleanup()
  36. autoreport.autoreport(testName)
  37. os._exit(1)
  38. def operations(numberOfRuns):
  39. global avatarLogs
  40. global timeStamp
  41. #create a temp directory for pcap files
  42. if not os.path.exists(wiresTempDir):
  43. os.makedirs(wiresTempDir)
  44. #create a directory for log files
  45. timeStamp = time.strftime("%Y-%m-%dT%H:%M:%S%Z", time.localtime())
  46. avatarLogs = logsDir + "/logs_" + timeStamp
  47. os.makedirs(avatarLogs)
  48. os.chdir(rexbinDir)
  49. for i in range(1,numberOfRuns+1):
  50. run_clients(numberOfClients, i)
  51. os.chdir(scriptDir)
  52. def run_clients(numberOfClients, i):
  53. for j in range(1,numberOfClients+1):
  54. #os.name options: 'posix', 'nt', 'os2', 'mac', 'ce' or 'riscos'
  55. if os.name == 'posix' or os.name == 'mac':
  56. #modify wireshark temp folder owner (required for tshark when capturing all devices)
  57. posixModTempfolder("root")
  58. p1 = Process(target=posixRunTshark, args=(i, j))
  59. p2 = Process(target=posixRunViewer, args=(i, j))
  60. #start tshark
  61. p1.start()
  62. print "writing network log to file captured" + str(i) + "." + str(j) + ".pcap"
  63. #start viewer
  64. p2.start()
  65. print "writing log to file naaliLog" + str(i) + "." + str(j) + ".log"
  66. running = True
  67. elif os.name == 'nt': #NOT TESTED
  68. p1 = Process(target=ntRunTshark, args=(i, j))
  69. p2 = Process(target=ntRunViewer, args=(i, j))
  70. #start tshark
  71. p1.start()
  72. print "writing network log to file captured" + str(i) + "." + str(j) + ".pcap"
  73. #start viewer
  74. p2.start()
  75. print "writing log to file naaliLog" + str(i) + "." + str(j) + ".log"
  76. running = True
  77. else:
  78. print "os not supported"
  79. #while-loop to check if viewer is running
  80. while running == True:
  81. if not p2.is_alive():
  82. running = False
  83. posixModTempfolder("user")
  84. p1.terminate()
  85. else:
  86. time.sleep(1)
  87. def posixModTempfolder(mode):
  88. if mode == "root":
  89. subprocess.call(['sudo','chown','root:root', wiresTempDir])
  90. elif mode == "user":
  91. subprocess.call(['sudo','chown','-R', getpass.getuser(), wiresTempDir])
  92. def posixRunTshark(i,j):
  93. subprocess.call(['sudo','tshark','-i','any','-f','port 2345','-w', wiresTempDir + '/captured' + str(i) + '.' + str(j) + '.pcap'])
  94. def posixRunViewer(i,j):
  95. x = "./Tundra --headless --run " + js + " 2>&1 | tee " + avatarLogs + "/naaliLog" + str(i) + "." + str(j) + ".log"
  96. subprocess.call(x, shell = True)
  97. def ntRunTshark(i,j):
  98. subprocess.call([tsharkLocation + 'tshark.exe','-i','any','-f','port 2345','-w', wiresTempDir + '/captured' + str(i) + '.' + str(j) + '.pcap'])
  99. def ntRunViewer(i,j):
  100. x = rexbinDir + "/viewer.exe --headless --run " + js + " 2>&1 | tee " + avatarLogs + "/naaliLog" + str(i) + "." + str(j) + ".log"
  101. subprocess.call(x, shell = True)
  102. def movePcapFiles():
  103. print "moving pcap files..."
  104. pcap = glob.glob(wiresTempDir + '/captured*.*.pcap')
  105. for i in range(0,len(pcap)):
  106. shutil.move(pcap[i], avatarLogs)
  107. print "pcap files moved to " + avatarLogs
  108. def cleanup():
  109. print "cleaning up..."
  110. if os.path.exists(wiresTempDir):
  111. os.removedirs(wiresTempDir)
  112. if __name__ == "__main__":
  113. parser = OptionParser()
  114. parser.add_option("-r", "--runs", type="int", dest="numberOfRuns")
  115. parser.add_option("-c", "--clients", type="int", dest="numberOfClients")
  116. parser.add_option("-j", "--js", dest="js")
  117. (options, args) = parser.parse_args()
  118. if options.numberOfRuns:
  119. numberOfRuns = options.numberOfRuns
  120. if options.numberOfClients:
  121. numberOfClients = options.numberOfClients
  122. if options.js == "local":
  123. js = scriptDir + "/autoConnectLocal.js"
  124. if options.js == "chiru":
  125. js = scriptDir + "/autoConnect.js"
  126. main()