Beispiel #1
0
def run_in_command_line_mode():
    '''
    main entry point for ATOM command line mode.
    '''
    global obj_options
    
    Env.log('INFO', 'ATOM Command Line Mode:%s' % ' '.join(sys.argv))
    
    '''
    list_case contains 3 part:
        1. loose case
        2. content of test list file
        3. test suite
        folders are expended and checked in TestManager.py/parse_case_list()
    '''
    list_case = []
    str_work_directory = ''
    #whether to remove duplicated cases in the task;
    #determined by --dedub.
    flag_dedup=obj_options.dedup
    flag_upload=obj_options.flag_upload
    test_run_id=obj_options.test_run_id

    #test suite: folder.
    test_suite=obj_options.test_suite
    if test_suite:
        msg='Include cases in test suite(%s) into task file'%test_suite
        Env.log('INFO', msg)
        list_case+=test_suite.split(',')

    #test list file
    list_file=obj_options.str_case_list
    if list_file:
        msg='Include cases in list file(%s) into task file'%list_file
        Env.log('INFO', msg)
        #parse the file and add to list_case
        if not os.path.isfile(list_file):
            Env.log('WARNING', 'Case list file(%s) not found'%list_file)
        else:
            lines = open(list_file,'r').read().splitlines()
            for i in lines:
                if i.startswith('#' or '/'):
                    #commented out. ignore.
                    continue
                list_case.append(i.strip())

    #loose test cases
    if obj_options.str_cases:
        Env.log('INFO', 'Included cases(%s) into task file' % obj_options.str_cases)
        list_case+=obj_options.str_cases.split(',')

    if obj_options.str_enclosure_id != '':
        list_enclosure_id = obj_options.str_enclosure_id.split(',')
        list_target_enclosures = []
        for each_enclosure in list_enclosure_id:
            if Env.dict_ID_enclosure.has_key(each_enclosure):
                list_target_enclosures.append(each_enclosure)
    elif obj_options.str_platform != '':
        list_target_enclosures = Env.get_all_enclosure_id(obj_options.str_platform)
    else:
        list_target_enclosures = []
            
    if list_target_enclosures == []:
        Env.log('ERROR', 'No DUT information')
        return
    
    str_platform = Env.get_enclosure(list_target_enclosures[0]).platform()
    
    #alias: datetime+given name.
    test_run_alias=time.strftime('%Y%m%d%H%M%S')
    if obj_options.str_alias != '':
        if '\\' in obj_options.str_alias:
            obj_options.str_alias = obj_options.str_alias.replace('\\','_')
        test_run_alias += '_%s' % obj_options.str_alias

    str_work_directory = os.path.join(Env.str_log_folder, \
                                      str_platform, \
                                      test_run_alias)
    Env.log('INFO', 'Logs are available at: %s' % str_work_directory)
    
    #check test result uploading setting

    if test_run_id:
        #test_run_id is provided but --upload is not:
        #shall we use the id or just ignore it?
        #use the id first.
        flag_upload=True

    if flag_upload:
        testrail=TestRailClient()

        if test_run_id:
            # test run id provided. validate it
            try:
                testrail.get_test_run(test_run_id)
                msg='Test result will be uploaded to test run %d'%test_run_id
                Env.log('INFO', msg)
            except:
                Env.log('ERROR', traceback.format_exc())
                raise Exception('Invalid test run id: %d'%test_run_id)
        else:
            '''
            need to create a new test run.
            set include_all so the test scope remains the same.
            '''
            test_run_name='ATOM_%s_%s'%(str_platform, test_run_alias)
            PROJECT_FFV=1
            TEST_SUITE_ATOM=13
            ret=testrail.add_test_run(PROJECT_FFV, test_run_name, [], TEST_SUITE_ATOM)
            Env.log('INFO', 'new test run %s created.'%test_run_name)
            test_run_id=ret['id']

    str_task_file = TestManager.create_task_file(str_platform, \
                                                 str_work_directory, \
                                                 list_file, \
                                                 list_case, \
                                                 obj_options.str_release_folder, \
                                                 list_target_enclosures, \
                                                 flag_dedup, \
                                                 test_run_id)
    if obj_options.str_task != '':
        str_task_file = obj_options.str_task
        
    if str_task_file == '':
        Env.log('ERROR', 'Command invalid, please check the arguments')
        return 
        
    obj_TE = TestExecutor.CTestExecutor(str_task_file)
    if not obj_TE.is_valid():
        Env.log('INFO', 'Task file(%s) invalid' % str_task_file)
        return

    obj_TE.create_logger(obj_TE.task_id())
    
    obj_TE.run()
    
    now = time.clock()
    while True:
        # exit if all the queued emails are sent
        if Env.obj_email_server.is_idle():
            break
        # quit when the 10 min timer expired
        if time.clock() - now > 600:
            break
        time.sleep(0.01)
    
    Env.log('INFO', 'ATOM quit command line mode')