本文整理汇总了Python中mock.MagicMock.type方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.type方法的具体用法?Python MagicMock.type怎么用?Python MagicMock.type使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.type方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_service_registration
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import type [as 别名]
def test_service_registration(self):
""" Verifies that the Pipeline class can correctly register services. The service registration feature allows
devices to offer services or script access to other devices in the pipeline.
"""
# Load the pipeline configuration and create a test pipeline
self.config.read_configuration(self.source_data_directory+'/hardware/pipelines/tests/data/pipeline_configuration_valid.yml')
test_pipeline = pipeline.Pipeline(self.config.get('pipelines')[0], self.device_manager, self.command_parser)
test_service_type = 'waffle'
# Create and register a mock service
test_service = MagicMock()
test_service.id = 'test_service'
test_service.type = test_service_type
test_pipeline.register_service(test_service)
self.assertTrue(test_pipeline.services[test_service_type]['test_service'] is test_service)
# Create and register a mock service with the same type but a different ID
test_service_2 = MagicMock()
test_service_2.id = 'test_service_2'
test_service_2.type = test_service_type
test_pipeline.register_service(test_service_2)
self.assertTrue(test_pipeline.services[test_service_type]['test_service_2'] is test_service_2)
# Try to register a third service with the same type and ID as an earlier service
test_service_3 = MagicMock()
test_service_3.id = 'test_service'
test_service_3.type = test_service_type
self.assertRaises(pipeline.ServiceAlreadyRegistered, test_pipeline.register_service, test_service_3)
示例2: test_nodetype
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import type [as 别名]
def test_nodetype(self):
mock_node = MagicMock(id=333)
mock_node.type = 'vos:ContainerNode'
client = Client()
client.get_node = Mock(return_value=mock_node)
self.assertEquals('vos:ContainerNode',
client._node_type('vos:/somenode'))
self.assertTrue(client.isdir('vos:/somenode'))
mock_node.type = 'vos:DataNode'
self.assertEquals('vos:DataNode', client._node_type('vos:/somenode'))
self.assertTrue(client.isfile('vos:/somenode'))
# through a link
mock_node.type = 'vos:ContainerNode'
mock_link_node = Mock(type='vos:LinkNode')
mock_link_node.target = 'vos:/somefile'
client.get_node = Mock(side_effect=[mock_link_node, mock_node,
mock_link_node, mock_node])
self.assertEquals('vos:ContainerNode',
client._node_type('vos:/somenode'))
self.assertTrue(client.isdir('vos:/somenode'))
# through an external link - not sure why the type is DataNode in
# this case???
mock_link_node.target = '/somefile'
client.get_node = Mock(side_effect=[mock_link_node, mock_link_node])
self.assertEquals('vos:DataNode', client._node_type('vos:/somenode'))
self.assertTrue(client.isfile('vos:/somenode'))
示例3: test_service_activation_and_lookup
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import type [as 别名]
def test_service_activation_and_lookup(self):
""" This tests that the service activation and lookup methods are working as expected. In order for a service to be
query-able, it must be active (as specified by the configuration for the session using the pipeline).
"""
# Create a test pipeline to work with
self.config.read_configuration(self.source_data_directory+'/hardware/pipelines/tests/data/pipeline_configuration_valid.yml')
test_pipeline = pipeline.Pipeline(self.config.get('pipelines')[0], self.device_manager, self.command_parser)
# Create some mock services and register them with the pipeline
test_tracker_service = MagicMock()
test_tracker_service.id = "sgp4"
test_tracker_service.type = "tracker"
test_pipeline.register_service(test_tracker_service)
test_logger_service = MagicMock()
test_logger_service.id = "basic"
test_logger_service.type = "logger"
test_pipeline.register_service(test_logger_service)
test_cornballer_service = MagicMock()
test_cornballer_service.id = "deluxe"
test_cornballer_service.type = "cornballer"
test_pipeline.register_service(test_cornballer_service)
# Define a callback to continue the test after the schedule has been loaded
def continue_test(reservation_schedule):
# Load a reservation that specifies some active services
test_reservation_config = self._load_reservation_config(reservation_schedule, 'RES.5')
# Create a new session
test_session = session.Session(test_reservation_config, test_pipeline, self.command_parser)
# Register the session with the pipeline; this will also activate the reservation's services
test_pipeline.register_session(test_session)
# Make sure the active services can be loaded
self.assertTrue(test_pipeline.load_service("tracker") is test_tracker_service)
self.assertTrue(test_pipeline.load_service("logger") is test_logger_service)
self.assertRaises(pipeline.ServiceTypeNotFound, test_pipeline.load_service, "cornballer")
# Add an unknown active service type to the reservation configuration and re-register it
test_pipeline.current_session = None
test_reservation_config['active_services']['nonexistent_type'] = "nonexistent_service"
test_session = session.Session(test_reservation_config, test_pipeline, self.command_parser)
self.assertRaises(pipeline.ServiceInvalid, test_pipeline.register_session, test_session)
# Add an unknown active service ID to the reservation configuration and re-register it
test_pipeline.current_session = None
test_reservation_config['active_services'].pop("nonexistent_type", None)
test_reservation_config['active_services']['tracker'] = "nonexistent_service"
test_session = session.Session(test_reservation_config, test_pipeline, self.command_parser)
self.assertRaises(pipeline.ServiceInvalid, test_pipeline.register_session, test_session)
# Load up a test schedule to work with
schedule_update_deferred = self._load_test_schedule()
schedule_update_deferred.addCallback(continue_test)
return schedule_update_deferred
示例4: test_run
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import type [as 别名]
def test_run(self, strp_fun):
"""Test running the ReqMan
"""
socket = self.context.socket.return_value
socks = ((socket, server.POLLIN),)
cnt = [0]
def side_effect(timeout):
del timeout
time.sleep(0.1)
cnt[0] += 1
return socks
ping = MagicMock()
ping.type = "ping"
req = MagicMock()
req.type = "request"
req.data = {"type": "scanline",
"satellite": MagicMock(),
"utctime": MagicMock()}
notice = MagicMock()
notice.type = "notice"
notice.data = {"type": "scanline"}
unknown = MagicMock()
msgs = [ping, req, notice, unknown]
def msg(*args, **kwargs):
if "rawstr" in kwargs:
return msgs[cnt[0] % len(msgs)]
else:
return MagicMock()
server.Message.side_effect = msg
self.reqman.pong = MagicMock()
self.reqman.notice = MagicMock()
self.reqman.scanline = MagicMock()
self.reqman.unknown = MagicMock()
sys.modules["zmq"].Poller.return_value.poll.side_effect = side_effect
self.reqman.start()
time.sleep(0.4)
self.reqman.stop()
self.reqman.join()
self.reqman.pong.assert_called_once_with()
self.reqman.notice.assert_called_once_with(notice)
self.reqman.scanline.assert_called_once_with(req)
self.reqman.unknown.assert_called_once_with(unknown)
sys.modules["zmq"].Poller.return_value.side_effect = None
server.Message.side_effect = None
示例5: test_session_termination
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import type [as 别名]
def test_session_termination(self):
""" This test checks that the session coordinator can correctly clean up sessions once they expire (as determined by
their reservation timestamp range).
"""
# Load in some valid configuration and set the defaults using validate_configuration()
self.config.read_configuration(self.source_data_directory+'/core/tests/data/test_config_basic.yml')
self.config.read_configuration(self.source_data_directory+'/hardware/pipelines/tests/data/pipeline_configuration_valid.yml')
self.config.validate_configuration()
# Setup the pipeline manager
test_pipelines = pipeline_manager.PipelineManager(self.device_manager, self.command_parser)
# Create the expected mock services
test_tracker_service = MagicMock()
test_tracker_service.id = "sgp4"
test_tracker_service.type = "tracker"
test_pipelines.pipelines['test_pipeline3'].register_service(test_tracker_service)
test_logger_service = MagicMock()
test_logger_service.id = "basic"
test_logger_service.type = "logger"
test_pipelines.pipelines['test_pipeline3'].register_service(test_logger_service)
# Setup the schedule manager
test_schedule = schedule.ScheduleManager(self.source_data_directory+'/sessions/tests/data/test_schedule_valid.json')
# Initialize the session coordinator
session_coordinator = coordinator.SessionCoordinator(test_schedule,
self.device_manager,
test_pipelines,
self.command_parser)
def continue_test(loaded_schedule):
# Activate the test reservation
session_coordinator._check_for_new_reservations()
self.assertTrue('RES.6' in session_coordinator.active_sessions)
res6 = session_coordinator.active_sessions['RES.6']
res6.kill_session = MagicMock()
# Change the expiration time on the reservation to make it expire
res6.configuration['time_end'] = 1383264000
# Kill the expired session
session_coordinator._check_for_finished_sessions()
res6.kill_session.assert_called_once_with()
self.assertTrue('RES.6' in session_coordinator.closed_sessions and
'RES.6' not in session_coordinator.active_sessions)
# Update the schedule to load in the reservations
schedule_update_deferred = test_schedule.update_schedule()
schedule_update_deferred.addCallback(continue_test)
return schedule_update_deferred
示例6: test_session_startup_pipeline_setup_command_errors
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import type [as 别名]
def test_session_startup_pipeline_setup_command_errors(self):
""" Tests that the Session class correctly handles fatal pipeline setup command errors when starting a new session.
"""
# First create a pipeline that contains invalid pipeline setup commands (to force an error)
test_pipeline = pipeline.Pipeline(self.config.get('pipelines')[2], self.device_manager, self.command_parser)
# Create the expected mock services
test_tracker_service = MagicMock()
test_tracker_service.id = "sgp4"
test_tracker_service.type = "tracker"
test_pipeline.register_service(test_tracker_service)
test_logger_service = MagicMock()
test_logger_service.id = "basic"
test_logger_service.type = "logger"
test_pipeline.register_service(test_logger_service)
# Define a callback to check the results of the session start procedure
def check_results(session_start_failure, test_session):
# Check if the correct error was generated (caused by a failed pipeline setup command)
self.assertTrue(isinstance(session_start_failure.value, parser.CommandFailed))
# Make sure the session is not active
self.assertTrue(not test_session.is_active)
# Make sure that the pipeline was freed after the error
self.assertTrue(not test_pipeline.is_active)
for temp_device in test_pipeline.devices:
# Try to lock the devices, if this fails then something wasn't unlocked correctly
test_pipeline.devices[temp_device].reserve_device()
# Define a callback to continue the test after the schedule has been loaded
def continue_test(reservation_schedule):
# Find the reservation that we want to test with
test_reservation_config = self._load_reservation_config(reservation_schedule, 'RES.5')
# Create a new session
test_session = session.Session(test_reservation_config, test_pipeline, self.command_parser)
# Start the session
session_start_deferred = test_session.start_session()
session_start_deferred.addErrback(check_results, test_session)
return session_start_deferred
# Now load up a test schedule to work with
schedule_update_deferred = self._load_test_schedule()
schedule_update_deferred.addCallback(continue_test)
return schedule_update_deferred
示例7: add_change
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import type [as 别名]
def add_change(op, dns_name, rtype, ttl, identifier, weight):
if op == 'CREATE':
x = MagicMock(weight=weight, identifier=identifier)
x.name = "myapp.example.org."
x.type = "CNAME"
records[identifier] = x
return MagicMock(name='change')
示例8: test_walk_WalkModeNode
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import type [as 别名]
def test_walk_WalkModeNode(self):
nw = GraphDSLNodeWalker(self.graphmgr)
node = MagicMock()
node.type.value = 'type'
node.direction.value = 'dir'
node.begin.value = 'begin'
node.end.value = 'end'
nw.walk_WalkModeNode(node, [])
self.assertEqual(node.type, 'type')
self.assertEqual(node.direction, 'dir')
self.assertEqual(node.begin, 'begin')
self.assertEqual(node.end, 'end')
node = MagicMock()
node.type = None
node.direction = None
node.begin = None
node.end = None
nw.walk_WalkModeNode(node, [])
self.assertIsNone(node.type)
self.assertIsNone(node.direction)
self.assertIsNone(node.begin)
self.assertIsNone(node.end)
示例9: test_HostapdConfGenerator_getMainOptions
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import type [as 别名]
def test_HostapdConfGenerator_getMainOptions():
wifiIface = MagicMock()
wifiIface.ssid = "Paradrop"
wifiIface.maxassoc = 200
wifiIface.wmm = True
wifiIface._ifname = "wlan0"
wifiDevice = MagicMock()
wifiDevice.country = "US"
wifiDevice.hwmode = "11a"
wifiDevice.channel = 36
wifiDevice.beacon_int = 100
wifiDevice.rts = -1
wifiDevice.frag = -1
interface = MagicMock()
interface.type = "bridge"
interface.config_ifname = "br-lan"
generator = HostapdConfGenerator(wifiIface, wifiDevice, interface)
options = generator.getMainOptions()
print(options)
assert ("interface", "wlan0") in options
assert ("bridge", "br-lan") in options
assert ("ssid", "Paradrop") in options
assert ("country_code", "US") in options
assert ("ieee80211d", 1) in options
assert ("hw_mode", "a") in options
assert ("beacon_int", 100) in options
assert ("max_num_sta", 200) in options
assert ("rts_threshold", -1) in options
assert ("fragm_threshold", -1) in options
assert ("wmm_enabled", 1) in options
示例10: test_HostapdConfGenerator_getRadiusOptions
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import type [as 别名]
def test_HostapdConfGenerator_getRadiusOptions():
wifiIface = ConfigWifiIface()
wifiIface.ssid = "Paradrop"
wifiIface.maxassoc = 200
wifiIface.wmm = True
wifiIface.ifname = "wlan0"
wifiIface.auth_server = "10.42.0.1"
wifiIface.auth_secret = "secret"
wifiIface.acct_server = "10.42.0.1"
wifiIface.acct_secret = "secret"
wifiDevice = MagicMock()
wifiDevice.country = "US"
wifiDevice.hwmode = "11a"
wifiDevice.channel = 36
wifiDevice.beacon_int = 100
wifiDevice.rts = -1
wifiDevice.frag = -1
interface = MagicMock()
interface.type = "bridge"
interface.config_ifname = "br-lan"
generator = HostapdConfGenerator(wifiIface, wifiDevice, interface)
options = generator.getRadiusOptions()
print(options)
示例11: test_reap_tmp_images
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import type [as 别名]
def test_reap_tmp_images(self, _os_datastore_path, _uuid):
""" Test that stray images are found and deleted by the reaper """
def _fake_ds_folder(datastore, folder):
return "%s__%s" % (datastore, folder)
ds = MagicMock()
ds.id = "dsid"
ds.type = DatastoreType.EXT3
# In a random transient directory, set up a directory to act as the
# tmp images folder and to contain a stray image folder with a file.
tmpdir = file_util.mkdtemp(delete=True)
tmp_images_folder = _fake_ds_folder(ds.id, TMP_IMAGE_FOLDER_NAME)
tmp_images_dir = os.path.join(tmpdir, tmp_images_folder)
tmp_image_dir = os.path.join(tmp_images_dir, "stray_image")
os.mkdir(tmp_images_dir)
os.mkdir(tmp_image_dir)
(fd, path) = tempfile.mkstemp(prefix='strayimage_', dir=tmp_image_dir)
self.assertTrue(os.path.exists(path))
def _fake_os_datastore_path(datastore, folder):
return os.path.join(tmpdir, _fake_ds_folder(datastore, folder))
_os_datastore_path.side_effect = _fake_os_datastore_path
ds_manager = MagicMock()
ds_manager.get_datastores.return_value = [ds]
image_manager = EsxImageManager(self.vim_client, ds_manager)
image_manager.reap_tmp_images()
# verify stray image is deleted
self.assertFalse(os.path.exists(path))
示例12: test_socket_set_with_acceptable_socket
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import type [as 别名]
def test_socket_set_with_acceptable_socket(self):
sock = MagicMock()
sock.family = socket.AF_INET
sock.type = socket.SOCK_STREAM
sock.setblocking = MagicMock()
self.channel._socket_set(sock)
self.assertTrue(self.channel._socket is sock)
sock.setblocking.assert_called_once_with(False)
示例13: test_hook_finished
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import type [as 别名]
def test_hook_finished(self, m_log):
self.scan.date = "scan_date"
self.fw.scan = self.scan
self.fw.file.sha256 = "sha256"
self.fw.name = "filename"
self.fw.file.timestamp_first_scan = "ts_first_scan"
self.fw.file.timestamp_last_scan = "ts_last_scan"
self.fw.file.size = "size"
pr1, pr2 = MagicMock(), MagicMock()
self.fw.probe_results = [pr1, pr2]
pr1.name = "probe1"
pr1.type = "antivirus"
pr1.status = "status1"
pr1.duration = "duration1"
pr1.results = "results1"
pr2.name = "probe2"
pr2.type = "metadata"
pr2.status = "status2"
pr2.duration = None
pr2.results = "results2"
pr1.get_details.return_value = pr1
pr2.get_details.return_value = pr2
self.fw.hook_finished()
expected1 = "[files_results] date: %s file_id: %s scan_id: %s "
expected1 += "status: %s probes: %s submitter: %s submitter_id: %s"
call1 = call(expected1,
'scan_date',
self.fw.external_id,
self.fw.scan.external_id, 'Clean', 'probe1, probe2',
'unknown', 'undefined')
expected2 = '[av_results] date: %s av_name: "%s" '
expected2 += "status: %d virus_name: \"%s\" file_id: %s "
expected2 += "file_sha256: %s scan_id: %s duration: %f "
expected2 += "submitter: %s submitter_id: %s"
call2 = call(expected2,
'scan_date',
'probe1',
'status1',
'results1',
self.fw.external_id,
'sha256', self.fw.scan.external_id, 'duration1',
'unknown', 'undefined')
expected3 = '[probe_results] date: %s name: "%s" '
expected3 += "status: %d file_sha256: %s file_id: %s "
expected3 += "duration: %f submitter: %s submitter_id: %s"
call3 = call(expected3,
'scan_date',
'probe2',
'status2',
self.fw.external_id,
'sha256', 0, 'unknown', 'undefined')
m_log.info.assert_has_calls([call1])
m_log.info.assert_has_calls([call2])
m_log.info.assert_has_calls([call3])
示例14: get_datastore_mock
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import type [as 别名]
def get_datastore_mock(self, datastores):
result = []
for datastore in datastores:
mock = MagicMock()
mock.name = datastore[0]
mock.id = datastore[1]
mock.type = datastore[2]
mock.local = datastore[3]
result.append(mock)
return result
示例15: test_security
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import type [as 别名]
def test_security(self):
"""
Does the set_security method get called correctly?
"""
args = MagicMock()
args.type = 'open'
ap_module = MagicMock()
ap_instance = MagicMock(spec=apcommand.accesspoints.atheros.AtherosAR5KAP)
ap_module.AtherosAR5KAP.return_value = ap_instance
error_message = 'security setting error'
ap_instance.set_security.side_effect = Exception(error_message)
with patch('apcommand.accesspoints.atheros', ap_module):
self.sub_command.security(args)
ap_instance.set_security.assert_called_with(security_type=args.type)
return