class TestUSBDevice(unittest.TestCase): def setUp(self): self.mock_walk_patcher = patch('os.walk') self.mock_walk = self.mock_walk_patcher.start() self.mock_walk.side_effect = mock_walk self.mock_isdir_patcher = patch('os.path.isdir') self.mock_isdir = self.mock_isdir_patcher.start() self.mock_isdir.side_effect = mock_isdir self.mock_join_patcher = patch('os.path.join') self.mock_join = self.mock_join_patcher.start() self.mock_join.side_effect = mock_join self.mock_shutil_move_patcher = patch('shutil.move') self.mock_shutil_move = self.mock_shutil_move_patcher.start() self.mock_config = MagicMock(spec=config.Config) self.mock_config.usb_media_directories.return_value = ['to-index', 'also-to-index', 'third-to-index'] self.mock_config.staging_directory.return_value = '/root/staging/device' self.mock_util = MagicMock(spec=util.Util) self.mock_util.get_uuid.return_value = 'some-uuid' self.test_model = USBDevice(self.mock_config, self.mock_util) def tearDown(self): self.mock_walk_patcher.stop() self.mock_isdir_patcher.stop() self.mock_join_patcher.stop() self.mock_shutil_move_patcher.stop() def test_transfer_media_should_use_the_same_staging_directory_for_all_usb_devices(self): self.test_model.transfer_media('/Volumes/my-dev', 'my-dev') self.mock_config.staging_directory.assert_called_once_with('USB') def test_transfer_media_should_transfer_the_right_media_files(self): self.test_model.transfer_media('/Volumes/my-dev', 'my-dev') calls = [call('/Volumes/my-dev/to-index/file-top.jpg', '/root/staging/device/some-uuid.jpg'), call('/Volumes/my-dev/to-index/subfolder1/file-1.jpg', '/root/staging/device/some-uuid.jpg'), call('/Volumes/my-dev/to-index/subfolder1/subfolder2/file-2.jpg', '/root/staging/device/some-uuid.jpg'), call('/Volumes/my-dev/also-to-index/file-top.jpg', '/root/staging/device/some-uuid.jpg'), call('/Volumes/my-dev/also-to-index/subfolder1/file-1.jpg', '/root/staging/device/some-uuid.jpg'), call('/Volumes/my-dev/also-to-index/subfolder1/subfolder2/file-2.jpg', '/root/staging/device/some-uuid.jpg')] self.mock_shutil_move.assert_has_calls(calls)
def setUp(self): self.mock_walk_patcher = patch('os.walk') self.mock_walk = self.mock_walk_patcher.start() self.mock_walk.side_effect = mock_walk self.mock_isdir_patcher = patch('os.path.isdir') self.mock_isdir = self.mock_isdir_patcher.start() self.mock_isdir.side_effect = mock_isdir self.mock_join_patcher = patch('os.path.join') self.mock_join = self.mock_join_patcher.start() self.mock_join.side_effect = mock_join self.mock_shutil_move_patcher = patch('shutil.move') self.mock_shutil_move = self.mock_shutil_move_patcher.start() self.mock_config = MagicMock(spec=config.Config) self.mock_config.usb_media_directories.return_value = ['to-index', 'also-to-index', 'third-to-index'] self.mock_config.staging_directory.return_value = '/root/staging/device' self.mock_util = MagicMock(spec=util.Util) self.mock_util.get_uuid.return_value = 'some-uuid' self.test_model = USBDevice(self.mock_config, self.mock_util)
config = USBConfigurationDescriptor( interfaces = [interface] ) usbDevDescriptor = USBDeviceDescriptor( bDeviceClass = 0, bDeviceSubClass = 0, bDeviceProtocol = 0, bMaxPacketSize = 64, idVendor = 0x0763, idProduct = 0x1002, configurations = [ config ] ) usbDev = USBDevice(usbDevDescriptor) # create an INET, STREAMing socket serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # bind the socket to a public host, and a well-known port serversocket.bind(("10.160.67.51", 1024)) # become a server socket serversocket.listen(5) emu = usb_emulator(serversocket.accept()[0], 1) emu.connect_device(usbDev) data = lsusbDescriptionParser("dev_desc/multi_flash.txt").parse() payload = data[0] if_info_packet = data[3] ep_info_packet = data[4]