Ejemplo n.º 1
0
    def test_verify_tape_label_bad_xml_should_return_False(self):
        current_time = timezone.now()
        mock_medium = self.get_mocked_medium("code id", current_time, 103, 512,
                                             325)
        xml_content = """badXML"""

        self.assertFalse(verify_tape_label(mock_medium, xml_content.encode()))
Ejemplo n.º 2
0
    def test_verify_tape_label_id_differs_should_return_False(self):
        current_time = timezone.now()
        mock_medium = self.get_mocked_medium("code id", current_time, 103, 512,
                                             325)
        xml_content = self.get_valid_xml_with_date("some other id",
                                                   current_time, 103, 512, 325)

        self.assertFalse(verify_tape_label(mock_medium, xml_content.encode()))
Ejemplo n.º 3
0
    def test_verify_tape_label_tape_tag_missing_should_return_False(self):
        current_time = timezone.now()
        mock_medium = self.get_mocked_medium("code id", current_time, 103, 512, 325)
        xml_content = f"""\
<?xml version='1.0' encoding='UTF-8'?>
<label>
  <format format="103" blocksize="512" drivemanufacture="325"/>
</label>
"""

        self.assertEqual(verify_tape_label(mock_medium, xml_content.encode()), False)
Ejemplo n.º 4
0
    def run(self, medium=None, drive=None, timeout=120):
        """
        Mounts tape into drive

        Args:
            medium: Which medium to mount
            drive: Which drive to load to
        """

        medium = StorageMedium.objects.get(pk=medium)
        slot = medium.tape_slot.slot_id
        tape_drive = TapeDrive.objects.get(pk=drive)

        mount_tape(tape_drive.robot.device, slot, drive)

        wait_to_come_online(tape_drive.device, timeout)

        if medium.format not in [100, 101]:
            label_root = Path.objects.get(entity='label').value
            xmlpath = os.path.join(label_root,
                                   '%s_label.xml' % medium.medium_id)

            if tape_empty(tape_drive.device):
                create_tape_label(medium, xmlpath)
                rewind_tape(tape_drive.device)
                write_to_tape(tape_drive.device, xmlpath)
            else:
                tar = tarfile.open(tape_drive.device, 'r|')
                first_member = tar.getmembers()[0]

                if first_member.name.endswith('_label.xml'):
                    xmlstring = tar.extractfile(first_member).read()
                    tar.close()
                    if not verify_tape_label(medium, xmlstring):
                        raise ValueError(
                            'Tape contains labelfile with wrong tapeid')
                elif first_member.name == 'reuse':
                    tar.close()

                    create_tape_label(medium, xmlpath)
                    rewind_tape(tape_drive.device)
                    write_to_tape(tape_drive.device, xmlpath)
                else:
                    raise ValueError('Tape contains unknown information')

        TapeDrive.objects.filter(pk=drive).update(
            num_of_mounts=F('num_of_mounts') + 1, )
        StorageMedium.objects.filter(pk=medium.pk).update(
            num_of_mounts=F('num_of_mounts') + 1, tape_drive_id=drive)
Ejemplo n.º 5
0
def write_medium_label_to_drive(drive_id, medium, slot_id, tape_drive):
    xmlfile = tempfile.NamedTemporaryFile(delete=False)
    try:
        arcname = '%s_label.xml' % medium.medium_id

        if medium.format not in [100, 101]:
            if tape_empty(tape_drive.device):
                create_tape_label(medium, xmlfile.name)
                rewind_tape(tape_drive.device)
                write_to_tape(tape_drive.device, xmlfile.name, arcname=arcname)
            else:
                tar = tarfile.open(tape_drive.device, 'r|')
                first_member = tar.getmembers()[0]
                tar.close()
                rewind_tape(tape_drive.device)

                if first_member.name.endswith('_label.xml'):
                    tar = tarfile.open(tape_drive.device, 'r|')
                    xmlstring = tar.extractfile(first_member).read()
                    tar.close()
                    if not verify_tape_label(medium, xmlstring):
                        raise ValueError('Tape contains invalid label file')
                elif first_member.name == 'reuse':
                    create_tape_label(medium, xmlfile.name)
                    rewind_tape(tape_drive.device)
                    write_to_tape(tape_drive.device,
                                  xmlfile.name,
                                  arcname=arcname)
                else:
                    raise ValueError('Tape contains unknown information')

                rewind_tape(tape_drive.device)
    except BaseException:
        StorageMedium.objects.filter(pk=medium.pk).update(
            status=100,
            last_changed_local=timezone.now(),
        )
        TapeDrive.objects.filter(pk=drive_id).update(locked=False, status=100)
        TapeSlot.objects.filter(slot_id=slot_id).update(status=100)
        raise
    finally:
        xmlfile.close()
        TapeDrive.objects.filter(pk=drive_id).update(locked=False)
