def is_fb_running(cls): result = False if PlatformHelper.is_Linux(): cmd = "service filebeat status" cmd_output = CmdHelper.run(cmd) LogHelper.debug(cmd_output) if cmd_output.find("running") >= 0: result = True if PlatformHelper.is_mac(): cmd = "ps aux | grep 'filebeat -c' | grep -v 'grep'" output = CmdHelper.run(cmd) if output.find("filebeat") >= 0: result = True if PlatformHelper.is_win(): cmd = "sc query filebeat" output = CmdHelper.run(cmd) if output.find("RUNNING") >= 0: result = True if result: LogHelper.debug("filebeat is running") else: LogHelper.info("filebeat is not running") return result
def download_and_install(cls, build=-1, job='', pattern=r''): if build == 0: # keep current build return True if not pattern: pattern = cls.default_pattern if not job: job = cls.default_job installer = cls.create() lynx_controller = LynxCtrl() if job == "product": pattern = "mozypro-%ssetup" % (PlatformHelper.get_arch()) else: #MozyEnterprise-rpm-64-1_5_0_5252.rpm pattern = "MozyEnterprise.*%s.*" % (PlatformHelper.get_arch()) if not lynx_controller.is_client_installed( ): # is client is not installed, then install print pattern installer.install(job, build, pattern) else: version = lynx_controller.get_client_version() if version.find("%s" % build) < 0: installer.uninstall() installer.install(job, build, pattern)
def setUpClass(cls): cls.start_time = strftime("%Y-%m-%dT%H:%M:%SZ", gmtime()) cls.hostname = PlatformHelper.get_hostname() cls.ip = PlatformHelper.get_ip_address() cls.tr_id = "Test_Run" cls.tr = TestRun(cls.tr_id, cls.start_time, qa_env="QA12", build=123, client="Windows", oem="MozyEnterprise", runby="*****@*****.**") cls.tr.save_to_elasticsearch() cls.tc_id = "KAL-A" cls.tc = TestCase(testrun=cls.tr.name, start_time=cls.start_time, hostname=cls.hostname, product="mozypro", ip=cls.ip, tags=["smoke", "activate", "windows", "keyless"], _id=cls.tc_id, build="111") result = cls.tc.write_to_elasticsearch()
def kill_fb(cls): """ kill file beat process :return: """ result = False if PlatformHelper.is_mac() or PlatformHelper.is_Linux(): cmd = "ps aux | grep 'filebeat -c' | grep -v 'grep'" output = CmdHelper.run(cmd) process_id = output.split()[1] kill_fb_cmd = "kill -9 %s" % process_id output = CmdHelper.run(kill_fb_cmd) if PlatformHelper.is_win(): cmd = "sc stop filebeat" output = CmdHelper.run(cmd) LogHelper.debug(output) if cls.is_fb_running(): LogHelper.error( "filebeat service CAN NOT be stopped successfully.") else: LogHelper.info("filebeat service is stopped") result = True return result
def start_fb(cls): if PlatformHelper.is_mac(): fb_binary_dir = os.path.join(cls.config_path, cls.package_name.split(".tar.gz")[0]) os.chdir(fb_binary_dir) cmd = "./filebeat -c filebeat.yml &" output = os.system(cmd) if PlatformHelper.is_Linux(): cmd = "service filebeat start" output = CmdHelper.run(cmd) if PlatformHelper.is_win(): cmd = "sc start filebeat" # This is to make sure filebeat service can be started after install time.sleep(1) output = CmdHelper.run(cmd) # service = "Get-WmiObject -ClassWin32_Service-Filter name='filebeat'" # service.StartService() # service.StopService() LogHelper.info("start filebeat result %s" % output) return output
def __init__(self, testrun=None, start_time=None, duration=None, tags=None, feature=None, summary=None, ip=None, hostname=None, logfile=None, product=None, _id=None, build=None, error=None, end_time=None, thost=None): self.testrun = testrun self.start_time = start_time or strftime("%Y-%m-%dT%H:%M:%SZ", gmtime()) self.duration = duration or 0 self.tags = tags or [] self.summary = summary self.feature = feature self.logfile = logfile self.machine_ip = ip or PlatformHelper.get_ip_address() self.machine_hostname = hostname or PlatformHelper.get_hostname() self.product = product self.test_result = "unknown" self.id = _id self.name = self.id self.build = build or "-999" self.error = error self.thost = thost self.end_time = end_time
def main(): # ----create log files for runner----- logger_path = ConfigAdapter.get_log_path() logfile = os.path.join(logger_path, PlatformHelper.get_ip_address(), "worker_logger.log") LogHelper.create_logger(logfile, fmt=PlatformHelper.get_ip_address() + " %(asctime)s %(levelname)s " + "worker" + " | %(message)s |") LogHelper.info("Worker %s" %(PlatformHelper.get_ip_address()))
def get_log_path(cls): if PlatformHelper.is_Linux(): logger_path = ConfigAdapter.log_path("LINUX") elif PlatformHelper.is_win(): logger_path = ConfigAdapter.log_path("WINDOWS") elif PlatformHelper.is_mac(): logger_path = ConfigAdapter.log_path("MAC") return logger_path
def create(): installer = None if PlatformHelper.is_debian_dist(): installer = lynx_installer_dpkg.LynxInstallerDpkg() if PlatformHelper.is_rpm_dist(): installer = lynx_installer_rpm.LynxInstallerRpm() return installer
def __create_default_logger(cls): if PlatformHelper.is_Linux(): logger_path = os.path.join("/", "tmp") elif PlatformHelper.is_win(): logger_path = os.path.join("c:", "tmp") elif PlatformHelper.is_mac(): logger_path = os.path.join("/", "tmp") log_filepath = os.path.join(logger_path, "automation.log") return cls.create_logger(log_filepath)
def get_started(file, start): """ Get the started line number from log. :param file: file :param start: time after start :return: int(line number) """ if PlatformHelper.is_win(): search_pattern = re.compile("^(.*\s.*)\s.*\.exe.*$", re.IGNORECASE) elif PlatformHelper.is_Linux(): search_pattern = re.compile("^(.*)\.\d{6}.*$", re.IGNORECASE) indices = FileHelper.find_text_indices(file, search_pattern) return indices[0]
def get_backup_start(file): search_pattern = "start backup" if PlatformHelper.is_win(): search_pattern = "Starting backup:" elif PlatformHelper.is_Linux(): search_pattern = "Executing : start" elif PlatformHelper.is_mac(): search_pattern = "Starting backup:" else: pass indices = FileHelper.find_text_indices(file, search_pattern) if indices: return indices[-1]
def step_impl(context): host = PlatformHelper.get_hostname().upper() context.kpi = KPI(testcase=context.tc.name, category="Web", start_time=strftime("%Y-%m-%dT%H:%M:%SZ", gmtime()), name="Frejya View Machine", result="Fail", hostname=context.tc.machine_hostname, ip=context.tc.machine_ip, env=context.env) result = FreyjaPage.click_machine(host) try: (result).should.be(True) except AssertionError: context.kpi.message = "Frejya fail to view machine." FreyjaPage.quit() else: context.kpi.result = "SUCCESS" finally: context.kpi.end_time = strftime("%Y-%m-%dT%H:%M:%SZ", gmtime()) context.kpi.write_to_elasticsearch(context.senddb) context.kpi = None context.execute_steps(unicode("When I wait 5 seconds"))
def public_lsh_restore_flow(self, include_all_version=False, wait_for_export_completed=True): time.sleep(2) browse_app = application.Application().connect(title=u'Browse For Folder', found_index=0) browse_folder = browse_app.Window_(title=u'Browse For Folder', found_index=0) if PlatformHelper().is_win7(): browse_folder.child_window(title="Tree View").GetItem([u'Desktop', u'Computer']).Click() time.sleep(1) browse_folder.child_window(title="Tree View").GetItem([u'Desktop', u'Computer', u'OSDisk (C:)']).Click() time.sleep(1) browse_folder.child_window(title="Tree View").GetItem( [u'Desktop', u'Computer', u'OSDisk (C:)', u'auto_restores']).Click() time.sleep(1) else: browse_folder.child_window(title="Tree View").GetItem([u'Desktop', u'This PC']).Click() time.sleep(1) browse_folder.child_window(title="Tree View").GetItem([u'Desktop', u'This PC', u'Windows (C:)']).Click() time.sleep(1) browse_folder.child_window(title="Tree View").GetItem( [u'Desktop', u'This PC', u'Windows (C:)', u'auto_restores']).Click() time.sleep(1) browse_folder.OK.Click() if include_all_version: self.restore_manager.CheckBox.toggle() self.restore_manager.child_window(title="Start", control_type="Button").click() if wait_for_export_completed is True: self.wait_for_export_completed()
def config_fb(cls, new_config={}): if PlatformHelper.is_mac(): cls.filebeat_config = os.path.join( cls.config_path, cls.package_name.split(".tar.gz")[0], "filebeat.yml") if PlatformHelper.is_Linux() or PlatformHelper.is_win(): cls.filebeat_config = os.path.join(cls.config_path, 'filebeat.yml') # yaml_file = open(cls.filebeat_config, 'r') # config = yaml.load(yaml_file) # print config with open(cls.filebeat_config, 'w') as f: config_dict = yaml.dump(new_config, f) return config_dict
def download_fb( cls, product=None, dry_run=False, ): current_platform = PlatformHelper.get_system() fb_site = "https://artifacts.elastic.co/downloads/beats/filebeat/" package_name = "" if current_platform == "Linux" and PlatformHelper.get_arch( ) == "deb-64": package_name = 'filebeat-5.2.0-amd64.deb' if current_platform == "Linux" and PlatformHelper.get_arch( ) == "deb-32": package_name = 'filebeat-5.2.0-i386.deb' if current_platform == "Windows": package_name = 'filebeat-5.2.0-windows-x86_64.zip' if current_platform == "Darwin": package_name = 'filebeat-5.2.0-darwin-x86_64.tar.gz' installer_dir = ConfigAdapter.get_installer_path(product) cls.package_name = package_name installer_path = os.path.join(installer_dir, package_name) url = fb_site + package_name LogHelper.info("installer_path is " + installer_path) LogHelper.info("package url is " + url) if not FileHelper.dir_exist(installer_dir): FileHelper.create_directory(installer_dir) if FileHelper.file_exist(installer_path): FileHelper.delete_file(installer_path) if not dry_run: downloaded_package = urllib2.urlopen(url) LogHelper.info("download result is %s" % downloaded_package) with open(installer_path, 'wb') as output: output.write(downloaded_package.read()) return installer_path
def before_scenario(context, scenario): testrun = RUNNER_CONFIG.get('TESTRUN') or 'testrun' context.log_starttime = strftime("%Y-%m-%dT%H:%M:%SZ", localtime()) start_time = strftime("%Y-%m-%dT%H:%M:%SZ", gmtime()) hostname = PlatformHelper.get_hostname() ip = PlatformHelper.get_ip_address() product = RUNNER_CONFIG.get("PRODUCT") build = RUNNER_CONFIG.get('BUILD') context.senddb = RUNNER_CONFIG.get('DATABASE_ENABLED') context.env = RUNNER_CONFIG.get('ENVIRONMENT') logger_path = os.path.join(ConfigAdapter.get_log_path(), testrun) tc_prefix = ConfigAdapter.get_testlink_prefix(product) match = filter(lambda x: x.find(tc_prefix) >= 0, scenario.tags) if len(match)>0: # differentiate scenarios in scenario outline if hasattr(context, 'active_outline') and type(context.active_outline) == behave.model.Row: suffix = match.pop() for example_key in context.active_outline: suffix += ".%s" % (example_key) tc_id = testrun + "_" + suffix else: tc_id = testrun + "_" + match.pop() else: #no test link project id foud tc_id = hashlib.md5(testrun + "_" + scenario.name.encode()).hexdigest() if not FileHelper.dir_exist(logger_path): FileHelper.create_directory(logger_path) logger_filename = "%s.log" % (tc_id) logfile = os.path.join(logger_path, logger_filename) client_ip = PlatformHelper.get_ip_address() LogHelper.create_logger(logfile, fmt=client_ip+" %(asctime)s %(levelname)s " + product+" | %(message)s |") tc = TestCase(testrun=testrun, start_time=start_time, hostname=hostname, product=product, ip=ip, summary=scenario.name, feature=scenario.filename, tags=scenario.tags, logfile=logfile, _id=tc_id, build=build) context.tc = tc LogHelper.info("test start: " + scenario.name)
def get_kpis(file, starttime=0): """ Get service KPIs :param file: Mozy backup log :param starttime: Only analyse latest log :return: Key Service KPI. e.g.["/client/sync": response time, "/batch": response time] """ kpi = None with open(file, "r") as f: for line_no, line in enumerate(f.readlines()): if PlatformHelper.is_win(): KPIHelper.get_windows_kpi(kpi, line, starttime) #datetime.datetime.strptime(starttime, '%d%b%Y %H:%M:%S').strftime('%Y-%m-%dT%H:%M:%SZ')) elif PlatformHelper.is_Linux(): KPIHelper.get_linux_kpi(kpi, line.strip(), starttime) elif PlatformHelper.is_mac(): KPIHelper.get_mac_kpis(file, line.strip())
def step_impl(context): from lib.platformhelper import PlatformHelper if PlatformHelper.is_win(): from apps.windows.windows_client import Windows_Client winclient = Windows_Client(RUNNER_CONFIG.get('OEM_CLIENT', "mozypro")) if winclient.gui.status_window.client_is_activated(): winclient.controller.set_unconfigured_status() winclient.controller.reset_reg(RUNNER_CONFIG.get( 'ENVIRONMENT', "QA12")) winclient.controller.restart_services(force=True)
def __init__(self, _id=None, testcase=None, category=None, name=None, apimethod=None, result=None, start_time=None, end_time=None, apicode=None, exterrorcode=None, message=None, throughput=None, duration=None, thost=None, ip=None, hostname=None, env=None, client=None): self.testcase = testcase or "Unknown" self.env = env or "PROD" self.client = client self.category = category self.name = name self.apimethod = apimethod self.apicode = apicode self.exterrorcode = exterrorcode self.result = result self.message = message self.start_time = start_time or strftime("%Y-%m-%dT%H:%M:%SZ", gmtime()) self.end_time = end_time or strftime("%Y-%m-%dT%H:%M:%SZ", gmtime()) self.machine_hostname = hostname or PlatformHelper.get_hostname() self.machine_ip = ip or PlatformHelper.get_ip_address() self.throughput = throughput self.duration = duration self.thost = thost self.id = _id
def capture_worker_info(sender, instance, **kwargs): """ dump worker infomation whenever worker is connected """ info = PlatformHelper.get_platform_info() info['worker_name']='{0}'.format(sender) filename = os.path.join(os.path.dirname(__file__), "worker_info.yaml") if FileHelper.file_exist(filename): LogHelper.info("delete config file %s" % filename) FileHelper.delete_file(filename) data = dict((k.upper(), v) for k, v in info.items()) with open(filename, 'w') as outfile: yaml.dump(data, outfile, default_flow_style=False)
def remove_tree(dir, topdown=True): for root, dirs, files in os.walk(dir, topdown=False): for name in files: filename = os.path.join(root, name) os.chmod(filename, stat.S_IWUSR) os.remove(filename) for name in dirs: try: os.removedirs(os.path.join(root, name)) except OSError as e: if PlatformHelper.is_Linux( ): # or PlatformHelper.is_mac(): CmdHelper.run('sudo rm -rf {path}'.format( path=os.path.join(root, name)))
def create_file(path, overwrite=True, size=1024, content="random"): """ method to create test files """ basedir = os.path.dirname(path) FileHelper.create_directory(basedir) if PlatformHelper.is_Linux(): # or PlatformHelper.is_mac(): cmd = 'sudo chmod -R 777 {path}'.format(path=basedir) CmdHelper.run(cmd) if content == "random": FileHelper._create_file_random(path, overwrite, size, 1024) else: FileHelper._create_file_pattern(path, overwrite, size, content)
def is_fb_installed(cls): result = False if PlatformHelper.is_Linux(): cmd = "service filebeat status" if PlatformHelper.is_mac(): cmd = "ps aux | grep 'filebeat -c' | grep -v 'grep'" if PlatformHelper.is_win(): cmd = "sc query filebeat" output = CmdHelper.run(cmd) LogHelper.debug(output) match = re.search(r"(FAILED)", output, re.IGNORECASE) if match: LogHelper.info("filebeat is not installed") else: result = True LogHelper.debug("filebeat is installed") return result
def close_key(registry_key): closed = False if registry_key: try: if PlatformHelper.is_64bit_machine(): win32api.RegCloseKey(registry_key) closed = True else: CloseKey(registry_key) closed = True except WindowsError as e: print str(e) LogHelper.error(str(e)) closed = False return closed
def get_reg_value(hkey, subkey, name): value = None registry_key = RegistryHelper.get_key(hkey, subkey, KEY_READ, False) if registry_key: try: if PlatformHelper.is_64bit_machine(): value, _ = win32api.RegQueryValueEx(registry_key, name) else: value, _ = QueryValueEx(registry_key, name) except WindowsError as e: LogHelper.error(str(e)) value = None finally: RegistryHelper.close_key(registry_key) return value
def create_directory(path, recursive=True): """ :param path: directory path :return: path """ path = os.path.abspath(path) sub_path = os.path.dirname(path) if not os.path.exists(sub_path) and recursive: FileHelper.create_directory(sub_path) if not os.path.exists(path): try: os.mkdir(path) except OSError: if PlatformHelper.is_Linux(): # or PlatformHelper.is_mac(): cmd = 'sudo mkdir -p {path}'.format(path=path) CmdHelper.run(cmd) return path
def set_reg_value(hkey, subkey, name, value, type): result = False registry_key = RegistryHelper.get_key(hkey, subkey, KEY_WRITE, True) if registry_key: try: if PlatformHelper.is_64bit_machine(): win32api.RegSetValueEx(registry_key, name, 0, type, value) result = True else: SetValueEx(registry_key, name, 0, type, value) result = True except WindowsError as e: LogHelper.error(str(e)) result = False finally: RegistryHelper.close_key(registry_key) return result
def delete_key(hkey, subkey, name): result = None registry_key = RegistryHelper.get_key(hkey, subkey, KEY_READ, False) print registry_key if registry_key: try: if PlatformHelper.is_64bit_machine(): result = win32api.RegDeleteKey(registry_key, name) else: result = DeleteKey(registry_key, name) print result except WindowsError as e: print str(e) LogHelper.error(str(e)) result = None finally: RegistryHelper.close_key(registry_key) return result
def install_fb(cls): installer = cls.download_fb() LogHelper.info("start to install") if PlatformHelper.is_Linux() and PlatformHelper.get_arch() == "deb-64": install_cmd = "dpkg -i %s" % installer cls.__set_fb_path("/etc/filebeat/") if PlatformHelper.is_Linux() and PlatformHelper.get_arch() == "deb-32": install_cmd = "rpm -vi %s" % installer cls.__set_fb_path("/etc/filebeat/") if PlatformHelper.is_win(): import zipfile win_fb_dir = "C:\\filebeat" target = os.path.join(win_fb_dir, "filebeat-5.2.0-windows-x86_64") installcommnd = os.path.join(target, "install-service-filebeat.ps1") zip_ref = zipfile.ZipFile(installer, 'r') zip_ref.extractall(win_fb_dir) zip_ref.close() # FileHelper.rename() install_cmd = "powershell.exe %s" % (installcommnd) cls.__set_fb_path(target) cmd_output = CmdHelper.runas_admin(install_cmd) return cmd_output # # TODO: Unzip to start" service # install_cmd = "to be implemented" # win_fb_dir = "C:\\filebeat" # if not FileHelper.dir_exist(win_fb_dir): # FileHelper.create_directory(win_fb_dir) # cls.__set_fb_path(win_fb_dir) # install_cmd = "unzip %s -C %s" % (installer, win_fb_dir) if PlatformHelper.is_mac(): mac_fb_dir = "/filebeat" if not FileHelper.dir_exist(mac_fb_dir): FileHelper.create_directory(mac_fb_dir) cls.__set_fb_path(mac_fb_dir) install_cmd = "tar xzvf %s -C %s" % (installer, mac_fb_dir) cmd_output = CmdHelper.run(install_cmd) LogHelper.info(cmd_output)