summaryrefslogtreecommitdiff
path: root/mailman/testing/layers.py
blob: 883a5c784a30e89a1be026c8e8ea8342ba482609 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Copyright (C) 2008 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.

"""Mailman test layers."""

__metaclass__ = type
__all__ = [
    'ConfigLayer',
    'SMTPLayer',
    ]


import os
import sys
import shutil
import logging
import tempfile

from pkg_resources import resource_string
from textwrap import dedent

from mailman.config import config
from mailman.core.initialize import initialize
from mailman.i18n import _
from mailman.testing.helpers import SMTPServer


NL = '\n'



class ConfigLayer:
    """Layer for pushing and popping test configurations."""

    var_dir = None

    @classmethod
    def setUp(cls):
        initialize()
        assert cls.var_dir is None, 'Layer already set up'
        # Calculate a temporary VAR_DIR directory so that run-time artifacts
        # of the tests won't tread on the installation's data.  This also
        # makes it easier to clean up after the tests are done, and insures
        # isolation of test suite runs.
        cls.var_dir = tempfile.mkdtemp()
        # Create a section with the var directory.
        test_config = dedent("""
        [mailman]
        var_dir: %s
        """ % cls.var_dir)
        # Read the testing config and push it.
        test_config += resource_string('mailman.testing', 'testing.cfg')
        config.push('test config', test_config)
        # Enable log message propagation.
        for logger_config in config.logger_configs:
            sub_name = logger_config.name.split('.')[-1]
            if sub_name == 'root':
                continue
            logger_name = 'mailman.' + sub_name
            log = logging.getLogger(logger_name)
            log.propagate = True
            log.setLevel(logging.DEBUG)
        # zope.testing sets up logging before we get to our own initialization
        # function.  This messes with the root logger, so explicitly set it to
        # go to stderr.
        if cls.stderr:
            console = logging.StreamHandler(sys.stderr)
            formatter = logging.Formatter(config.logging.root.format,
                                          config.logging.root.datefmt)
            console.setFormatter(formatter)
            logging.getLogger().addHandler(console)

    @classmethod
    def tearDown(cls):
        assert cls.var_dir is not None, 'Layer not set up'
        config.pop('test config')
        shutil.rmtree(cls.var_dir)
        cls.var_dir = None

    @classmethod
    def testSetUp(self):
        pass

    @classmethod
    def testTearDown(self):
        # Reset the database between tests.
        config.db._reset()
        # Remove all residual queue files.
        for dirpath, dirnames, filenames in os.walk(config.QUEUE_DIR):
            for filename in filenames:
                os.remove(os.path.join(dirpath, filename))
        # Clear out messages in the message store.
        for message in config.db.message_store.messages:
            config.db.message_store.delete_message(message['message-id'])
        config.db.commit()

    # Flag to indicate that loggers should propagate to the console.
    stderr = False

    @classmethod
    def handle_stderr(cls, *ignore):
        cls.stderr = True

    @classmethod
    def hack_options_parser(cls):
        """Hack our way into the zc.testing framework.

        Add our custom command line option parsing into zc.testing's.  We do
        the imports here so that if zc.testing isn't invoked, this stuff never
        gets in the way.  This is pretty fragile, depend on changes in the
        zc.testing package.  There should be a better way!
        """
        from zope.testing.testrunner.options import parser
        parser.add_option('-e', '--stderr',
                          action='callback', callback=cls.handle_stderr,
                          help=_('Propagate log errors to stderr.'))



class SMTPLayer(ConfigLayer):
    """Layer for starting, stopping, and accessing a test SMTP server."""

    smtpd = None

    @classmethod
    def setUp(cls):
        assert cls.smtpd is None, 'Layer already set up'
        cls.smtpd = SMTPServer()
        cls.smtpd.start()

    @classmethod
    def tearDown(cls):
        assert cls.smtpd is not None, 'Layer not set up'
        cls.smtpd.clear()
        cls.smtpd.stop()

    @classmethod
    def testSetUp(cls):
        pass

    @classmethod
    def testTearDown(cls):
        pass