def setUp(self, *args): self._device_id = random.randint(0, 500) self._format_id = random.randint(501, 1000) self.obj = DBusDevice(Mock(name="StorageDevice", id=self._device_id, parents=[], children=[]), Mock(name="ObjectManager")) self.obj._manager.get_object_by_id.return_value = Mock(name="DBusObject", object_path="/an/object/path")
def mock_request(method='subtract', args=None, kwargs=None): mock_request = Mock(RPCRequest) mock_request.method = method mock_request.args = args or [4, 6] mock_request.kwargs = kwargs or {} return mock_request
def test_argument_error(dispatch, invoke_with): method, args, kwargs, result = invoke_with protocol = JSONRPCProtocol() @dispatch.public def fn_a(a, b): return a - b @dispatch.public def fn_b(*a): return a[0] - a[1] @dispatch.public def fn_c(**a): return a['a'] - a['b'] mock_request = Mock(RPCRequest) mock_request.args = args mock_request.kwargs = kwargs mock_request.method = method dispatch._dispatch(mock_request, getattr(protocol, '_caller', None)) if inspect.isclass(result) and issubclass(result, Exception): assert type(mock_request.error_respond.call_args[0][0]) == result else: mock_request.respond.assert_called_with(result)
def test_match(self, *args): """Test matching for disklabel format populator.""" device_get_disklabel_type = args[0] device_get_format = args[1] device_is_biosraid_member = args[2] is_mpath_member = args[3] device = Mock() device.is_disk = True data = Mock() self.assertTrue(self.helper_class.match(data, device)) # ID_PART_TABLE_TYPE is required in udev data device_get_disklabel_type.return_value = None self.assertFalse(self.helper_class.match(data, device)) device_get_disklabel_type.return_value = "dos" # no match for whole-disk iso9660 filesystems (isohybrid media) device_get_format.return_value = "iso9660" self.assertFalse(self.helper_class.match(data, device)) device_get_format.return_value = None # no match for biosraid members device_is_biosraid_member.return_value = True self.assertFalse(self.helper_class.match(data, device)) device_is_biosraid_member.return_value = False # no match for multipath members is_mpath_member.return_value = True self.assertFalse(self.helper_class.match(data, device)) is_mpath_member.return_value = False
def test_class_method_argument_error(dispatch, invoke_with): method, args, kwargs, result = invoke_with protocol = JSONRPCProtocol() class Test: c = 0 @classmethod @public def fn_a(cls, a, b): return a - b - cls.c @classmethod @public def fn_b(cls, *a): return a[0] - a[1] - cls.c @classmethod @public def fn_c(cls, **a): return a['a'] - a['b'] - cls.c test = Test() dispatch.register_instance(test) mock_request = Mock(RPCRequest) mock_request.args = args mock_request.kwargs = kwargs mock_request.method = method dispatch._dispatch(mock_request, getattr(protocol, '_caller', None)) if inspect.isclass(result) and issubclass(result, Exception): assert type(mock_request.error_respond.call_args[0][0]) == result else: mock_request.respond.assert_called_with(result)
def test_argument_error(dispatch, invoke_with): method, args, kwargs, result = invoke_with protocol = JSONRPCProtocol() @dispatch.public def fn_a(a, b): return a-b @dispatch.public def fn_b(*a): return a[0]-a[1] @dispatch.public def fn_c(**a): return a['a']-a['b'] mock_request = Mock(RPCRequest) mock_request.args = args mock_request.kwargs = kwargs mock_request.method = method dispatch._dispatch(mock_request) if inspect.isclass(result) and issubclass(result, Exception): assert type(mock_request.error_respond.call_args[0][0]) == result else: mock_request.respond.assert_called_with(result)
def mock_protocol(): mproto = Mock(RPCProtocol) foo = Mock(RPCResponse) foo.result = None mproto.parse_reply = Mock(return_value=foo) return mproto
def test_dispatch_calls_method_and_responds(dispatch, mock_request): m = Mock() m.subtract = Mock(return_value=-2) dispatch.add_method(m.subtract, 'subtract') response = dispatch.dispatch(mock_request) assert m.subtract.called mock_request.respond.assert_called_with(-2)
def _ctor_kwargs(self): kwargs = super(LVMLogicalVolumeDeviceMethodsTestCase, self)._ctor_kwargs() vg_mock = Mock(name="testvg", spec=LVMVolumeGroupDevice) vg_mock.name = "testvg" vg_mock.pvs = vg_mock.parents = [Mock(name="pv.1", protected=False)] vg_mock.protected = False vg_mock.readonly = False kwargs["parents"] = [vg_mock] kwargs["pvs"] = [] return kwargs
def _ctor_kwargs(self): kwargs = super(MDRaidArrayDeviceMethodsTestCase, self)._ctor_kwargs() kwargs["level"] = "raid0" kwargs["parents"] = [Mock(name="member1", spec=StorageDevice), Mock(name="member2", spec=StorageDevice)] mdmember = get_device_format_class("mdmember") for member in kwargs["parents"]: member.format = Mock(spec=mdmember, exists=True) member.protected = False member.readonly = False return kwargs
def test_align_free_regions(self): # disk with two free regions -- first unaligned, second aligned disk = Mock() disk.format.alignment.grainSize = 2048 disk.format.parted_disk.getFreeSpaceRegions.return_value = [Mock(start=1, end=2049, length=2049), Mock(start=1, end=2048, length=2048)] free = get_free_regions([disk]) self.assertEqual(free[0].length, 2049) self.assertEqual(free[1].length, 2048) free = get_free_regions([disk], align=True) self.assertEqual(free[0].length, 2048) self.assertEqual(free[1].length, 2048)
def test_dispatch_handles_in_function_exceptions(dispatch, mock_request): m = Mock() m.subtract = Mock(return_value=-2) class MockError(Exception): pass m.subtract.side_effect = MockError('mock error') dispatch.add_method(m.subtract, 'subtract') response = dispatch.dispatch(mock_request) assert m.subtract.called mock_request.error_respond.assert_called_with(m.subtract.side_effect)
def test_client_passes_correct_reply(client, mock_protocol, method_name, method_args, method_kwargs, one_way_setting, mock_transport): transport_return = '023hoisdfh' mock_transport.send_message = Mock(return_value=transport_return) client.call(method_name, method_args, method_kwargs, one_way_setting) mock_protocol.parse_reply.assert_called_with(transport_return)
def test_get_helper(self, *args): """Test get_format_helper for disklabels.""" device_get_disklabel_type = args[0] device = Mock() device.is_disk = True data = Mock() self.assertEqual(get_format_helper(data, device), self.helper_class) # no disklabel type reported by udev/blkid -> get_format_helper does not return # disklabel helper device_get_disklabel_type.return_value = None self.assertNotEqual(get_format_helper(data, device), self.helper_class) device_get_disklabel_type.return_value = "dos"
def test_run(self, *args): """Test loop device populator.""" device_get_name = args[1] get_backing_file = args[2] devicetree = DeviceTree() data = Mock() # Add backing file and loop device. devicetree.get_device_by_name.return_value = None device_name = "loop3" device_get_name.return_value = device_name backing_file = "/some/file" get_backing_file.return_value = None helper = self.helper_class(devicetree, data) device = helper.run() self.assertIsNone(device) get_backing_file.return_value = backing_file device = helper.run() self.assertIsInstance(device, LoopDevice) self.assertTrue(device in devicetree.devices) self.assertTrue(device.exists) self.assertEqual(device.name, device_name) self.assertIsInstance(device.parents[0], FileDevice) self.assertTrue(device.parents[0].exists) self.assertEqual(devicetree.get_device_by_name.call_count, 1) # pylint: disable=no-member devicetree.get_device_by_name.assert_called_with(backing_file) # pylint: disable=no-member
def mock_protocol(): mproto = Mock(RPCProtocol) foo = Mock(RPCResponse) foo.result = None foo.unique_id = '0' mproto.parse_reply = Mock(return_value=foo) return mproto
def test_client_raises_error_replies(client, mock_protocol, method_name, method_args, method_kwargs, one_way_setting): error_response = RPCErrorResponse() error_response.error = 'foo' mock_protocol.parse_reply = Mock(return_value=error_response) with pytest.raises(RPCError): client.call(method_name, method_args, method_kwargs, one_way_setting)
def test_client_raises_error_replies(client, mock_protocol, method_name, method_args, method_kwargs, async_kwargs): method_kwargs.update(async_kwargs) error_response = RPCResponse() error_response.error = 'foo' client._send_and_handle_reply = Mock(return_value=error_response) with pytest.raises(RPCError): client.call(method_name, method_args, method_kwargs)
def test_run(self, *args): """Test md device populator.""" device_get_md_name = args[0] get_device_by_name = args[4] devicetree = DeviceTree() # base case: _add_slave_devices gets the array into the tree data = Mock() device = Mock() device.parents = [] device_name = "mdtest" device_get_md_name.return_value = device_name get_device_by_name.return_value = device helper = self.helper_class(devicetree, data) self.assertEqual(helper.run(), device)
def test_client_uses_correct_transport(client, mock_protocol, method_name, method_args, method_kwargs, async_kwargs, mock_transport): req = Mock(RPCRequest) req.unique_id = '0' req.method = 'test_method' req.callback = async_kwargs['callback'] req.sync = async_kwargs[ 'asynchronize'] if 'asynchronize' in async_kwargs else True req.args = method_args req.kwargs = method_kwargs mock_protocol.create_request = Mock(return_value=req) method_kwargs.update(async_kwargs) client.call(method_name, method_args, method_kwargs) assert mock_transport.send_message.called
def test_msdos_disk_chunk1(self): disk_size = Size("100 MiB") with sparsetmpfile("chunktest", disk_size) as disk_file: disk = DiskFile(disk_file) disk.format = get_format("disklabel", device=disk.path, exists=False, label_type="msdos") p1 = PartitionDevice("p1", size=Size("10 MiB"), grow=True) p2 = PartitionDevice("p2", size=Size("30 MiB"), grow=True) disks = [disk] partitions = [p1, p2] free = get_free_regions([disk]) self.assertEqual(len(free), 1, "free region count %d not expected" % len(free)) b = Mock(spec=Blivet) allocate_partitions(b, disks, partitions, free) requests = [PartitionRequest(p) for p in partitions] chunk = DiskChunk(free[0], requests=requests) # parted reports a first free sector of 32 for msdos on disk files. whatever. # XXX on gpt, the start is increased to 34 and the end is reduced from 204799 to 204766, # yielding an expected length of 204733 length_expected = 204768 self.assertEqual(chunk.length, length_expected) base_expected = sum(p.parted_partition.geometry.length for p in partitions) self.assertEqual(chunk.base, base_expected) pool_expected = chunk.length - base_expected self.assertEqual(chunk.pool, pool_expected) self.assertEqual(chunk.done, False) self.assertEqual(chunk.remaining, 2) chunk.grow_requests() self.assertEqual(chunk.done, True) self.assertEqual(chunk.pool, 0) self.assertEqual(chunk.remaining, 2) # # validate the growth (everything in sectors) # # The chunk length is 204768. The base of p1 is 20480. The base of # p2 is 61440. The chunk has a base of 81920 and a pool of 122848. # # p1 should grow by 30712 while p2 grows by 92136 since p2's base # size is exactly three times that of p1. self.assertEqual(requests[0].growth, 30712) self.assertEqual(requests[1].growth, 92136)
def test_client_passes_correct_reply(client, mock_protocol, method_name, method_args, method_kwargs, async_kwargs, mock_transport): req = Mock(RPCRequest) req.unique_id = '0' req.method = 'test_method' req.callback = async_kwargs['callback'] req.sync = async_kwargs[ 'asynchronize'] if 'asynchronize' in async_kwargs else True req.args = method_args req.kwargs = method_kwargs method_kwargs.update(async_kwargs) mock_protocol.create_request = Mock(return_value=req) transport_return = '023hoisdfh' client.receiver.transport.receive_reply = Mock( return_value=transport_return) time.sleep(0.1) client.call(method_name, method_args, method_kwargs) mock_protocol.parse_reply.assert_called_with(transport_return)
def test_handle_message_callback(transport, protocol, dispatcher): server = RPCServer(transport, protocol, dispatcher) server.trace = Mock(return_value=None) server.receive_one_message() assert server.trace.call_args_list == [ call('-->', CONTEXT, RECMSG), call('<--', CONTEXT, SERMSG) ] server.trace.assert_called()
def test_client_send_binary_message(client, mock_protocol, method_name, method_args, method_kwargs, one_way_setting, mock_transport): req = Mock(RPCRequest) req.serialize.return_value = u'unicode not acceptable' mock_protocol.create_request.return_value = req client.call(method_name, method_args, method_kwargs, one_way_setting) assert mock_transport.send_message.called assert isinstance(mock_transport.send_message.call_args[0][0], six.binary_type)
def test_weight_2(self): for spec in weighted: part = PartitionDevice('weight_test') part._format = Mock(name="fmt", type=spec.fstype, mountpoint=spec.mountpoint, mountable=spec.mountpoint is not None) with patch('blivet.devices.partition.arch') as _arch: for func in arch_funcs: f = getattr(_arch, func) f.return_value = func in spec.true_funcs self.assertEqual(part.weight, spec.weight)
def test_match(self): if self.udev_type is None: return data = dict() device = Mock() with patch("blivet.udev.device_get_format", return_value=self.udev_type): self.assertTrue(self.helper_class.match(data, device), msg="Failed to match %s against %s" % (self.udev_type, self.helper_name))
def test_run(self, *args): """Test lvm device populator.""" device_get_lv_vg_name = args[0] device_get_name = args[1] get_device_by_name = args[3] devicetree = DeviceTree() data = Mock() # Add slave/parent devices and then look up the device. device_get_name.return_value = sentinel.lv_name devicetree.get_device_by_name.return_value = None # pylint: disable=unused-argument def _get_device_by_name(name, **kwargs): if name == sentinel.lv_name: return sentinel.lv_device get_device_by_name.side_effect = _get_device_by_name device_get_lv_vg_name.return_value = sentinel.vg_name helper = self.helper_class(devicetree, data) self.assertEqual(helper.run(), sentinel.lv_device) self.assertEqual(devicetree.get_device_by_name.call_count, 3) # pylint: disable=no-member get_device_by_name.assert_has_calls([ call(sentinel.vg_name, hidden=True), call(sentinel.vg_name), call(sentinel.lv_name) ]) # Add slave/parent devices, but the device is still not in the tree get_device_by_name.side_effect = None get_device_by_name.return_value = None self.assertEqual(helper.run(), None) get_device_by_name.assert_called_with(sentinel.lv_name) # A non-vg device with the same name as the vg is already in the tree. # pylint: disable=unused-argument def _get_device_by_name2(name, **kwargs): if name == sentinel.lv_name: return sentinel.lv_device elif name == sentinel.vg_name: return sentinel.non_vg_device get_device_by_name.side_effect = _get_device_by_name2 if six.PY3: with self.assertLogs('blivet', level='WARNING') as log_cm: self.assertEqual(helper.run(), sentinel.lv_device) log_entry = "WARNING:blivet:found non-vg device with name %s" % sentinel.vg_name self.assertTrue(log_entry in log_cm.output) else: self.assertEqual(helper.run(), sentinel.lv_device)
def test_reset(self): dt = DeviceTree() names = ["fakedev1", "fakedev2"] for name in names: device = Mock(name=name, spec=StorageDevice, parents=[], exists=True) dt._devices.append(device) dt.actions._actions.append(Mock(name="fake action")) lvm.lvm_cc_addFilterRejectRegexp("xxx") lvm.config_args_data["filterAccepts"].append("yyy") dt.ignored_disks.append(names[0]) dt.exclusive_disks.append(names[1]) dt._hidden.append(dt._devices.pop(1)) dt.edd_dict = {"a": 22} dt.reset() empty_list = list() self.assertEqual(dt._devices, empty_list) self.assertEqual(list(dt.actions), empty_list) self.assertIsInstance(dt.actions, ActionList) self.assertEqual(dt._hidden, empty_list) self.assertEqual(lvm.config_args_data["filterAccepts"], empty_list) self.assertEqual(lvm.config_args_data["filterRejects"], empty_list) self.assertEqual(dt.exclusive_disks, empty_list) self.assertEqual(dt.ignored_disks, empty_list) self.assertEqual(dt.edd_dict, dict())
def test_get_helper(self, *args): if self.udev_type is None: return data = dict() device = Mock() with patch("blivet.udev.device_get_format", return_value=self.udev_type): self.assertEqual(get_format_helper(data, device), self.helper_class, msg="get_format_helper failed for %s" % self.udev_type)
def test_run(self, *args): """Test dm device populator.""" device_is_dm_livecd = args[2] device_get_name = args[1] devicetree = DeviceTree() # The general case for dm devices is that adding the slave/parent devices # will result in the dm device itself being in the tree. device = Mock() devicetree.get_device_by_name.return_value = device data = {"DM_UUID": sentinel.dm_uuid} helper = self.helper_class(devicetree, data) self.assertEqual(helper.run(), device) self.assertEqual(devicetree._add_slave_devices.call_count, 1) # pylint: disable=no-member self.assertEqual(devicetree.get_device_by_name.call_count, 1) # pylint: disable=no-member # since we faked the lookup as if the device was already in the tree # the helper should not have added it, meaning it shouldn't be there self.assertFalse(device in devicetree.devices) # The other case is adding a live media image parent = Mock() parent.parents = [] devicetree._add_slave_devices.return_value = [parent] devicetree._add_device(parent) devicetree.get_device_by_name.return_value = None device_name = "livedevice" device_get_name.return_value = device_name device_is_dm_livecd.return_value = True device = helper.run() self.assertIsInstance(device, DMDevice) self.assertTrue(device in devicetree.devices) self.assertEqual(device.dm_uuid, sentinel.dm_uuid) self.assertEqual(device.name, device_name) self.assertEqual(device.sysfs_path, sentinel.sysfs_path) self.assertEqual(list(device.parents), [parent])
def test_device_name(self): # check that devicetree.names property contains all device's names # mock lvs_info to avoid blockdev call allowing run as non-root with patch.object(LVsInfo, 'cache', new_callable=PropertyMock) as mock_lvs_cache: mock_lvs_cache.return_value = { "sdmock": "dummy", "testvg-testlv": "dummy" } tree = DeviceTree() dev_names = ["sda", "sdb", "sdc"] for dev_name in dev_names: dev = DiskDevice(dev_name, size=Size("1 GiB")) tree._add_device(dev) self.assertTrue(dev in tree.devices) self.assertTrue(dev.name in tree.names) dev.format = get_format("lvmpv", device=dev.path) vg = LVMVolumeGroupDevice("testvg", parents=[dev]) tree._add_device(vg) dev_names.append(vg.name) lv = LVMLogicalVolumeDevice("testlv", parents=[vg]) tree._add_device(lv) dev_names.append(lv.name) # frobnicate a bit with the hidden status of the devices: # * hide sda # * hide and unhide again sdb # * leave sdc unchanged tree.hide(tree.get_device_by_name("sda")) tree.hide(tree.get_device_by_name("sdb")) tree.unhide(tree.get_device_by_name("sdb", hidden=True)) # some lvs names may be already present in the system (mocked) lv_info = list(lvs_info.cache.keys()) # all devices should still be present in the tree.names self.assertEqual(set(tree.names), set(lv_info + dev_names)) # "remove" the LV, it should no longer be in the list tree.actions._actions.append( Mock(device=lv, type=ACTION_TYPE_DESTROY, obj=ACTION_OBJECT_DEVICE)) tree._remove_device(lv) self.assertFalse(lv.name in tree.names)
def test_ignored_disk_tags(self): tree = DeviceTree() fake_ssd = Mock(name="fake_ssd", spec=StorageDevice, parents=[], tags=[Tags.ssd], exists=True) fake_local = Mock(name="fake_local", spec=StorageDevice, parents=[], tags=[Tags.local], exists=True) tree._devices.extend([fake_ssd, fake_local]) self.assertFalse(tree._is_ignored_disk(fake_ssd)) self.assertFalse(tree._is_ignored_disk(fake_local)) tree.ignored_disks.append("@ssd") self.assertTrue(tree._is_ignored_disk(fake_ssd)) self.assertFalse(tree._is_ignored_disk(fake_local)) tree.exclusive_disks.append("@local") self.assertTrue(tree._is_ignored_disk(fake_ssd)) self.assertFalse(tree._is_ignored_disk(fake_local))
def get_disk(self, disk_type, primary_count=0, has_extended=False, logical_count=0): """ Return a mock representing a parted.Disk. """ disk = Mock() disk.type = disk_type label_type_info = disklabel_types[disk_type] (max_primaries, supports_extended) = label_type_info # primary partitions disk.primaryPartitionCount = primary_count disk.maxPrimaryPartitionCount = max_primaries # extended partitions disk.supportsFeature = Mock(return_value=supports_extended) disk.getExtendedPartition = Mock(return_value=has_extended) # logical partitions disk.getLogicalPartitions = Mock(return_value=[0] * logical_count) return disk
def test_batch_dispatch(dispatch): method1 = Mock(return_value='rv1') method2 = Mock(return_value=None) dispatch.add_method(method1, 'method1') dispatch.add_method(method2, 'method2') batch_request = RPCBatchRequest() batch_request.error_respond = Mock(return_value='ERROR') batch_request.append(mock_request('method1', args=[1,2])) batch_request.append(mock_request('non_existant_method', args=[5,6])) batch_request.append(mock_request('method2', args=[3,4])) batch_request.create_batch_response = lambda: RPCBatchResponse() assert batch_request.error_respond.call_count == 0 response = dispatch.dispatch(batch_request) # assert all methods are called method1.assert_called_with(1, 2) method2.assert_called_with(3, 4)
def protocol(): protocol = Mock(RPCProtocol) protocol.parse_request = Mock(return_value=PARMSG) return protocol
def client_capabilities(system): caps = Mock(name="Client.capabilities(%s)" % system.name) caps.configure_mock(**{"supported.return_value": system.raid}) return caps
def dispatcher(response): dispatcher = Mock(RPCDispatcher) dispatcher.dispatch = Mock(return_value=response) return dispatcher
def transport(): transport = Mock(ServerTransport) transport.receive_message = Mock(return_value=(CONTEXT, RECMSG)) return transport
def test_chunk(self): dev1 = Mock() attrs = {"req_grow": True, "id": 1, "name": "req1"} dev1.configure_mock(**attrs) req1 = Request(dev1) req1.base = 10 dev2 = Mock() attrs = {"req_grow": False, "id": 2, "name": "req2"} dev2.configure_mock(**attrs) req2 = Request(dev2) req2.base = 20 chunk = Chunk(110, requests=[req1, req2]) self.assertEqual(chunk.pool, 80) self.assertEqual(chunk.base, 10) dev3 = Mock() attrs = {"req_grow": True, "id": 3, "name": "req3"} dev3.configure_mock(**attrs) req3 = Request(dev3) req3.base = 20 req3.max_growth = 35 chunk.add_request(req3) self.assertEqual(chunk.pool, 60) self.assertEqual(chunk.base, 30) self.assertEqual(chunk.length_to_size(30), 30) self.assertEqual(chunk.size_to_length(40), 40) self.assertEqual(chunk.has_growable, True) chunk.grow_requests() # the chunk is done growing since its pool has been exhausted self.assertEqual(chunk.done, True) # there is still one request remaining since req1 has no maximum growth self.assertEqual(chunk.remaining, 1) # req1 is 10 units and growable with no limit # req2 is 20 units and not growable # req3 is 20 units and growable with a limits of 35 units of growth # # Requests are grown at rates proportional to their share of the # combined base size of all growable requests. If req3 had no max growth # it would get 40 units and req1 would get 20. Since req3 has a limit, # it will get 35 and req1 will get its 20 plus the leftovers from req3, # which comes out to 25. self.assertEqual(req1.growth, 25) self.assertEqual(req2.growth, 0) self.assertEqual(req3.growth, 35)
def test_update_volume_list(self): """Validate conversion of lsm data.""" _client_systems = [Mock(), Mock(), Mock()] _client_systems[0].configure_mock(name="Smart Array P840 in Slot 1", raid=True) _client_systems[1].configure_mock(name="LSI MegaRAID SAS", raid=True) _client_systems[2].configure_mock(name="Supermicro Superchassis", raid=False) _client_volumes = [Mock(system_id=_client_systems[0].id, nodes=["/dev/sda"], vpd83=0, raid_type=sentinel.RAID_TYPE_RAID0, stripe_size=262144, drives=4, min_io=262144, opt_io=1048576), Mock(system_id=_client_systems[1].id, nodes=["/dev/sdb"], vpd83=1, raid_type=sentinel.RAID_TYPE_OTHER, stripe_size=524288, drives=2, min_io=524288, opt_io=1048576), Mock(system_id=_client_systems[2].id, nodes=["/dev/sdc"], vpd83=2, raid_type=None, strip_size=None, drives=None, min_io=None, opt_io=None)] def client_capabilities(system): caps = Mock(name="Client.capabilities(%s)" % system.name) caps.configure_mock(**{"supported.return_value": system.raid}) return caps def client_volume_raid_info(volume): return (volume.raid_type, volume.stripe_size, volume.drives, volume.min_io, volume.opt_io) def vpd83_search(vpd83): return six.next((vol.nodes for vol in _client_volumes if vol.vpd83 == vpd83), None) def system_by_id(sys_id): return six.next((sys for sys in _client_systems if sys.id == sys_id), None) with patch("blivet.devicelibs.disk._lsm_required._check_avail", return_value=True): with patch("blivet.devicelibs.disk.lsm") as _lsm: _lsm.Volume.RAID_TYPE_RAID0 = sentinel.RAID_TYPE_RAID0 _lsm.Volume.RAID_TYPE_OTHER = sentinel.RAID_TYPE_OTHER _lsm.Capabilities.VOLUME_RAID_INFO = sentinel.VOLUME_RAID_INFO _lsm.LocalDisk.vpd83_search.side_effect = vpd83_search client_mock = Mock(name="lsm.Client") client_mock.configure_mock(**{"return_value": client_mock, "volumes.return_value": _client_volumes, "systems.return_value": _client_systems, "capabilities.side_effect": client_capabilities, "volume_raid_info.side_effect": client_volume_raid_info}) _lsm.Client = client_mock disklib.update_volume_info() for (i, lvol) in enumerate(_client_volumes): bvol = disklib.volumes[lvol.nodes[0]] system = system_by_id(lvol.system_id) self.assertEqual(bvol.system, system.name) if client_mock.capabilities(system).supported(sentinel.VOLUME_RAID_INFO): self.assertEqual(bvol.raid_type, disklib._get_lsm_raid_level(lvol.raid_type)) self.assertEqual(bvol.raid_stripe_size, Size(lvol.stripe_size)) self.assertEqual(bvol.raid_disk_count, lvol.drives) else: self.assertIsNone(bvol.raid_type) self.assertIsNone(bvol.raid_stripe_size) self.assertIsNone(bvol.raid_disk_count)
def testEventMask(self): handler_cb = Mock() with patch("blivet.events.manager.validate_cb", return_value=True): mgr = FakeEventManager(handler_cb=handler_cb) device = "sdc" action = "add" mgr.handle_event(action, device) time.sleep(1) self.assertEqual(handler_cb.call_count, 1) event = handler_cb.call_args[1]["event"] # pylint: disable=unsubscriptable-object self.assertEqual(event.device, device) self.assertEqual(event.action, action) # mask matches device but not action -> event is handled handler_cb.reset_mock() mask = mgr.add_mask(device=device, action=action + 'x') mgr.handle_event(action, device) time.sleep(1) self.assertEqual(handler_cb.call_count, 1) event = handler_cb.call_args[1]["event"] # pylint: disable=unsubscriptable-object self.assertEqual(event.device, device) self.assertEqual(event.action, action) # mask matches action but not device -> event is handled handler_cb.reset_mock() mask = mgr.add_mask(device=device + 'x', action=action) mgr.handle_event(action, device) time.sleep(1) self.assertEqual(handler_cb.call_count, 1) event = handler_cb.call_args[1]["event"] # pylint: disable=unsubscriptable-object self.assertEqual(event.device, device) self.assertEqual(event.action, action) # mask matches device and action -> event is ignored handler_cb.reset_mock() mgr.remove_mask(mask) mask = mgr.add_mask(device=device, action=action) mgr.handle_event(action, device) time.sleep(1) self.assertEqual(handler_cb.call_count, 0) # device-only mask matches -> event is ignored handler_cb.reset_mock() mgr.remove_mask(mask) mask = mgr.add_mask(device=device) mgr.handle_event(action, device) time.sleep(1) self.assertEqual(handler_cb.call_count, 0) # action-only mask matches -> event is ignored handler_cb.reset_mock() mgr.remove_mask(mask) mask = mgr.add_mask(action=action) mgr.handle_event(action, device) time.sleep(1) self.assertEqual(handler_cb.call_count, 0) mgr.remove_mask(mask)
def response(): response = Mock(RPCResponse) response.serialize = Mock(return_value=SERMSG) return response