Exemple #1
0
    def test_read_container_extract(self, mock_copy, mock_ip, mock_storage_obj,
                                    mock_extract):
        disk_storage_backend = DiskStorageBackend()
        full_path_to_src = "the/full/path/to/src"
        container_path = "container/path"

        mock_storage_obj.container = True
        mock_storage_obj.get_full_path.return_value = full_path_to_src
        mock_storage_obj.storage_medium.storage_target.target = container_path
        mock_storage_obj.ip = mock_ip
        mock_ip.aic.pk = 1234

        disk_storage_backend.read(storage_object=mock_storage_obj,
                                  dst="some_dest",
                                  extract=True)

        expected_copy_calls = [
            mock.call(f"{full_path_to_src}.xml", "some_dest",
                      block_size=65536),
            mock.call(os.path.join(container_path, "1234.xml"),
                      "some_dest",
                      block_size=65536),
        ]

        self.assertEqual(mock_copy.call_count, 2)
        mock_copy.assert_has_calls(expected_copy_calls)
        mock_extract.assert_called_once_with(mock_storage_obj, "some_dest")
Exemple #2
0
    def test_extract(self, mock_storage_obj):
        disk_storage_backend = DiskStorageBackend()

        self.create_file(1234, "xml", "aic_xml content")
        self.create_file("dummy", "xml", "xml_file content")
        self.create_file("dummy", "txt", "dummy text content")
        tar_path = self.create_container_files("archive_file", "tar")
        mock_storage_obj.get_full_path.return_value = tar_path

        dst = os.path.join(self.datadir, "extract_to_path")
        self.assertFalse(os.path.exists(dst))
        os.makedirs(dst)
        self.assertTrue(os.path.exists(dst))

        self.assertTrue(os.listdir(dst) == [])

        disk_storage_backend._extract(mock_storage_obj, dst)

        expected_extracted_file_paths = [
            os.path.join(dst, f)
            for f in ["1234.xml", "dummy.xml", "dummy.txt"]
        ]
        for file_path in expected_extracted_file_paths:
            self.assertTrue(
                os.path.exists(file_path) and os.path.isfile(file_path))
Exemple #3
0
    def test_write_src_with_multi_files(self, mock_st_medium, mock_st_method,
                                        mock_st_obj, mock_copy):
        disk_storage_backend = DiskStorageBackend()
        mock_st_medium.storage_target.target = self.datadir
        mock_st_method.containers = True
        src_list = ["some_src", "some_src2", "some_src3"]

        disk_storage_backend.write(src=src_list,
                                   ip=mock.ANY,
                                   container=mock_st_method.containers,
                                   storage_medium=mock_st_medium)

        expected_dest = self.datadir
        expected_copy_calls = [
            mock.call(src, expected_dest, block_size=DEFAULT_BLOCK_SIZE)
            for src in src_list
        ]

        self.assertEqual(mock_copy.call_count, 3)
        mock_copy.assert_has_calls(expected_copy_calls)
        mock_st_obj.assert_called_once_with(
            container=True,
            content_location_type=DISK,
            content_location_value='bar.tar',
            ip=mock.ANY,
            storage_medium=mock_st_medium,
        )
Exemple #4
0
    def test_read_container_default(self, mock_copy, mock_ip,
                                    mock_storage_obj):
        disk_storage_backend = DiskStorageBackend()
        full_path_to_src = "the/full/path/to/src"
        container_path = "container/path"

        mock_storage_obj.container = True
        mock_storage_obj.get_full_path.return_value = full_path_to_src
        mock_storage_obj.storage_medium.storage_target.target = container_path
        mock_storage_obj.ip = mock_ip
        mock_ip.aic.pk = 1234

        disk_storage_backend.read(storage_object=mock_storage_obj,
                                  dst="some_dest")

        expected_copy_calls = [
            mock.call(f"{full_path_to_src}.xml",
                      "some_dest",
                      block_size=DEFAULT_BLOCK_SIZE),
            mock.call(os.path.join(container_path, "1234.xml"),
                      "some_dest",
                      block_size=DEFAULT_BLOCK_SIZE),
            mock.call(f"{full_path_to_src}",
                      "some_dest",
                      block_size=DEFAULT_BLOCK_SIZE)
        ]

        self.assertEqual(mock_copy.call_count, 3)
        mock_copy.assert_has_calls(expected_copy_calls)
