Exemplo n.º 1
0
    def test_tailerfrommonitor(self):
        sys.stdout = MemoryWriter()

        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()
        default_config = DefaultConfig()
        log4tailer.setup_config(options_mock, default_config)
        args_mock = [self.log_name]
        log4tailer.monitor(options_mock, args_mock, default_config,
                self._wait_for())
        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()
Exemplo n.º 2
0
    def test_endtoend_withconfig(self):
        sys.stdout = MemoryWriter()
        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()
        default_config = DefaultConfig()
        log4tailer.setup_config(optionsmock_withconfig, default_config)
        args_mock = [self.log_name]
        log4tailer.monitor(optionsmock_withconfig, args_mock, default_config,
                           self._wait_for())
        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()
Exemplo n.º 3
0
    def test_options_with_screenshot(self):
        sys.stdout = MemoryWriter()
        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()
        shot_process = pjoin(TESTS_DIR, 'printashot.sh')
        default_config = DefaultConfig()
        log4tailer.setup_config(options_mock, default_config)
        default_config.actions[0].screenproc = shot_process
        args_mock = [self.log_name]
        log4tailer.monitor(options_mock, args_mock, default_config,
                self._wait_for(with_fatal_logtrace=True))
        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))
Exemplo n.º 4
0
 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()
Exemplo n.º 5
0
    def test_endtoend_withconfig(self):
        sys.stdout = MemoryWriter()
        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()
        default_config = DefaultConfig()
        log4tailer.setup_config(optionsmock_withconfig, default_config)
        args_mock = [self.log_name]
        log4tailer.monitor(optionsmock_withconfig, args_mock, default_config,
                self._wait_for())
        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()
Exemplo n.º 6
0
 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))
Exemplo n.º 7
0
    def test_tailerfrommonitor(self):
        sys.stdout = MemoryWriter()

        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()
        default_config = DefaultConfig()
        log4tailer.setup_config(options_mock, default_config)
        args_mock = [self.log_name]
        log4tailer.monitor(options_mock, args_mock, default_config,
                           self._wait_for())
        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()
Exemplo n.º 8
0
    def test_options_with_screenshot(self):
        sys.stdout = MemoryWriter()
        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()
        shot_process = pjoin(TESTS_DIR, 'printashot.sh')
        default_config = DefaultConfig()
        log4tailer.setup_config(options_mock, default_config)
        default_config.actions[0].screenproc = shot_process
        args_mock = [self.log_name]
        log4tailer.monitor(options_mock, args_mock, default_config,
                           self._wait_for(with_fatal_logtrace=True))
        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))
Exemplo n.º 9
0
    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()
        default_config = DefaultConfig()
        log4tailer.setup_config(options_mock, default_config)
        args = []
        self.mocker.replay()
        log4tailer.monitor(options_mock, args, default_config)
Exemplo n.º 10
0
    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()
Exemplo n.º 11
0
 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)
Exemplo n.º 12
0
 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)
Exemplo n.º 13
0
    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()
        default_config = DefaultConfig()
        log4tailer.setup_config(options_mock, default_config)
        args = []
        log4tailer.monitor(options_mock, args, default_config)
Exemplo n.º 14
0
 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)
Exemplo n.º 15
0
    def test_tailLastNlines(self):
        sys.stdout = MemoryWriter()
        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]
        default_config = DefaultConfig()
        log4tailer.setup_config(options_mock_nlines, default_config)
        log4tailer.monitor(options_mock_nlines, args, default_config)
Exemplo n.º 16
0
    def test_demonizedoptionsilence(self):
        sys.stdout = MemoryWriter()
        sys.stderr = MemoryWriter()
        sys.stdin = MemoryReader()
        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 connectSMTP(self):
                pass

        def setup_mail(properties):
            return ActionMock()

        def wait_for(secs):
            if self.raise_count == 0:
                write_log()
                return
            raise KeyboardInterrupt

        def chdir(directory):
            return True

        def umask(integer):
            return True

        def setsid():
            return True

        def dup2(one, two):
            return True

        def fork():
            return -1

        onelogtrace = 'this is an info booo log trace'
        anotherlogtrace = 'this is a debug log trace'

        def write_log():
            fh = open(self.log_name, 'a')
            fh.write(onelogtrace + '\n')
            fh.write(anotherlogtrace + '\n')
            fh.close()
            self.raise_count += 1

        log4tailer.setup_mail = setup_mail
        options_mock = OptionsMock2()
        args = [self.log_name]
        default_config = DefaultConfig()
        log4tailer.setup_config(options_mock, default_config)
        os.fork = fork
        os.chdir = chdir
        os.setsid = setsid
        os.dup2 = dup2
        os.umask = umask
        log4tailer.monitor(options_mock, args, default_config,
                wait_for)
        expectedlogtrace = ("\x1b[32m\x1b[44mthis is an info booo log "
            "trace\x1b[0m")
        assert_that(expectedlogtrace,
                is_in(sys.stdout.captured))
Exemplo n.º 17
0
    def test_demonizedoptionsilence(self):
        sys.stdout = MemoryWriter()
        sys.stderr = MemoryWriter()
        sys.stdin = MemoryReader()
        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 connectSMTP(self):
                pass

        def setup_mail(properties):
            return ActionMock()

        def wait_for(secs):
            if self.raise_count == 0:
                write_log()
                return
            raise KeyboardInterrupt

        def chdir(directory):
            return True

        def umask(integer):
            return True

        def setsid():
            return True

        def dup2(one, two):
            return True

        def fork():
            return -1

        onelogtrace = 'this is an info booo log trace'
        anotherlogtrace = 'this is a debug log trace'

        def write_log():
            fh = open(self.log_name, 'a')
            fh.write(onelogtrace + '\n')
            fh.write(anotherlogtrace + '\n')
            fh.close()
            self.raise_count += 1

        log4tailer.setup_mail = setup_mail
        options_mock = OptionsMock2()
        args = [self.log_name]
        default_config = DefaultConfig()
        log4tailer.setup_config(options_mock, default_config)
        os.fork = fork
        os.chdir = chdir
        os.setsid = setsid
        os.dup2 = dup2
        os.umask = umask
        log4tailer.monitor(options_mock, args, default_config, wait_for)
        expectedlogtrace = ("\x1b[32m\x1b[44mthis is an info booo log "
                            "trace\x1b[0m")
        assert_that(expectedlogtrace, is_in(sys.stdout.captured))