Ejemplo n.º 6
0
    def run(self, medium=None, drive=None, timeout=120):
        """
        Mounts tape into drive

        Args:
            medium: Which medium to mount
            drive: Which drive to load to
        """

        medium = StorageMedium.objects.get(pk=medium)
        slot = medium.tape_slot.slot_id
        tape_drive = TapeDrive.objects.get(pk=drive)

        if tape_drive.locked:
            raise TapeDriveLockedError()

        tape_drive.locked = True
        tape_drive.save(update_fields=['locked'])

        try:
            mount_tape(tape_drive.robot.device, slot, tape_drive.drive_id)
            wait_to_come_online(tape_drive.device, timeout)
        except:
            StorageMedium.objects.filter(pk=medium.pk).update(status=100)
            TapeDrive.objects.filter(pk=drive).update(locked=False, status=100)
            TapeSlot.objects.filter(slot_id=slot).update(status=100)
            raise

        TapeDrive.objects.filter(pk=drive).update(
            num_of_mounts=F('num_of_mounts') + 1,
            last_change=timezone.now(),
        )
        StorageMedium.objects.filter(pk=medium.pk).update(
            num_of_mounts=F('num_of_mounts') + 1, tape_drive_id=drive)

        xmlfile = tempfile.NamedTemporaryFile(delete=False)

        try:
            arcname = '%s_label.xml' % medium.medium_id

            if medium.format not in [100, 101]:
                if tape_empty(tape_drive.device):
                    create_tape_label(medium, xmlfile.name)
                    rewind_tape(tape_drive.device)
                    write_to_tape(tape_drive.device,
                                  xmlfile.name,
                                  arcname=arcname)
                else:
                    tar = tarfile.open(tape_drive.device, 'r|')
                    first_member = tar.getmembers()[0]
                    tar.close()
                    rewind_tape(tape_drive.device)

                    if first_member.name.endswith('_label.xml'):
                        tar = tarfile.open(tape_drive.device, 'r|')
                        xmlstring = tar.extractfile(first_member).read()
                        tar.close()
                        if not verify_tape_label(medium, xmlstring):
                            raise ValueError(
                                'Tape contains invalid label file')
                    elif first_member.name == 'reuse':
                        create_tape_label(medium, xmlfile.name)
                        rewind_tape(tape_drive.device)
                        write_to_tape(tape_drive.device,
                                      xmlfile.name,
                                      arcname=arcname)
                    else:
                        raise ValueError('Tape contains unknown information')

                    rewind_tape(tape_drive.device)
        except:
            StorageMedium.objects.filter(pk=medium.pk).update(status=100)
            TapeDrive.objects.filter(pk=drive).update(locked=False, status=100)
            TapeSlot.objects.filter(slot_id=slot).update(status=100)
            raise
        finally:
            xmlfile.close()
            TapeDrive.objects.filter(pk=drive).update(locked=False)
Ejemplo n.º 7
0
    def test_verify_tape_label_success_should_return_True(self):
        current_time = timezone.now()
        mock_medium = self.get_mocked_medium("code id", current_time, 103, 512, 325)
        xml_content = self.get_valid_xml_with_date("code id", current_time, 103, 512, 325)

        self.assertEqual(verify_tape_label(mock_medium, xml_content.encode()), True)