def get_a_available_key(self, key="user"): for _ in range(10): item_list = self.ftp.get_item_list(r'Files/multiple_display') if 'time.txt' not in item_list: self.create_file() break log.info('The user list file is occupied, wait ...') time.sleep(2) else: log.warning('user_list.yml has been occupied, can not get a available user') return '' self.ftp.download_file(self.remote_base_file('path'), self.file_path) time.sleep(2) yaml_obj = YamlOperator(self.file_path) total_users = yaml_obj.read().get(key, {}) for k, v in total_users.items(): if v in 'available': dic_key = k break else: dic_key = '' if dic_key: self.lock_key(dic_key, key) else: print('no available user could be assigned') self.ftp.delete_file('Files/multiple_display/time.txt') if os.path.exists(get_current_dir('time.txt')): os.remove(get_current_dir('time.txt')) return dic_key
def save_info_to_yaml(cls) -> dict: if not cls.network_information: cls.get_network_info() log.info(f"StaticIpUtils Save: {cls.network_information}") file_oper = YamlOperator(cls.temp_net_info_file) file_oper.write(cls.network_information) return cls.network_information
def set_vpn(self, type, profile="profile_1", **kwargs): log.info("start set vpn") if type.lower() not in ["cisco", "pptp", "none"]: log.error("vpn type error") return False pyautogui.click(wait_element(self.vpn_pic("_connection_type"), offset=(400, 10))[0], interval=1) pyautogui.click(wait_element( self.vpn_pic("_type_{}".format(type.lower())))[0], interval=1) if type.lower() == "none": pass else: profile_data = YamlOperator( self.network_profile()).read()["vpn"][type.lower()][profile] print(profile_data) for key, value in profile_data.items(): pic_name = "_{}".format("_".join(key.split())) pyautogui.click(wait_element(self.vpn_pic(pic_name), offset=(200, 10))[0], interval=0.2) self.__clear() pyautogui.write(value, interval=0.1) auto_start = kwargs.get("auto_start", "null") if auto_start.lower() == "enable": self.enable_auto_start() elif auto_start.lower() == "disable": self.disable_auto_start() else: pass log.info("set vpn complete") return True
def read_info_from_yaml(cls, *wants) -> dict: if not os.path.exists(cls.temp_net_info_file): network_information = cls.save_info_to_yaml() else: network_information = YamlOperator(cls.temp_net_info_file).read() if not wants: return network_information return dict( ([(key, network_information.get(key, "")) for key in wants]))
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 get_a_specify_value(self, key="user", **other_keys): self.ftp.download_file(self.remote_base_file, self.file_path) yaml_obj = YamlOperator(self.file_path) total_users = yaml_obj.read() value = total_users.get(key, {}) key_list = other_keys.values() for i in key_list: value = value.get(i, "") if not isinstance(value, dict): break return value
def change_key_state(self, user_name, state, key="user"): self.ftp.download_file(self.remote_base_file('path'), self.file_path) time.sleep(2) yaml_obj = YamlOperator(self.file_path) total_users = yaml_obj.read() value = total_users.get(key).get(user_name) if value: total_users[key][user_name] = state yaml_obj.write(total_users) self.ftp.upload_file(self.file_path, self.remote_base_file('path')) else: print('invalid user')
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 change_key_state(self, user_name, state, key="user"): self.ftp.download_file(self.remote_base_file, self.file_path) time.sleep(2) yaml_obj = YamlOperator(self.file_path) total_users = yaml_obj.read() value = total_users.get(key).get(user_name) if value: total_users[key][user_name] = state yaml_obj.write(total_users) self.ftp.upload_file(self.file_path, self.remote_base_file) else: log.error('invalid user {}'.format(user_name)) return False return True
def get_a_available_key(self, key="user", available="available"): self.ftp.download_file(self.remote_base_file, self.file_path) yaml_obj = YamlOperator(self.file_path) total_users = yaml_obj.read().get(key, {}) for k, v in total_users.items(): if v.lower() in available: dic_key = k break else: dic_key = '' if dic_key: self.lock_key(dic_key, key) else: log.info('no available user could be assigned') return dic_key
def __init__(self, user, **kwargs): super().__init__(user, **kwargs) """ domain='sh', user='******', password='******', url='https://sfnsvr.sh.dto/citrix/newstore/discovery' """ self.domain_list = vdi_config.get(self.parameters.get('vdi').lower(), {}).get(self.parameters.get( 'session').lower(), {}).get("ip_list", []) self.session = self.parameters.get("session", "") # self.domain_list = ['Autotest-C01.sh.dto'] self.connection_mode = kwargs.get("connection_mode", "workspace") self.url = vdi_config['citrix']['url'] self.citrix_server = vdi_config['citrix']['server'] self.vdi = "Selfservice" self.grep = "Citrix Workspace" self.settings = kwargs.get("setting", YamlOperator(self.registry_file).read()["set"]["citrix"]["multiple_display"]) self.set_dic = {"address": self.url, "domain": self.domain, "connectionMode": self.connection_mode, "password": self.password, "username": self.user }
def create_vdi_rdp(self, step_No): for _ in range(6): self.user = vdi_connection.MultiUserManger().get_a_available_key() if self.user: break time.sleep(180) self.log.info('user: {}'.format(self.user)) for _ in range(6): self.rdp_server = vdi_connection.MultiUserManger( ).get_a_available_key('rdp win10') if self.rdp_server: break time.sleep(180) self.log.info('rdp_server: {}'.format(self.rdp_server)) setting_file = os.path.join(self.path, "Test_Data", "td_common", "thinpro_registry.yml") setting_rdp = YamlOperator(setting_file).read()["set"]["RDP"] if step_No == 'step1&2': setting = setting_rdp["screen_lock_with_password_step2"] elif step_No == 'step3&4': setting = setting_rdp["screen_lock_with_password_step3"] else: setting = '{}' self.vdi = vdi_connection.RDPLinux(user=self.user, setting=setting, rdp_server=self.rdp_server) return self.vdi, self.user, self.rdp_server
def __init__(self, **kwargs): self.t = TimeCounter() self.ti = kwargs.get("time", self._sys_wait_time) self._sys_wait_time = kwargs.get("sys_time", self._sys_wait_time) self.domain_list = YamlOperator(self._yaml_path).read().get("Wol_server_list") self.ip_list = self.domains_to_ip_list(self.domain_list) assert self.ip_list, "ip list is None" print("init")
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 read_info_from_yaml(cls) -> dict: """ :return {ip:'ip', mask:'mask', gateway:'gateway' } """ if not os.path.exists(cls.temp_static_info_file): return cls.get_and_save_info_to_yaml() return YamlOperator(cls.temp_static_info_file).read()
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 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 __init__(self, user, **kwargs): super().__init__(user, **kwargs) self.vdi = "view" self.set_dic = { "credentialsType": "password", "domain": self.domain, "desktopSize": '"All Monitors"', "username": user, "password": self.password, "viewSecurityLevel": '"Allow all connections"', } self.domain_list = vdi_config.get(self.parameters.get('vdi').lower(), {}).get(self.parameters.get( 'session').lower(), {}).get("ip_list") self.settings = kwargs.get("setting", YamlOperator(self.registry_file).read()["set"]["view"]["multiple_display"])
def __init__(self, user, **kwargs): super().__init__(user, **kwargs) self.vdi = "freerdp" self.domain = kwargs.get("domain", self.domain) self.set_dic = { "credentialsType": "password", "domain": self.domain, "password": self.password, "username": user, "securityLevel": "0", "windowType": "full", } self.server = kwargs.get("rdp_server", "") self.domain_list = [self.server] self.settings = kwargs.get("setting", YamlOperator(self.registry_file).read()["set"]["RDP"]["multiple_display"])
def add(self, ssid, **kwargs): ip = '15.83.252.100' mask = '255.255.255.192' gateway = '15.83.252.65' log.info("add '{}' wireless profile".format(ssid)) profile = YamlOperator(self.network_profile()).read()[ssid] log.info("click add button") pyautogui.click(wait_element(self.pic("_add"))[0], interval=2) # click add click = kwargs.get('click', '') if click: res = self.set_wireless_profile(profile, click=True) if res: return res else: self.set_wireless_profile(profile) static_ip = kwargs.get("static_ip", "") if static_ip: self.set_ipv4_static(ip=ip, subnet_mask=mask, gateway=gateway)
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 create_vdi_view(self, step_No): for _ in range(6): self.user = vdi_connection.MultiUserManger().get_a_available_key() if self.user: break time.sleep(30) self.log.info('user: {}'.format(self.user)) setting_file = os.path.join(self.path, "Test_Data", "td_common", "thinpro_registry.yml") setting_view = YamlOperator(setting_file).read()["set"]["view"] if step_No == 'step1&2': setting = setting_view["screen_lock_with_password_step2"] self.parameters = {'vdi': 'view', 'session': 'blast win10'} elif step_No == 'step3&4': setting = setting_view["screen_lock_with_password_step3"] self.parameters = {'vdi': 'view', 'session': 'blast win10'} else: setting = '{}' self.parameters = {'vdi': 'view', 'session': 'blast win10'} self.vdi = vdi_connection.ViewLinux(user=self.user, setting=setting, parameters=self.parameters) return self.vdi, self.user
def __save(cls, **data) -> dict: log.info(f"StaticIpUtils Save: {data}") file_oper = YamlOperator(cls.temp_static_info_file) file_oper.write(data) return data
def vdi_user_info(self): vdi_server = os.path.join(common_function.get_current_dir(), "Test_Data", "td_common", "vdi_server_config.yml") return YamlOperator(vdi_server).read()
def get_key_dict(self, key="user"): self.ftp.download_file(self.remote_base_file('path'), self.file_path) time.sleep(2) yaml_obj = YamlOperator(self.file_path).read() return yaml_obj.get(key, {})
def vdi_setting(): setting_file = os.path.join(cf.get_current_dir(), "Test_Data", "td_common", "thinpro_registry.yml") return YamlOperator(setting_file).read()
def config_args(self): yaml_obj = YamlOperator(self.config_base_file) content = yaml_obj.read() return content
def parameters(self): f = YamlOperator( common_function.get_current_dir('Test_Data', 'td_network', 'network_profile_info.yml')) return f.read()
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)