1# Copyright 2014 The Chromium OS Authors. All rights reserved.  Use of
2# this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import syslog
6
7from autotest_lib.client.bin import test
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.common_lib import utils
10
11class platform_LogDupSuppression(test.test):
12    """Test that we suppress duplicate messages from one process to syslog"""
13    DUP_DETECT_SIG = "spam: last message repeated"
14    NON_SPAM_MSG = 'not spam'
15    NUM_SPAM_MSGS = 10
16    SPAM_LOG_PATH = '/var/log/spam.log'
17    SPAM_MSG = 'SPAM SPAM SPAM'
18    SYSLOG_BIN = 'rsyslogd'
19    SYSLOG_OPTS = '-c4'  # allow version 4 commands
20    SYSLOG_JOB = 'syslog'
21    version = 1
22
23
24    def run_once(self):
25        syslog.openlog('spam')
26        try:
27            utils.run('stop %s' % self.SYSLOG_JOB,
28                      ignore_status=True)  # might not have been running
29            utils.run('truncate -s 0 %s' % self.SPAM_LOG_PATH)
30            utils.run('chown syslog %s' % self.SPAM_LOG_PATH)
31            utils.run('%s %s -f %s/rsyslog.test' %
32                      (self.SYSLOG_BIN, self.SYSLOG_OPTS, self.bindir))
33
34            for i in range(self.NUM_SPAM_MSGS):
35                syslog.syslog(self.SPAM_MSG)
36            syslog.syslog(self.NON_SPAM_MSG)
37
38            cmd_result = utils.run(
39                'grep "%s" %s' % (self.DUP_DETECT_SIG, self.SPAM_LOG_PATH),
40                ignore_status=True)
41            if cmd_result.exit_status:
42                raise error.TestFail(
43                    'duplicate suppression signature not found')
44
45            spam_count = int(
46                utils.run('grep -c "%s" %s' %
47                          (self.SPAM_MSG, self.SPAM_LOG_PATH)).stdout)
48            if spam_count != 1:
49                raise error.TestFail(
50                    'got %s spams, expected exactly one' % spam_count)
51        finally:
52            utils.run('pkill %s' % self.SYSLOG_BIN)
53            utils.run('start %s' % self.SYSLOG_JOB)
54