本文整理汇总了Python中asynctest.patch方法的典型用法代码示例。如果您正苦于以下问题:Python asynctest.patch方法的具体用法?Python asynctest.patch怎么用?Python asynctest.patch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类asynctest
的用法示例。
在下文中一共展示了asynctest.patch方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_cpu_usage_good
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import patch [as 别名]
def test_get_cpu_usage_good():
fake_task = mock.create_autospec(mesos.task.Task)
fake_task.cpu_limit = asynctest.CoroutineMock(return_value=0.35)
fake_duration = 100
fake_task.stats = asynctest.CoroutineMock(
return_value={"cpus_system_time_secs": 2.5, "cpus_user_time_secs": 0.0}
)
current_time = datetime.datetime.now()
fake_task.__getitem__.return_value = [
{
"state": "TASK_RUNNING",
"timestamp": int(current_time.strftime("%s")) - fake_duration,
}
]
with asynctest.patch(
"paasta_tools.mesos_tools.datetime.datetime", autospec=True
) as mock_datetime:
mock_datetime.now.return_value = current_time
actual = await mesos_tools.get_cpu_usage(fake_task)
assert "10.0%" == actual
示例2: test_get_cpu_usage_bad
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import patch [as 别名]
def test_get_cpu_usage_bad():
fake_task = mock.create_autospec(mesos.task.Task)
fake_task.cpu_limit = asynctest.CoroutineMock(return_value=1.1)
fake_duration = 100
fake_task.stats = asynctest.CoroutineMock(
return_value={"cpus_system_time_secs": 50.0, "cpus_user_time_secs": 50.0}
)
current_time = datetime.datetime.now()
fake_task.__getitem__.return_value = [
{
"state": "TASK_RUNNING",
"timestamp": int(current_time.strftime("%s")) - fake_duration,
}
]
with asynctest.patch(
"paasta_tools.mesos_tools.datetime.datetime", autospec=True
) as mock_datetime:
mock_datetime.now.return_value = current_time
actual = await mesos_tools.get_cpu_usage(fake_task)
assert PaastaColors.red("100.0%") in actual
示例3: test_get_count_running_tasks_on_slave
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import patch [as 别名]
def test_get_count_running_tasks_on_slave():
with asynctest.patch(
"paasta_tools.mesos_tools.get_mesos_master", autospec=True
) as mock_get_master, asynctest.patch(
"paasta_tools.mesos_tools.get_mesos_task_count_by_slave", autospec=True
) as mock_get_mesos_task_count_by_slave:
mock_master = mock.Mock()
mock_mesos_state = mock.Mock()
mock_master.state_summary = asynctest.CoroutineMock(
func=asynctest.CoroutineMock(), # https://github.com/notion/a_sync/pull/40
return_value=mock_mesos_state,
)
mock_get_master.return_value = mock_master
mock_slave_counts = [
{"task_counts": mock.Mock(count=3, slave={"hostname": "host1"})},
{"task_counts": mock.Mock(count=0, slave={"hostname": "host2"})},
]
mock_get_mesos_task_count_by_slave.return_value = mock_slave_counts
assert mesos_tools.get_count_running_tasks_on_slave("host1") == 3
assert mesos_tools.get_count_running_tasks_on_slave("host2") == 0
assert mesos_tools.get_count_running_tasks_on_slave("host3") == 0
assert mock_master.state_summary.called
mock_get_mesos_task_count_by_slave.assert_called_with(mock_mesos_state)
示例4: test_get_task
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import patch [as 别名]
def test_get_task():
with asynctest.patch(
"paasta_tools.mesos_tools.get_running_tasks_from_frameworks", autospec=True
) as mock_get_running_tasks_from_frameworks:
mock_task_1 = {"id": "123"}
mock_task_2 = {"id": "789"}
mock_task_3 = {"id": "789"}
mock_get_running_tasks_from_frameworks.return_value = [
mock_task_1,
mock_task_2,
mock_task_3,
]
ret = await mesos_tools.get_task("123", app_id="app_id")
mock_get_running_tasks_from_frameworks.assert_called_with("app_id")
assert ret == mock_task_1
with raises(mesos_tools.TaskNotFound):
await mesos_tools.get_task("111", app_id="app_id")
with raises(mesos_tools.TooManyTasks):
await mesos_tools.get_task("789", app_id="app_id")
示例5: test_get_all_running_tasks
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import patch [as 别名]
def test_get_all_running_tasks():
with asynctest.patch(
"paasta_tools.mesos_tools.get_current_tasks", autospec=True
) as mock_get_current_tasks, asynctest.patch(
"paasta_tools.mesos_tools.filter_running_tasks", autospec=True
) as mock_filter_running_tasks, asynctest.patch(
"paasta_tools.mesos_tools.get_mesos_master", autospec=True
) as mock_get_mesos_master:
mock_task_1 = mock.Mock()
mock_task_2 = mock.Mock()
mock_task_3 = mock.Mock()
mock_get_current_tasks.return_value = [mock_task_1, mock_task_2]
mock_orphan_tasks = asynctest.CoroutineMock(return_value=[mock_task_3])
mock_mesos_master = mock.Mock(orphan_tasks=mock_orphan_tasks)
mock_get_mesos_master.return_value = mock_mesos_master
ret = await mesos_tools.get_all_running_tasks()
mock_get_current_tasks.assert_called_with("")
mock_filter_running_tasks.assert_called_with(
[mock_task_1, mock_task_2, mock_task_3]
)
assert ret == mock_filter_running_tasks.return_value
示例6: test_get_zookeeper_instances
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import patch [as 别名]
def test_get_zookeeper_instances():
fake_marathon_config = marathon_tools.MarathonServiceConfig(
service="service",
instance="instance",
cluster="cluster",
config_dict={"instances": 5, "max_instances": 10},
branch_dict=None,
)
with mock.patch(
"paasta_tools.utils.KazooClient", autospec=True
) as mock_zk_client, mock.patch(
"paasta_tools.utils.load_system_paasta_config", autospec=True
):
mock_zk_get = mock.Mock(return_value=(7, None))
mock_zk_client.return_value = mock.Mock(get=mock_zk_get)
assert fake_marathon_config.get_instances() == 7
assert mock_zk_get.call_count == 1
示例7: test_get_zookeeper_instances_defaults_to_config_out_of_bounds
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import patch [as 别名]
def test_get_zookeeper_instances_defaults_to_config_out_of_bounds():
fake_marathon_config = marathon_tools.MarathonServiceConfig(
service="service",
instance="instance",
cluster="cluster",
config_dict={"min_instances": 5, "max_instances": 10},
branch_dict=None,
)
with mock.patch(
"paasta_tools.utils.KazooClient", autospec=True
) as mock_zk_client, mock.patch(
"paasta_tools.utils.load_system_paasta_config", autospec=True
):
mock_zk_client.return_value = mock.Mock(get=mock.Mock(return_value=(15, None)))
assert fake_marathon_config.get_instances() == 10
mock_zk_client.return_value = mock.Mock(get=mock.Mock(return_value=(0, None)))
assert fake_marathon_config.get_instances() == 5
示例8: test_update_instances_for_marathon_service
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import patch [as 别名]
def test_update_instances_for_marathon_service():
with mock.patch(
"paasta_tools.marathon_tools.load_marathon_service_config", autospec=True
) as mock_load_marathon_service_config, mock.patch(
"paasta_tools.utils.KazooClient", autospec=True
) as mock_zk_client, mock.patch(
"paasta_tools.utils.load_system_paasta_config", autospec=True
):
zk_client = mock.Mock(get=mock.Mock(side_effect=NoNodeError))
mock_zk_client.return_value = zk_client
mock_load_marathon_service_config.return_value = marathon_tools.MarathonServiceConfig(
service="service",
instance="instance",
cluster="cluster",
config_dict={"min_instances": 5, "max_instances": 10},
branch_dict=None,
)
autoscaling_service_lib.set_instances_for_marathon_service(
"service", "instance", instance_count=8
)
zk_client.set.assert_called_once_with(
"/autoscaling/service/instance/instances", "8".encode("utf8")
)
示例9: test_get_http_utilization_for_all_tasks
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import patch [as 别名]
def test_get_http_utilization_for_all_tasks():
fake_marathon_tasks = [
mock.Mock(id="fake-service.fake-instance", host="fake_host", ports=[30101])
]
mock_json_mapper = mock.Mock(return_value=0.5)
with asynctest.patch(
"paasta_tools.autoscaling.autoscaling_service_lib.get_json_body_from_service",
autospec=True,
):
assert (
autoscaling_service_lib.get_http_utilization_for_all_tasks(
marathon_service_config=mock.Mock(),
marathon_tasks=fake_marathon_tasks,
endpoint="fake-endpoint",
json_mapper=mock_json_mapper,
)
== 0.5
)
示例10: test_http_metrics_provider
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import patch [as 别名]
def test_http_metrics_provider():
fake_marathon_tasks = [
mock.Mock(id="fake-service.fake-instance", host="fake_host", ports=[30101])
]
with asynctest.patch(
"paasta_tools.autoscaling.autoscaling_service_lib.get_json_body_from_service",
autospec=True,
) as mock_get_json_body_from_service:
mock_get_json_body_from_service.return_value = {"utilization": 0.5}
assert (
autoscaling_service_lib.http_metrics_provider(
marathon_service_config=mock.Mock(), marathon_tasks=fake_marathon_tasks
)
== 0.5
)
示例11: test_uwsgi_metrics_provider
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import patch [as 别名]
def test_uwsgi_metrics_provider():
fake_marathon_tasks = [
mock.Mock(id="fake-service.fake-instance", host="fake_host", ports=[30101])
]
with asynctest.patch(
"paasta_tools.autoscaling.autoscaling_service_lib.get_json_body_from_service",
autospec=True,
) as mock_get_json_body_from_service:
mock_get_json_body_from_service.return_value = {
"workers": [
{"status": "idle"},
{"status": "busy"},
{"status": "busy"},
{"status": "busy"},
]
}
assert (
autoscaling_service_lib.uwsgi_metrics_provider(
marathon_service_config=mock.Mock(), marathon_tasks=fake_marathon_tasks
)
== 0.75
)
示例12: test_autoscaling_is_paused
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import patch [as 别名]
def test_autoscaling_is_paused():
with mock.patch(
"paasta_tools.utils.KazooClient", autospec=True
) as mock_zk, mock.patch(
"paasta_tools.autoscaling.autoscaling_service_lib.time", autospec=True
) as mock_time, mock.patch(
"paasta_tools.utils.load_system_paasta_config", autospec=True
):
# Pausing expired 100 seconds ago
mock_zk_get = mock.Mock(return_value=(b"100", None))
mock_zk.return_value = mock.Mock(get=mock_zk_get)
mock_time.time = mock.Mock(return_value=200)
assert not autoscaling_is_paused()
# Pause set until 300, still has 100 more seconds of pausing
mock_zk_get.return_value = (b"300", None)
mock_time.time = mock.Mock(return_value=200)
assert autoscaling_is_paused()
# With 0 we should be unpaused
mock_zk_get.return_value = (b"0", None)
assert not autoscaling_is_paused()
示例13: test_async_setup
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import patch [as 别名]
def test_async_setup(cloud_mock):
"""Test async setup."""
auth_api.CognitoAuth(cloud_mock)
assert len(cloud_mock.iot.mock_calls) == 2
on_connect = cloud_mock.iot.mock_calls[0][1][0]
on_disconnect = cloud_mock.iot.mock_calls[1][1][0]
with patch("random.randint", return_value=0), patch(
"hass_nabucasa.auth.CognitoAuth.async_renew_access_token"
) as mock_renew:
await on_connect()
# Let handle token sleep once
await asyncio.sleep(0)
# Let handle token refresh token
await asyncio.sleep(0)
assert len(mock_renew.mock_calls) == 1
await on_disconnect()
# Make sure task is no longer being called
await asyncio.sleep(0)
await asyncio.sleep(0)
assert len(mock_renew.mock_calls) == 1
示例14: test_patch_as_decorator_uses_MagicMock
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import patch [as 别名]
def test_patch_as_decorator_uses_MagicMock(self):
called = []
@asynctest.mock.patch('test.test_mock.Test')
def test_mock_class(mock):
self.assertIsInstance(mock, asynctest.mock.MagicMock)
called.append("test_mock_class")
@asynctest.mock.patch('test.test_mock.Test.a_function')
def test_mock_function(mock):
self.assertIsInstance(mock, asynctest.mock.MagicMock)
called.append("test_mock_function")
test_mock_class()
test_mock_function()
self.assertIn("test_mock_class", called)
self.assertIn("test_mock_function", called)
示例15: test_patch_decorates_coroutine
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import patch [as 别名]
def test_patch_decorates_coroutine(self):
obj = Test()
with self.subTest("old style coroutine"):
@asynctest.patch.object(obj, "is_patched", new=lambda: True)
@asyncio.coroutine
def a_coroutine():
return obj.is_patched()
self.assertTrue(run_coroutine(a_coroutine()))
with self.subTest("native coroutine"):
@asynctest.patch.object(obj, "is_patched", new=lambda: True)
async def a_native_coroutine():
return obj.is_patched()
self.assertTrue(run_coroutine(a_native_coroutine()))