def run_tests(self): test_cases = [] for test in self.tests: desc = test['desc'] name = test['name'] index = test['id'] test_case = TestCase(name, self.name) if '[.]' in desc: print('skipping test "{}"'.format(name)) test_case.add_skipped_info(message="Skipped test marked with [.]") else: test_output = StringIO() self.sp.logfile = test_output t_start = time.time() result = self.run_test(index) t_stop = time.time() self.sp.logfile = None test_case.elapsed_sec = t_stop - t_start debug_print('test output was:') debug_print(test_output.getvalue()) if result == BSTestRunner.SUCCESS: test_case.stdout = test_output.getvalue() print('test "{}" passed'.format(name)) else: print('test "{}" failed'.format(name)) test_case.add_failure_info('Test failed', output=test_output.getvalue()) test_output.close() test_cases += [test_case]; return TestSuite(self.name, test_cases)
def run_tests(self): test_cases = [] should_update_env = True for test in self.tests: desc = test['desc'] name = test['name'] index = test['id'] test_case = TestCase(name, self.name) if '[.]' in desc: print('skipping test "{}"'.format(name)) test_case.add_skipped_info( message="Skipped test marked with [.]") else: test_output = StringIO() self.sp.logfile = test_output print('running test "{}"'.format(name)) if should_update_env: res = self.update_env(self.env_vars) if res != BSTestRunner.SUCCESS: print('failed to set environment variables') break res = self.pretest() if res != BSTestRunner.SUCCESS: print('failed to run pretest init') break should_update_env = False if name in self.mocks: debug_print('setting up mocks') self.mocks[name]['request_env'] = self.request_env self.mocks[name]['setup']() extra_env = mock_decorators.get_all_envs(name) if extra_env is not None: self.update_env(extra_env) t_start = time.time() result = self.run_test(index) if name in self.mocks: debug_print('tearing down mocks') try: self.mocks[name]['teardown']() except AssertionError: debug_print('teardown assert failure') result = BSTestRunner.FAIL t_stop = time.time() self.sp.logfile = None test_case.elapsed_sec = t_stop - t_start debug_print('test output was:') debug_print(test_output.getvalue()) if result == BSTestRunner.SUCCESS: test_case.stdout = filter(lambda c: ord(c) < 128, test_output.getvalue()) print('test "{}" passed'.format(name)) else: print('test "{}" failed'.format(name)) test_case.add_failure_info('Test failed', output=test_output.getvalue()) should_update_env = True test_output.close() test_cases += [test_case] return TestSuite(self.name, test_cases)
def run_tests(self): test_cases = [] should_update_env = True for test in self.tests: desc = test['desc'] name = test['name'] index = test['id'] test_case = TestCase(name, self.name) if '[.]' in desc: print('skipping test "{}"'.format(name)) test_case.add_skipped_info(message="Skipped test marked with [.]") else: test_output = StringIO() self.sp.logfile = test_output print('running test "{}"'.format(name)) if should_update_env: res = self.update_env(self.env_vars) if res != BSTestRunner.SUCCESS: print('failed to set environment variables') break; should_update_env = False if name in self.mocks: debug_print('setting up mocks') self.mocks[name]['request_env'] = self.request_env self.mocks[name]['setup']() extra_env = mock_decorators.get_all_envs(name) if extra_env is not None: self.update_env(extra_env) t_start = time.time() result = self.run_test(index) if name in self.mocks: debug_print('tearing down mocks') try: self.mocks[name]['teardown']() except AssertionError: debug_print('teardown assert failure') result = BSTestRunner.FAIL t_stop = time.time() self.sp.logfile = None test_case.elapsed_sec = t_stop - t_start debug_print('test output was:') debug_print(test_output.getvalue()) if result == BSTestRunner.SUCCESS: test_case.stdout = test_output.getvalue() print('test "{}" passed'.format(name)) else: print('test "{}" failed'.format(name)) test_case.add_failure_info('Test failed', output=test_output.getvalue()) should_update_env = True test_output.close() test_cases += [test_case]; return TestSuite(self.name, test_cases)
def create_junit_results(data, output=None, **kwargs): """ Creates a Junit result, can write to a file if desired, or return xml string. (used by Jenkins) input either dict(dict(dict())) or dict(list(dict())) dict = {suite: {test: {stderr,stdout,time,class,err,fail,skip}}} list = {suite: [(test, {stderr,stdout,time,class,err,fail,skip})]} :param data: A dictionary with dict or list hierarchy :param output: A filename to write results to /path/to/file/*.junit.xml :return: Returns an XML string if no output, else nothing. """ log.debug('creating junit results: output={}'.format(output)) stdout_format = kwargs.pop('stdout_format', None) test_class = kwargs.pop('test_class', None) package = kwargs.pop('package', None) from junit_xml import TestSuite, TestCase test_suites = [] for suite, tests in data.items(): test_cases = [] for test, result in (tests if isinstance(tests, list) else tests.items()): tc = TestCase(test) stdout = result.get('stdout') if stdout_format is not None and callable(stdout_format): if hasattr(stdout_format, 'func_code') and 'kwargs' in stdout_format.func_code.co_varnames: stdout = stdout_format(stdout, suite_name=suite, test_name=test, **kwargs) else: stdout = stdout_format(stdout) tc.stdout = stdout tc.stderr = result.get('stderr') tc.elapsed_sec = result.get('time') tc.classname = result.get('class', test_class) err = result.get('err') if err: tc.add_error_info(*err if isinstance(err, (list, tuple)) else [err]) fail = result.get('fail') if fail: tc.add_failure_info(*fail if isinstance(fail, (list, tuple)) else [fail]) skip = result.get('skip') if skip: tc.add_skipped_info(*skip if isinstance(skip, (list, tuple)) else [skip]) test_cases.append(tc) ts = TestSuite(suite, test_cases, package=package) test_suites.append(ts) if output: check_makedir(os.path.dirname(output)) with open(output, 'w') as out: TestSuite.to_file(out, test_suites) return output else: return TestSuite.to_xml_string(test_suites)
def render_jsonnet(self, name, s, object_pairs_hook=OrderedDict): test_case = TestCase(name, file=name) self.test_cases.append(test_case) start = timer() try: return super(JunitRenderer, self).render_jsonnet(name, s, object_pairs_hook) except Exception: test_case.add_error_info(str(sys.exc_info()[1]), traceback.format_exc()) raise finally: end = timer() test_case.elapsed_sec = end - start
def do_build(self, args): try: # add arguments doParser = self.arg_build() doArgs = doParser.parse_args(shlex.split(args)) # if the help command is called, parse_args returns None object if not doArgs: return 2 # -- template = validate(doArgs.file) if template is None: return 2 if doArgs.id: myAppliance = self.api.Users(self.login).Appliances().Getall(Query="dbId==" + doArgs.id) myAppliance = myAppliance.appliances.appliance else: # Get template which correpond to the template file myAppliance = ( self.api.Users(self.login) .Appliances() .Getall( Query="name=='" + template["stack"]["name"] + "';version=='" + template["stack"]["version"] + "'" ) ) myAppliance = myAppliance.appliances.appliance if myAppliance is None or len(myAppliance) != 1: printer.out("No template found on the plateform") return 0 myAppliance = myAppliance[0] rInstallProfile = self.api.Users(self.login).Appliances(myAppliance.dbId).Installprofile("").Getdeprecated() if rInstallProfile is None: printer.out("No installation found on the template '" + template["stack"]["name"] + "'", printer.ERROR) return 0 try: i = 1 if doArgs.junit is not None: test_results = [] for builder in template["builders"]: try: printer.out( "Generating '" + builder["type"] + "' image (" + str(i) + "/" + str(len(template["builders"])) + ")" ) if doArgs.junit is not None: test = TestCase("Generation " + builder["type"]) test_results.append(test) start_time = time.time() format_type = builder["type"] targetFormat = generate_utils.get_target_format_object(self.api, self.login, format_type) if targetFormat is None: printer.out("Builder type unknown: " + format_type, printer.ERROR) return 2 myimage = image() myinstallProfile = installProfile() if rInstallProfile.partitionAuto: if "installation" in builder: if "swapSize" in builder["installation"]: myinstallProfile.swapSize = builder["installation"]["swapSize"] if "diskSize" in builder["installation"]: myinstallProfile.diskSize = builder["installation"]["diskSize"] else: myinstallProfile.swapSize = rInstallProfile.swapSize myinstallProfile.diskSize = rInstallProfile.partitionTable.disks.disk[0].size func = getattr( generate_utils, "generate_" + generics_utils.remove_special_chars(targetFormat.format.name), None, ) if func: myimage, myinstallProfile = func(myimage, builder, myinstallProfile, self.api, self.login) else: printer.out("Builder type unknown: " + format_type, printer.ERROR) return 2 if myimage is None: return 2 myimage.targetFormat = targetFormat myimage.installProfile = myinstallProfile if doArgs.simulated is not None and doArgs.simulated: myimage.simulated = True if doArgs.forced is not None and doArgs.forced: myimage.forceCheckingDeps = True rImage = self.api.Users(self.login).Appliances(myAppliance.dbId).Images().Generate(myimage) status = rImage.status statusWidget = progressbar_widget.Status() statusWidget.status = status widgets = [Bar(">"), " ", statusWidget, " ", ReverseBar("<")] progress = ProgressBar(widgets=widgets, maxval=100).start() while not (status.complete or status.error or status.cancelled): statusWidget.status = status progress.update(status.percentage) status = ( self.api.Users(self.login).Appliances(myAppliance.dbId).Images(rImage.dbId).Status.Get() ) time.sleep(2) statusWidget.status = status progress.finish() if status.error: printer.out( "Generation '" + builder["type"] + "' error: " + status.message + "\n" + status.errorMessage, printer.ERROR, ) if status.detailedError: printer.out(status.detailedErrorMsg) if doArgs.junit is not None: test.elapsed_sec = time.time() - start_time test.add_error_info("Error", status.message + "\n" + status.errorMessage) elif status.cancelled: printer.out( "Generation '" + builder["type"] + "' canceled: " + status.message, printer.WARNING ) if doArgs.junit is not None: test.elapsed_sec = time.time() - start_time test.add_failure_info("Canceled", status.message) else: printer.out("Generation '" + builder["type"] + "' ok", printer.OK) printer.out("Image URI: " + rImage.uri) printer.out("Image Id : " + generics_utils.extract_id(rImage.uri)) if doArgs.junit is not None: test.elapsed_sec = time.time() - start_time # the downloadUri already contains downloadKey at the end if rImage.downloadUri is not None: test.stdout = self.api._url + "/" + rImage.downloadUri i += 1 except Exception as e: if is_uforge_exception(e): print_uforge_exception(e) if doArgs.junit is not None and "test_results" in locals() and len(test_results) > 0: test = test_results[len(test_results) - 1] test.elapsed_sec = time.time() - start_time test.add_error_info("Error", get_uforge_exception(e)) else: raise if doArgs.junit is not None: testName = myAppliance.distributionName + " " + myAppliance.archName ts = TestSuite("Generation " + testName, test_results) with open(doArgs.junit, "w") as f: TestSuite.to_file(f, [ts], prettyprint=False) return 0 except KeyError as e: printer.out("unknown error in template file", printer.ERROR) except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_build() except KeyboardInterrupt: printer.out("\n") if generics_utils.query_yes_no("Do you want to cancel the job ?"): if ( "myAppliance" in locals() and "rImage" in locals() and hasattr(myAppliance, "dbId") and hasattr(rImage, "dbId") ): self.api.Users(self.login).Appliances(myAppliance.dbId).Images(rImage.dbId).Status.Cancel() else: printer.out("Impossible to cancel", printer.WARNING) else: printer.out("Exiting command") except Exception as e: print_uforge_exception(e) if doArgs.junit is not None and "test_results" in locals() and len(test_results) > 0: test = test_results[len(test_results) - 1] if "start_time" in locals(): elapse = time.time() - start_time else: elapse = 0 test.elapsed_sec = elapse test.add_error_info("Error", get_uforge_exception(e)) else: return 2 finally: if ( "doArgs" in locals() and doArgs.junit is not None and "test_results" in locals() and len(test_results) > 0 ): if "myAppliance" in locals(): testName = myAppliance.distributionName + " " + myAppliance.archName else: testName = "" ts = TestSuite("Generation " + testName, test_results) with open(doArgs.junit, "w") as f: TestSuite.to_file(f, [ts], prettyprint=False)
def _create_skipped_test_case(name, index): skipped_test_case = TestCase(name + "; Skip index" + str(index)) skipped_test_case.classname = "Skipped" skipped_test_case.skipped_output = "Skipped test case" skipped_test_case.elapsed_sec = 0 return skipped_test_case
def do_build(self, args): try: #add arguments doParser = self.arg_build() doArgs = doParser.parse_args(shlex.split(args)) #if the help command is called, parse_args returns None object if not doArgs: return 2 #-- template = validate(doArgs.file) if template is None: return 2 if doArgs.id: myAppliance = self.api.Users( self.login).Appliances().Getall(Query="dbId==" + doArgs.id) myAppliance = myAppliance.appliances.appliance else: #Get template which correpond to the template file myAppliance = self.api.Users(self.login).Appliances().Getall( Query="name=='" + template["stack"]["name"] + "';version=='" + template["stack"]["version"] + "'") myAppliance = myAppliance.appliances.appliance if myAppliance is None or len(myAppliance) != 1: printer.out("No template found on the plateform") return 0 myAppliance = myAppliance[0] rInstallProfile = self.api.Users(self.login).Appliances( myAppliance.dbId).Installprofile("").Getdeprecated() if rInstallProfile is None: printer.out( "No installation found on the template '" + template["stack"]["name"] + "'", printer.ERROR) return 0 try: i = 1 if doArgs.junit is not None: test_results = [] for builder in template["builders"]: try: printer.out("Generating '" + builder["type"] + "' image (" + str(i) + "/" + str(len(template["builders"])) + ")") if doArgs.junit is not None: test = TestCase('Generation ' + builder["type"]) test_results.append(test) start_time = time.time() format_type = builder["type"] targetFormat = generate_utils.get_target_format_object( self.api, self.login, format_type) if targetFormat is None: printer.out("Builder type unknown: " + format_type, printer.ERROR) return 2 myimage = image() myinstallProfile = installProfile() if rInstallProfile.partitionAuto: if "installation" in builder: if "swapSize" in builder["installation"]: myinstallProfile.swapSize = builder[ "installation"]["swapSize"] if "diskSize" in builder["installation"]: myinstallProfile.diskSize = builder[ "installation"]["diskSize"] else: myinstallProfile.swapSize = rInstallProfile.swapSize myinstallProfile.diskSize = rInstallProfile.partitionTable.disks.disk[ 0].size func = getattr( generate_utils, "generate_" + generics_utils.remove_special_chars( targetFormat.format.name), None) if func: myimage, myinstallProfile = func( myimage, builder, myinstallProfile, self.api, self.login) else: printer.out("Builder type unknown: " + format_type, printer.ERROR) return 2 if myimage is None: return 2 myimage.targetFormat = targetFormat myimage.installProfile = myinstallProfile if doArgs.simulated is not None and doArgs.simulated: myimage.simulated = True if doArgs.forced is not None and doArgs.forced: myimage.forceCheckingDeps = True rImage = self.api.Users(self.login).Appliances( myAppliance.dbId).Images().Generate(myimage) status = rImage.status statusWidget = progressbar_widget.Status() statusWidget.status = status widgets = [ Bar('>'), ' ', statusWidget, ' ', ReverseBar('<') ] progress = ProgressBar(widgets=widgets, maxval=100).start() while not (status.complete or status.error or status.cancelled): statusWidget.status = status progress.update(status.percentage) status = self.api.Users(self.login).Appliances( myAppliance.dbId).Images( rImage.dbId).Status.Get() time.sleep(2) statusWidget.status = status progress.finish() if status.error: printer.out( "Generation '" + builder["type"] + "' error: " + status.message + "\n" + status.errorMessage, printer.ERROR) if status.detailedError: printer.out(status.detailedErrorMsg) if doArgs.junit is not None: test.elapsed_sec = time.time() - start_time test.add_error_info( "Error", status.message + "\n" + status.errorMessage) elif status.cancelled: printer.out( "Generation '" + builder["type"] + "' canceled: " + status.message, printer.WARNING) if doArgs.junit is not None: test.elapsed_sec = time.time() - start_time test.add_failure_info("Canceled", status.message) else: printer.out( "Generation '" + builder["type"] + "' ok", printer.OK) printer.out("Image URI: " + rImage.uri) printer.out("Image Id : " + generics_utils.extract_id(rImage.uri)) if doArgs.junit is not None: test.elapsed_sec = time.time() - start_time #the downloadUri already contains downloadKey at the end if rImage.downloadUri is not None: test.stdout = self.api.getUrl( ) + "/" + rImage.downloadUri i += 1 except Exception as e: if is_uforge_exception(e): print_uforge_exception(e) if doArgs.junit is not None and "test_results" in locals( ) and len(test_results) > 0: test = test_results[len(test_results) - 1] test.elapsed_sec = time.time() - start_time test.add_error_info("Error", get_uforge_exception(e)) else: raise if doArgs.junit is not None: testName = myAppliance.distributionName + " " + myAppliance.archName ts = TestSuite("Generation " + testName, test_results) with open(doArgs.junit, 'w') as f: TestSuite.to_file(f, [ts], prettyprint=False) return 0 except KeyError as e: printer.out("unknown error in template file", printer.ERROR) except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_build() except KeyboardInterrupt: printer.out("\n") if generics_utils.query_yes_no("Do you want to cancel the job ?"): if 'myAppliance' in locals() and 'rImage' in locals( ) and hasattr(myAppliance, 'dbId') and hasattr(rImage, 'dbId'): self.api.Users(self.login).Appliances( myAppliance.dbId).Images(rImage.dbId).Status.Cancel() else: printer.out("Impossible to cancel", printer.WARNING) else: printer.out("Exiting command") except Exception as e: print_uforge_exception(e) if doArgs.junit is not None and "test_results" in locals( ) and len(test_results) > 0: test = test_results[len(test_results) - 1] if "start_time" in locals(): elapse = time.time() - start_time else: elapse = 0 test.elapsed_sec = elapse test.add_error_info("Error", get_uforge_exception(e)) else: return 2 finally: if "doArgs" in locals( ) and doArgs.junit is not None and "test_results" in locals( ) and len(test_results) > 0: if "myAppliance" in locals(): testName = myAppliance.distributionName + " " + myAppliance.archName else: testName = "" ts = TestSuite("Generation " + testName, test_results) with open(doArgs.junit, 'w') as f: TestSuite.to_file(f, [ts], prettyprint=False)
def test_availible_endpoints(output): s = Settings([ 'PLATFORM', 'ENVIRONMENT', 'SERVICEURL', 'WSDLURL', "CONSUMERHSAID", "CERTFILE" ]) test_cases = [] cache = SqliteCache(path='/tmp/_gcdrunner_sqlite.db', timeout=60 * 24 * 7) session = requests.Session() #session.verify = False #session.cert=s.CERTFILE transport = Transport(session=session, cache=cache) client = Client(s.WSDLURL, transport=transport) service = client.create_service(client.service._binding.name.text, s.SERVICEURL) session.verify = False session.cert = s.CERTFILE pid_fact = client.type_factory('ns2') adr_fact = client.type_factory('ns5') pid_t = pid_fact.PersonIdType('191212121212', '1.2.752.129.2.1.3.1') connectionPointId = get_connection_point_id(s.PLATFORM, s.ENVIRONMENT) requests.packages.urllib3.disable_warnings() endpoints = get_tak_la_for_tk(connectionPointId, s.CONSUMERHSAID) endpoints = list( filter(lambda e: e["logicalAddress"] not in IGNORE_LA_LIST, endpoints)) endpoints.sort(key=lambda e: e["description"]) for endpoint in endpoints: testname = "{} ({})".format(endpoint["description"], endpoint["logicalAddress"]) print("Running test: {}".format(testname)) logicalAddress_t = adr_fact.LogicalAddressType( endpoint['logicalAddress']) tc = TestCase(name=testname) tc.stdout = "Testing Logical address: {}, with id: {}, description: {}".format( endpoint['logicalAddress'], endpoint['id'], endpoint['description']) try: start = time.time() response = service.GetCareDocumentation( patientId=pid_t, _soapheaders={'LogicalAddress': logicalAddress_t}) tc.elapsed_sec = time.time() - start tc.stdout = repr(response['result']) except Exception as e: tc.stderr = str(e) failtype = None if tc.stderr.startswith("VP"): failtype = tc.stderr.split()[0] tc.add_failure_info(output=str(e), failure_type=failtype) test_cases.append(tc) ts = TestSuite("Spider TK tests for TK: " + TKNS, test_cases) with open(output, 'w') as f: TestSuite.to_file(f, [ts])