Best Python code snippet using autotest_python
monitors_util_unittest.py
Source:monitors_util_unittest.py
...7import textwrap8import time9import unittest10import monitors_util11def InlineStringIO(text):12 return StringIO.StringIO(textwrap.dedent(text).strip())13class WriteLoglineTestCase(unittest.TestCase):14 def setUp(self):15 self.time_tuple = (2008, 10, 31, 18, 58, 17, 4, 305, 1)16 self.format = '[%Y-%m-%d %H:%M:%S]'17 self.formatted_time_tuple = '[2008-10-31 18:58:17]'18 self.msg = 'testing testing'19 # Stub out time.localtime()20 self.orig_localtime = time.localtime21 time.localtime = lambda: self.time_tuple22 def tearDown(self):23 time.localtime = self.orig_localtime24 def test_prepend_timestamp(self):25 timestamped = monitors_util.prepend_timestamp(26 self.msg, self.format)27 self.assertEquals(28 '%s\t%s' % (self.formatted_time_tuple, self.msg), timestamped)29 def test_write_logline_with_timestamp(self):30 logfile = StringIO.StringIO()31 monitors_util.write_logline(logfile, self.msg, self.format)32 logfile.seek(0)33 written = logfile.read()34 self.assertEquals(35 '%s\t%s\n' % (self.formatted_time_tuple, self.msg), written)36 def test_write_logline_without_timestamp(self):37 logfile = StringIO.StringIO()38 monitors_util.write_logline(logfile, self.msg)39 logfile.seek(0)40 written = logfile.read()41 self.assertEquals(42 '%s\n' % self.msg, written)43class AlertHooksTestCase(unittest.TestCase):44 def setUp(self):45 self.msg_template = 'alert yay %s haha %s'46 self.params = ('foo', 'bar')47 self.epoch_seconds = 1225501829.930061148 # Stub out time.time49 self.orig_time = time.time50 time.time = lambda: self.epoch_seconds51 def tearDown(self):52 time.time = self.orig_time53 def test_make_alert(self):54 warnfile = StringIO.StringIO()55 alert = monitors_util.make_alert(warnfile, "MSGTYPE",56 self.msg_template)57 alert(*self.params)58 warnfile.seek(0)59 written = warnfile.read()60 ts = str(int(self.epoch_seconds))61 expected = '%s\tMSGTYPE\t%s\n' % (ts, self.msg_template % self.params)62 self.assertEquals(expected, written)63 def test_build_alert_hooks(self):64 warnfile = StringIO.StringIO()65 patterns_file = InlineStringIO("""66 BUG67 ^.*Kernel panic ?(.*)68 machine panic'd (%s)69 BUG70 ^.*Oops ?(.*)71 machine Oops'd (%s)72 """)73 hooks = monitors_util.build_alert_hooks(patterns_file, warnfile)74 self.assertEquals(len(hooks), 2)75class ProcessInputTestCase(unittest.TestCase):76 def test_process_input_simple(self):77 input = InlineStringIO("""78 woo yay79 this is a line80 booya81 """)82 logfile = StringIO.StringIO()83 monitors_util.process_input(input, logfile)84 input.seek(0)85 logfile.seek(0)86 self.assertEquals(87 '%s\n%s\n' % (input.read(), monitors_util.TERM_MSG),88 logfile.read())89class FollowFilesTestCase(unittest.TestCase):90 def setUp(self):91 self.logfile_dirpath = tempfile.mkdtemp()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!