def __init__(self, module_id, module_version, ctr=None, abort_on_fail=None): self.module_id = module_id self.module_version = module_version if ctr is None: self.test_ctr = 0 else: self.test_ctr = ctr if abort_on_fail is None: self.abort_on_fail = False else: self.abort_on_fail = abort_on_fail # Test if running in travis-ci emulator (DEV_ENVIRONMENT) if 'DEV_ENVIRONMENT' in os.environ \ and os.environ['DEV_ENVIRONMENT'].find(self.module_id): LOCAL_ECHO = False else: LOCAL_ECHO = True tlog_fn = './' + self.module_id + "_test.log" os0.set_tlog_file(tlog_fn, echo=LOCAL_ECHO, dir4debug=True) title = "%s regression test. Version: %s" % (self.module_id, self.module_version) os0.wlog(title)
def init_logger(ctx): if ctx['quiet_mode']: os0.set_tlog_file(ctx['logfn'], echo=False) else: os0.set_tlog_file(ctx['logfn'], echo=True)
def build_prgm(self, prm): if not prm.get('module'): return 1 conf_obj = prm['_conf_obj'] if prm['template'] and conf_obj.has_section(prm['template']): prm['tpl_path'] = './templates/' + prm['template'] prm['tgt_path'] = './building' s = prm['template'] p = 'config_fn' if conf_obj.has_option(s, p): tpl_config_fn = conf_obj.get(s, p) else: tpl_config_fn = prm['template'] + ".conf" tpl_config_ffn = prm['tpl_path'] + '/' + tpl_config_fn if os.path.isfile(tpl_config_ffn): conf_obj = ConfigParser.SafeConfigParser(default_conf()) conf_fns = prm['conf_fn'] if isinstance(prm['conf_fn'], list): conf_fns = prm['conf_fn'] conf_fns.append(tpl_config_ffn) else: conf_fns = [prm['conf_fn'], tpl_config_ffn] conf_obj.read(conf_fns) prm['logfile'] = os.path.abspath(prm['tpl_path'] + '/_' + prm['module'] + '.log') os0.set_tlog_file(prm['logfile'], echo=True) return self.build_tree(prm) else: print "Invalid template", prm['template'] return 1
def test_02(self, ctx): fzero = False fexts = False if not ctx.get('dry_run', False): title = TITLE os0.trace_debug("- Inititializing ...") os0.set_debug_mode(False) os0.trace_debug("TEST FAILED!!!!") os0.set_debug_mode(True) os0.trace_debug("- Inititializing (2) ...") os0.wlog(title) os0.wlog("- os0 version:", os0.version) os0.wlog("- platform:", _platform) os0.set_tlog_file("os0_test.log", echo=ctx['opt_echo']) try: tlog_fd = open(os0.tlog_fn, 'r') fexts = True tlog_fd.seek(0, os.SEEK_END) if tlog_fd.tell() == 0: fzero = True tlog_fd.close() except: pass sts = self.Z.test_result(ctx, "Check for empty tracelog", True, fexts) if sts == TEST_SUCCESS: sts = self.Z.test_result(ctx, "Check for empty tracelog", True, fzero) return sts
def test_01(self, ctx): self.SetUp(ctx) fzero = False fexts = False if not ctx.get('dry_run', False): os0.set_tlog_file(FLOGTMP) tlog_pathname = os.path.join(os.environ['HOME'], FLOGTMP) sts = self.Z.test_result(ctx, "set logfile", tlog_pathname, os0.tlog_fn) if not ctx.get('dry_run', False): tlog_fd = open(os0.tlog_fn, 'w') tlog_fd.close() os0.set_tlog_file(FLOGTMP, new=True, echo=ctx['opt_echo']) try: tlog_fd = open(os0.tlog_fn, 'r') fexts = True tlog_fd.seek(0, os.SEEK_END) if tlog_fd.tell() == 0: fzero = True tlog_fd.close() except: pass sts = self.Z.test_result(ctx, "new logfile (1)", True, fexts) sts = self.Z.test_result(ctx, "new logfile (2)", True, fzero) if os.path.isfile(tlog_pathname): os.remove(tlog_pathname) tlog_pathname = os.path.join('.', FLOGTMP) fzero = False fexts = False if not ctx.get('dry_run', False): os0.set_tlog_file(tlog_pathname, new=True, echo=ctx['opt_echo']) try: tlog_fd = open(os0.tlog_fn, 'r') fexts = True tlog_fd.seek(0, os.SEEK_END) if tlog_fd.tell() == 0: fzero = True tlog_fd.close() except: pass sts = self.Z.test_result(ctx, "new logfile (3)", True, fexts) sts = self.Z.test_result(ctx, "new logfile (4)", True, fzero) if os.path.isfile(tlog_pathname): os.remove(tlog_pathname) tlog_pathname = os.path.join(self.Z.test_dir, FLOGTMP) fzero = False fexts = False if not ctx.get('dry_run', False): os0.set_tlog_file(ctx['logfn'], new=ctx['opt_new'], echo=ctx['opt_echo']) if ctx.get('dry_run', False): os0.wlog("Since now, test messages are store in", os0.tlog_fn) return sts
def main(self): """Unit test main.""" # Check for target software version required_version = os.environ.get(self.module_id.upper() + '_VERSION') if not required_version: required_version = REQ_TEST_VERSION pkg_version = build.version() if required_version and pkg_version.find(required_version) < 0: print "Test not executable: invalid version!" print "Required version:", required_version print "Package version:", pkg_version return TEST_FAILED # Need debug mode to avoid security fault in Linux os0.set_debug_mode(True) title = "%s regression test. Version: %s" % (self.module_id, REQ_TEST_VERSION) # Remove test log file if executed previous crashed test tlog_fn = self.module_id + "_test.log" os0.set_tlog_file(tlog_fn) tlog_pathname = os0.tlog_fn # Set no file log os0.set_tlog_file('', echo=True) if os.path.isfile(tlog_pathname): os.remove(tlog_pathname) # Test if running in travis-ci emulator (DEV_ENVIRONMENT) if 'DEV_ENVIRONMENT' in os.environ \ and os.environ['DEV_ENVIRONMENT'].find(self.module_id): LOCAL_ECHO = False else: LOCAL_ECHO = True tlog_fn = './' + self.module_id + "_test.log" os0.set_tlog_file(tlog_fn, new=True, echo=LOCAL_ECHO) os0.wlog(title) # Test execution body test_num = 0 sts = 0 for i in range(MAX_TEST_NUM): tname = "test_{0:02}".format(test_num) if hasattr(self, tname): sts = getattr(self, tname)(test_num) if sts: break test_num += 1 # Result if sts == 0: os0.wlog("Ran {0} {1} tests".format(self.test_ctr, MODULE_ID)) else: os0.wlog("****** Test {0} failed ******".format(MODULE_ID)) return sts
def __init__(self, ctx): self.hostname = ctx['hostname'] os0.set_debug_mode(ctx['dbg_mode']) self.prodhost = ctx['production_host'] self.devhost = ctx['development_host'] self.mirrorhost = ctx['mirror_host'] self.pgdir = ctx['pg_dir'] self.mysqldir = ctx['mysql_dir'] homedir = os.path.expanduser("~") self.ftp_cfn = homedir + "/" + ctx['ftp_script'] self.flist = homedir + "/" + ctx['list_file'] os0.set_tlog_file(ctx['logfn']) # Log begin execution os0.wlog("Restore configuration files", __version__) # Simulate backup self.dry_run = ctx['dry_run'] if ctx['saveset'] == "bckdb" or \ ctx['saveset'] == "bckconf" or \ ctx['saveset'] == "bckwww": if self.hostname == self.prodhost: os0.wlog("Running on production machine") if ctx['alt']: self.bck_host = self.mirrorhost self.fconf = homedir + "/" + \ ctx['no_translation'] else: self.bck_host = self.devhost self.fconf = homedir + "/" + \ ctx['data_translation'] elif self.hostname == self.mirrorhost: os0.wlog("Running on mirror machine") if ctx['alt']: self.bck_host = self.prodhost self.fconf = homedir + "/" + \ ctx['no_translation'] else: self.bck_host = self.devhost self.fconf = homedir + "/" + \ ctx['data_translation'] elif self.hostname == self.devhost: os0.wlog("This command cannot run on development machine") if not ctx['dry_run']: raise Exception("Command aborted due invalid machine") else: os0.wlog("Unknown machine - Command aborted") if not ctx['dry_run']: raise Exception("Command aborted due unknown machine") elif ctx['saveset'] == "restdb" or \ ctx['saveset'] == "restconf" or \ ctx['saveset'] == "restwww": if self.hostname == self.prodhost: os0.wlog("This command cannot run on production machine") if not ctx['dry_run']: raise Exception("Command aborted due production machine") elif self.hostname == self.mirrorhost: os0.wlog("Running on mirror machine") if ctx['alt']: self.bck_host = self.prodhost self.fconf = homedir + "/" + \ ctx['no_translation'] else: self.bck_host = self.devhost self.fconf = homedir + "/" + \ ctx['data_translation'] elif self.hostname == self.devhost: os0.wlog("Running on development machine") if ctx['alt']: self.bck_host = self.mirrorhost self.fconf = homedir + "/" + \ ctx['data_translation'] else: self.bck_host = self.devhost self.fconf = homedir + "/" + \ ctx['data_translation'] else: os0.wlog("Unknown machine - Command aborted") if not ctx['dry_run']: raise Exception("Command aborted due unknown machine") # May be (.gz or .bz2) self.tar_ext = ctx['tar_ext'] # May be (z or j) self.tar_opt = ctx['tar_opt'] # May be (null or .sql) self.pre_ext = ctx['pre_ext'] # May be (null or .sql) self.sql_ext = ctx['sql_ext'] self.psql_uu = ctx['pgsql_user'] self.psql_db = ctx['pgsql_def_db'] self.mysql_uu = ctx['mysql_user'] self.mysql_db = ctx['mysql_def_db'] self.pid = os.getpid() self.ftp_rootdir = "" self.ftp_dir = "" self.dbtype = "" self.create_dict()
def main(): """Tool main""" sts = WOW_SUCCESS # pdb.set_trace() prm = parse_args(sys.argv[1:]) dbg_mode = prm['dbg_mode'] conf_fn = prm['conf_fn'] mute = prm['mute'] list_mode = prm['list'] opt_obj = prm['_opt_obj'] Z = ZarLib(dbg_mode) if dbg_mode: print __version__ if prm['xtall']: sts = Z.install(prm) sys.exit(sts) if not os.path.isfile(conf_fn): print "Configuration file {0} not found!".format(conf_fn) f_alrdy_run = Z.check_if_running() cfg = Z.get_conf(conf_fn) s = "Environment" os0.set_tlog_file(cfg.get(s, "tracelog")) if prm['simulate']: opt = "in simulating mode" else: opt = "" # Log begin execution os0.wlog(Z.pid, "ZAR {0} beginning".format(__version__), opt) if f_alrdy_run: os0.wlog(Z.pid, "Another instance is running!!") if not mute: print "***Another instance is running!!!" raise SystemExit saveset_list = cfg.get(s, "saveset_list") if saveset_list == "": os0.wlog( Z.pid, "No saveset defined in {0} configuration file!!" .format(conf_fn)) if not mute: print "***No saveset defined in {0} configuration file!!!"\ .format(conf_fn) raise SystemExit saveset_list = saveset_list.split(',') if hasattr(opt_obj, "saveset"): sel_saveset = opt_obj.saveset else: sel_saveset = saveset_list for sset in saveset_list: s = sset.strip() prm['src_host'] = cfg.get(s, "source_host") prm['tgt_host'] = cfg.get(s, "target_host") prm['specimen'] = cfg.get(s, "specimen") if list_mode: if sset not in sel_saveset: opt = " (not selected)" else: opt = "" if Z.hostname == prm['src_host'] \ or Z.hostname == "localhost": print "Saveset {0}{1}: backup to {2}"\ .format(sset, opt, prm['tgt_host']) elif Z.hostname == prm['tgt_host'] \ or Z.hostname == "localhost": print "Saveset {0}{1}: restore from {2}"\ .format(sset, opt, prm['src_host']) else: print "Invalid saveset {0}: source={1}, target={2}"\ .format(sset, prm['src_host'], prm['tgt_host']) else: BM = Backup_Mirror(cfg, sset) # RI = Restore_Image(cfg, sset) if sset not in sel_saveset: if dbg_mode: print "Saveset {0} not selected".format(sset) continue elif Z.hostname == prm['src_host'] \ or Z.hostname == "localhost": os0.wlog( Z.pid, "Saveset [{0}]: backup to {1}" .format(sset, prm['tgt_host'])) if not mute: print "Saveset [{0}]: backup to {1}"\ .format(sset, prm['tgt_host']) if prm['specimen'] == "file" \ or prm['specimen'] == "dir": BM.Backup_files(cfg, sset, prm) elif Z.hostname == prm['tgt_host'] \ or Z.hostname == "localhost": os0.wlog( Z.pid, "Saveset [{0}]: restore from {1}" .format(sset, prm['src_host'])) if not mute: print "Saveset [{0}] restore from {1}"\ .format(sset, prm['src_host']) else: os0.wlog(Z.pid, "Invalid saveset [{0}]: source={1}, target={2}" .format(sset, prm['src_host'], prm['tgt_host'])) print "Invalid saveset [{0}]: source={1}, target={2}"\ .format(sset, prm['src_host'], prm['tgt_host']) os0.wlog(Z.pid, "ZAR terminated!") return sts