Exemplo n.º 1
0
    def testConfigureLogging(self, file_config):
        callback = mock.Mock(name='DefaultLoggingCallback')
        disabled = mock.Mock(name='DisableExistingLogger')
        with utils.bootstrap_scenario() as spec:
            utils.refresh_bootstrapper()
            mandrel.bootstrap.DEFAULT_LOGGING_CALLBACK = callback
            mandrel.bootstrap.DISABLE_EXISTING_LOGGERS = disabled
            with mock.patch('mandrel.bootstrap.find_logging_configuration') as finder:
                self.assertEqual(False, mandrel.bootstrap.logging_is_configured())
                path = str(mock.Mock(name='LoggingConfigPath'))
                finder.return_value = path
                mandrel.bootstrap.configure_logging()
                self.assertEqual(0, len(mandrel.bootstrap.DEFAULT_LOGGING_CALLBACK.call_args_list))
                file_config.assert_called_once_with(path, disable_existing_loggers=disabled)
                self.assertEqual(True, mandrel.bootstrap.logging_is_configured())

            utils.refresh_bootstrapper()
            mandrel.bootstrap.DEFAULT_LOGGING_CALLBACK = callback
            mandrel.bootstrap.DISABLE_EXISTING_LOGGERS = disabled
            file_config.reset_mock()
            with mock.patch('mandrel.bootstrap.find_logging_configuration') as finder:
                self.assertEqual(False, mandrel.bootstrap.logging_is_configured())
                def failure(*a, **k):
                    raise exception.UnknownConfigurationException
                finder.side_effect = failure
                mandrel.bootstrap.configure_logging()
                self.assertEqual(0, len(file_config.call_args_list))
                callback.assert_called_once_with()
                self.assertEqual(True, mandrel.bootstrap.logging_is_configured())
 def testRootNested(self):
     with utils.bootstrap_scenario(dir='~') as spec:
         with utils.tempdir(dir=spec[0]) as nested_a:
             with utils.workdir(dir=nested_a) as nested_b:
                 utils.refresh_bootstrapper()
                 self.assertEqual(spec[0], mandrel.bootstrap.ROOT_PATH)
                 self.assertEqual(spec[1], mandrel.bootstrap.BOOTSTRAP_FILE)
 def testFileEvaluation(self):
     with utils.bootstrap_scenario(text="bootstrap.EVAL_CHECK = bootstrap\nconfig.EVAL_CHECK = config") as spec:
         utils.refresh_bootstrapper()
         # We check that the bootstrap file is evaluated in a scope with:
         # - mandrel.bootstrap bound to local name "bootstrap"
         # - mandrel.config bound to local name "config"
         self.assertIs(mandrel.bootstrap, mandrel.bootstrap.EVAL_CHECK)
         self.assertIs(mandrel.config, mandrel.config.EVAL_CHECK)
Exemplo n.º 4
0
 def testInitializeSimpleLogging(self):
     with utils.bootstrap_scenario() as spec:
         utils.refresh_bootstrapper()
         with mock.patch('logging.basicConfig') as basic_config:
             with mock.patch('mandrel.bootstrap.DEFAULT_LOGGING_LEVEL') as default_level:
                 with mock.patch('mandrel.bootstrap.DEFAULT_LOGGING_FORMAT') as default_format:
                     with mock.patch('mandrel.bootstrap.DEFAULT_LOGGING_DATE_FORMAT') as default_date_fmt:
                         mandrel.bootstrap.initialize_simple_logging()
                         basic_config.assert_called_once_with(format=default_format, datefmt=default_date_fmt, level=default_level)
Exemplo n.º 5
0
    def testFindLoggingConfiguration(self):
        with utils.bootstrap_scenario() as spec:
            utils.refresh_bootstrapper()
            mandrel.bootstrap.LOGGING_CONFIG_BASENAME = str(mock.Mock(name='MockLoggingConfigPath'))
            mandrel.bootstrap.SEARCH_PATHS = mock.Mock(name='MockSearchPaths')
            with mock.patch('mandrel.util.find_files') as find_files:
                path = mock.Mock(name='MockPath')
                find_files.return_value = iter([path])
                result = mandrel.bootstrap.find_logging_configuration()
                find_files.assert_called_once_with(mandrel.bootstrap.LOGGING_CONFIG_BASENAME, mandrel.bootstrap.SEARCH_PATHS, matches=1)
                self.assertEqual(path, result)

                find_files.reset_mock()
                find_files.return_value = iter([])
                self.assertRaises(exception.UnknownConfigurationException, lambda: mandrel.bootstrap.find_logging_configuration())
