我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用fixtures.LoggerFixture()。
def setUp(self): super(TestCase, self).setUp() if os.environ.get('OS_STDOUT_CAPTURE') in self.true: stdout = self.useFixture(fixtures.StringStream('stdout')).stream self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) if os.environ.get('OS_STDERR_CAPTURE') in self.true: stderr = self.useFixture(fixtures.StringStream('stderr')).stream self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) if (os.environ.get('OS_LOG_CAPTURE') != 'False' and os.environ.get('OS_LOG_CAPTURE') != '0'): self.useFixture(fixtures.LoggerFixture(nuke_handlers=False, level=None))
def test_start_disk_erasing_sets_status_on_post_commit_error(self): # When start_disk_erasing encounters an error in its post-commit hook, # it will set the node's status to FAILED_DISK_ERASING. admin = factory.make_admin() node = factory.make_Node(status=NODE_STATUS.ALLOCATED) # Patch out some things that we don't want to do right now. self.patch(node, '_start').return_value = None # Fake an error during the post-commit hook. error_message = factory.make_name("error") error_type = factory.make_exception_type() _start_async = self.patch_autospec(node, "_start_disk_erasing_async") _start_async.side_effect = error_type(error_message) # Capture calls to _set_status. self.patch_autospec(Node, "_set_status") with LoggerFixture("maas") as logger: with ExpectedException(error_type): with post_commit_hooks: node.start_disk_erasing(admin) # The status is set to be reverted to its initial status. self.assertThat(node._set_status, MockCalledOnceWith( node.system_id, status=NODE_STATUS.FAILED_DISK_ERASING)) # It's logged too. self.assertThat(logger.output, Contains( "%s: Could not start node for disk erasure: %s\n" % (node.hostname, error_message)))
def test_full_clean_logs_node_status_transition(self): node = factory.make_Node( status=NODE_STATUS.DEPLOYING, owner=factory.make_User()) node.status = NODE_STATUS.DEPLOYED with LoggerFixture("maas") as logger: node.full_clean() stat = map_enum_reverse(NODE_STATUS) self.assertThat(logger.output.strip(), Equals( "%s: Status transition from %s to %s" % ( node.hostname, stat[NODE_STATUS.DEPLOYING], stat[NODE_STATUS.DEPLOYED]) ) )
def test_start_rescue_mode_reverts_status_on_post_commit_error(self): self.disable_node_query() # When start_rescue_mode encounters an error in its post-commit # hook, it will revert the node to its previous status. admin = factory.make_admin() status = random.choice([ NODE_STATUS.READY, NODE_STATUS.BROKEN, NODE_STATUS.DEPLOYED]) node = factory.make_Node(status=status) # Patch out some things that we don't want to do right now. self.patch(node, '_power_cycle').return_value = None # Fake an error during the post-commit hook. error_message = factory.make_name("error") error_type = factory.make_exception_type() _start_async = self.patch_autospec(node, "_start_rescue_mode_async") _start_async.side_effect = error_type(error_message) # Capture calls to _set_status. self.patch_autospec(Node, "_set_status") with LoggerFixture("maas") as logger: with ExpectedException(error_type): with post_commit_hooks: node.start_rescue_mode(admin) # The status is set to be reverted to its initial status. self.expectThat(node._set_status, MockCalledOnceWith( node.system_id, status=status)) # It's logged too. self.expectThat(logger.output, Contains( "%s: Could not start rescue mode for node: %s\n" % (node.hostname, error_message)))
def setUp(self): super(BaseTestCase, self).setUp() if not self.setUpClassCalled: raise RuntimeError("setUpClass does not calls the super's" "setUpClass in the " + self.__class__.__name__) at_exit_set.add(self.__class__) test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0) try: test_timeout = int(test_timeout) except ValueError: test_timeout = 0 if test_timeout > 0: self.useFixture(fixtures.Timeout(test_timeout, gentle=True)) if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or os.environ.get('OS_STDOUT_CAPTURE') == '1'): stdout = self.useFixture(fixtures.StringStream('stdout')).stream self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or os.environ.get('OS_STDERR_CAPTURE') == '1'): stderr = self.useFixture(fixtures.StringStream('stderr')).stream self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) if (os.environ.get('OS_LOG_CAPTURE') != 'False' and os.environ.get('OS_LOG_CAPTURE') != '0'): log_format = '%(asctime)-15s %(message)s' self.useFixture(fixtures.LoggerFixture(nuke_handlers=False, format=log_format, level=None))
def setUp(self): super(TestCase, self).setUp() stdout = self.useFixture(fixtures.StringStream('stdout')).stream self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) stderr = self.useFixture(fixtures.StringStream('stderr')).stream self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) self.useFixture(fixtures.LoggerFixture(nuke_handlers=False, level=None))