1 # Copyright (C) 2019-2022 by the Free Software Foundation, Inc.
3 # This file is part of Postorius
5 # mailman.client is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by the
7 # Free Software Foundation, version 3 of the License.
9 # mailman.client is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12 # License for more details.
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with mailman.client. If not, see <http://www.gnu.org/licenses/>.
17 """Testing utilities."""
22 from textwrap
import dedent
25 from mailman
.config
import config
26 from mailman
.core
import initialize
27 from mailman
.core
.initialize
import INHIBIT_CONFIG_FILE
28 from mailman
.testing
.helpers
import (
29 TestableMaster
, reset_the_world
, wait_for_webservice
)
32 @pytest.fixture(scope
='session')
33 def config_initialize(request
):
34 # Set up the basic configuration stuff. Turn off path creation until
35 # we've pushed the testing config.
36 config
.create_paths
= False
37 initialize
.initialize_1(INHIBIT_CONFIG_FILE
)
38 # Calculate a temporary VAR_DIR directory so that run-time artifacts
39 # of the tests won't tread on the installation's data. This also
40 # makes it easier to clean up after the tests are done, and insures
41 # isolation of test suite runs.
42 var_dir
= tempfile
.mkdtemp()
43 # We need a test configuration both for the foreground process and any
44 # child processes that get spawned. lazr.config would allow us to do
45 # it all in a string that gets pushed, and we'll do that for the
46 # foreground, but because we may be spawning processes (such as
47 # runners) we'll need a file that we can specify to the with the -C
48 # option. Craft the full test configuration string here, push it, and
49 # also write it out to a temp file for -C.
51 # Create a dummy postfix.cfg file so that the test suite doesn't try
52 # to run the actual postmap command, which may not exist anyway.
53 postfix_cfg
= os
.path
.join(var_dir
, 'postfix.cfg')
54 with
open(postfix_cfg
, 'w') as fp
:
58 transport_file_type: hash
60 test_config
= dedent("""
70 recipient: you@yourdomain.com
75 incoming: mailman.testing.mta.FakeMTA
83 [archiver.mail_archive]
88 """.format(var_dir
, postfix_cfg
))
89 config
.create_paths
= True
90 config
.push('test config', test_config
)
91 # Initialize everything else.
92 initialize
.initialize_2(testing
=True)
93 initialize
.initialize_3()
95 config_file
= os
.path
.join(var_dir
, 'test.cfg')
96 with
open(config_file
, 'w') as fp
:
99 config
.filename
= config_file
100 # Start the Mailman's test runner.
101 server
= TestableMaster(wait_for_webservice
)
102 server
.start('rest', 'in')
103 request
.addfinalizer(server
.stop
)
107 # Destroy the test database after the tests are done so that there is
108 # no data in case the tests are rerun with a database layer like mysql
109 # or postgresql which are not deleted in teardown.
110 shutil
.rmtree(var_dir
)
111 # Prevent the bit of post-processing on the .pop() that creates
112 # directories. We're basically shutting down everything and we don't
113 # need the directories created. Plus, doing so leaves a var directory
114 # turd in the source tree's top-level directory. We do it this way
115 # rather than shutil.rmtree'ing the resulting var directory because
116 # it's possible the user created a valid such directory for
117 # operational or test purposes.
118 config
.create_paths
= False
119 config
.pop('test config')
122 @pytest.fixture(scope
='class', autouse
=True)
123 def mailman_rest_layer(config_initialize
, request
):
125 request
.addfinalizer(reset_the_world
)