Example #1
0
 def setUp(cls):
     # Set up the basic configuration stuff.  Turn off path creation until
     # we've pushed the testing config.
     config.create_paths = False
     initialize.initialize_1(INHIBIT_CONFIG_FILE)
     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()
     # We need a test configuration both for the foreground process and any
     # child processes that get spawned.  lazr.config would allow us to do
     # it all in a string that gets pushed, and we'll do that for the
     # foreground, but because we may be spawning processes (such as
     # runners) we'll need a file that we can specify to the with the -C
     # option.  Craft the full test configuration string here, push it, and
     # also write it out to a temp file for -C.
     #
     # Create a dummy postfix.cfg file so that the test suite doesn't try
     # to run the actual postmap command, which may not exist anyway.
     postfix_cfg = os.path.join(cls.var_dir, 'postfix.cfg')
     with open(postfix_cfg, 'w') as fp:
         print(dedent("""
         [postfix]
         postmap_command: true
         """), file=fp)
     test_config = dedent("""
     [mailman]
     layout: testing
     [paths.testing]
     var_dir: {0}
     [devmode]
     testing: yes
     [mta]
     configuration: {1}
     """.format(cls.var_dir, postfix_cfg))
     # Read the testing config and push it.
     more = resource_bytes('mailman.testing', 'testing.cfg')
     test_config += more.decode('utf-8')
     config.create_paths = True
     config.push('test config', test_config)
     # Initialize everything else.
     initialize.initialize_2(testing=True)
     initialize.initialize_3()
     # When stderr debugging is enabled, subprocess root loggers should
     # also be more verbose.
     if cls.stderr:
         test_config += dedent("""
         [logging.root]
         level: debug
         """)
     # Enable log message propagation and reset the log paths so that the
     # doctests can check the output.
     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 = cls.stderr
         # Reopen the file to a new path that tests can get at.  Instead of
         # using the configuration file path though, use a path that's
         # specific to the logger so that tests can find expected output
         # more easily.
         path = os.path.join(config.LOG_DIR, sub_name)
         get_handler(sub_name).reopen(path)
         log.setLevel(logging.DEBUG)
         # If stderr debugging is enabled, make sure subprocesses are also
         # more verbose.
         if cls.stderr:
             test_config += expand(dedent("""
             [logging.$name]
             propagate: yes
             level: debug
             """), dict(name=sub_name, path=path))
     # The root logger will already have a handler, but it's not the right
     # handler.  Remove that and set our own.
     if cls.stderr:
         console = logging.StreamHandler(sys.stderr)
         formatter = logging.Formatter(config.logging.root.format,
                                       config.logging.root.datefmt)
         console.setFormatter(formatter)
         root = logging.getLogger()
         del root.handlers[:]
         root.addHandler(console)
     # Write the configuration file for subprocesses and set up the config
     # object to pass that properly on the -C option.
     config_file = os.path.join(cls.var_dir, 'test.cfg')
     with open(config_file, 'w') as fp:
         fp.write(test_config)
         print(file=fp)
     config.filename = config_file
Example #2
0
 def setUp(cls):
     # Set up the basic configuration stuff.  Turn off path creation until
     # we've pushed the testing config.
     config.create_paths = False
     initialize.initialize_1(INHIBIT_CONFIG_FILE)
     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()
     # We need a test configuration both for the foreground process and any
     # child processes that get spawned.  lazr.config would allow us to do
     # it all in a string that gets pushed, and we'll do that for the
     # foreground, but because we may be spawning processes (such as
     # runners) we'll need a file that we can specify to the with the -C
     # option.  Craft the full test configuration string here, push it, and
     # also write it out to a temp file for -C.
     test_config = dedent(
         """
     [mailman]
     layout: testing
     [passwords]
     password_scheme: cleartext
     [paths.testing]
     var_dir: %s
     [devmode]
     testing: yes
     """
         % cls.var_dir
     )
     # Read the testing config and push it.
     test_config += resource_string("mailman.testing", "testing.cfg")
     config.create_paths = True
     config.push("test config", test_config)
     # Initialize everything else.
     initialize.initialize_2()
     initialize.initialize_3()
     # When stderr debugging is enabled, subprocess root loggers should
     # also be more verbose.
     if cls.stderr:
         test_config += dedent(
             """
         [logging.root]
         propagate: yes
         level: debug
         """
         )
     # Enable log message propagation and reset the log paths so that the
     # doctests can check the output.
     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
         # Reopen the file to a new path that tests can get at.  Instead of
         # using the configuration file path though, use a path that's
         # specific to the logger so that tests can find expected output
         # more easily.
         path = os.path.join(config.LOG_DIR, sub_name)
         get_handler(sub_name).reopen(path)
         log.setLevel(logging.DEBUG)
         # If stderr debugging is enabled, make sure subprocesses are also
         # more verbose.
         if cls.stderr:
             test_config += expand(
                 dedent(
                     """
             [logging.$name]
             propagate: yes
             level: debug
             """
                 ),
                 dict(name=sub_name, path=path),
             )
     # 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)
     # Write the configuration file for subprocesses and set up the config
     # object to pass that properly on the -C option.
     config_file = os.path.join(cls.var_dir, "test.cfg")
     with open(config_file, "w") as fp:
         fp.write(test_config)
         print(file=fp)
     config.filename = config_file