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_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 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 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 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 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)