Ejemplo n.º 1
0
def local_start(case_name, result_file, test_data, local_pic_name):
    """
    make sure mouse position will not affect testing
    """
    pic_min_sim = 0.99
    x, y = mouse.position()
    if x >= y > 200:
        pyautogui.moveTo(200, 200)
    elif 200 > x >= y:
        pyautogui.moveTo(x + 20, y + 20)
    else:
        pyautogui.moveTo(250, 250)
    time.sleep(20)
    # check local resolution
    pic_sim, diff_res, first_collect_flag = check_layout(os.path.join(test_data, 'td_multiple_display', 'local_pic', local_pic_name))
    if "1_ms" in local_pic_name:
        log.info("Change Sim to 0.93")
        pic_min_sim = 0.93
    if pic_sim >= pic_min_sim:
        steps = {
            'step_name': 'check local layout',
            'result': 'Pass',
            'expect': 'similarity > 99%',  # can be string or pic path
            'actual': 'layout and resolution set correctly',
            'note': ''}
        common_function.update_cases_result(result_file, case_name, steps)
        return True, first_collect_flag
    else:
        if not os.path.exists(common_function.get_current_dir('Test_Report', 'img')):
            os.mkdir(common_function.get_current_dir('Test_Report', 'img'))
        try:
            common_function.check_free_memory()
            picture_operator.save_from_data('{}'.format(common_function.get_current_dir('Test_Report', 'img',
                                                                                        '{}.png'.format(case_name))),
                                            diff_res)
            save_path = common_function.get_current_dir('Test_Report', 'img', '{}.jpg'.format(case_name))

            shutil.copy(common_function.get_current_dir('temp.png'),
                        save_path)
            common_function.check_free_memory("after")
        except AssertionError as e:
            raise e
        except:
            save_fail_path = common_function.get_current_dir('Test_Report', 'img', 'save_fail_{}.txt'.format(case_name))
            f = open(save_fail_path, "w")
            f.close()
        finally:
            log.debug(gc.garbage)
            gc.collect()

        steps = {
            'step_name': 'check local layout',
            'result': 'Fail',
            'expect': 'similarity > 99%',  # can be string or pic path
            'actual': 'img/{}.png'.format(case_name),  # can be string or pic path
            'note': 'actual similarity: {}'.format(pic_sim)}
        common_function.update_cases_result(result_file, case_name, steps)
        return False, first_collect_flag
Ejemplo n.º 2
0
 def start(self):
     self.__set()
     assert self.__method_name_list, "Need a method list!"
     index = self.read_a_file()
     log.info("Get Status: {}".format(index))
     if index == -1:
         log.info(" This {} Flow has Ended".format(self.__class_name))
         return
     flag = True
     self.current_step = index
     new_list = self.__method_name_list[index:]
     for method_name in new_list:
         if method_name in self.__skip_list:
             self.current_step += 1
             continue
         method = self.__dic.get(method_name)
         assert method, "{} Not Exist".format(method_name)
         log.info("Start Step Method {}".format(method_name))
         try:
             result_dict = {"event_method": method, "return": True}
             method(self)
             self.result_handler.update_class_property(**result_dict)
             self.result_handler.start()
             self.exec_callback(self.__work_around_list_success)
         except self.exception as e:
             flag = False
             self.suspend_exec()
             self.end_flow()
             log.error(e)
             result_dict = {
                 "event_method": method,
                 "error_msg": {
                     "actual":
                     "Fail at Index {} : {}".format(self.current_step, e)
                 },
                 "return": False
             }
             self.result_handler.update_class_property(**result_dict)
             self.result_handler.start()
             self.exec_callback(self.__work_around_list_fail)
         if not self.__exec:
             break
         self.current_step += 1
     log.debug("Capture Flag Return : {}".format(flag))
     return flag
Ejemplo n.º 3
0
def capture_screen(file_name, param="-m", auto_fix=False):
    """
    use native method scrot(used in hptc-snipping-tool)
    scrot:
      -b, --border              When selecting a window, grab wm border too
      -c, --count               show a countdown before taking the shot
      -d, --delay NUM           wait NUM seconds before taking a shot
      -e, --exec APP            run APP on the resulting screenshot
      -q, --quality NUM         Image quality (1-100) high value means
                                high size, low compression. Default: 75.
                                For lossless compression formats, like png,
                                low quality means high compression.
      -m, --multidisp           For multiple heads, grab shot from each
                                and join them together.
      -s, --select              interactively choose a window or rectangle
                                with the mouse
      -u, --focused             use the currently focused window
      -t, --thumb NUM           generate thumbnail too. NUM is the percentage
                                of the original size for the thumbnail to be,
                                or the geometry in percent, e.g. 50x60 or 80x20.
      -z, --silent              Prevent beeping
    """
    dir_path = os.path.dirname(file_name)
    if dir_path and not os.path.exists(dir_path):
        os.makedirs(dir_path)
    call = subprocess.Popen("scrot {} {}".format(param, file_name),
                            stderr=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stdin=subprocess.PIPE,
                            shell=True,
                            env=os.environ)
    stdout, stderr = call.communicate(timeout=30)
    log.debug("[capture]info of capture,out:{},error:{}".format(
        stdout, stderr))
    if auto_fix:
        img = cv2.imread(file_name)
        n1_f = np.sum(img, axis=2) == 0
        a_g = np.array([102, 102, 102])
        img[n1_f == True] = a_g
        cv2.imwrite(file_name, img)
    return file_name
Ejemplo n.º 4
0
def get_icon_by_pic(name, offset=(10, 10), **kwargs):
    """
    find a location in a picture by name
    :param name: path+name
    :param offset: diy a point
    :return: (offset:(x,y),shape:(y,x,3))
    """
    rate = kwargs.get("rate", 0.9)
    path_demo = get_current_dir('demo.png')
    capture_screen(path_demo)
    img_name = cv2.imread(name)
    t = cv2.matchTemplate(cv2.imread(path_demo), img_name,
                          cv2.TM_CCOEFF_NORMED)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(t)
    log.debug(
        "[get pic icon]current match picture: {}, Similarity:{:.3f}/{}".format(
            name, max_val, rate))
    if max_val > rate:
        x = max_loc[0]
        y = max_loc[1]
        return (x + offset[0], y + offset[1]), img_name.shape
    else:
        return None
Ejemplo n.º 5
0
def start(case_name=case_names[0], **kwargs):
    global user, rdp_server
    wait_time = 30
    time_path = common_function.get_current_dir("time_temp.txt")
    log.info("Start Wait {}".format(wait_time))
    if os.path.exists(time_path):
        with open(time_path, "r") as f:
            now_time = f.read()
    else:
        with open(time_path, "w") as f:
            now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            f.write(now_time)
    current_time = time.time()
    before_time = time.mktime(time.strptime(now_time, "%Y-%m-%d %H:%M:%S"))
    if current_time - before_time > 3600:
        os.remove(time_path)
        common_function.reboot_command()
    common_function.SwitchThinProMode("user")
    query_list = ["xen", 'freerdp', "view", "firefox"]
    shadow_dict = {}
    for i in query_list:
        d = vdi_connection.query_item_id(i)
        shadow_dict.update(d)
    vdi_connection.hide_desktop_icon(id_dict=shadow_dict)
    common_function.cache_collection(3)
    case_name = case_name.lower()
    result_file = os.path.join(common_function.get_current_dir(),
                               r'Test_Report',
                               '{}.yaml'.format(common_function.check_ip_yaml()))
    user_manager = vdi_connection.MultiUserManger()
    performance = Performance.get_system_obj()
    test_data = os.path.join(common_function.get_current_dir(), 'Test_Data')
    # --------------- pre define --------------------------
    log.info('Begin to start test {}'.format(case_name))
    common_function.new_cases_result(result_file, case_name)
    pic_name = generate_pic_name(case_name)
    local_pic_name = generate_local_name(case_name)
    parameters = display_function.analyze_name(case_name)
    parameters = display_function.check_resolution_support(parameters)
    log.info('analyze parameter from case name')
    # display_function.LinuxTools().generate_profile(parameters['layout'], parameters['monitors'])
    display_function.set_display_profile(parameters)
    log.info('generate xml file')
    log.info('set local resolution, Wait 20s for background refresh')
    display_function.LinuxTools().set_local_resolution()
    log.debug("check local resolution set success")
    if not set_resolution_success() and common_function.need_reboot() != -1:
        common_function.change_reboot_status(-1)
        common_function.reboot_command()
    common_function.change_reboot_status(0)
    display_function.set_background()
    log.info('set local background success')
    flag, first_collect_flag = local_start(case_name, result_file, test_data, local_pic_name)
    if not flag:
        return False
    # check fresh rate-----------------------------------------------
    check_fresh_rate(case_name, result_file, parameters)
    # check fresh rate end-------------------------------------------
    user = user_manager.get_a_available_key()
    log.info('get valid user : {}'.format(user))
    remote_collect_flag = False
    try:
        flag, remote_collect_flag = remote_start(case_name, result_file, test_data, pic_name, user_manager, parameters, performance)
    except:
        debug_path = common_function.get_current_dir("Test_Report/debug.log")
        with open(debug_path, "a") as f:
            traceback.print_exc(file=f)
        traceback.print_exc()
        steps = {
            'step_name': 'case exception',
            'result': 'Fail',
            'expect': '',  # can be string or pic path
            'actual': 'img/{}.jpg'.format(case_name),
            'note': traceback.format_exc()}
        common_function.update_cases_result(result_file, case_name, steps)
    finally:
        log.info('try to reset user')
        if user:
            user_manager.reset_key(user)
        if parameters['vdi'].upper() == 'RDP' and rdp_server:
            user_manager.reset_key(rdp_server, key=session.lower())
        if first_collect_flag or remote_collect_flag:
            steps = {
                'step_name': "Cannot find template PIC",
                'result': 'Fail',
                'expect': '',  # can be string or pic path
                'actual': '',
                'note': 'This case has no pic in library'}
            common_function.update_cases_result(result_file, case_name, steps)
        log.debug(gc.garbage)
        gc.collect()
Ejemplo n.º 6
0
def remote_start(case_name, result_file, test_data, pic_name, user_manager, parameters, performance):
    global session, rdp_server, user
    if parameters['vdi'].upper() == 'RDP':
        session = parameters.get('session')
        rdp_server = user_manager.get_a_available_key(session.lower())
        conn = vdi_connection.RDPLinux(user=user, parameters=parameters, rdp_server=rdp_server)
    elif parameters['vdi'].upper() == 'VIEW':
        conn = vdi_connection.ViewLinux(user=user, parameters=parameters)
    elif parameters['vdi'].upper() == 'CITRIX':
        conn = vdi_connection.CitrixLinux(user=user, parameters=parameters)
    else:
        conn = None
    log.info('init connection instance')
    first_collect_flag = False
    if conn:
        # VDI Test
        conn_flag = False
        for count in range(2):
            logon = conn.logon(parameters['session'])
            if logon is True:
                pymouse.PyMouse().click(1, 1)
                log.info('successfully logon session: {}'.format(parameters['session']))
                steps = {
                    'step_name': 'logon vdi',
                    'result': 'Pass',
                    'expect': '',  # can be string or pic path
                    'actual': 'logon successfully',
                    'note': ''}
                common_function.update_cases_result(result_file, case_name, steps)
                conn_flag = True
                pic_sim, diff_res, first_collect_flag = check_layout(os.path.join(test_data,
                                                              'td_multiple_display',
                                                              '{}_pic'.format(parameters['vdi']).lower(),
                                                              pic_name))
                if pic_sim >= 0.99:
                    log.info('check layout pass')
                    steps = {
                        'step_name': 'check vdi layout',
                        'result': 'Pass',
                        'expect': 'similarity > 99%',  # can be string or pic path
                        'actual': 'layout and resolution set correctly: {}'.format(pic_sim),
                        'note': ''}
                    common_function.update_cases_result(result_file, case_name, steps)
                else:
                    log.error('check layout Fail, autual: {}'.format(pic_sim))
                    shutil.copy(common_function.get_current_dir('temp.png'),
                                common_function.get_current_dir('Test_Report', 'img', '{}.jpg'.format(case_name)))
                    steps = {
                        'step_name': 'check vdi layout',
                        'result': 'Fail',
                        'expect': 'similarity > 99%',  # can be string or pic path
                        'actual': 'img/{}.png'.format(case_name),  # can be string or pic path
                        'note': 'actual result: {}'.format(pic_sim)}
                    common_function.update_cases_result(result_file, case_name, steps)
                    if not os.path.exists(common_function.get_current_dir('Test_Report', 'img')):
                        os.mkdir(common_function.get_current_dir('Test_Report', 'img'))
                    try:
                        common_function.check_free_memory("beforeremote")
                        picture_operator.save_from_data(
                            '{}'.format(common_function.get_current_dir('Test_Report', 'img',
                                                                        '{}.png'.format(
                                                                            case_name))),
                            diff_res)
                        common_function.check_free_memory("afterremote")
                    except:
                        save_fail_path = common_function.get_current_dir('Test_Report', 'img', 'save_fail_remote_{}.txt'.format(case_name))
                        f = open(save_fail_path, "w")
                        f.write(traceback.format_exc())
                        f.close()
                    finally:
                        log.debug(gc.garbage)
                        gc.collect()

                log.info("collecting performance data")
                path = "{}/Test_Report/performance/".format(common_function.get_current_dir())
                if not os.path.exists(path):
                    os.makedirs(path)
                performance.file = path + "{}.txt".format("".join(case_name.split("_")[2:-4]).replace(" ", ""))
                performance.start()
                conn.logoff()
                break
            elif isinstance(logon, str):
                log.warning('logon fail in cycle {}'.format(count))
                # wait 2min try again
                time.sleep(120)
                if not user:
                    log.info("user get none before, try to get a new user")
                    user = user_manager.get_a_available_key()
                    conn.user = user
                if parameters['vdi'].upper() == 'RDP' and not rdp_server:
                    log.info("try to get rdp_server")
                    session = parameters.get('session')
                    rdp_server = user_manager.get_a_available_key(session.lower())
                    conn.server = rdp_server
                    conn.domain_list = [rdp_server]
                continue
            else:
                log.error("logon fail in cycle {}& white list can't catch it, break!".format(count))
                break

        if not conn_flag:
            log.error('logon fail, and try count finished')
            steps = {
                'step_name': 'logon vdi',
                'result': 'Fail',
                'expect': '',  # can be string or pic path
                'actual': 'Fail to logon within 6 mins',  # can be string or pic path
                'note': ''}
            common_function.update_cases_result(result_file, case_name, steps)
            return False, first_collect_flag
    return True, first_collect_flag
Ejemplo n.º 7
0
def main():
    # show_desktop()
    if os.path.exists(get_current_dir('flag.txt')):
        with open(get_current_dir('flag.txt')) as f:
            test_flag = f.read()
            if 'TEST FINISHED' in test_flag.upper():
                return
    if not os.path.exists('time.txt'):
        with open('time.txt', 'w') as f:
            f.write(time.ctime())
    prepare_for_framework()
    additional_path = get_current_dir('Test_Data', 'additional.yml')
    file_obj = YamlOperator(additional_path)
    content = file_obj.read()
    site = content.get('AutoDash_Site')
    if not site:
        load_data_from_ftp()
        script_name = os.path.basename(__file__).split('.')[0]
        add_linux_script_startup([script_name])
        check_water_mark()
    path = get_current_dir('reboot.txt')
    if not os.path.exists(path):
        with open(path, 'w+') as f:
            f.write("0")
        time.sleep(5)
        if os.path.exists(path):
            SwitchThinProMode("admin")
            os.popen('reboot')
            time.sleep(30)
    if not os.path.exists(get_current_dir('Test_Report')):
        os.mkdir(get_current_dir('Test_Report'))
    test_data_path = os.path.join(get_current_dir(), 'Test_Data')
    with open(get_current_dir('flag.txt'), 'w') as f:
        f.write('testing')
    if not os.path.exists(os.path.join(test_data_path, 'script.yml')):
        log.info('script.yml not exist, please check if no cases planned')
        with open(get_current_dir('flag.txt'), 'w') as f:
            f.write('test finished')
        return
    with open(os.path.join(test_data_path, 'script.yml'), 'r') as f:
        scripts = yaml.safe_load(f)
    with open(os.path.join(test_data_path, 'additional.yml'), 'r') as f:
        additional = yaml.safe_load(f)
    for script in scripts:
        script_name, script_status = list(script.items())[0]
        if script_status.upper() == 'NORUN':
            log.info('Begin to Test case {}'.format(script_name))
            try:
                if need_reboot() == 1:
                    change_reboot_status(0)
                    reboot_command()
                memory_check(limit=MEMORY_LIMIT)
                globals()[script_name.split('__')[0]].start(
                    case_name=script_name.split('__')[1], kwargs=additional)
                script[script_name] = 'Finished'
                with open(os.path.join(test_data_path, 'script.yml'),
                          'w') as f:
                    yaml.safe_dump(scripts, f)
            except MemoryNotSufficient as e:
                log.debug(e)
                log.debug("start reboot")
                reboot_command()
            except:
                with open(get_current_dir('Test_Report', 'debug.log'),
                          'a') as f:
                    f.write(traceback.format_exc())
                capture_screen(
                    get_current_dir(
                        'Test_Report',
                        'img',
                        '{}.jpg'.format(script_name.split('__')[1]),
                    ))
                script[script_name] = 'Finished'
                with open(os.path.join(test_data_path, 'script.yml'),
                          'w') as f:
                    yaml.safe_dump(scripts, f)
                steps = {
                    'step_name': 'case exception',
                    'result': 'Fail',
                    'expect': '',  # can be string or pic path
                    'actual': 'img/{}.jpg'.format(script_name.split('__')[1]),
                    'note': traceback.format_exc()
                }
                base_name = get_report_base_name()
                report_file = get_current_dir('Test_Report', base_name)
                # result_file = get_current_dir(r'Test_Report', '{}.yaml'.format(check_ip_yaml()))
                update_cases_result(report_file,
                                    script_name.split('__')[1], steps)
        else:
            log.info('Test case {} status is Finished, Skip test'.format(
                script_name))
    if os.path.exists(path):
        if need_reboot() == 0:
            change_reboot_status(9)
            log.info("Start Reboot before Report")
            reboot_command()
        else:
            os.remove(path)
    if site:
        share_folder = content.get('share_folder')
        host = share_folder.split('/')[0]
        folder_path = '/'.join(share_folder.split('/')[1:])
        user = content.get('user')
        password = content.get('password')
        flag_path = '/{}/{}.txt'.format(folder_path, site)
        log.info('end_flag: {}'.format(flag_path))

        with open(get_current_dir('{}.txt'.format(site)), 'w') as f:
            f.write('test finished')
        ftp = FTPUtils(host, user, password)
        ftp.ftp.cwd('ThinPro_Automation_Site')
        local_folder = get_current_dir('Test_Report')
        ftp_folder = r'/{}/Test_Report'.format(folder_path)

        num = 0
        while True:
            try:

                ftp = FTPUtils(host, user, password)
                log.info('upload Test_Report folder to ftp')
                log.info(local_folder)
                log.info(ftp_folder)
                ftp.new_dir(ftp_folder)
                local_report = get_current_dir('Test_Report',
                                               '{}.yaml'.format(site))
                ftp_report = '/{}/Test_Report/{}.yaml'.format(
                    folder_path, site)
                ftp.upload_file(local_report, ftp_report)
                ftp.upload_file(get_current_dir(r"{}.txt".format(site)),
                                flag_path)
                break
            except:
                if num > 30:
                    traceback.print_exc()
                    break
                else:
                    num += 5
                    time.sleep(5)
    else:
        with open(get_current_dir('flag.txt'), 'w') as f:
            f.write('test finished')
        with open('time.txt') as f:
            start = f.read()
        end = time.ctime()
        report = email_tool.GenerateReport(start, end)
        report.generate()
        file = email_tool.zip_dir()
        log.info('zipped file name: {}'.format(file_obj))
        additional_email = additional.get('email') if additional.get(
            'email') else ''
        email_tool.send_mail(
            recipient=['*****@*****.**', additional_email],
            subject='Automation Report Linux {}'.format(
                zip_file_name(get_report_number(), get_report_value())),
            attachment=file)
        os.remove(file)
        os.remove('time.txt')
        try:
            collect_report()
        except Exception as e:
            print(e)
            log.error(e)