def test_tailerfrommonitor(self):
     sys.stdout = Writer()
     class OptionsMock(object):
         def __init__(self):
             pass
         def __getattr__(self, name):
             if name == 'remote':
                 return False
             elif name == 'configfile':
                 return 'anythingyouwant'
             return False
     options_mock = OptionsMock()
     log4tailer.initialize(options_mock)
     args_mock = [self.log_name]
     interruptor = Interruptor()
     interruptor.start()
     log4tailer.monitor(options_mock, args_mock)
     interruptor.join()
     finish_trace = re.compile(r'because colors are fun')
     found = False
     for num, line in enumerate(sys.stdout.captured):
         if finish_trace.search(line):
             found = True
     if not found:
         self.fail()
 def test_options_with_screenshot(self):
     sys.stdout = Writer()
     fh  = open(ACONFIG, 'w')
     fh.write('screenshot = ' + self.apicture + '\n')
     fh.close()
     class OptionWithScreenshot(object):
         def __init__(self):
             pass
         def __getattr__(self, method):
             if method == 'configfile':
                 return ACONFIG
             elif method == 'screenshot':
                 return True
             return False
     options_mock = OptionWithScreenshot()
     log4tailer.initialize(options_mock)
     args_mock = [self.log_name]
     interruptor = Interruptor()
     interruptor.with_fatal_logtrace()
     interruptor.start()
     log4tailer.monitor(options_mock, args_mock)
     #FIXME when executed first time ever this test normally fails, but not
     #next times. This is due to it needs to shell out the import linux
     #command line tool.
     time.sleep(0.2)
     interruptor.join()
     finish_trace = re.compile(r'because colors are fun')
     found = False
     for num, line in enumerate(sys.stdout.captured):
         if finish_trace.search(line):
             found = True
     if not found:
         self.fail()
     self.assertTrue(os.path.exists(self.apicture))
 def test_endtoend_withconfig(self):
     sys.stdout = Writer()
     fh = open(ACONFIG, 'w')
     fh.write('info = green, on_blue\n')
     fh.write('debug = yellow\n')
     fh.close()
     class OptionsMockWithConfig(object):
         def __init__(self):
             pass
         def __getattr__(self, method):
             if method == 'configfile':
                 return ACONFIG
             return False
     optionsmock_withconfig = OptionsMockWithConfig()        
     log4tailer.initialize(optionsmock_withconfig)
     args_mock = [self.log_name]
     interruptor = Interruptor()
     interruptor.start()
     log4tailer.monitor(optionsmock_withconfig, args_mock)
     interruptor.join()
     finish_trace = re.compile(r'because colors are fun')
     found = False
     for num, line in enumerate(sys.stdout.captured):
         if finish_trace.search(line):
             found = True
     if not found:
         self.fail()
 def test_corner_mark_setup(self):
     options_mock = self.mocker.mock()
     options_mock.cornermark
     self.mocker.count(1, 2)
     self.mocker.result(True)
     params = {'configfile' : 'anythingyouwant', 
             'version' : False,
             'filter' : False,
             'tailnlines' : False,
             'target' : False,
             'executable' : False,
             'pause' : False,
             'throttle' : False,
             'silence' : False, 
             'mail' : False,
             'inactivity' : False,
             'nomailsilence' : False,
             'post' : False,
             'screenshot' : False}
     self.__options_mocker_generator(options_mock, params)
     self.mocker.replay()
     log4tailer.initialize(options_mock)
     actions = log4tailer.defaults['actions']
     self.assertEquals(2, len(actions))
     self.assertTrue(isinstance(actions[0], notifications.Print))
     self.assertTrue(isinstance(actions[1], notifications.CornerMark))
    def test_demonizedoptionsilence(self):
        sys.stdout = Writer()
        sys.stderr = Writer()
        sys.stdin = Reader()
        fh = open(ACONFIG, 'w')
        fh.write('info = green, on_blue\n')
        fh.write('debug = yellow\n')
        fh.close()
        class OptionsMock2(object):
            def __init__(self):
                pass
            def __getattr__(self, method):
                if method == 'silence':
                    return True
                if method == 'configfile':
                    return ACONFIG
                return False
        class ActionMock(object):
            def __init__(self):
                pass
            def notify(self, message, log):
                pass
        def setup_mail(properties):
            return ActionMock()

        def chdir(directory):
            return True
        def umask(integer):
            return True
        def setsid():
            return True
        def dup2(one, two):
            return True
        def fork():
            return -1
        log4tailer.setup_mail = setup_mail
        options_mock = OptionsMock2()
        args = [self.log_name]
        log4tailer.initialize(options_mock)
        interruptor = Interruptor()
        os.fork = fork
        os.chdir = chdir
        os.setsid = setsid
        os.dup2 = dup2
        os.umask = umask
        interruptor.start()
        log4tailer.monitor(options_mock, args)
        interruptor.join()
 def test_options_withremotewrongconfig(self):
     fh = open(ACONFIG, 'w')
     fh.write('anything = anything\n')
     fh.close()
     class OptionWithRemoteAndConfig(object):
         def __init__(self):
             pass
         def __getattr__(self, method):
             if method == 'configfile':
                 return ACONFIG
             elif method == 'remote':
                 return True
             return False
     options_mock = OptionWithRemoteAndConfig()
     log4tailer.initialize(options_mock)
     args = []
     log4tailer.monitor(options_mock, args)
 def test_options_with_poster_notification(self):
     fh = open(ACONFIG, 'w')
     fh.write('server_url = localhost\n')
     fh.write('server_port = 8000\n')
     fh.write('server_service_uri = /\n')
     fh.close()
     class OptionWithPostandConfig(object):
         def __init__(self):
             pass
         def __getattr__(self, method):
             if method == 'configfile':
                 return ACONFIG
             elif method == 'post':
                 return True
             return False
     options_mock = OptionWithPostandConfig()
     log4tailer.initialize(options_mock)
     self.assertTrue(log4tailer.defaults['post'])
 def test_tailLastNlines(self):
     sys.stdout = Writer()
     fh = open(self.log_name, 'w')
     onelogtrace = 'this is an info log trace'
     anotherlogtrace = 'this is a debug log trace'
     fh.write(onelogtrace + '\n')
     fh.write(anotherlogtrace + '\n')
     fh.close()
     class OptionsMockWithNlines(object):
         def __init__(self):
             pass
         def __getattr__(self, method):
             if method == 'tailnlines':
                 return '50'
             return False
     options_mock_nlines = OptionsMockWithNlines()
     args = [self.log_name]
     log4tailer.initialize(options_mock_nlines)
     log4tailer.monitor(options_mock_nlines, args)
 def test_monitor_inactivity_withmail(self):
     properties_mock = self.mocker.mock()
     properties_mock.get_value('inactivitynotification')
     self.mocker.result('mail')
     properties_mock.get_keys()
     self.mocker.result([])
     defaults = getDefaults()
     defaults['properties'] = properties_mock
     log4tailer.defaults = defaults
     utils_mock = self.mocker.replace('log4tailer.utils.setup_mail')
     utils_mock(ANY)
     self.mocker.result(True)
     self.mocker.replay()
     log4tailer.initialize(self.OptionsMock())
     actions = log4tailer.defaults['actions']
     self.assertEquals(2, len(actions))
     self.assertTrue(isinstance(actions[0], notifications.Print))
     self.assertTrue(isinstance(actions[1], notifications.Inactivity))
     self.assertFalse(isinstance(actions[0], notifications.CornerMark))
 def test_options_withremotegoodconfignoconnection(self):
     getpass_mock = self.mocker.replace('getpass.getpass')
     getpass_mock()
     self.mocker.result('anypass')
     fh = open(ACONFIG, 'w')
     fh.write('sshhostnames = 127.999.9.9\n')
     fh.write('127.999.9.9 = /var/log/error.log\n')
     fh.close()
     class OptionWithRemoteAndConfig(object):
         def __init__(self):
             pass
         def __getattr__(self, method):
             if method == 'configfile':
                 return ACONFIG
             elif method == 'remote':
                 return True
             return False
     options_mock = OptionWithRemoteAndConfig()
     log4tailer.initialize(options_mock)
     args = []
     self.mocker.replay()
     log4tailer.monitor(options_mock, args)