Commits

Akeem Wells authored and Ville Suoranta committed 5b51f583b1c Merge
Pull request #799: CAS-14307

Merge in CASA/casa6 from CAS-14307 to master * commit '00acc41f90a75d5bb6ddd18da1780dccfaba2e0a': Fix for NameError when running tests inside casa

casatestutils/runtest.py

Modified
146 146
147 147 file_obj = open(filepath,'w')
148 148 file_obj.write(string)
149 149 file_obj.close()
150 150
151 151 def clean_working_directory(workdir):
152 152 print("Cleaning: {}".format(workdir))
153 153 if os.path.exists(workdir):
154 154 shutil.rmtree(workdir)
155 155
156 -def list_tests(local_path):
156 +def list_tests(local_path, test_paths=[]):
157 157 print('Full list of unit tests')
158 158 print('-----------------------')
159 159 if os.path.isdir(local_path +"/testlist/"):
160 160 shutil.rmtree(local_path +"/testlist/")
161 161 os.makedirs(local_path +"/testlist/")
162 - testpaths = fetch_tests(local_path +"/testlist/", 'master')
162 + testpaths = fetch_tests(local_path +"/testlist/", 'master', test_paths)
163 163 for path in testpaths:
164 164 gather_all_tests(path, local_path +"/testlist/")
165 165 tests = sorted(os.listdir(local_path +"/testlist/"))
166 166 for test in tests:
167 167 if test.startswith("test_"):
168 168 print(test)
169 169
170 170 def gather_all_tests(path, workpath):
171 171
172 172 if sys.version_info[0] > 2:
333 333 else:
334 334 cmd = ("git merge --no-edit --verbose origin/{}".format( re.findall("\/(.*)",branch)[0])).split()
335 335 else:
336 336 if "release" in branch:
337 337 cmd = ("git merge --no-edit --verbose origin/{}".format(branch)).split()
338 338 else:
339 339 cmd = ("git merge --no-edit --verbose origin/{}".format( re.findall("([^\/]+$)",branch)[0])).split()
340 340
341 341 return cmd
342 342
343 -def fetch_tests(work_dir, branch, merge_target=None):
343 +def fetch_tests(work_dir, branch, merge_target=None, test_paths=[]):
344 344
345 345 if merge_target is not None:
346 346 print("Merge Target Enabled: \n\tTarget Branch: {} \n\tFeature Branch: {}".format(merge_target, branch))
347 347
348 348 repo_path = "https://open-bitbucket.nrao.edu/scm/casa/"
349 349 source_dir = work_dir + "/casasources"
350 350 # Test if https is restricted
351 351 p1 = subprocess.Popen(shlex.split("curl -k -X GET https://open-bitbucket.nrao.edu/rest/api/1.0/projects/CASA/repos/casa6"), stdout=subprocess.PIPE)
352 352 p2 = subprocess.Popen(shlex.split('grep "not permitted"'), stdin=p1.stdout)
353 353 p2.communicate()
479 479 cmd = ("git checkout origin/master").split()
480 480
481 481 print("\tRunning: ", " ".join(str(x) for x in cmd))
482 482 run_shell_command(cmd, source_dir + "/" + repo)
483 483
484 484 for x in get_repo_test_paths(repo):
485 485 test_paths.append(source_dir + "/" + x)
486 486
487 487 return test_paths
488 488
489 -def run_cmd(cmd):
489 +def run_cmd(cmd, pytest_args=[]):
490 490 try:
491 491 from casampi.MPIEnvironment import MPIEnvironment
492 492 if MPIEnvironment.is_mpi_enabled:
493 493 pytest.main(cmd)
494 494 else:
495 495 result = subprocess.run([sys.executable,"-m","pytest"] + pytest_args + cmd , env={**os.environ})
496 496 except:
497 497 result = subprocess.run([sys.executable,"-m","pytest"] + pytest_args + cmd, env={**os.environ})
498 498
499 499 return result
500 500
501 -def setup_and_run(cmd,workdir, workpath, dirname, DRY_RUN ):
501 +def setup_and_run(cmd,workdir, workpath, dirname, DRY_RUN, pytest_args ):
502 502 # https://docs.pytest.org/en/stable/usage.html
503 503 cmd = ["--verbose"] + ["-ra"] + ["--tb=short"] + cmd
504 504
505 505 if DRY_RUN:
506 506 cmd = ["--collect-only"] + cmd
507 507
508 508 if not os.path.isdir(workpath + '/xml/{}/'.format(dirname)):
509 509 os.makedirs(workpath + '/xml/{}/'.format(dirname))
510 510 xmlfile = workpath + 'xml/{}/nose.xml'.format(dirname)
511 511 cmd = ["--junitxml={}".format(xmlfile)] + ["-s"] + ["--disable-pytest-warnings"] + cmd
512 512 if len(os.listdir(workpath)) < 1: # If only the XML dir was created
513 513 print("No Tests to Run")
514 514 sys.exit()
515 515 else:
516 516 myworkdir = os.getcwd()
517 517 os.chdir(workdir + "{}/".format(dirname))
518 518 print("Test Directory: {}\n".format(os.getcwd()))
519 519 print("Running Command: pytest " + " ".join(str(x) for x in cmd))
520 520 write_pytestini(os.path.join(os.getcwd(),"pytest.ini"),dirname)
521 521 write_conftest(os.path.join(os.getcwd(),"conftest.py"))
522 - result = run_cmd(cmd)
522 + result = run_cmd(cmd, pytest_args)
523 523 update_xml(xmlfile, result, name= os.getcwd().split("/")[-1])
524 524 #os.remove(os.path.join(os.getcwd(),"conftest.py"))
525 525 os.remove(os.path.join(os.getcwd(),"pytest.ini"))
526 526 os.chdir(myworkdir)
527 527 ########################################################################################################################
528 528 ############################################## Run ###############################################
529 529 ########################################################################################################################
530 530
531 -def run(testnames, branch=None, merge_target=None, DRY_RUN=False):
531 +def run(testnames, branch=None, merge_target=None, DRY_RUN=False, pytest_args=[], test_paths=[]):
532 532
533 533 if not HAVE_PYTEST:
534 534 raise ImportError('No Module Named Pytest. Pytest is Required for runtest.py')
535 535
536 536 if HAVE_PYTEST:
537 537 cwd = os.getcwd() + "/"
538 538 workpath = os.getcwd() +"/nosedir/"
539 539 workdir = os.getcwd() +"/nosedir/"
540 540
541 541 clean_working_directory(workpath)
553 553 if inlist:
554 554 setlist.append(test)
555 555 inlist = False
556 556 testnames = setlist
557 557 print("Tests: {}".format(sorted(testnames)))
558 558 gittest = True
559 559 if branch ==None:
560 560 branch = 'master'
561 561 # Only Checkout When Needed
562 562 if any([False if ".py" in x else True for x in testnames ]):
563 - testpaths = fetch_tests(workdir, branch, merge_target)
563 + testpaths = fetch_tests(workdir, branch, merge_target, test_paths)
564 564 os.makedirs(workdir + "tests/")
565 565 for path in testpaths:
566 566 gather_all_tests(path, workdir + "tests/")
567 567 print("Directory Of Tests: ", workdir + "tests/")
568 568
569 569 for testname in testnames:
570 570 #print(testname)
571 571 cmd = []
572 572
573 573 # Copy Test To nosedir Directory if in cwd
600 600 #print("Copying: {} to {}".format(test, workdir + "{}/".format(dirname)))
601 601 shutil.copy2(test, workdir + "{}/".format(dirname))
602 602 except:
603 603 traceback.print_exc()
604 604 else:
605 605 try:
606 606 #print("Copying: {} to {}".format(workdir + "tests/",test), workdir + "{}/".format(dirname))
607 607 shutil.copy2("{}{}.py".format(workdir + "tests/",test), workdir + "{}/".format(dirname))
608 608 except:
609 609 traceback.print_exc()
610 - setup_and_run(cmd, workdir, workpath, dirname, DRY_RUN )
610 + setup_and_run(cmd, workdir, workpath, dirname, DRY_RUN , pytest_args)
611 611
612 612 ##################################################
613 613 ########## Real Path ##########
614 614 ##################################################
615 615 # Copy Test To nosedir Directory assuming it's in another location
616 616 elif testname.startswith("/"):
617 617 testpath = testname.split("[")[0]
618 618 cmd = []
619 619 dirname = testname.split("/")[-1]
620 620 test = dirname
635 635 # Set up Test Working Directory
636 636 if not os.path.exists(workdir + "{}/".format(dirname)):
637 637 print("\nSetting Working Directory: {}".format(workdir + "{}/".format(dirname)))
638 638 os.makedirs(workdir + "{}/".format(dirname))
639 639 cmd = [ workdir + "{}/".format(dirname) ] + cmd
640 640 try:
641 641 shutil.copy2(testpath, workdir + "{}/".format(dirname))
642 642 except:
643 643 traceback.print_exc()
644 644
645 - setup_and_run(cmd, workdir, workpath, dirname, DRY_RUN )
645 + setup_and_run(cmd, workdir, workpath, dirname, DRY_RUN, pytest_args )
646 646 #build_xml(workpath + '/xml/xUnit.xml', workpath + '/xml/')
647 647 os.chdir(cwd)
648 648
649 649 def run_bamboo_test(r, cmd, timeout, cwd):
650 650 print("Running cmd " + str(cmd) + "in " + cwd)
651 651 if not os.path.exists(cwd):
652 652 os.makedirs(cwd)
653 653 starttime = datetime.datetime.now()
654 654 output = r.runshell(cmd, timeout,cwd)
655 655 endtime = datetime.datetime.now()
686 686 if sys.platform != "darwin":
687 687 xvfb.start_virtual_frame_buffer()
688 688
689 689 if args.branch == None:
690 690 branch = "master"
691 691
692 692 print ("run_bamboo fetch_tests branch" + branch)
693 693
694 694 # Clone a default set of repositories to if test paths are not provided from command line
695 695 if len(test_paths) == 0 :
696 - test_paths = fetch_tests(str(work_dir), branch, merge_target)
696 + test_paths = fetch_tests(str(work_dir), branch, merge_target, test_paths)
697 697
698 698 if test_config_path == None:
699 699 test_config_path = work_dir + "/casasources/casa6/casatestutils/casatestutils/component_to_test_map.json"
700 700 # Read the JSON configuration
701 701 print ("Reading config from: " + test_config_path)
702 702 with open(test_config_path ) as f:
703 703 test_config = json.load(f)
704 704
705 705 # Get the actual tests as list
706 706 test_config_elems = test_config['testlist']
1109 1109 indices = []
1110 1110 for i, t in enumerate(testnames):
1111 1111 if t.split("/")[-1].replace(".py","") in tests_to_ignore:
1112 1112 indices.append(i)
1113 1113 testnames = [v for i,v in enumerate(testnames) if i not in indices]
1114 1114 if testnames == [] or len(testnames) == 0:
1115 1115 print("List of tests is empty")
1116 1116 parser.print_help(sys.stderr)
1117 1117 sys.exit(1)
1118 1118 print("Running {} Test(s)".format(len(testnames)))
1119 - run(testnames, args.branch, args.merge_target, DRY_RUN)
1119 + run(testnames, args.branch, args.merge_target, DRY_RUN, pytest_args, test_paths if args.test_paths is not None else [])
1120 1120 except:
1121 1121 traceback.print_exc()

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut