def test_get_logger(self):
        path = '/tmp/__test_console_logger_1.log'
        self._clear([path + suffix for suffix in ['', '.1', '.2', '.3']])

        out = console_logger.get_console_logger(path, 100, 2)

        for args in [
                '0' * 10, '1' * 20, '2' * 100, '3' * 10, '4' * 20, '5' * 100
        ]:
            subprocess.call(args='echo %s' % args,
                            shell=True,
                            cwd='/tmp',
                            stdin=sys.stdin,
                            stdout=out,
                            stderr=sys.stderr)

        time.sleep(1)

        self.assertFalse(os.path.exists(path + '.3'))
        with open(path) as f:
            self.assertEqual([l[26:] for l in f.readlines()],
                             ['5' * 100 + '\n'])
        with open(path + '.1') as f:
            self.assertEqual([l[26:] for l in f.readlines()],
                             ['3' * 10 + '\n', '4' * 20 + '\n'])
        with open(path + '.2') as f:
            self.assertEqual([l[26:] for l in f.readlines()],
                             ['2' * 100 + '\n'])
        self._clear([path + suffix for suffix in ['', '.1', '.2', '.3']])
    def test_get_logger_unicode(self):
        path = '/tmp/__test_console_logger_2.log'
        self._clear([path])

        out = console_logger.get_console_logger(path, 10000, 1)
        args = ['/bin/sh', '-c', to_str('echo "あいうえお"')]

        # Note: set shell=False to avoid a Python 3.2 bug
        subprocess.call(args=args, shell=False, cwd='/tmp', stdin=sys.stdin, stdout=out, stderr=sys.stderr)

        time.sleep(1)

        self.assertFalse(os.path.exists(path + '.1'))
        with open(path) as f:
            self.assertEqual([l[26:] for l in f.readlines()], [to_str('あいうえお\n')])
        self._clear([path])
Example #3
0
    def _execute_application(self, now):
        if self.failed:
            return self

        failed = False
        self.logger.info(
            '%s started: config=%s, args=%s' % (
                self.setting.app_setting.name, self.setting.config_path, self.setting.extra_args))

        time_start = time.time()
        if self.setting.dry_run:
            print('Would execute: cwd=%s, cmd=[\n%s\n], env={\n%s\n}, output=%s' % (
                self.setting.app_setting.home,
                '\n'.join('  %s' % s for s in self.setting.get_args(now)),
                '\n'.join('  %s: %s' % kv for kv in self.setting.get_environ(now).items()),
                oget(self.setting.log_setting.console.get_path(now), 'stdout')
            ))
            ret = 0
        else:
            out_path = self.setting.log_setting.console.get_path(now)
            if out_path is None:
                stdout = sys.stdout
                stderr = sys.stderr
            else:
                stdout = get_console_logger(
                    out_path,
                    self.setting.log_setting.console.max_size.bytes(),
                    self.setting.log_setting.console.backup)
                stderr = stdout
            ret = execute_command_with_pid(
                self.setting.get_args(now),
                self.setting.app_setting.pid_file,
                shell=False,
                cwd=self.setting.app_setting.home,
                env=dict(os.environ, **(oget(self.setting.get_environ(now), {}))),
                stdin=sys.stdin, stdout=stdout, stderr=stderr)

        elapsed = time.time() - time_start

        if ret == 0:
            self.logger.info('%s ended successfully: elapsed=%ds' % (self.setting.app_setting.name, elapsed))
        else:
            self.logger.error(
                '%s ended with error: return_code=%d, elapsed=%ds' % (self.setting.app_setting.name, ret, elapsed))
            failed = True
        return self.copy(failed=failed)
    def test_get_logger(self):
        path = '/tmp/__test_console_logger_1.log'
        self._clear([path + suffix for suffix in ['', '.1', '.2', '.3']])

        out = console_logger.get_console_logger(path, 100, 2)

        for args in ['0' * 10, '1' * 20, '2' * 100, '3' * 10, '4' * 20, '5' * 100]:
            subprocess.call(
                args='echo %s' % args, shell=True, cwd='/tmp', stdin=sys.stdin, stdout=out, stderr=sys.stderr)

        time.sleep(1)

        self.assertFalse(os.path.exists(path + '.3'))
        with open(path) as f:
            self.assertEqual([l[26:] for l in f.readlines()], ['5' * 100 + '\n'])
        with open(path + '.1') as f:
            self.assertEqual([l[26:] for l in f.readlines()], ['3' * 10 + '\n', '4' * 20 + '\n'])
        with open(path + '.2') as f:
            self.assertEqual([l[26:] for l in f.readlines()], ['2' * 100 + '\n'])
        self._clear([path + suffix for suffix in ['', '.1', '.2', '.3']])
    def test_get_logger_unicode(self):
        path = '/tmp/__test_console_logger_2.log'
        self._clear([path])

        out = console_logger.get_console_logger(path, 10000, 1)
        args = ['/bin/sh', '-c', to_str('echo "あいうえお"')]

        # Note: set shell=False to avoid a Python 3.2 bug
        subprocess.call(args=args,
                        shell=False,
                        cwd='/tmp',
                        stdin=sys.stdin,
                        stdout=out,
                        stderr=sys.stderr)

        time.sleep(1)

        self.assertFalse(os.path.exists(path + '.1'))
        with open(path) as f:
            self.assertEqual([l[26:] for l in f.readlines()],
                             [to_str('あいうえお\n')])
        self._clear([path])