2018-04-13 14:15:14 +02:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
2014-02-28 10:19:02 -07:00
|
|
|
from common import get_check
|
|
|
|
from random import shuffle, sample
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import os
|
|
|
|
import binascii
|
|
|
|
import re
|
|
|
|
import shutil
|
2014-04-30 16:21:39 -06:00
|
|
|
from nose.plugins.skip import SkipTest
|
2018-10-22 14:33:13 -04:00
|
|
|
from six.moves import range
|
2014-02-28 10:19:02 -07:00
|
|
|
|
|
|
|
class TestPostfix(unittest.TestCase):
|
|
|
|
#
|
|
|
|
# you can execute this dd-agent unit test via python's nose tool
|
|
|
|
#
|
|
|
|
# example:
|
|
|
|
#
|
|
|
|
# nosetests --nocapture --tests=dd-agent/tests/test_postfix.py
|
|
|
|
#
|
2014-07-01 14:27:12 -07:00
|
|
|
|
2014-02-28 10:19:02 -07:00
|
|
|
def setUp(self):
|
|
|
|
self.queue_root = '/tmp/dd-postfix-test/var/spool/postfix'
|
|
|
|
|
|
|
|
self.queues = [
|
|
|
|
'active',
|
|
|
|
'maildrop',
|
|
|
|
'bounce',
|
|
|
|
'incoming',
|
|
|
|
'deferred'
|
|
|
|
]
|
|
|
|
|
|
|
|
self.in_count = {}
|
|
|
|
|
|
|
|
# create test queues
|
|
|
|
for queue in self.queues:
|
2014-07-01 14:27:12 -07:00
|
|
|
try:
|
|
|
|
os.makedirs(os.path.join(self.queue_root, queue))
|
|
|
|
self.in_count[queue] = [0, 0]
|
|
|
|
except Exception:
|
|
|
|
pass
|
2014-02-28 10:19:02 -07:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# clean up test queues
|
|
|
|
shutil.rmtree('/tmp/dd-postfix-test')
|
|
|
|
|
|
|
|
def stripHeredoc(self, text):
|
|
|
|
indent = len(min(re.findall('\n[ \t]*(?=\S)', text) or ['']))
|
|
|
|
pattern = r'\n[ \t]{%d}' % (indent - 1)
|
|
|
|
return re.sub(pattern, '\n', text)
|
|
|
|
|
|
|
|
def test_checks(self):
|
2014-04-30 16:21:39 -06:00
|
|
|
raise SkipTest('Requires root access to postfix')
|
2014-02-28 10:19:02 -07:00
|
|
|
self.config = self.stripHeredoc("""init_config:
|
|
|
|
|
|
|
|
instances:
|
|
|
|
- directory: %s
|
|
|
|
queues:
|
|
|
|
- bounce
|
|
|
|
- maildrop
|
|
|
|
- incoming
|
|
|
|
- active
|
|
|
|
- deferred
|
2014-07-01 14:27:12 -07:00
|
|
|
""" % (self.queue_root))
|
2014-02-28 10:19:02 -07:00
|
|
|
|
|
|
|
# stuff 10K msgs in random queues
|
2018-10-22 14:33:13 -04:00
|
|
|
for _ in range(1, 10000):
|
2014-02-28 10:19:02 -07:00
|
|
|
shuffle(self.queues)
|
|
|
|
rand_queue = sample(self.queues, 1)[0]
|
|
|
|
queue_file = binascii.b2a_hex(os.urandom(7))
|
|
|
|
|
|
|
|
open(os.path.join(self.queue_root, rand_queue, queue_file), 'w')
|
|
|
|
|
|
|
|
# keep track of what we put in
|
|
|
|
self.in_count[rand_queue][0] += 1
|
|
|
|
|
|
|
|
check, instances = get_check('postfix', self.config)
|
|
|
|
|
|
|
|
check.check(instances[0])
|
|
|
|
out_count = check.get_metrics()
|
|
|
|
|
|
|
|
# output what went in... per queue
|
2014-07-28 22:26:27 +02:00
|
|
|
print()
|
2017-04-02 23:38:23 +08:00
|
|
|
for queue, count in self.in_count.items():
|
2016-07-22 03:24:14 +00:00
|
|
|
print('Test messages put into', queue, '= ', self.in_count[queue][0])
|
2014-02-28 10:19:02 -07:00
|
|
|
|
|
|
|
# output postfix.py dd-agent plugin counts... per queue
|
2014-07-28 22:26:27 +02:00
|
|
|
print()
|
2014-02-28 10:19:02 -07:00
|
|
|
for tuple in out_count:
|
2014-05-02 16:41:50 -06:00
|
|
|
queue = tuple[3]['dimensions'][0].split(':')[1]
|
2014-08-05 20:48:55 +02:00
|
|
|
self.assertEqual(int(tuple[2]), int(self.in_count[queue][0]))
|
2014-07-28 22:26:27 +02:00
|
|
|
print('Test messages counted by dd-agent for', queue, '= ', tuple[2])
|
2014-02-28 10:19:02 -07:00
|
|
|
|
|
|
|
#
|
|
|
|
# uncomment this to see the raw dd-agent metric output
|
|
|
|
#
|
2014-07-01 14:27:12 -07:00
|
|
|
# print out_count
|
2014-02-28 10:19:02 -07:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|