Exemple #5
0
    def test_write_src_with_multi_files_not_container(self, mock_st_medium,
                                                      mock_st_method,
                                                      mock_st_obj, mock_copy):
        disk_storage_backend = DiskStorageBackend()
        mock_st_medium.storage_target.target = self.datadir
        mock_st_method.containers = False
        ip = InformationPackage.objects.create(
            object_identifier_value='objid', )
        src_list = ["some_src", "some_src2", "some_src3"]

        disk_storage_backend.write(src=src_list,
                                   ip=ip,
                                   container=mock_st_method.containers,
                                   storage_medium=mock_st_medium)

        expected_dest = os.path.join(self.datadir, "objid")
        expected_copy_calls = [
            mock.call(src, expected_dest, block_size=DEFAULT_BLOCK_SIZE)
            for src in src_list
        ]

        self.assertEqual(mock_copy.call_count, 3)
        mock_copy.assert_has_calls(expected_copy_calls)
        mock_st_obj.assert_called_once_with(
            container=False,
            content_location_type=DISK,
            content_location_value='objid',
            ip=mock.ANY,
            storage_medium=mock_st_medium,
        )
Exemple #6
0
    def test_delete(self, mock_storage_obj):
        disk_storage_backend = DiskStorageBackend()
        mock_storage_obj.container = False
        mock_storage_obj.content_location_value = self.datadir

        self.assertTrue(os.path.exists(self.datadir))

        disk_storage_backend.delete(mock_storage_obj)

        self.assertFalse(os.path.exists(self.datadir))
Exemple #7
0
    def test_read_not_container_should_call_copy(self, mock_copy,
                                                 mock_storage_obj):
        disk_storage_backend = DiskStorageBackend()

        mock_storage_obj.container = False
        mock_storage_obj.get_full_path.return_value = "the_full_path"

        disk_storage_backend.read(storage_object=mock_storage_obj,
                                  dst="some_dest")

        mock_copy.assert_called_once_with("the_full_path",
                                          "some_dest",
                                          block_size=65536)
Exemple #8
0
    def test_write_dest_is_path_to_container(self, mock_st_medium,
                                             mock_st_method, mock_st_obj,
                                             mock_copy):
        disk_storage_backend = DiskStorageBackend()
        mock_st_medium.storage_target.target = self.datadir
        mock_st_method.containers = True

        disk_storage_backend.write(src="some_src",
                                   ip=mock.ANY,
                                   storage_method=mock_st_method,
                                   storage_medium=mock_st_medium)

        mock_copy.assert_called_once()
        mock_st_obj.assert_called_once()
Exemple #9
0
    def test_read_container_no_xml(self, mock_copy, mock_ip, mock_storage_obj):
        disk_storage_backend = DiskStorageBackend()
        full_path_to_src = "the/full/path/to/src"
        container_path = "container/path"

        mock_storage_obj.container = True
        mock_storage_obj.get_full_path.return_value = full_path_to_src
        mock_storage_obj.storage_medium.storage_target.target = container_path
        mock_storage_obj.ip = mock_ip
        mock_ip.aic.pk = 1234

        disk_storage_backend.read(storage_object=mock_storage_obj,
                                  dst="some_dest",
                                  include_xml=False)

        mock_copy.assert_called_once_with(f"{full_path_to_src}",
                                          "some_dest",
                                          block_size=65536)
