def test_add_command(self): command_block = FileSystemServiceCommandBlock() command_block.add_command(DeleteCommand(path='/a/path')) et = command_block.get_etree() self.assertEqual(et.tag, 'commands') self.assertEqual(1, len(list(et))) self.assertEqual(0, len(et.keys())) self.assertIsNotNone(et.find('./{}'.format(DeleteCommand.command_name)))
def test_send_command_block_all_ok(self): command_block = FileSystemServiceCommandBlock() command_block.add_command(DeleteCommand(path='/a/path/1.txt')) command_block.add_command(DeleteCommand(path='/a/path/2.txt')) fsr = FileSystemResponse() fsr.add_device_block(self.dev1_id, (GENERIC_COMMAND_BLOCK.format(command='rm') + GENERIC_COMMAND_BLOCK.format(command='rm'))) fsr.add_device_block(self.dev2_id, (GENERIC_COMMAND_BLOCK.format(command='rm') + GENERIC_COMMAND_BLOCK.format(command='rm'))) self.prep_sci_response(fsr) out_dict = self.fss_api.send_command_block(self.target, command_block) # Should just be Nones because they are delete commands with no errors expected_dict = { self.dev1_id: [None, None], self.dev2_id: [None, None], } self.assertDictEqual(expected_dict, out_dict)
def test_send_command_block_mixed_commands(self): command_block = FileSystemServiceCommandBlock() command_block.add_command(DeleteCommand(path='/a/path/1.txt')) command_block.add_command(GetCommand(path='/a/path/2.txt')) data_string = base64.b64encode(six.b('testing string')).decode('ascii') fsr = FileSystemResponse() fsr.add_device_block(self.dev1_id, (GENERIC_COMMAND_BLOCK.format(command='rm') + GET_FILE_BLOCK.format(data=data_string))) fsr.add_device_block(self.dev2_id, (GENERIC_COMMAND_BLOCK.format(command='rm') + GET_FILE_BLOCK.format(data=data_string))) self.prep_sci_response(fsr) out_dict = self.fss_api.send_command_block(self.target, command_block) expected_dict = { self.dev1_id: [None, six.b('testing string')], self.dev2_id: [None, six.b('testing string')], } self.assertDictEqual(expected_dict, out_dict)
def test_send_command_block_errors(self): command_block = FileSystemServiceCommandBlock() command_block.add_command(DeleteCommand(path='/a/path/1.txt')) command_block.add_command(GetCommand(path='/a/path/2.txt')) fsr = FileSystemResponse() fsr.add_device_block(self.dev1_id, (GENERIC_COMMAND_BLOCK.format(command='rm') + ERROR_BLOCK.format(command='get_file', errno=1, errtext="an error message"))) fsr.add_device_block(self.dev2_id, (GENERIC_COMMAND_BLOCK.format(command='rm') + ERROR_BLOCK.format(command='get_file', errno=1, errtext="an error message"))) self.prep_sci_response(fsr) out_dict = self.fss_api.send_command_block(self.target, command_block) # Check empty DeleteCommand responses self.assertIsNone(out_dict[self.dev1_id][0]) self.assertIsNone(out_dict[self.dev2_id][0]) # Check error info values self.assertEqual(1, out_dict[self.dev1_id][1].errno) self.assertEqual(1, out_dict[self.dev2_id][1].errno) self.assertEqual("an error message", out_dict[self.dev1_id][1].message) self.assertEqual("an error message", out_dict[self.dev2_id][1].message)
delete_test_file(fssapi, target, tmp_server_device_file) print("\nList of files in {}".format(base_dir)) out_dict = fssapi.list_files(target, base_dir) print(out_dict) if __name__ == "__main__": dc = get_authenticated_dc() device_id = "your-device-id-here" target = DeviceTarget(device_id) base_dir = '/a/directory/on/your/device' use_filesystem(dc, target, base_dir) fssapi = dc.get_fss_api() tmp_file_path = "{}/{}".format(base_dir, 'test_file.txt') put_test_file(fssapi, target, tmp_file_path) cutoff_time = time.time() get_modified_files(fssapi, target, base_dir, cutoff_time) print("\nModifying file {}".format(tmp_file_path)) fssapi.put_file(target, tmp_file_path, file_data=six.b("data"), offset=4) time.sleep(5) get_modified_files(fssapi, target, base_dir, cutoff_time) delete_test_file(fssapi, target, tmp_file_path) command_block = FileSystemServiceCommandBlock() command_block.add_command(LsCommand(base_dir)) command_block.add_command(LsCommand('/another/directory/on/your/device')) print(fssapi.send_command_block(target, command_block))
def test_get_command_string(self): command_block = FileSystemServiceCommandBlock() self.assertEqual(six.b('<commands />'), command_block.get_command_string()) command_block.add_command(DeleteCommand(path='/a/path')) self.assertEqual(six.b('<commands><rm path="/a/path" /></commands>'), command_block.get_command_string())
def test_init(self): command_block = FileSystemServiceCommandBlock() et = command_block.get_etree() self.assertEqual(et.tag, 'commands') self.assertEqual(0, len(list(et))) self.assertEqual(0, len(et.keys()))