Exemple #1
0
    def test_eoproduct_get_quicklook_ok_existing(self):
        """EOProduct.get_quicklook must return the path to an already downloaded quicklook"""  # noqa
        quicklook_dir = os.path.join(tempfile.gettempdir(), "quicklooks")
        quicklook_basename = "the_quicklook.png"
        existing_quicklook_file_path = os.path.join(quicklook_dir,
                                                    quicklook_basename)
        if not os.path.exists(quicklook_dir):
            os.mkdir(quicklook_dir)
        with open(existing_quicklook_file_path, "wb") as fh:
            fh.write(b"content")
        product = EOProduct(self.provider,
                            self.eoproduct_props,
                            productType=self.product_type)
        product.properties["quicklook"] = "https://fake.url.to/quicklook"
        mock_downloader = mock.MagicMock(
            spec_set=Download(provider=self.provider, config=None))
        mock_downloader.config = config.PluginConfig.from_mapping(
            {"outputs_prefix": tempfile.gettempdir()})
        product.register_downloader(mock_downloader, None)

        quicklook_file_path = product.get_quicklook(
            filename=quicklook_basename)
        self.assertEqual(self.requests_http_get.call_count, 0)
        self.assertEqual(quicklook_file_path, existing_quicklook_file_path)
        os.remove(existing_quicklook_file_path)
        os.rmdir(quicklook_dir)
Exemple #2
0
    def test_get_data_bad_download_on_unsupported_dataset_address_scheme_error(
            self):
        """If downloader doesn't return the downloaded file path, get_data must return an empty array"""  # noqa
        product = EOProduct(self.provider,
                            self.eoproduct_props,
                            productType=self.product_type)

        product.driver = mock.MagicMock(spec_set=NoDriver())
        product.driver.get_data_address.side_effect = UnsupportedDatasetAddressScheme

        mock_downloader = mock.MagicMock(spec_set=Download(
            provider=self.provider,
            config=config.PluginConfig.from_mapping({"extract": False}),
        ))
        mock_downloader.download.return_value = None
        mock_authenticator = mock.MagicMock(spec_set=Authentication(
            provider=self.provider,
            config=config.PluginConfig.from_mapping({})))

        product.register_downloader(mock_downloader, mock_authenticator)

        self.assertRaises(DownloadError, product.download)

        data, band = self.execute_get_data(product, give_back=("band", ))

        self.assertEqual(product.driver.get_data_address.call_count, 1)
        product.driver.get_data_address.assert_called_with(product, band)
        self.assertIsInstance(data, xr.DataArray)
        self.assertEqual(data.values.size, 0)
Exemple #3
0
    def test_eoproduct_get_quicklook_http_error(self):
        """EOProduct.get_quicklook must return an empty string if there was an error during retrieval"""  # noqa
        product = EOProduct(self.provider,
                            self.eoproduct_props,
                            productType=self.product_type)
        product.properties["quicklook"] = "https://fake.url.to/quicklook"

        self.requests_http_get.return_value.__enter__.return_value.raise_for_status.side_effect = (  # noqa
            requests.HTTPError)
        mock_downloader = mock.MagicMock(
            spec_set=Download(provider=self.provider, config=None))
        mock_downloader.config = config.PluginConfig.from_mapping(
            {"outputs_prefix": tempfile.gettempdir()})
        product.register_downloader(mock_downloader, None)

        quicklook_file_path = product.get_quicklook()
        self.requests_http_get.assert_called_with(
            "https://fake.url.to/quicklook", stream=True, auth=None)
        self.assertEqual(quicklook_file_path, "")
Exemple #4
0
    def test_eoproduct_get_quicklook_ok(self):
        """EOProduct.get_quicklook must return the path to the successfully downloaded quicklook"""  # noqa
        product = EOProduct(self.provider,
                            self.eoproduct_props,
                            productType=self.product_type)
        product.properties["quicklook"] = "https://fake.url.to/quicklook"

        self.requests_http_get.return_value = self._quicklook_response()
        mock_downloader = mock.MagicMock(
            spec_set=Download(provider=self.provider, config=None))
        mock_downloader.config = config.PluginConfig.from_mapping(
            {"outputs_prefix": tempfile.gettempdir()})
        product.register_downloader(mock_downloader, None)

        quicklook_file_path = product.get_quicklook()
        self.requests_http_get.assert_called_with(
            "https://fake.url.to/quicklook", stream=True, auth=None)
        self.assertEqual(os.path.basename(quicklook_file_path),
                         product.properties["id"])
        self.assertEqual(
            os.path.dirname(quicklook_file_path),
            os.path.join(tempfile.gettempdir(), "quicklooks"),
        )
        os.remove(quicklook_file_path)

        # Test the same thing as above but with an explicit name given to the downloaded File
        quicklook_file_path = product.get_quicklook(
            filename="the_quicklook.png")
        self.requests_http_get.assert_called_with(
            "https://fake.url.to/quicklook", stream=True, auth=None)
        self.assertEqual(self.requests_http_get.call_count, 2)
        self.assertEqual(os.path.basename(quicklook_file_path),
                         "the_quicklook.png")
        self.assertEqual(
            os.path.dirname(quicklook_file_path),
            os.path.join(tempfile.gettempdir(), "quicklooks"),
        )
        os.remove(quicklook_file_path)

        # Overall teardown
        os.rmdir(os.path.dirname(quicklook_file_path))
Exemple #5
0
    def test_get_data_download_on_unsupported_dataset_address_scheme_error(
            self):
        """If a product is not on the local filesystem, it must download itself before returning the data"""  # noqa
        product = EOProduct(self.provider,
                            self.eoproduct_props,
                            productType=self.product_type)

        def get_data_address(*args, **kwargs):
            eo_product = args[0]
            if eo_product.location.startswith("https"):
                raise UnsupportedDatasetAddressScheme
            return self.local_band_file

        product.driver = mock.MagicMock(spec_set=NoDriver())
        product.driver.get_data_address.side_effect = get_data_address

        mock_downloader = mock.MagicMock(spec_set=Download(
            provider=self.provider,
            config=config.PluginConfig.from_mapping({
                "extract": False,
                "archive_depth": 1
            }),
        ))
        mock_downloader.download.return_value = self.local_product_as_archive_path
        # mock_downloader.config = {'extract': False, 'archive_depth': 1}
        mock_authenticator = mock.MagicMock(spec_set=Authentication(
            provider=self.provider,
            config=config.PluginConfig.from_mapping({})))

        product.register_downloader(mock_downloader,
                                    mock_authenticator.authenticate())
        data, band = self.execute_get_data(product, give_back=("band", ))

        self.assertEqual(product.driver.get_data_address.call_count, 2)
        product.driver.get_data_address.assert_called_with(product, band)
        self.assertIsInstance(data, xr.DataArray)
        self.assertNotEqual(data.values.size, 0)