def _validate(self, criteria, toValidate): if None == criteria: PresentValidator._LOGGER.debug("None criteria, so returning True") return True elif None == toValidate: PresentValidator._LOGGER.debug("None toValidate, so returning False") return False elif not 'name' in criteria: PresentValidator._LOGGER.debug("'name' not in criteria, so returning False") return False elif None == DictUtils.defaultIfNone(toValidate, None, criteria['name']): PresentValidator._LOGGER.debug("criteria['name']: " + criteria['name'] + " not in toValidate, so returning False") return False elif not 'expected' in criteria: PresentValidator._LOGGER.debug("no expected found in criteria, so returning True") return True else: toValidateVal = DictUtils.defaultIfNone(toValidate, None, criteria['name']) if list == type(criteria['expected']): PresentValidator._LOGGER.debug("will match nested criteria later") return True elif str == type(criteria['expected']): PresentValidator._LOGGER.debug("matching string") return criteria['expected'] == toValidateVal elif type(criteria['expected']) != type(toValidateVal): PresentValidator._LOGGER.debug("type mismatch criteria[expected]:" + str(type(criteria['expected'])) + ", toValidate['" + criteria['name'] + "']: " + str(type(toValidateVal)) + ", so returning False") return False else: PresentValidator._LOGGER.debug("oops! validation failed") return False
def validate(criteria, toValidate): ValidatorFactory._LOGGER.debug("criteria: " + str(criteria)) ValidatorFactory._LOGGER.debug("toValidate: " + str(toValidate)) if None == criteria: ValidatorFactory._LOGGER.debug("None criteria, so returning True") return True if not 'check' in criteria: ValidatorFactory._LOGGER.debug("'check' not in criteria, so returning True, criteria: " + str(criteria)) return True validator = ValidatorFactory.getValidator(criteria['check']) if None == validator: ValidatorFactory._LOGGER.error("None validator found for, so returning False, check: " + criteria['check']) return False isValid = validator.validate(criteria, toValidate) ValidatorFactory._LOGGER.debug("isValid: " + str(isValid)) if not isValid: return False if isinstance(validator, PresentValidator) and 'expected' in criteria and list == type(criteria['expected']): ValidatorFactory._LOGGER.debug("will match nested criteria now ...") for nextCriteria in criteria['expected']: ValidatorFactory._LOGGER.debug("nextCriteria:" + str(nextCriteria)) if not 'check' in nextCriteria: continue if not 'name' in nextCriteria: ValidatorFactory._LOGGER.error("no name property found for check: " + str(nextCriteria)) return False nextToValidate = DictUtils.defaultIfNone(toValidate, None, criteria['name']) ValidatorFactory._LOGGER.debug("nextToValidate:" + str(nextToValidate)) isCheckValid = ValidatorFactory.validate(nextCriteria, nextToValidate) if not isCheckValid: return False return True
def run(self): Tester.__LOGGER.info("in run") if not 'steps' in self.__config: Tester.__LOGGER.info("no test steps to execute") return default = DictUtils.defaultIfNone(self.__config, None, 'default') control = {'loop':{'running': False, 'count': 0, 'steps': []}, 'session':{'running': False, 'steps': {}}, 'result':{'total':{'count':0, 'time':0}, 'passed':{'count':0, 'time':0}, 'failed':{'count':0, 'time':0}, 'steps':{} } } for step in self.__config['steps']: if False == self.__isValidStep(step): continue executor = ExecutorFactory.getExecutor(step['construct']) if None == executor: Tester.__LOGGER.error("no executor found for construct: " + step['construct']) continue executor.execute(default, step, control) if isinstance(executor, EndLoopExecutor): while control['loop']['running']: for tStep in control['loop']['steps']: tStep['executor'].execute(default, tStep['step'], control) executor.execute(default, step, control) Tester.__LOGGER.info("================================") Tester.__LOGGER.info("[SUMMARY JSON]") Tester.__LOGGER.info(str(control['result'])) Tester.__LOGGER.info("================================") Tester.__LOGGER.info("================================") Tester.__LOGGER.info("[SUMMARY]") Tester.__LOGGER.info(self.__formatResultSeparator()) Tester.__LOGGER.info(self.__formatResultHead1()) Tester.__LOGGER.info(self.__formatResultSeparator()) Tester.__LOGGER.info(self.__formatResultHead2()) Tester.__LOGGER.info(self.__formatResultSeparator()) for step in self.__config['steps']: if not 'sid' in step: continue sid = step['sid'] sidData = control['result']['steps'][sid] Tester.__LOGGER.info(self.__formatResultStr(sid, sidData)) Tester.__LOGGER.info(self.__formatResultSeparator()) Tester.__LOGGER.info(self.__formatResultStr('OVERALL', control['result'])) Tester.__LOGGER.info(self.__formatResultSeparator()) Tester.__LOGGER.info("================================")
def _validate(self, criteria, toValidate): if None == criteria: ExactValidator._LOGGER.debug("None criteria, so returning True") return True elif None == toValidate: ExactValidator._LOGGER.debug("None toValidate, so returning False") return False elif not 'name' in criteria: ExactValidator._LOGGER.debug("'name' not in criteria, so returning False") return False elif None == DictUtils.defaultIfNone(toValidate, None, criteria['name']): ExactValidator._LOGGER.debug("criteria['name']: " + criteria['name'] + " not in toValidate, so returning False") return False elif not 'expected' in criteria: ExactValidator._LOGGER.debug("no expected found in criteria, so returning True") return True else: toValidateVal = DictUtils.defaultIfNone(toValidate, None, criteria['name']) if type(criteria['expected']) != type(toValidateVal): ExactValidator._LOGGER.debug("type mismatch, so returning True - type(criteria['expected']): " + str(type(criteria['expected'])) + ", type(toValidate['" + criteria['name'] + "']): " + str(type(toValidateVal))) return False else: ExactValidator._LOGGER.debug("matching string criteria['expected']: " + str(criteria['expected']) + ", toValidate['" + criteria['name'] + "']: " + str(toValidateVal)) return criteria['expected'] == toValidateVal
def _execute(self, default, step, control): if control['loop']['running'] and 0 == control['loop']['count']: control['loop']['steps'].append({'step':step, 'executor':self}) sid = DictUtils.defaultIfNone(step, None, 'sid') if None == sid: TestExecutor._LOGGER.error("missing id for step: " + str(step)) sys.exit(1) host = DictUtils.defaultIfNone(step, default, 'host') path = DictUtils.defaultIfNone(step, default, 'path') method = DictUtils.defaultIfNone(step, default, 'method') commonInputData = DictUtils.defaultIfNone(None, default, 'input') inputData = DictUtils.defaultIfNone(step, default, 'input') if None != path: if path.startswith('/'): url = host + path else: url = host + '/' + path else: url = host url = self.__detemplatizeStr(url, control) TestExecutor._LOGGER.debug("url: " + url) if None == inputData: inputData = commonInputData elif None != commonInputData: inputData.update(commonInputData) if None != inputData: inputData = self.__detemplatize(inputData, control, boolToStr=True) # data = DictUtils.recursiveUrlencode(inputData) else: inputData = dict() TestExecutor._LOGGER.debug("request inputData: " + str(inputData)) startTime = time.time() try: if 'POST' == method: #res = urllib2.urlopen(url, data) files = self.__extractFiles(inputData) data, headers = DictUtils.encode_multipart(inputData, files) if None == files or 0 == len(files): TestExecutor._LOGGER.debug("request data: " + str(data)) else: TestExecutor._LOGGER.debug("request data: SOME POST DATA with files (won't log)") req = urllib2.Request(url, data=data, headers=headers) res = urllib2.urlopen(req) else: data = DictUtils.recursiveUrlencode(inputData) TestExecutor._LOGGER.debug("request data: " + data) url += "?" + data res = urllib2.urlopen(url) except IOError, e: TestExecutor._LOGGER.debug("caught exception e:" + str(e)) isSuccess = False if hasattr(e, 'code'): statusCode = e.code else: statusCode = 500 if hasattr(e, 'reason'): response = e.reason else: response = ""
TestExecutor._LOGGER.debug("caught exception e:" + str(e)) responseDict = None if control['session']['running']: control['session']['steps'].update({sid: {'IN':inputData, 'SC':statusCode, 'OUT':responseDict}}) TestExecutor._LOGGER.debug("updated session: " + str(control['session'])) if not isSuccess: self.__recordHit(control, sid, timeTaken, False) return if not 'output' in step or None == step['output']: self.__recordHit(control, sid, timeTaken, isSuccess) return statusCodeCheck = DictUtils.defaultIfNone(step, default, ['output', 'statusCode']) if None != statusCodeCheck and int(statusCodeCheck) != int(statusCode): TestExecutor._LOGGER.error("FAILED statusCodeCheck: expected=" + str(statusCodeCheck) + ", found=" + str(statusCode)) if not 'params' in step['output'] or None == step['output']['params']: self.__recordHit(control, sid, timeTaken, isSuccess) return isValid = True for param in step['output']['params']: param = self.__detemplatize(param, control) isCheckValid = ValidatorFactory.validate(param, responseDict) if not isCheckValid: isValid = False break