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 testNormalizeUserPath(self): with utils.bootstrap_scenario() as spec: utils.refresh_bootstrapper() paths = ['~', os.path.join('~', 'fooey')] expect = [os.path.realpath(os.path.expanduser(p)) for p in paths] self.assertEqual( expect, [mandrel.bootstrap.normalize_path(p) for p in paths])
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 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)
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)
def testTargetedListStructure(self, mock_list): with utils.bootstrap_scenario() as spec: mock_list.return_value.__iter__ = mock.Mock() mock_list.return_value.__iter__.return_value = iter(['foo']) utils.refresh_bootstrapper() # Verify that we're using the normalize_path as a transform function on a transforming list mock_list.assert_called_once_with(mandrel.bootstrap.normalize_path) self.assertIs(mock_list.return_value, mandrel.bootstrap.SEARCH_PATHS)
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 testNormalizeAbsolutePath(self): with utils.bootstrap_scenario() as spec: utils.refresh_bootstrapper() with utils.tempdir() as root: paths = [ os.path.join(root, x) for x in ('blibby', 'blooby', 'blobby') ] self.assertEqual( paths, [mandrel.bootstrap.normalize_path(p) for p in paths])
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)
def testNormalizeRelativePath(self): with utils.bootstrap_scenario() as spec: utils.refresh_bootstrapper() p1 = 'foo' p2 = os.path.join('bar', 'baz', 'blee') p3 = '.' paths = [mandrel.bootstrap.normalize_path(p) for p in (p1, p2, p3)] expect = [ os.path.realpath(os.path.join(spec[0], p)) for p in (p1, p2, p3) ] self.assertEqual(expect, paths)
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 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)
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
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
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)
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)
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 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 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)
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)
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)
def testDefaultLoggingCallback(self): with utils.bootstrap_scenario() as spec: utils.refresh_bootstrapper() self.assertIs(mandrel.bootstrap.initialize_simple_logging, mandrel.bootstrap.DEFAULT_LOGGING_CALLBACK)
def testDisableExistingLoggers(self): with utils.bootstrap_scenario() as spec: utils.refresh_bootstrapper() self.assertEqual(True, mandrel.bootstrap.DISABLE_EXISTING_LOGGERS)
def testDefaultSearchPath(self): with utils.bootstrap_scenario(dir='~') as spec: utils.refresh_bootstrapper() self.assertEqual([spec[0]], list(mandrel.bootstrap.SEARCH_PATHS))
def testDefaultLoggingConfig(self): with utils.bootstrap_scenario(dir='~') as spec: utils.refresh_bootstrapper() self.assertEqual('logging.cfg', mandrel.bootstrap.LOGGING_CONFIG_BASENAME)
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)