def scenario(**files_to_levels):
    levels = []
    with utils.tempdir() as a:
        levels.append(a)
        with utils.tempdir() as b:
            levels.append(b)
            with utils.tempdir() as c:
                levels.append(c)
                for name, dirs in files_to_levels.items():
                    for level in dirs:
                        with open(os.path.join(levels[level], name), 'w') as f:
                            f.write(str(level))

                with utils.bootstrap_scenario() as spec:
                    with mock.patch('mandrel.bootstrap.SEARCH_PATHS', new=levels):
                        yield levels
Exemplo n.º 7
0
 def func(*a, **kw):
     values = sys.argv[:]
     libs = sys.path[:]
     del sys.argv[1:]
     sys.argv.extend(opts)
     if driver_opt.get('ensure_target', True):
         sys.argv.append(str(mock.Mock(name='MockTarget')))
     result = None
     try:
         with utils.bootstrap_scenario() as spec:
             utils.refresh_bootstrapper()
             result = wrapped(*(a + (spec[0],)), **kw)
     finally:
         del sys.argv[:]
         sys.argv[:] = values[:]
         sys.path[:] = libs[:]
     return result
Exemplo n.º 8
0
        def func(*positionals, **keywords):
            with utils.bootstrap_scenario(text=BOOTSTRAP) as spec:
                path, bootstrap_file = spec
                os.mkdir("specific_config")

                for name, levels in (("common", common), ("app", app)):
                    for level in levels:
                        p = os.path.join(os.path.realpath(level), "%s%s.yaml" % (name, scenario_name))
                        with open(p, "w") as stream:
                            yaml.dump({"%s_a" % name: "a_%s" % level, "%s_b" % name: "b_%s" % level}, stream)

                for level in log:
                    p = os.path.join(os.path.realpath(level), "logging.cfg")
                    with open(p, "w") as stream:
                        stream.write(logger_conf(scenario_name, "%s.log" % level))

                utils.refresh_bootstrapper()
                return wrapped(*positionals, **keywords)
Exemplo n.º 9
0
    def testGetLogger(self, getLogger):
        name = str(mock.Mock(name='SomeLoggerName'))
        with utils.bootstrap_scenario() as spec:
            utils.refresh_bootstrapper()
            with mock.patch('mandrel.bootstrap.configure_logging') as configure_logging:
                with mock.patch('mandrel.bootstrap.logging_is_configured') as logging_is_configured:
                    logging_is_configured.return_value = False
                    result = mandrel.bootstrap.get_logger(name)
                    configure_logging.assert_called_once_with()
                    getLogger.assert_called_once_with(name)
                    self.assertEqual(getLogger.return_value, result)

                    configure_logging.reset_mock()
                    getLogger.reset_mock()
                    logging_is_configured.return_value = True
                    result = mandrel.bootstrap.get_logger(name)
                    self.assertEqual(0, len(configure_logging.call_args_list))
                    getLogger.assert_called_once_with(name)
                    self.assertEqual(getLogger.return_value, result)
Exemplo n.º 10
0
 def testDefaultLoggingCallback(self):
     with utils.bootstrap_scenario() as spec:
         utils.refresh_bootstrapper()
         self.assertIs(mandrel.bootstrap.initialize_simple_logging, mandrel.bootstrap.DEFAULT_LOGGING_CALLBACK)
Exemplo n.º 11
0
 def testDisableExistingLoggers(self):
     with utils.bootstrap_scenario() as spec:
         utils.refresh_bootstrapper()
         self.assertEqual(True, mandrel.bootstrap.DISABLE_EXISTING_LOGGERS)
Exemplo n.º 12
0
 def testSimpleLoggingDefaults(self, mock_info):
     with utils.bootstrap_scenario() as spec:
         utils.refresh_bootstrapper()
         self.assertEqual(mock_info, mandrel.bootstrap.DEFAULT_LOGGING_LEVEL)
         self.assertEqual('%(asctime)s.%(msecs)04d #%(process)d - %(levelname)s %(name)s: %(message)s', mandrel.bootstrap.DEFAULT_LOGGING_FORMAT)
         self.assertEqual('%Y-%m-%dT%H:%M:%S', mandrel.bootstrap.DEFAULT_LOGGING_DATE_FORMAT)
 def testDefaultLoggingConfig(self):
     with utils.bootstrap_scenario(dir='~') as spec:
         utils.refresh_bootstrapper()
         self.assertEqual('logging.cfg', mandrel.bootstrap.LOGGING_CONFIG_BASENAME)
 def testDefaultSearchPath(self):
     with utils.bootstrap_scenario(dir='~') as spec:
         utils.refresh_bootstrapper()
         self.assertEqual([spec[0]], list(mandrel.bootstrap.SEARCH_PATHS))
 def testRootImmediate(self):
     with utils.bootstrap_scenario(dir='~') as spec:
         utils.refresh_bootstrapper()
         self.assertEqual(spec[0], mandrel.bootstrap.ROOT_PATH)
         self.assertEqual(spec[1], mandrel.bootstrap.BOOTSTRAP_FILE)