本文整理汇总了Python中mock.mock.MagicMock.reset_mock方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.reset_mock方法的具体用法?Python MagicMock.reset_mock怎么用?Python MagicMock.reset_mock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.reset_mock方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_post_rolling_restart
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import reset_mock [as 别名]
def test_post_rolling_restart(self, urlopen_mock, time_mock):
# load the NN and JN JMX files so that the urllib2.urlopen mock has data
# to return
num_journalnodes = 3
journalnode_jmx_file = os.path.join(RMFTestCase._getStackTestsFolder(),
self.UPGRADE_STACK_VERSION, "configs", "journalnode-upgrade-jmx.json")
namenode_jmx_file = os.path.join(RMFTestCase._getStackTestsFolder(),
self.UPGRADE_STACK_VERSION, "configs", "journalnode-upgrade-namenode-jmx.json")
namenode_status_active_file = os.path.join(RMFTestCase._getStackTestsFolder(),
self.UPGRADE_STACK_VERSION, "configs", "journalnode-upgrade-namenode-status-active.json")
namenode_status_standby_file = os.path.join(RMFTestCase._getStackTestsFolder(),
self.UPGRADE_STACK_VERSION, "configs", "journalnode-upgrade-namenode-status-standby.json")
journalnode_jmx = open(journalnode_jmx_file, 'r').read()
namenode_jmx = open(namenode_jmx_file, 'r').read()
namenode_status_active = open(namenode_status_active_file, 'r').read()
namenode_status_standby = open(namenode_status_standby_file, 'r').read()
url_stream_mock = MagicMock()
url_stream_mock.read.side_effect = [namenode_status_active, namenode_status_standby] + (num_journalnodes * [namenode_jmx, journalnode_jmx])
urlopen_mock.return_value = url_stream_mock
# run the post_rolling_restart using the data from above
self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/journalnode.py",
classname = "JournalNode", command = "post_rolling_restart",
config_file = "journalnode-upgrade.json",
hdp_stack_version = self.UPGRADE_STACK_VERSION,
target = RMFTestCase.TARGET_COMMON_SERVICES )
# ensure that the mock was called with the http-style version of the URL
urlopen_mock.assert_called
urlopen_mock.assert_called_with("http://c6407.ambari.apache.org:8480/jmx")
url_stream_mock.reset_mock()
url_stream_mock.read.side_effect = [namenode_status_active, namenode_status_standby] + (num_journalnodes * [namenode_jmx, journalnode_jmx])
urlopen_mock.return_value = url_stream_mock
# now try with HDFS on SSL
self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/journalnode.py",
classname = "JournalNode", command = "post_rolling_restart",
config_file = "journalnode-upgrade-hdfs-secure.json",
hdp_stack_version = self.UPGRADE_STACK_VERSION,
target = RMFTestCase.TARGET_COMMON_SERVICES )
# ensure that the mock was called with the http-style version of the URL
urlopen_mock.assert_called
urlopen_mock.assert_called_with("https://c6407.ambari.apache.org:8481/jmx")
示例2: test_metric_alert_uses_refresh_processor
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import reset_mock [as 别名]
def test_metric_alert_uses_refresh_processor(self, http_response_mock, http_connection_mock):
"""
Tests that the RefreshHeaderProcessor is correctly chained and called
:param http_response_mock:
:param http_connection_mock:
:return:
"""
http_conn = http_connection_mock.return_value
http_conn.getresponse.return_value = MagicMock(status=200)
http_response_mock.return_value = MagicMock(code=200)
url_opener = urllib2.build_opener(RefreshHeaderProcessor())
response = url_opener.open("http://foo.bar.baz/jmx")
self.assertFalse(response is None)
self.assertTrue(http_conn.request.called)
self.assertTrue(http_conn.getresponse.called)
self.assertTrue(http_response_mock.called)
# now we know that the refresh header is intercepting, reset the mocks
# and try with a METRIC alert
MagicMock.reset_mock(http_response_mock)
MagicMock.reset_mock(http_connection_mock)
definition_json = self._get_metric_alert_definition()
configuration = {'hdfs-site' :
{ 'dfs.datanode.http.address': 'c6401.ambari.apache.org:80'}
}
collector = AlertCollector()
cluster_configuration = self.__get_cluster_configuration()
self.__update_cluster_configuration(cluster_configuration, configuration)
alert = MetricAlert(definition_json, definition_json['source'])
alert.set_helpers(collector, cluster_configuration)
alert.set_cluster("c1", "c6401.ambari.apache.org")
alert.collect()
self.assertFalse(response is None)
self.assertTrue(http_conn.request.called)
self.assertTrue(http_conn.getresponse.called)
self.assertTrue(http_response_mock.called)
示例3: TestDataCleaner
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import reset_mock [as 别名]
class TestDataCleaner(unittest.TestCase):
def setUp(self):
self.test_dir = [('/test_path', [],
['errors-12.txt','output-12.txt','site-12.pp','site-13.pp','site-15.pp','version'])]
self.config = MagicMock()
self.config.get.side_effect = [2592000,3600 + 1,"/test_path"]
DataCleaner.logger = MagicMock()
def test_init_success(self):
config = MagicMock()
config.get.return_value = 2592000
DataCleaner.logger.reset_mock()
cleaner = DataCleaner.DataCleaner(config)
self.assertFalse(DataCleaner.logger.warn.called)
def test_init_warn(self):
config = MagicMock()
config.get.return_value = 10
DataCleaner.logger.reset_mock()
cleaner = DataCleaner.DataCleaner(config)
self.assertTrue(DataCleaner.logger.warn.called)
self.assertTrue(cleaner.file_max_age == 3600)
@patch('os.walk')
@patch('time.time')
@patch('os.path.getmtime')
@patch('os.remove')
def test_cleanup_success(self,remMock,mtimeMock,timeMock,walkMock):
self.config.reset_mock()
DataCleaner.logger.reset_mock()
walkMock.return_value = iter(self.test_dir)
timeMock.return_value = 2592000 + 2
mtimeMock.side_effect = [1,1,1,2,1,1]
cleaner = DataCleaner.DataCleaner(self.config)
cleaner.cleanup()
self.assertTrue(len(remMock.call_args_list) == 4)
remMock.assert_any_call('/test_path/errors-12.txt');
remMock.assert_any_call('/test_path/output-12.txt');
remMock.assert_any_call('/test_path/site-12.pp');
remMock.assert_any_call('/test_path/site-15.pp');
pass
@patch('os.walk')
@patch('time.time')
@patch('os.path.getmtime')
@patch('os.remove')
def test_cleanup_remove_error(self,remMock,mtimeMock,timeMock,walkMock):
self.config.reset_mock()
DataCleaner.logger.reset_mock()
walkMock.return_value = iter(self.test_dir)
timeMock.return_value = 2592000 + 2
mtimeMock.side_effect = [1,1,1,2,1,1]
def side_effect(arg):
if arg == '/test_path/site-15.pp':
raise Exception("Can't remove file")
remMock.side_effect = side_effect
cleaner = DataCleaner.DataCleaner(self.config)
cleaner.cleanup()
self.assertTrue(len(remMock.call_args_list) == 4)
self.assertTrue(DataCleaner.logger.error.call_count == 1)
pass
示例4: TestDataCleaner
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import reset_mock [as 别名]
class TestDataCleaner(unittest.TestCase):
def setUp(self):
self.test_dir = [('test_path', [],
['errors-12.txt', 'output-12.txt', 'site-12.pp', 'site-13.pp', 'site-15.pp',
'structured-out-13.json', 'command-13.json', 'version'])]
self.config = MagicMock()
self.config.get.side_effect = [2592000, (3600 + 1), 10000, "test_path"]
DataCleaner.logger = MagicMock()
def test_init_success(self):
config = MagicMock()
config.get.side_effect = [2592000, (3600 + 1), 10000, "test_path"]
DataCleaner.logger.reset_mock()
cleaner = DataCleaner.DataCleaner(config)
self.assertFalse(DataCleaner.logger.warn.called)
@patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))
def test_config(self):
"""
Verify that if the config does not have a property, default values are used.
"""
DataCleaner.logger.reset_mock()
config = AmbariConfig.AmbariConfig()
config.remove_option('agent', 'data_cleanup_max_age')
config.remove_option('agent', 'data_cleanup_interval')
config.remove_option('agent', 'data_cleanup_max_size_MB')
cleaner = DataCleaner.DataCleaner(config)
self.assertEqual(cleaner.file_max_age, 86400)
self.assertEqual(cleaner.cleanup_interval, 3600)
self.assertEqual(cleaner.cleanup_max_size_MB, 10000)
def test_init_warn(self):
config = MagicMock()
config.get.side_effect = [1, (3600 - 1), (10000 + 1), "test_path"]
DataCleaner.logger.reset_mock()
cleaner = DataCleaner.DataCleaner(config)
self.assertTrue(DataCleaner.logger.warn.called)
self.assertTrue(cleaner.file_max_age == 86400)
self.assertTrue(cleaner.cleanup_interval == 3600)
self.assertTrue(cleaner.cleanup_max_size_MB == 10000)
@patch('os.walk')
@patch('time.time')
@patch('os.path.getmtime')
@patch('os.remove')
@patch('os.path.getsize')
def test_cleanup_success(self, sizeMock, remMock, mtimeMock, timeMock, walkMock):
self.config.reset_mock()
DataCleaner.logger.reset_mock()
walkMock.return_value = iter(self.test_dir)
timeMock.return_value = 2592000 + 2
mtimeMock.side_effect = [1, 1, 1, 2, 1, 1, 1, 1]
sizeMock.return_value = 100
cleaner = DataCleaner.DataCleaner(self.config)
cleaner.cleanup()
self.assertTrue(len(remMock.call_args_list) == 6)
remMock.assert_any_call(os.path.join('test_path', 'errors-12.txt'))
remMock.assert_any_call(os.path.join('test_path', 'output-12.txt'))
remMock.assert_any_call(os.path.join('test_path', 'site-12.pp'))
remMock.assert_any_call(os.path.join('test_path', 'site-15.pp'))
remMock.assert_any_call(os.path.join('test_path', 'structured-out-13.json'))
remMock.assert_any_call(os.path.join('test_path', 'command-13.json'))
pass
@patch('os.walk')
@patch('time.time')
@patch('os.path.getmtime')
@patch('os.remove')
@patch('os.path.getsize')
def test_cleanup_remove_error(self, sizeMock, remMock, mtimeMock, timeMock, walkMock):
self.config.reset_mock()
DataCleaner.logger.reset_mock()
walkMock.return_value = iter(self.test_dir)
timeMock.return_value = 2592000 + 2
mtimeMock.side_effect = [1, 1, 1, 2, 1, 1, 1, 1]
sizeMock.return_value = 100
def side_effect(arg):
if arg == os.path.join('test_path', 'site-15.pp'):
raise Exception("Can't remove file")
remMock.side_effect = side_effect
cleaner = DataCleaner.DataCleaner(self.config)
cleaner.cleanup()
self.assertTrue(len(remMock.call_args_list) == 6)
self.assertTrue(DataCleaner.logger.error.call_count == 1)
pass
示例5: test_urllib2_refresh_header_processor
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import reset_mock [as 别名]
def test_urllib2_refresh_header_processor(self):
from urllib2 import Request
# setup the original request
original_url = "http://foo.bar.baz/jmx?qry=someQuery"
request = Request(original_url)
# ensure that we get back a 200 with a refresh header to redirect us
response = MagicMock(code=200)
info_response = MagicMock()
info_response.keys.return_value = ["Refresh"]
info_response.getheader.return_value = "3; url=http://foobar.baz.qux:8080"
response.info.return_value = info_response
# add a mock parent to the refresh processor
parent_mock = MagicMock()
refresh_processor = RefreshHeaderProcessor()
refresh_processor.parent = parent_mock
# execute
refresh_processor.http_response(request, response)
# ensure that the parent was called with the modified URL
parent_mock.open.assert_called_with("http://foobar.baz.qux:8080/jmx?qry=someQuery")
# reset mocks
MagicMock.reset_mock(parent_mock)
# alter the refresh header to remove the time value
info_response.getheader.return_value = "url=http://foobar.baz.qux:8443"
# execute
refresh_processor.http_response(request, response)
# ensure that the parent was called with the modified URL
parent_mock.open.assert_called_with("http://foobar.baz.qux:8443/jmx?qry=someQuery")
# reset mocks
MagicMock.reset_mock(parent_mock)
# use an invalid refresh header
info_response.getheader.return_value = "http://foobar.baz.qux:8443"
# execute
refresh_processor.http_response(request, response)
# ensure that the parent was not called
self.assertFalse(parent_mock.open.called)
# reset mocks
MagicMock.reset_mock(parent_mock)
# remove the refresh header
info_response.keys.return_value = ["SomeOtherHeaders"]
# execute
refresh_processor.http_response(request, response)
# ensure that the parent was not called
self.assertFalse(parent_mock.open.called)
# reset mocks
MagicMock.reset_mock(parent_mock)
# use and invalid http code but include a refresh header
response.code = 401
info_response.keys.return_value = ["Refresh"]
info_response.getheader.return_value = "3; url=http://foobar.baz.qux:8080"
# execute
refresh_processor.http_response(request, response)
# ensure that the parent was not called
self.assertFalse(parent_mock.open.called)
示例6: test_post_upgrade_restart
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import reset_mock [as 别名]
def test_post_upgrade_restart(self, time_mock):
# load the NN and JN JMX files so that the urllib2.urlopen mock has data
# to return
num_journalnodes = 3
journalnode_jmx_file = os.path.join(RMFTestCase._getStackTestsFolder(),
self.UPGRADE_STACK_VERSION, "configs", "journalnode-upgrade-jmx.json")
namenode_jmx_file = os.path.join(RMFTestCase._getStackTestsFolder(),
self.UPGRADE_STACK_VERSION, "configs", "journalnode-upgrade-namenode-jmx.json")
namenode_status_active_file = os.path.join(RMFTestCase._getStackTestsFolder(),
self.UPGRADE_STACK_VERSION, "configs", "journalnode-upgrade-namenode-status-active.json")
namenode_status_standby_file = os.path.join(RMFTestCase._getStackTestsFolder(),
self.UPGRADE_STACK_VERSION, "configs", "journalnode-upgrade-namenode-status-standby.json")
journalnode_jmx = open(journalnode_jmx_file, 'r').read()
namenode_jmx = open(namenode_jmx_file, 'r').read()
namenode_status_active = open(namenode_status_active_file, 'r').read()
namenode_status_standby = open(namenode_status_standby_file, 'r').read()
import utils
import urllib2
from namenode_ha_state import NamenodeHAState
url_stream_mock = MagicMock()
url_stream_mock.read.side_effect = (num_journalnodes * [namenode_jmx, journalnode_jmx])
urlopen_mock = MagicMock(return_value = url_stream_mock)
#urlopen_mock.return_value = url_stream_mock
curl_krb_request_mock = MagicMock(side_effect=(num_journalnodes * [(namenode_jmx, "", 1), (journalnode_jmx, "", 1)]))
get_address_mock = MagicMock(return_value="c6406.ambari.apache.org")
with patch.object(utils, "curl_krb_request", curl_krb_request_mock):
with patch.object(urllib2, "urlopen", urlopen_mock):
with patch.object(NamenodeHAState, "get_address", get_address_mock):
self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/journalnode.py",
classname = "JournalNode", command = "post_upgrade_restart",
config_file = "journalnode-upgrade.json",
checked_call_mocks = [(0, str(namenode_status_active)), (0, str(namenode_status_standby))],
hdp_stack_version = self.UPGRADE_STACK_VERSION,
target = RMFTestCase.TARGET_COMMON_SERVICES )
# ensure that the mock was called with the http-style version of the URL
urlopen_mock.assert_called
urlopen_mock.assert_called_with("http://c6407.ambari.apache.org:8480/jmx")
url_stream_mock.reset_mock()
curl_krb_request_mock.reset_mock()
get_address_mock.reset_mock()
# now try with HDFS on SSL
with patch.object(utils, "curl_krb_request", curl_krb_request_mock):
with patch.object(urllib2, "urlopen", urlopen_mock):
with patch.object(NamenodeHAState, "get_address", get_address_mock):
self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/journalnode.py",
classname = "JournalNode", command = "post_upgrade_restart",
config_file = "journalnode-upgrade-hdfs-secure.json",
checked_call_mocks = [(0, str(namenode_status_active)), (0, str(namenode_status_standby))],
hdp_stack_version = self.UPGRADE_STACK_VERSION,
target = RMFTestCase.TARGET_COMMON_SERVICES )
# ensure that the mock was called with the http-style version of the URL
curl_krb_request_mock.assert_called
curl_krb_request_mock.assert_called_with("/tmp", "/etc/security/keytabs/smokeuser.headless.keytab",
"[email protected]", "https://c6407.ambari.apache.org:8481/jmx",
"jn_upgrade", "/usr/bin/kinit", False, None, "ambari-qa")