Exemple #10
0
    def test_write_src_with_multi_files(self, mock_st_medium, mock_st_method,
                                        mock_st_obj, mock_copy):
        disk_storage_backend = DiskStorageBackend()
        mock_st_medium.storage_target.target = self.datadir
        mock_st_method.containers = True
        src_list = ["some_src", "some_src2", "some_src3"]

        disk_storage_backend.write(src=src_list,
                                   ip=mock.ANY,
                                   storage_method=mock_st_method,
                                   storage_medium=mock_st_medium)

        expected_dest = self.datadir
        expected_copy_calls = [
            mock.call(src, expected_dest, block_size=65536) for src in src_list
        ]

        self.assertEqual(mock_copy.call_count, 3)
        mock_copy.assert_has_calls(expected_copy_calls)
        mock_st_obj.assert_called_once()
Exemple #11
0
    def test_write_dest_is_path_to_container(self, mock_st_medium,
                                             mock_st_method, mock_st_obj,
                                             mock_copy):
        disk_storage_backend = DiskStorageBackend()
        mock_st_medium.storage_target.target = self.datadir
        mock_st_method.containers = True

        disk_storage_backend.write(src="some_src",
                                   ip=mock.ANY,
                                   container=mock_st_method.containers,
                                   storage_medium=mock_st_medium)

        mock_copy.assert_called_once()
        mock_st_obj.assert_called_once_with(
            container=True,
            content_location_type=DISK,
            content_location_value='bar.tar',
            ip=mock.ANY,
            storage_medium=mock_st_medium,
        )
Exemple #12
0
    def test_delete_container(self, mock_storage_obj):
        disk_storage_backend = DiskStorageBackend()

        aic_pk = 1234
        archive_filename = "archive_file"
        xml_file = self.create_file(archive_filename, "xml")
        aic_xml = self.create_file(aic_pk, "xml")
        tar_path = self.create_container_files(archive_filename, "tar")

        mock_storage_obj.container = True
        mock_storage_obj.ip.aic.pk = aic_pk
        mock_storage_obj.content_location_value = tar_path

        self.assertTrue(os.path.exists(xml_file))
        self.assertTrue(os.path.exists(aic_xml))
        self.assertTrue(os.path.exists(tar_path))

        disk_storage_backend.delete(mock_storage_obj)

        self.assertFalse(os.path.exists(xml_file))
        self.assertFalse(os.path.exists(aic_xml))
        self.assertFalse(os.path.exists(tar_path))
Exemple #13
0
    def test_write_src_with_multi_files_not_container(self, st_medium,
                                                      mock_st_method,
                                                      mock_st_obj, mock_copy,
                                                      ip):
        disk_storage_backend = DiskStorageBackend()
        st_medium.storage_target.target = self.datadir
        mock_st_method.containers = False
        ip.object_identifier_value = "some/object/path"
        src_list = ["some_src", "some_src2", "some_src3"]

        disk_storage_backend.write(src=src_list,
                                   ip=ip,
                                   storage_method=mock_st_method,
                                   storage_medium=st_medium)

        expected_dest = os.path.join(self.datadir, "some/object/path")
        expected_copy_calls = [
            mock.call(src, expected_dest, block_size=65536) for src in src_list
        ]

        self.assertEqual(mock_copy.call_count, 3)
        mock_copy.assert_has_calls(expected_copy_calls)
        mock_st_obj.assert_called_once()
Exemple #14
0
    def test_write_destination_not_a_dir_should_raise_exception(
            self, mock_storage_medium, mock_storage_method):
        disk_storage_backend = DiskStorageBackend()
        mock_storage_medium.storage_target.target = "some_bad_destination"

        with self.assertRaisesRegexp(
                ValueError,
                "{} is not a directory".format("some_bad_destination")):
            disk_storage_backend.write(src="some_src",
                                       ip=mock.ANY,
                                       storage_method=mock_storage_method,
                                       storage_medium=mock_storage_medium)

        with self.assertRaisesRegexp(
                ValueError,
                "{} is not a directory".format("some_bad_destination")):
            disk_storage_backend.write(src=["some_src"],
                                       ip=mock.ANY,
                                       storage_method=mock_storage_method,
                                       storage_medium=mock_storage_medium)