def test_set_tape_file_number_returncode_is_2_raise_exception(self, mock_popen, mock_get_tape_fn, mock_tape_op): attrs = {'communicate.return_value': ('output', 'error'), 'returncode': 2} mock_popen.return_value.configure_mock(**attrs) with self.assertRaises(MTFailedOperationException): set_tape_file_number("device_to_update", 3) cmd = 'mt -f device_to_update someOp 43' mock_popen.assert_called_once_with(cmd, shell=True, stderr=PIPE, stdout=PIPE) mock_get_tape_fn.assert_called_once() mock_tape_op.assert_called_once()
def read(self, storage_object, dst, extract=False, include_xml=True, block_size=DEFAULT_TAPE_BLOCK_SIZE): tape_pos = int(storage_object.content_location_value) medium = storage_object.storage_medium ip = storage_object.ip block_size = medium.block_size * 512 # TODO: Create temp dir inside configured temp directory tmp_path = tempfile.mkdtemp() try: drive = TapeDrive.objects.get(storage_medium=medium) except TapeDrive.DoesNotExist: raise ValueError("Tape not mounted") set_tape_file_number(drive.device, tape_pos) read_tape(drive.device, path=tmp_path, block_size=block_size) drive.last_change = timezone.now() drive.save(update_fields=['last_change']) src = os.path.join(tmp_path, ip.object_identifier_value) if storage_object.container: src_tar = src + '.tar' src_xml = src + '.xml' src_aic_xml = os.path.join(tmp_path, str(ip.aic.pk)) + '.xml' if include_xml: copy(src_xml, dst, block_size=block_size) copy(src_aic_xml, dst, block_size=block_size) if extract: with tarfile.open(src_tar) as t: root = os.path.commonprefix(t.getnames()) t.extractall(dst) new = os.path.join(dst, root) else: new = copy(src_tar, dst, block_size=block_size) else: new = copy(src, dst, block_size=block_size) try: shutil.rmtree(tmp_path) except OSError as e: if e.errno != errno.ENOENT: raise return new
def write(self, src, ip, container, storage_medium, block_size=DEFAULT_TAPE_BLOCK_SIZE): block_size = storage_medium.block_size * 512 last_written_obj = StorageObject.objects.filter( storage_medium=storage_medium).annotate( content_location_value_int=Cast( 'content_location_value', IntegerField())).order_by( 'content_location_value_int').only( 'content_location_value').last() if last_written_obj is None: tape_pos = 1 else: tape_pos = last_written_obj.content_location_value_int + 1 try: drive = TapeDrive.objects.get(storage_medium=storage_medium) except TapeDrive.DoesNotExist: raise ValueError("Tape not mounted") try: set_tape_file_number(drive.device, tape_pos) write_to_tape(drive.device, src, block_size=block_size) except OSError as e: if e.errno == errno.ENOSPC: storage_medium.mark_as_full() raise StorageMediumFull( 'No space left on storage medium "{}"'.format( str(storage_medium.pk))) else: logger.exception('Error occurred when writing to tape') raise drive.last_change = timezone.now() drive.save(update_fields=['last_change']) return StorageObject.objects.create(content_location_value=tape_pos, content_location_type=TAPE, ip=ip, storage_medium=storage_medium, container=container)
def test_set_tape_file_number_success_should_return_proc_output(self, mock_popen, mock_get_tape_fn, mock_tape_op): attrs = {'communicate.return_value': ('the output', ''), 'returncode': 0} mock_popen.return_value.configure_mock(**attrs) self.assertEqual(set_tape_file_number("device_to_update", 2), "the output") cmd = 'mt -f device_to_update someOp 43' mock_popen.assert_called_once_with(cmd, shell=True, stderr=PIPE, stdout=PIPE) mock_get_tape_fn.assert_called_once() mock_tape_op.assert_called_once()
def run(self, medium=None, num=0): """ Sets the current file number (position) of the given tape """ try: drive = TapeDrive.objects.get(storage_medium__pk=medium) except TapeDrive.DoesNotExist: raise ValueError("Tape not mounted") return set_tape_file_number(drive.device, num)
def verify(self): if self.content_location_type == TAPE: verifydir = Path.objects.get(entity='verify').value tmppath = os.path.join(verifydir, self.storage_medium.storage_target.target) if not os.path.exists(tmppath): try: os.mkdir(tmppath) except OSError as e: if e.errno != errno.EEXIST: raise drive = self.storage_medium.tape_drive if drive is None: raise ValueError("Tape not mounted") set_tape_file_number(drive.device, int(self.content_location_value)) read_tape(drive.device, path=tmppath, block_size=self.storage_medium.block_size * 512) drive.last_change = timezone.now() drive.save(update_fields=['last_change']) filename = os.path.join(tmppath, self.ip.object_identifier_value + '.tar'), algorithm = self.ip.get_message_digest_algorithm_display() options = { 'expected': self.ip.message_digest, 'algorithm': algorithm } validator = ChecksumValidator(context='checksum_str', options=options) validator.validate(filename)
def test_set_tape_file_number_num_is_0_should_rewind_tape( self, mock_rewind_tape): set_tape_file_number("device_to_update", 0) mock_rewind_tape.assert_called_once_with("device_to_update")