def __init__(self, projectConfig): ''' Constructor ''' try: self.serverAddress = projectConfig.server_address except: raise Exception('Please provide valid server address') if projectConfig.HTTPS: self.protocolHTTPS = True try: self.http_connection = httplib.HTTPSConnection( self.serverAddress) except httplib.BadStatusLine as e: print str(e) except httplib.HTTPException: logs.error("'Could not open connection with the server!'") raise else: self.protocolHTTPS = False try: self.http_connection = httplib.HTTPConnection( self.serverAddress) except httplib.BadStatusLine as e: print str(e) except httplib.HTTPException: logs.error("Could not open connection with the server!\n") raise
def validateSchema(self, report, schemaFileName ): if schemaFileName=="Invalid": logs.error("Invalid File Name") return if 'xsd' in schemaFileName and not 'stix' in schemaFileName: result, message = XMLSchemaValidator(report, schemaFileName).validate() if result: return True, message else: return False, message elif 'schema' in schemaFileName: result, message = JSONSchemaValidator(report, schemaFileName).validate() if result: return True, message else: return False, message elif 'stix' in schemaFileName: result, message = self.stixValidator(report) if result: return True, message else: return False, message elif 'csv' in schemaFileName: result = self.csvValidator(report) if result: return True, "" else: return False, "csv output does not have required headers" else: logs.error("Schema is not known type") return False
def TestLinkUpdate(self, testCaseId, testCaseStatus, platformID=None, reason=None): try: if testCaseStatus == "PASS": testCaseStatus = 'p' else: testCaseStatus = 'f' ''' We need to mark every test case as Automated, so this need to be exectuted for first few cycles later on we can remove this ''' self.tls.setTestCaseExecutionType(testCaseId, 1, self.projectID, 2) if reason: Notes = str(reason) else: Notes = "Automated Results" tcinfo = self.tls.getTestCase(None, testcaseexternalid=testCaseId) self.tls.reportTCResult(testcaseid=tcinfo[0]['testcase_id'], testplanid=self.testPlanID, buildname=Globals.BUILDNO, status=testCaseStatus, notes=Notes) except Exception as e: logs.error( "Perhaps test case Id not found in test Link, Exception is :{}" .format(e))
def getAllTestLinesFromFileWithID(self, fileName): '''This function removes all the comments, lines starting from format_supported, endpoint and other lines except tests For Advance search having matchedon in test line, it also returns the dictionary which contains {<testline>:<matched on>} ''' self.getFileHandler(fileName) lines = self.file.readlines() basicSearch=True tests=[] testline_matchedon_dict = {}#{'dummy':'dummy'} for line in lines: line = str(line).rstrip('\n').rstrip('\t') if not str(line).startswith("format") and len(line)> 0 and not str(line).startswith("#") and not str(line).startswith("endpoint"): if not 'matchedon' in str(line).lower(): basicSearch=True tests+=[line] else: basicSearch=False ## Advance search test_matchedon = str(line).split(";",1) try: key=str(test_matchedon[0]).rstrip('\n').rstrip('\t').strip() value = str(test_matchedon[1]).rstrip('\n').rstrip('\t').strip() testline_matchedon_dict[key]= value except Exception as e: logs.error("Error in forming dictionary ", e) if basicSearch: return tests else: return testline_matchedon_dict
def getFileHandler(self, fileName): import os script_dir = os.path.dirname(__file__) try: #self.file = open("../input/config/"+ fileName,"r") rel_path = "/../input/config/"+fileName self.file = open(script_dir+ rel_path,"r") except Exception: logs.error("File "+ fileName +" Not Found")
def createBuild(self): ''' Create a Build in testLink which is passed in command line ''' try: self.tls.createBuild( self.testPlanID, Globals.BUILDNO, str(datetime.datetime.now().strftime("%I:%M%p %d %B %Y"))) except Exception: logs.error( "Build already exist under test Plan {} and build No {}". format(self.testPlanID, Globals.BUILDNO))
def validate(self): self.__createSchemaTree() #extraInfo="" try: #with open(self.__report, 'r') as f: etree.fromstring(self.__report, self.__xml_parser) return True, "" except Exception as e: logs.error(str(e)) logs.debug("Schema File used is \"{}\"".format(self.__schema_file)) return False, str(e)
def convertToEpoch(self, date_time=None, pattern="%B %d, %Y %H:%M:%S %p"): """ Pattern default : "%Y-%m-%d %H:%M:%S" """ if date_time is None: return int(time.time()) else: if isinstance(date_time, str) or isinstance(date_time, unicode): epoch = int(calendar.timegm(time.strptime(date_time, pattern))) else: logs.error( "Date time is not passed properly in pattern: {}".format( pattern)) return epoch
def compareJsonResponses(self, fromURL, fromTest): fromURL = self.cleanResponse(fromURL).split(",") fromTest = self.cleanResponse(fromTest).split(",") finalResult = [] self.notFound = [] for test in fromTest: if not test in fromURL and not "$" in test: finalResult.append(False) self.notFound.append(test) # return False if False in finalResult and self.notFound: logs.error( "These values {} from reference string does not match with actual response" .format(", ".join(self.notFound))) return False, "These elements mismatched %s " % ",".join( self.notFound) return True, ""
def request(self, method, url, headers={}, body=None): Globals.apiCount += 1 if not (url and method): logs.error('Invalid parameters are passed to http request.\n') raise Exception('Invalid parameters are passed to http request.') logs.info(str(Globals.apiCount) + ". API Hit Detail start\n") logs.info("URL = %s", url) logs.info("method = %s", method) logs.info("Headers = %s", json.dumps(headers)) logs.info("Post Body = %s", json.dumps(body)) logs.info(str(Globals.apiCount) + "API Hit Detail finished\n") try: self.http_connection.request(method, url, body, headers) self.http_response = self.http_connection.getresponse() self.response = self.http_response.read() except Exception as e: print str(e) raise return self.http_response.status
def getElementsSortedListByFieldName(self, response, field, URL='', sort=True, intValues=True, limit=None, lowerCase=False, removeEmptyValue=True, text='id=":', closetext='" version'): ''' returns sorted list of elements from response Check if response is xml, json or csv. cut the response till the field is found, then take the other part and take out its value, again repeat this process and so on, keep storing all values in list and return as per parameter passed. ''' if URL: self.__URL = URL else: self.__URL = response response = str(response).replace("'", "\"") csvFile = False if 'stix' in URL: pass elif self.is_xml(response) or '<?xml version=\"1.0\"' in response: text = "<" + field + ">" closetext = "</" + field + ">" elif self.is_json(self.__URL) or 'json' in self.__URL: text = "\"" + field + "\":" closetext = "," elif 'csv' in self.__URL or self.isResponseCsv(response): csvFile = True with (open("/tmp/temp.csv", 'w')) as f: f.write(response) else: logs.debug("Making default as json") text = "\"" + field + "\":" closetext = "," findList = [] if csvFile: i = 1 index = 0 with (open("/tmp/temp.csv", 'r')) as f: for line in f: line = self.removeExtraCommaFromCSV(line) line = self.cleanResponse(line) if i == 1: #get the index for required field in list split by "," array = line.split(",") try: index = list(array).index(field) i += 1 except Exception as e: logs.error( "Required field {} not found in line: {}, returning empty array" .format(field, line)) logs.error(str(e)) return [] else: #then for that index take out the value in next coming rows try: value = line.split(",")[index] if intValues: findList.append(int(value)) elif lowerCase: findList.append(str(value).lower().strip()) else: findList.append(value.strip()) except: pass f.close() else: string = response maximum = string.count(text) if limit < maximum and not limit == None: maximum = limit for temp in range(0, maximum): try: tup = str(string).partition(text) string = tup[2] tup2 = string.partition(closetext) if intValues: findList.append( int(str(tup2[0]).strip().replace("\"", ""))) elif lowerCase: findList.append( str(tup2[0]).replace("{", '').replace( '[', '').replace(']', '').replace('}', '').replace( "\"", '').strip().lower()) else: findList.append( str(tup2[0]).replace("{", '').replace( '[', '').replace(']', '').replace('}', '').replace( "\"", '').strip()) except: pass if removeEmptyValue: new = [] # To remove empty values in a list for li in findList: if li: new.append(li) findList = new if sort: findList = sorted(findList) return findList
def getURLResponse(self, url, requestType='GET', headers=None, body='', verifyCert=False, returnTimeTaken=False, fileObjectDict=None, responseIsFile=False, responseHeaders=False, **kwargs): ''' give full URL to this like "http://google.com" ## kwargs arguments : 'updateHeaders' to allow update headers with Content-Type = application/json and 'certPath' which accepts certificate path from system. Returns variable responses, see argument list, first is response Code and other is response string and so on as per arguments given => If u dont pass headers default will be {'Content-Type':'application/json', 'Accept' : 'application/json', } => If u dont want any headers to be passed or passed header should not be updated with default value of Content-Type and Accept, pass 'updateHeaders=False' from your calling place of getURLResponse() If any thing more need to be given, pass that in headers, or if need to be overridden pass that too so if Accept should be text/xml, pass that in headers, it will be overridden. ''' Globals.apiCount += 1 logs.debug("Total API Hit Till Now {}".format(Globals.apiCount)) finalHeaders = { 'Content-Type': 'application/json', 'Accept': 'application/json', } if headers: if kwargs and kwargs.get('updateHeaders'): finalHeaders.update(headers) headers = finalHeaders # print "headers", headers certPath = kwargs.get('certPath') if certPath: print "using certificate Path '" + certPath + "' from certPath argument" logs.debug("Headers used for this query is {}".format(headers)) try: logs.debug("Hitting {}".format(url)) logs.debug("Post Data for this : {}".format(body)) try: start = time.time() # self.flag = "request" # url = self.baseURL+url # url = "https://"+url if self.protocolHTTPS else "http://"+url if "post" in requestType.lower(): response = self.__request.post( url, data=body, headers=headers, verify=verifyCert, cert=certPath, files=fileObjectDict if fileObjectDict else None) elif "get" in requestType.lower(): response = self.__request.get(url, headers=headers, verify=verifyCert, cert=certPath) elif "put" in requestType.lower(): response = self.__request.put(url, data=body, headers=headers, verify=verifyCert, cert=certPath) elif "delete" in requestType.lower(): response = self.__request.delete(url, data=body, headers=headers, verify=verifyCert, cert=certPath) elif "patch" in requestType.lower(): response = self.__request.patch(url, data=body, headers=headers, verify=verifyCert, cert=certPath) else: return "Method Not supported in request module", "Method Not supported in request module", 000 timeTaken = time.time() - start timeTaken = float("%.2f" % timeTaken) print "Got response for {} took {} seconds".format( url, time.time() - start) if responseIsFile: filePath = os.path.join(os.getcwd(), "temp_" + str(int(time.time()))) with open(filePath, "a") as f: f.write(response.content) if returnTimeTaken: return response.status_code, filePath, timeTaken else: return response.status_code, filePath if returnTimeTaken and not responseHeaders: return response.status_code, response.content, timeTaken elif returnTimeTaken and responseHeaders: return response.status_code, response.content, timeTaken, response.headers elif not returnTimeTaken and responseHeaders: return response.status_code, response.content, response.headers else: return response.status_code, response.content #http://stackoverflow.com/questions/8734617/python-django-badstatusline-error except BadStatusLine as e: logs.debug("Query {} took {} seconds".format( url, time.time() - start)) self.__init__() logs.error("BAD STATUS LINE EXCEPTION OCCURRED" + str(e)) logs.error(traceback.format_exc()) if returnTimeTaken: return "", "Bad Status Line Exception" + traceback.format_exc( ), "NotCalculated" else: return "", "Bad Status Line Exception" + traceback.format_exc( ) except Exception as e: logs.error(e) print(traceback.format_exc()) if returnTimeTaken: return "Exception Occurred", traceback.format_exc( ), "Not Calculated" else: return "Exception Occurred", traceback.format_exc()