How to use log_stacktrace method in autotest

Best Python code snippet using autotest_python

run.py

Source:run.py Github

copy

Full Screen

1import os2import sys3import json4import traceback5import base646from StringIO import StringIO7from __ALGO__ import __ALGO__8####9# Disable stdout, stderr10old_stdout = sys.stdout11old_stderr = sys.stderr12old_fd_stdout = os.dup(1)13old_fd_stderr = os.dup(2)14# Redirect file descriptors to handle child processes15os.dup2(os.open(os.devnull, os.O_RDWR), 1)16os.dup2(os.open(os.devnull, os.O_RDWR), 2)17captured_stdout = StringIO()18sys.stdout = captured_stdout19sys.stderr = captured_stdout20####21input_object = None22####23#Parse and validate Input24###25try:26 ###27 # Read input from stdin and parse as JSON28 ###29 inputString = sys.stdin.read()30 input_json = json.loads(inputString)31 #input_json: Request32 # input33 # body - []34 # name35 # filename - option36 # data37 # content_type - "void", "text", "json", "binary"38 # mime_type39 # modifiers40 # log_stdout41 # log_stacktrace42 ###43 #Validate input structure44 ####45 if 'input' not in input_json:46 raise Exception("did not find 'input'")47 if 'modifiers' not in input_json:48 raise Exception("did not find 'modifiers'")49 if 'log_stdout' not in input_json["modifiers"]:50 raise Exception("did not find 'modifiers.log_stdout'")51 if input_json["modifiers"]["log_stdout"] != True and input_json["modifiers"]["log_stdout"] != False:52 raise Exception("'modifiers.log_stdout' is not Boolean")53 if 'log_stacktrace' not in input_json["modifiers"]:54 raise Exception("did not find 'modifiers.log_stacktrace'")55 if input_json["modifiers"]["log_stacktrace"] != True and input_json["modifiers"]["log_stacktrace"] != False:56 raise Exception("'modifiers.log_stacktrace' is not Boolean")57 if 'body' not in input_json["input"]:58 raise Exception("did not find 'input.body'")59 for part in input_json["input"]["body"]:60 if 'name' not in part:61 raise Exception("did not find 'input.body[].name'")62 if 'data' not in part:63 raise Exception("did not find 'input.body[].data'")64 if 'content_type' not in part:65 raise Exception("did not find 'input.body[].content_type'")66 if (part["content_type"] != "void" and part["content_type"] != "text" and part["content_type"] != "json" and part["content_type"] != "binary"):67 raise Exception("'input.body[].content_type' did not parse as valid enum value")68 if 'mime_type' not in part:69 raise Exception("did not find 'input.body[].mime_type'")70 ###71 #Create input_object72 ###73 if len(input_json["input"]["body"]) == 1:74 body_part = input_json["input"]["body"][0]75 input_data = body_part["data"]76 if body_part["content_type"] == "void":77 input_object = None78 elif body_part["content_type"] == "text":79 input_object = input_data80 elif body_part["content_type"] == "json":81 input_object = json.loads(input_data)82 elif body_part["content_type"] == "binary":83 input_object = bytearray(base64.b64decode(input_data))84 else:85 input_object = body_part["data"]86 else:87 input_object = input_json["input"]88except Exception as e:89 ###90 # Protocol Exception91 ###92 # Enable stdout, stderr93 os.dup2(old_fd_stdout, 1)94 os.dup2(old_fd_stderr, 2)95 sys.stdout = old_stdout96 sys.stderr = old_stderr97 #class ProtocolErrorResponse:98 # error99 # type - "protocol_error"100 # message101 # metadata102 # stacktrace103 protocol_error_response = {"error":{"type":"protocol_error","message":str(e)},"metadata":{"stacktrace":traceback.format_exc()}}104 print(json.dumps(protocol_error_response))105 sys.exit(0)106try:107 ###108 # Call Algorithm109 ###110 output = __ALGO__.apply(input_object)111 ###112 # Create Algorithm Result113 ###114 result_json = None115 content_type = "void"116 if output is None:117 result_json = None118 content_type = "void"119 elif isinstance(output, basestring):120 result_json = output121 content_type = "text"122 elif isinstance(output, bytearray):123 result_json = base64.b64encode(output)124 content_type = "binary"125 else:126 result_json = json.dumps(output)127 content_type = "json"128 # Enable stdout, stderr129 os.dup2(old_fd_stdout, 1)130 os.dup2(old_fd_stderr, 2)131 sys.stdout = old_stdout132 sys.stderr = old_stderr133 #class AlgorithmResultResponse:134 # result135 # data136 # metadata137 # content_type138 # stdout - option139 if input_json["modifiers"]["log_stdout"] == True:140 algorithm_result_response = {"result":{"data":result_json},"metadata":{"content_type":content_type,"stdout":captured_stdout.getvalue()}}141 else:142 algorithm_result_response = {"result":{"data":result_json},"metadata":{"content_type":content_type}}143 print(json.dumps(algorithm_result_response))144 sys.exit(0)145except Exception as e:146 ###147 # Algorithm Exception148 ###149 # Enable stdout, stderr150 os.dup2(old_fd_stdout, 1)151 os.dup2(old_fd_stderr, 2)152 sys.stdout = old_stdout153 sys.stderr = old_stderr154 #class AlgorithmErrorResponse:155 # error156 # type - "algorithm_error"157 # message158 # metadata - option159 # stdout - option160 # stacktrace - option161 if input_json["modifiers"]["log_stdout"] == True and input_json["modifiers"]["log_stacktrace"] == True:162 algorithm_error_response = {"error":{"type":"algorithm_error","message":str(e)},"metadata":{"stdout":captured_stdout.getvalue(),"stacktrace":traceback.format_exc()}}163 elif input_json["modifiers"]["log_stdout"] == True:164 algorithm_error_response = {"error":{"type":"algorithm_error","message":str(e)},"metadata":{"stdout":captured_stdout.getvalue()}}165 elif input_json["modifiers"]["log_stacktrace"] == True:166 algorithm_error_response = {"error":{"type":"algorithm_error","message":str(e)},"metadata":{"stacktrace":traceback.format_exc()}}167 else:168 algorithm_error_response = {"error":{"type":"algorithm_error","message":str(e)}}169 print(json.dumps(algorithm_error_response))...

Full Screen

Full Screen

responses.py

Source:responses.py Github

copy

Full Screen

1from pkg.utils.context import get_current_context2from pkg.utils.errors import response_error3from sanic import response4from typing import Union5def response_ok(result: Union[dict, list, str],6 message: str = None):7 obj = {'result': result}8 if message:9 obj['message'] = message10 return response.json(obj)11def response_400(log_stacktrace: bool = True, log_error: bool = True):12 return response_error(400, f'Request data is invalid', 400, log_stacktrace=log_stacktrace, log_error=log_error)13def response_403(log_stacktrace: bool = True, log_error: bool = True):14 return response_error(403, f'Forbidden', 403, log_stacktrace=log_stacktrace, log_error=log_error)15def response_403_short():16 return response_403(log_stacktrace=False, log_error=False)17def response_404(log_stacktrace: bool = True, log_error: bool = True):18 ctx = get_current_context()19 return response_error(404, f'Requested URL {ctx.request.path} not found', 404,...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful