def prepare_for_framework(): file_path = get_current_dir('Test_Data', 'additional.yml') file_obj = YamlOperator(file_path) content = file_obj.read() site = content.get('AutoDash_Site') if site: return else: user_defined_data = get_current_dir('Test_Data', 'User_Defined_Data') if os.path.exists(user_defined_data): log.info('removing {}'.format(user_defined_data)) shutil.rmtree(user_defined_data) time.sleep(3) for k, v in content.items(): if 'User_Defined_Data'.upper() in str(k).upper(): log.info('will download user data') break else: log.info('no uer defined data to be download') return file = get_current_dir('Test_Data', 'ftp_config.yaml') fo = YamlOperator(file) ftp_para = fo.read() for k, v in content.items(): if 'User_Defined_Data'.upper() in str(k).upper(): source = str(v) linux_path = source.replace('\\', '/') host = linux_path.split('/')[2] for each in ftp_para: ip = each.get('ip') user = each.get('user') password = each.get('password') if ip == host: break else: log.info('ftp_config.yaml has no parameters for {}'.format( host)) continue log.info('download user data from {} to UUT'.format(host)) last_level_folder = os.path.split(linux_path)[1] folder_path = '/'.join(linux_path.split('/')[3:]) if last_level_folder.upper() in ['USER_DEFINED_DATA']: dst = get_current_dir('Test_Data', last_level_folder) else: dst = get_current_dir('Test_Data', 'User_Defined_Data', last_level_folder) n = 0 while True: try: log.info('download {} to {}'.format(source, dst)) ftp = FTPUtils(host, user, password) ftp.download_dir(folder_path, dst) break except: if n > 30: log.info(traceback.format_exc()) break else: n += 5 time.sleep(5)
def add_to_startup_script(i): with open('/writable/root/auto_start_setup.sh', 'a') as s: res = get_folder_items(get_current_dir(), file_only=True) if i in res: s.write("{}\n".format(get_current_dir() + '/' + i)) elif '/' in i: s.write("{}\n".format(i)) time.sleep(0.2) os.system("chmod 777 /writable/root/auto_start_setup.sh") time.sleep(0.2)
def case_steps_run_control(steps_list, name, *args, **kwargs): case_steps_file = os.path.join(get_current_dir(), "{}_case_steps.yml".format(name)) if not os.path.exists(case_steps_file): list_dict = {} for s in steps_list: list_dict[s] = "norun" steps_yml = YamlOperator(case_steps_file) steps_yml.write(list_dict) steps_yml = YamlOperator(case_steps_file) for step in steps_list: steps_dict = steps_yml.read() for key, value in steps_dict.items(): if step == key and value.lower() != "finished": steps_dict[key] = "finished" steps_yml.write(steps_dict) result = getattr(sys.modules[name], step)(*args, **kwargs) # result = eval(key) if result is False: os.remove(case_steps_file) return False if steps_list.index(step) == len(steps_list) - 1: os.remove(case_steps_file) return True
def import_cert(cert="ROOTCA.pem"): time.sleep(1) log.info("start import certificate") rootca_pem_1 = os.path.exists("/etc/ssl/certs/ROOTCA.pem") if rootca_pem_1: log.info("certificate is already exist") return True else: log.info("certificate not exist, start install cert") shutil.copy( os.path.join(get_current_dir(), 'Test_Utility', 'ROOTCA.pem'), '/usr/local/share/ca-certificates/ROOTCA.pem') time.sleep(0.2) c = os.path.exists("/usr/local/share/ca-certificates/{}".format(cert)) if not c: log.error('copy cert fail') return False log.info('copy cert success') subprocess.getstatusoutput("/usr/bin/hptc-cert-mgr --apply") time.sleep(4) rootca_pem_2 = os.path.exists("/etc/ssl/certs/ROOTCA.pem") if not rootca_pem_2: log.error('install certificates fail') return False else: log.info('install certificates success') time.sleep(1) return True
def get_ip_from_yml(): report_path = get_current_dir('Test_Report') files = os.walk(report_path) for file in files: if '.yaml' in file.lower(): ip_addr = file.split('.').remove[-1] return '.'.join(ip_addr) return 'null'
def load_data_from_ftp(): file_obj = YamlOperator( get_current_dir('Test_Data', 'td_common', 'global_config.yml')) content = file_obj.read() ftp_server = content['td_ftp']['server'] ftp_user = content['td_ftp']['username'] ftp_passwd = content['td_ftp']['password'] td_path = content['td_ftp']['td_path'] try: ftp = FTPUtils(ftp_server, ftp_user, ftp_passwd) ftp.change_dir(td_path) folders = ftp.get_item_list('') for folder in folders: if not ftp.is_item_file(folder): ftp.download_dir(folder, get_current_dir(folder)) ftp.close() except: log.error('ftp exception:\n{}'.format(traceback.format_exc()))
def add_linux_script_startup(script_name): src = '/root/auto_start_setup.sh' src_auto = get_current_dir("Test_Utility/auto.service") dst_auto = "/etc/systemd/system/auto.service" dst_wants = "/etc/systemd/system/multi-user.target.wants/auto.service" if os.path.exists(src): os.remove(src) os.system("fsunlock") time.sleep(0.2) with open('/etc/init/auto-run-automation-script.conf', 'w+') as f: f.write("start on lazy-start-40\nscript\n") f.write("\t/writable/root/auto_start_setup.sh\nend script\n") time.sleep(0.5) os.system("chmod 777 /etc/init/auto-run-automation-script.conf") os.system('fsunlock') time.sleep(0.1) bash_script = "#! /bin/bash\nsource /etc/environment\nsource /etc/profile\nexec 2>/root/log.log\nsleep 50\nexport DISPLAY=:0\nfsunlock\ncd /root\n" with open('/writable/root/auto_start_setup.sh', 'w+') as s: s.write(bash_script) res = get_folder_items(get_current_dir(), file_only=True) if type(script_name) == list: for i in script_name: if i in res: s.write("sudo {}\n".format( os.path.join(get_current_dir(), i))) elif '/' in i: s.write("sudo {}\n".format(i)) else: if script_name in res: s.write("sudo {}\n".format( os.path.join(get_current_dir(), script_name))) elif '/' in script_name: s.write("sudo {}\n".format(script_name)) time.sleep(0.2) os.system("chmod 777 /writable/root/auto_start_setup.sh") time.sleep(0.2) if not os.path.exists(dst_auto): shutil.copyfile(src_auto, dst_auto) time.sleep(1) os.system("ln -s {} {}".format(dst_auto, dst_wants)) return False
def get_report_base_name(): additional_path = get_current_dir('Test_Data', 'additional.yml') if os.path.exists(additional_path): file_obj = YamlOperator(additional_path) content = file_obj.read() site = content.get('AutoDash_Site') else: site = None if site: base_name = '{}.yaml'.format(site) else: base_name = '{}.yaml'.format(check_ip_yaml()) return base_name
def need_reboot() -> int: """ 0: not need reboot another: need reboot """ path = get_current_dir("reboot.txt") if not os.path.exists(path): return 0 with open(path, "r") as f: res = f.read().strip() if not res: return 0 return int(res)
def get_position(img, region=None, similaity=0.97, base_dir=os.path.join(get_current_dir(), 'Test_Data', 'td_power_manager', 'AD')): # img=os.path.join(os.getcwd(),"Test_Data","import_cert_and_lunch_firefox",img) if base_dir: img = os.path.join(base_dir, img) # print(img) count = 5 count1 = count while count: part_img = cv2.imread(img, 0) w, h = part_img.shape[::-1] if region is None: pyautogui.screenshot().save("temp.png") full_img = cv2.imread("temp.png", 0) res = cv2.matchTemplate(part_img, full_img, cv2.TM_CCOEFF_NORMED) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) if max_val > similaity: # print("find :" + img + " with similaity " + str(max_val) + " in full screen") log.info("find :" + img + " with similaity " + str(max_val) + " in full screen") return (max_loc[0], max_loc[1], w, h), (int(max_loc[0] + w / 2), int(max_loc[1] + h / 2)) else: # print("Not find :" + img + " with similaity " + str(max_val) + "in region:" + str(region)) log.info("Not find :" + img + " with similaity " + str(max_val) + "in region:" + str(region)) else: pyautogui.screenshot(region=region).save("temp.png") full_img = cv2.imread("temp.png", 0) res = cv2.matchTemplate(part_img, full_img, cv2.TM_CCOEFF_NORMED) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) if max_val > similaity: # print("find :"+img+" with similaity "+str(max_val)+"in region:"+str(region)) log.info("find :" + img + " with similaity " + str(max_val) + "in region:" + str(region)) return (max_loc[0], max_loc[1], w, h), (int(max_loc[0] + w / 2), int(max_loc[1] + h / 2)) else: # print("Not find :" + img + " with similaity " + str(max_val) + "in region:" + str(region)) log.info("Not find :" + img + " with similaity " + str(max_val) + "in region:" + str(region)) count = count - 1 # print("can not find :" + img + " :wait 1s repeat") log.info("can not find :" + img + " :wait 1s repeat") # print("can not find " + img + " in "+str(count1)+" repeats") log.info("can not find " + img + " in " + str(count1) + " repeats") return False
def check_ip_yaml(): ip_yaml_path = get_current_dir('Test_Data', 'ip.yml') if os.path.exists(ip_yaml_path): with open(ip_yaml_path, encoding='utf-8') as f: ip = yaml.safe_load(f) if ip: return ip[0] else: return '127.0.0.1' else: f = open(ip_yaml_path, 'w') f.close() with open(ip_yaml_path, 'w', encoding='utf-8') as f: yaml.dump([get_ip()], f, default_flow_style=False) return get_ip()
def collect_report(): """ collect report and send to ALM server for automated return result to ALM alm need addition.yml(case<->alm information), ip.yml(cases result) :return: #By: balance """ # expect only ip.yml exist in test_report global_conf = YamlOperator( get_current_dir('Test_Data', 'td_common', 'global_config.yml')).read() ftp_svr = global_conf['alm_ftp']['server'] ftp_user = global_conf['alm_ftp']['username'] ftp_pass = global_conf['alm_ftp']['password'] ftp_path = global_conf['alm_ftp']['report_path'] result_file = get_folder_items(get_current_dir('Test_Report'), file_only=True, filter_name='.yaml')[0] log.info(f'[common][collect result]Get result file: {result_file}') prefix = time.strftime("test_%m%d%H%M%S", time.localtime()) log.info( '[common][collect result]Copy additional.yml and ip.yml to test report' ) shutil.copy(get_current_dir('Test_Data', 'additional.yml'), get_current_dir('Test_Report', '{}_add.yml'.format(prefix))) shutil.copy(get_current_dir('Test_Report', result_file), get_current_dir('Test_Report', '{}_result.yml'.format(prefix))) try: ftp = file_transfer.FTPUtils(ftp_svr, ftp_user, ftp_pass) ftp.change_dir(ftp_path) ftp.upload_file( get_current_dir('Test_Report', '{}_result.yml'.format(prefix)), '{}_result.yml'.format(prefix)) ftp.upload_file( get_current_dir('Test_Report', '{}_add.yml'.format(prefix)), '{}_add.yml'.format(prefix)) ftp.close() log.info('[common][collect result]upload report to ftp server') except: log.error('[common][collect result]FTP Fail Exception:\n{}'.format( traceback.format_exc()))
def log_cache(dtype="-k", relative_path="free.txt"): """ command: free relative_path: path start from the project -b, --bytes show output in bytes --kilo show output in kilobytes --mega show output in megabytes --giga show output in gigabytes --tera show output in terabytes --peta show output in petabytes -k, --kibi show output in kibibytes -m, --mebi show output in mebibytes -g, --gibi show output in gibibytes --tebi show output in tebibytes --pebi show output in pebibytes -h, --human show human-readable output --si use powers of 1000 not 1024 """ path = get_current_dir(relative_path) os.system(f"free {dtype} >> {path}") return path
def get_global_config(*keys): """ :params: keys, tuple if key not exist, raise ValueError """ file_dict = {} path = get_current_dir('Test_Data/td_common/global_config.yml') if os.path.exists(path): file_obj = YamlOperator(path) file_dict = file_obj.read() else: log.warning("Not Exist {}".format(path)) if not keys: return file_dict new_value = copy.deepcopy(file_dict) for i in keys: value = new_value.get(i, None) if isinstance(new_value, dict) else None if not value: index = keys.index(i) raise ValueError("Key not Exist, origin: {}".format(" -> ".join( keys[:index + 1]))) new_value = value return new_value
def change_reboot_status(status=1): path = get_current_dir("reboot.txt") with open(path, "w") as f: f.write(str(status))
def load_global_parameters(): # path = r'{}/Test_Data/additional.yml'.format(get_current_dir()) path = os.path.join(get_current_dir(), 'Test_Data', 'additional.yml') data_dic = yaml.safe_load(open(path)) return data_dic
def import_profile(): os.popen('mclient import {}'.format( get_current_dir('Test_Data', 'profile.xml')))