Best Python code snippet using autotest_python
s3test
Source:s3test
1#!/usr/bin/python2#3# Copyright (C) 2011 Canonical4#5# This program is free software; you can redistribute it and/or6# modify it under the terms of the GNU General Public License7# as published by the Free Software Foundation; either version 28# of the License, or (at your option) any later version.9#10# This program is distributed in the hope that it will be useful,11# but WITHOUT ANY WARRANTY; without even the implied warranty of12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13# GNU General Public License for more details.14#15# You should have received a copy of the GNU General Public License16# along with this program; if not, write to the Free Software17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.18#19#20# TODO:21# Intelligent diagnosis from function trace log22#23import getopt, sys, os24import commands25import signal26import subprocess27import errno28import shutil29import select30reboot_func = "#"31flash_func = "#"32beep_func = "#"33my_pid = os.getpid()34auto_s3 = False35minimodem = 036baud = ""37pmdebug_path = "/usr/share/pmdebug-tools/"38def usage():39 print sys.argv[0] + " [-r func|--reboot=func] [-f func|--flash=func] [-b func |--beep=func] [-s|--suspend]"40try:41 opts, args = getopt.getopt(sys.argv[1:], "r:f:b:sm", [ "help", "reboot", "flash=", "beep=", "suspend", "minimodem", "minimodem-funcs", "minimodem-devs", "minimodem-tasks", "minimodem-tests", "minimodem-baud=" ])42except getopt.GetoptError, err:43 print str(err)44 usage()45 sys.exit(2)46#47# s3.stp sends us a SIGUSR1 to indicate it is ready to run48#49def sighandler(signum, frame):50 if signum == signal.SIGUSR1:51 if auto_s3:52 print "Suspending machine.."53 os.system("pm-suspend")54 else:55 print "Now suspend the machine."56 print "Please wake machine up manually."57if os.getuid() != 0:58 print "You need to run this script with root privileges."59 sys.exit(2)60 61for opt, optarg in opts:62 if opt in ("--help"):63 usage()64 sys.exit(2)65 elif opt in ("-r", "--reboot"):66 reboot_func = optarg67 elif opt in ("-f", "--flash"):68 flash_func = optarg69 elif opt in ("-b", "--beep"):70 beep_func = optarg71 elif opt in ("-s", "--suspend"):72 auto_s3 = True73 elif opt in ("-m", "--minimodem"):74 minimodem = 102475 elif opt in ("--minimodem-funcs"):76 minimodem = minimodem | 177 elif opt in ("--minimodem-devs"):78 minimodem = minimodem | 279 elif opt in ("--minimodem-tasks"):80 minimodem = minimodem | 481 elif opt in ("--minimodem-tests"):82 minimodem = minimodem | 883 elif opt in ("--minimodem-baud"):84 baud = optarg85 print "Baud: " + baud86 if not int(baud) in (110, 300, 600, 1200):87 print "Baud rates must be 110, 300, 600 or 1200"88 sys.exit(2)89 else:90 print "Unknown option " + opt91 usage()92shutil.copyfile("/proc/kallsyms", "kallsyms.log")93#94# Go build systemtap debug and run95#96print "Building systemtap debugger (this may take some time).."97print "If the machine hangs, reboot and run 'locatehang'."98if os.path.exists("s3.stp"):99 s3stp = "s3.stp"100else:101 s3stp = pmdebug_path + "s3.stp"102#103# Filter output from systemtap script to various logs104#105full_log = open('s3-systemtap.log', 'w')106trace_log = open('s3-trace.log', 'w')107test_log = open('s3-test.log', 'w')108device_log = open('s3-devices.log', 'w')109task_log = open('s3-tasks.log', 'w')110stderr_log = open('s3-error.log', 'w')111signal.signal(signal.SIGUSR1, sighandler)112cmd = [ "stap", "-g", "-DSTP_NO_OVERLOAD", s3stp, str(my_pid), reboot_func, flash_func, beep_func, str(minimodem), baud ]113proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)114errors = False115stderr_open = True116stdout_open = True117while stderr_open or stdout_open:118 try:119 readable, writeable, exceptional = select.select([proc.stderr.fileno(), proc.stdout.fileno()], [], []) 120 for i in readable:121 if i == proc.stderr.fileno():122 line = proc.stderr.readline()123 if line == "":124 stderr_open = False125 else:126 stderr_log.write(line)127 if line.find("error:") <> -1:128 errors = True129 if i == proc.stdout.fileno():130 line = proc.stdout.readline()131 if line == "":132 stdout_open = False133 134 if not line:135 break136 if line.find("PM-INFO:",0) == 0:137 print line,138 if line.find("PM-TEST:",0) == 0:139 print line,140 test_log.write(line)141 if line.find("PM-TRACE:",0) == 0:142 #143 # need to plug in a function trace144 # back end to figure out when things145 # fail and provide diagnosis146 #147 trace_log.write(line)148 if line.find("PM-DEV-DEBUG:",0) == 0:149 device_log.write(line)150 if line.find("PM-TASK-DEBUG:",0) == 0:151 task_log.write(line)152 full_log.write(line) 153 #154 # SIGUSR1 causes EAGAIN, so don't raise155 #156 except select.error, x: 157 if x[0] != errno.EINTR:158 raise159 except IOError, x:160 if x.errno != errno.EAGAIN:161 raise162proc.wait()163full_log.close()164trace_log.close()165test_log.close()166device_log.close()167task_log.close()168stderr_log.close()169if errors:170 print '\nError:'171 print ' SystemTap failed to execute. This could be because the kernel debug .ddeb'172 print ' has not been installed or the kernel functions being traced have '173 print ' changed in this kernel version. Check s3-error.log for more information.\n'174 print 'If you need to install the kernel .ddeb package do the following:\n'175 print ' echo "deb http://ddebs.ubuntu.com $(lsb_release -cs) main restricted universe multiverse | sudo tee -a /etc/apt/sources.list.d/ddebs.list'176 print ' sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 428D7C01'177 print ' sudo apt-get install linux-image-`uname -r`-dbgsym'178 sys.exit(3)179 180print "Note:"181print " SystemTap error log in s3-errors.log"182print " Full trace log in s3-systemtap.log"183print " Function trace log in s3-trace.log"184print " Device state log in s3-devices.log"185print " Task state log in s3-tasks.log"186print " Test results in s3-test.log"...
s4test
Source:s4test
1#!/usr/bin/python2#3# Copyright (C) 2011 Canonical4#5# This program is free software; you can redistribute it and/or6# modify it under the terms of the GNU General Public License7# as published by the Free Software Foundation; either version 28# of the License, or (at your option) any later version.9#10# This program is distributed in the hope that it will be useful,11# but WITHOUT ANY WARRANTY; without even the implied warranty of12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13# GNU General Public License for more details.14#15# You should have received a copy of the GNU General Public License16# along with this program; if not, write to the Free Software17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.18#19#20# TODO:21# Intelligent diagnosis from function trace log22#23import getopt, sys, os24import commands25import signal26import subprocess27import errno28import shutil29import select30reboot_func = "#"31flash_func = "#"32beep_func = "#"33my_pid = os.getpid()34auto_s4 = False35minimodem = 036baud = ""37def usage():38 print sys.argv[0] + " [-r|--reboot func] [-f|--flash func] [-b|--beep func] [-h|--hibernate]"39try:40 opts, args = getopt.getopt(sys.argv[1:], "r:f:b:h", [ "help", "reboot", "flash", "beep", "hibernate", "minimodem", "minimodem-funcs", "minimodem-devs", "minimodem-tasks", "minimodem-tests", "minimodem-baud=" ])41except getopt.GetoptError, err:42 print str(err)43 usage()44 sys.exit(2)45#46# s4.stp sends us a SIGUSR1 to indicate it is ready to run47#48def sighandler(signum, frame):49 if signum == signal.SIGUSR1:50 if auto_s4:51 print "Hibernating machine.."52 os.system("pm-hibernate")53 else:54 print "Now hibernate the machine."55 print "Please wake machine up manually."56if os.getuid() != 0:57 print "You need to run this script with root privileges."58 sys.exit(2)59for opt, optarg in opts:60 if opt in ("-r", "--reboot"):61 reboot_func = optarg62 elif opt in ("-f", "--flash"):63 flash_func = optarg64 elif opt in ("-b", "--beep"):65 beep_func = optarg66 elif opt in ("-h", "--hibernate"):67 auto_s4 = True68 elif opt in ("-m", "--minimodem"):69 minimodem = 102470 elif opt in ("--minimodem-funcs"):71 minimodem = minimodem | 172 elif opt in ("--minimodem-devs"):73 minimodem = minimodem | 274 elif opt in ("--minimodem-tasks"):75 minimodem = minimodem | 476 elif opt in ("--minimodem-tests"):77 minimodem = minimodem | 878 elif opt in ("--minimodem-baud"):79 baud = optarg80 if not int(baud) in (110, 300, 600, 1200):81 print "Baud rates must be 110, 300, 600 or 1200"82 sys.exit(2)83 elif opt in ("--help"):84 usage()85 sys.exit(2)86 else:87 print "Unknown option " + opt88 usage()89shutil.copyfile("/proc/kallsyms", "kallsyms.log")90#91# Go build systemtap debug and run92#93print "Building systemtap debugger (this may take some time).."94print "If the machine hangs, reboot and run 'locatehang'."95if os.path.exists("s4.stp"):96 s4stp = "s4.stp"97else:98 s4stp = pmdebug_path + "s4.stp"99signal.signal(signal.SIGUSR1, sighandler)100cmd = [ "stap", "-g", "-DSTP_NO_OVERLOAD", s4stp, str(my_pid), reboot_func, flash_func, beep_func, str(minimodem), baud ]101proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)102#103# Filter output from systemtap script to various logs104#105full_log = open('s4-systemtap.log', 'w')106trace_log = open('s4-trace.log', 'w')107test_log = open('s4-test.log', 'w')108device_log = open('s4-devices.log', 'w')109task_log = open('s4-tasks.log', 'w')110stderr_log = open('s4-error.log', 'w')111errors = False112stderr_open = True113stdout_open = True114while stderr_open or stdout_open:115 try:116 readable, writeable, exceptional = select.select([proc.stderr.fileno(), proc.stdout.fileno()], [], [])117 for i in readable:118 if i == proc.stderr.fileno():119 line = proc.stderr.readline()120 if line == "":121 stderr_open = False122 else:123 stderr_log.write(line)124 if line.find("error:") <> -1:125 errors = True126 127 if i == proc.stdout.fileno():128 line = proc.stdout.readline()129 if line == "":130 stdout_open = False131 if not line:132 break133 if line.find("PM-INFO:",0) == 0:134 print line,135 if line.find("PM-TEST:",0) == 0:136 print line,137 test_log.write(line)138 if line.find("PM-TRACE:",0) == 0:139 #140 # need to plug in a function trace141 # back end to figure out when things142 # fail and provide diagnosis143 #144 trace_log.write(line)145 if line.find("PM-DEV-DEBUG:",0) == 0:146 device_log.write(line)147 if line.find("PM-TASK-DEBUG:",0) == 0:148 task_log.write(line)149 full_log.write(line)150 #151 # SIGUSR1 causes exception, don't raise152 #153 except select.error, x:154 if x[0] != errno.EINTR:155 raise156 except IOError, x:157 if x.errno == errno.EAGAIN:158 raise159proc.wait()160full_log.close()161trace_log.close()162test_log.close()163device_log.close()164task_log.close()165stderr_log.close()166if errors:167 print '\nError:'168 print ' SystemTap failed to execute. This could be because the kernel debug .ddeb'169 print ' has not been installed or the kernel functions being traced have '170 print ' changed in this kernel version. Check s3-error.log for more information.\n'171 print 'If you need to install the kernel .ddeb package do the following:\n'172 print ' echo "deb http://ddebs.ubuntu.com $(lsb_release -cs) main restricted universe multiverse | sudo tee -a /etc/apt/sources.list.d/ddebs.list'173 print ' sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 428D7C01'174 print ' sudo apt-get install linux-image-`uname -r`-dbgsym'175 sys.exit(3)176print "Note:"177print " Full trace log in s4-systemtap.log"178print " Function trace log in s4-trace.log"179print " Device state log in s4-devices.log"180print " Task state log in s4-tasks.log"181print " Test results in s4-test.log"...
task_ctp.py
Source:task_ctp.py
...16 reboot_func = trading_reboot_rule17 vn_product_filter = [Product.FUTURES.value, Product.OPTION.value]18 child_task = {n: None for n in range(total_n)}19 while True:20 trading = reboot_func()21 # Start child process in trading period22 if trading:23 for n in range(total_n):24 if child_task[n] is None: # æ¤å¤å¤ææ¯å¦æåè¿ç¨å¨è¿è¡25 child_task[n] = multiprocessing.Process(26 target=run_task, args=(n, total_n,27 CtpGateway, 'CTP', load_json('connect_ctp_list.json'), reboot_func,28 True, vn_product_filter29 )30 )31 child_task[n].start()32 print(f"{datetime.datetime.now()}ã{n}/{total_n}ãåè¿ç¨å¯å¨")33 # éè®°å½æ¶é´åéåºåè¿ç¨34 if not trading:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!