def test_dlog(self): """ Test method dlog on daemon object """ lfname = self.tmpdir('daemon.dlog.log') lf = CrawlConfig.log(logpath=lfname) a = daemon.Daemon(self.tmpdir("daemon_pid"), logger=lf) logmsg = "testing the dlog method of %s" % a a.dlog(logmsg) self.assertTrue( logmsg in util.contents(lfname), "Expected '%s' in '%s'" % (logmsg, util.line_quote(util.contents(lfname))))
def test_dlog(self): """ Test method dlog on daemon object """ lfname = self.tmpdir('daemon.dlog.log') lf = CrawlConfig.log(logpath=lfname) a = daemon.Daemon(self.tmpdir("daemon_pid"), logger=lf) logmsg = "testing the dlog method of %s" % a a.dlog(logmsg) self.assertTrue(logmsg in util.contents(lfname), "Expected '%s' in '%s'" % (logmsg, util.line_quote(util.contents(lfname))))
def test_alert_shell_nospec(self): """ Generate a shell alert and verify that it ran. With no '%s' in the shell alert string, no message should be offered for formatting. """ self.dbgfunc() logfile = self.tmpdir('alert_shell.log') outfile = self.tmpdir('alert_shell.out') runfile = self.tmpdir('runme') f = open(runfile, 'w') f.write("#!/bin/bash\n") f.write("echo \"ALERT: $*\" > %s\n" % outfile) f.close() os.chmod(runfile, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH) cfg = CrawlConfig.CrawlConfig() cfg.add_section('crawler') cfg.add_section('AlertTest') cfg.add_section('alert_section') cfg.set('crawler', 'logpath', logfile) cfg.set('AlertTest', 'alerts', 'alert_section') cfg.set('alert_section', 'shell', runfile) CrawlConfig.log(logpath=logfile, close=True) x = Alert.Alert(caller='AlertTest', msg='this is a test message', cfg=cfg) expected = "ran: '%s'" % runfile self.expected_in(expected, util.contents(logfile)) self.assertPathPresent(outfile)
def muh_prep(request, tmpdir): """ Set up maybe_update_hsi (muh) tests """ rf = request.function rf.hsihome = '/sw/sources/hpss/bin' rf.bin = tmpdir.mkdir('bin') rf.file = rf.bin.join('hsi').ensure() rf.file.chmod(0755) if 'yes' in rf.func_name: # we want maybe_update to do the update rf.file.write('BINARYVERSION=5.0.2.1\n' + 'echo "not changed"\n') elif 'cant' in rf.func_name: # we want maybe_update be unable to do the update rf.file.write('BINARYVERSION=5.0.2.1\n' + 'echo "not changed"\n') rf.file.chmod(0555) else: # copy hsi from hsihome and edit it hsi = U.pathjoin(rf.hsihome, 'hsi') q = U.contents(hsi).split("\n") z = U.grep("${EXECUTABLE}", q, regex=False, index=True) q[z[0]] = "exec " + q[z[0]] rf.file.write("\n".join(q) + "\n") rf.then = time.time() - 30 rf.file.setmtime(rf.then) pass
def test_alert_email_mtcaller(self): """ Generate an e-mail alert and verify that it was sent (this is where we use 'monkey patching'). For this case, caller is ''. """ self.dbgfunc() fakesmtp.inbox = [] logfile = self.tmpdir('alert_email.log') targets = "[email protected], [email protected], [email protected]" payload = 'this is an e-mail alert' sender = 'hpssic@' + util.hostname(long=True) cfg = CrawlConfig.CrawlConfig() cfg.add_section('crawler') cfg.add_section('alerts') cfg.set('crawler', 'logpath', logfile) cfg.set('alerts', 'email', targets) CrawlConfig.log(logpath=logfile, close=True) x = Alert.Alert(caller='', msg=payload, cfg=cfg) m = fakesmtp.inbox[0] self.expected(targets, ', '.join(m.to_address)) self.expected(m.from_address, sender) self.expected_in('sent mail to', util.contents(logfile)) self.expected_in(payload, m.fullmessage)
def test_alert_shell_nospec(self): """ Generate a shell alert and verify that it ran. With no '%s' in the shell alert string, no message should be offered for formatting. """ self.dbgfunc() logfile = self.tmpdir('alert_shell.log') outfile = self.tmpdir('alert_shell.out') runfile = self.tmpdir('runme') f = open(runfile, 'w') f.write("#!/bin/bash\n") f.write("echo \"ALERT: $*\" > %s\n" % outfile) f.close() os.chmod( runfile, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH) cfg = CrawlConfig.CrawlConfig() cfg.add_section('crawler') cfg.add_section('AlertTest') cfg.add_section('alert_section') cfg.set('crawler', 'logpath', logfile) cfg.set('AlertTest', 'alerts', 'alert_section') cfg.set('alert_section', 'shell', runfile) CrawlConfig.log(logpath=logfile, close=True) x = Alert.Alert(caller='AlertTest', msg='this is a test message', cfg=cfg) expected = "ran: '%s'" % runfile self.expected_in(expected, util.contents(logfile)) self.assertPathPresent(outfile)
def test_content_list(self): """ contents() reads and returns the contents of a file as a list """ self.dbgfunc() filename = sys.modules['hpssic.util'].__file__.replace(".pyc", ".py") x = util.contents(filename, string=False) self.expected(type(x), list) self.expected_in('def contents\(', x)
def test_content_str(self): """ contents() is supposed to read and return the contents of a file as a string. """ self.dbgfunc() filename = sys.modules['hpssic.util'].__file__.replace(".pyc", ".py") x = util.contents(filename) self.expected(str, type(x)) self.expected_in('def contents\(', x)
def test_alert_log(self): """ Generate a log alert and verify that the message was written to the correct log file. """ self.dbgfunc() logfile = self.tmpdir('alert_log.log') cfg = CrawlConfig.CrawlConfig() cfg.add_section('crawler') cfg.add_section('AlertTest') cfg.add_section('alert_section') cfg.set('crawler', 'logpath', logfile) cfg.set('AlertTest', 'alerts', 'alert_section') cfg.set('alert_section', 'log', "%s") CrawlConfig.log(logpath=logfile, close=True) x = Alert.Alert(caller='AlertTest', msg='this is a test message', cfg=cfg) self.expected_in('this is a test message', util.contents(logfile))
def test_alert_use_other(self): """ A use directive sends us to another config section where we generate a log alert and verify that the message was written to the correct log file. """ self.dbgfunc() logfile = self.tmpdir('alert_use.log') cfg = CrawlConfig.CrawlConfig() cfg.add_section('crawler') cfg.add_section('AlertTest') cfg.add_section('alert_section') cfg.add_section('other_section') cfg.set('crawler', 'logpath', logfile) cfg.set('AlertTest', 'alerts', 'alert_section') cfg.set('alert_section', 'use', "other_section") cfg.set('other_section', 'log', "%s") CrawlConfig.log(logpath=logfile, close=True) payload = 'this is a test message from %s' % util.my_name() x = Alert.Alert(caller='AlertTest', msg=payload, cfg=cfg) self.expected_in(payload, util.contents(logfile))
def test_alert_email_defcfg(self): """ Generate an e-mail alert using the default config and verify that it was sent (this is where we use 'monkey patching'). """ self.dbgfunc() fakesmtp.inbox = [] CrawlConfig.add_config(close=True) # with U.tmpenv('CRAWL_CONF', 'hpssic_test.cfg'): with U.tmpenv('CRAWL_CONF', None): logfile = self.tmpdir('alert_email.log') targets = "[email protected], [email protected]" payload = 'this is an e-mail alert' sender = 'hpssic@' + util.hostname(long=True) CrawlConfig.log(logpath=logfile, close=True) x = Alert.Alert(caller='cv', msg=payload) m = fakesmtp.inbox[0] self.expected(', '.join(m.to_address), targets) self.expected(m.from_address, sender) self.expected_in('sent mail to', util.contents(logfile)) self.expected_in(payload, m.fullmessage)