Beispiel #1
0
    def test_fail_download(self):
        url_artifact_zip = "https://cdn.askanna.eu/v1/artifact/a-fail.zip"
        content_lenght = 213243556  # 21 chunks of 20 MiB per chunk
        output_zip_file = f"{self.tempdir}/artifact_download_fail.zip"

        self.responses.add(
            responses.HEAD,
            url=url_artifact_zip,
            headers={"Content-Length": str(content_lenght), "Accept-Ranges": "bytes"},
            content_type="application/zip",
            status=200,
        )
        self.responses.add(
            responses.GET,
            url=url_artifact_zip,
            headers={"Range": f"bytes=0-{content_lenght}"},
            stream=True,
            content_type="application/zip",
            status=200,
        )

        download = ChunkedDownload(url=url_artifact_zip)
        with pytest.raises(SystemExit) as pytest_wrapped_e:
            download.download(output_file=output_zip_file)

        assert pytest_wrapped_e.type == SystemExit
        assert pytest_wrapped_e.value.code == 1
Beispiel #2
0
def get(suuid, output_path):
    """
    Download a result of a run
    """
    result_url = f"{client.base_url}result/{suuid}/"
    stable_download = ChunkedDownload(result_url)

    if stable_download.status_code != 200:
        click.echo(f"{stable_download.status_code} - We cannot find this result for you", err=True)
        sys.exit(1)

    if not output_path:
        file_extension = content_type_file_extension(str(stable_download.content_type))
        output_path = f"result_{suuid}{file_extension}"

    if os.path.isdir(output_path):
        click.echo(
            "The output argument is a directory. Please provide a file name for the output.",
            err=True
        )
        sys.exit(1)

    if os.path.exists(output_path):
        click.echo(
            "The output file already exists. We will not overwrite the existing file.",
            err=True
        )
        sys.exit(1)

    click.echo("Downloading the result has started...")
    stable_download.download(output_file=output_path)
    click.echo("We have succesfully downloaded the result.")
    click.echo(f"The result is saved in: {output_path}")
Beispiel #3
0
    def test_actual_download_one_chunk(self):
        url_artifact_zip = "https://cdn.askanna.eu/v1/artifact/a-zip-file-one-chunk.zip"
        zip_file = create_zip_file(self.tempdir, 100)
        output_zip_file = f"{self.tempdir}/artifact_download_random_json_one_chunk.zip"

        with open(zip_file, "rb") as f:
            content = f.read(os.path.getsize(zip_file))

        self.responses.add(
            responses.HEAD,
            url=url_artifact_zip,
            headers={"Content-Length": str(os.path.getsize(zip_file)), "Accept-Ranges": "bytes"},
            content_type="application/zip",
            status=200,
        )
        self.responses.add(
            responses.GET,
            url=url_artifact_zip,
            headers={"Range": f"bytes=0-{os.path.getsize(zip_file)}"},
            stream=True,
            content_type="application/zip",
            status=206,
            body=content
        )

        download = ChunkedDownload(url=url_artifact_zip)
        download.download(output_file=output_zip_file)

        self.assertEqual(os.path.getsize(zip_file), os.path.getsize(output_zip_file))

        os.remove(zip_file)
        os.remove(output_zip_file)

        self.assertEqual(download.status_code, 200)
Beispiel #4
0
def get(suuid, output_path):
    """
    Download an artifact of a run
    """
    url = f"{client.base_url}artifact/{suuid}/"

    if not output_path:
        output_path = f"artifact_{suuid}.zip"

    if os.path.isdir(output_path):
        click.echo(
            "The output argument is a directory. Please provide a file name (zip) for the output.",
            err=True)
        sys.exit(1)

    if os.path.exists(output_path):
        click.echo(
            "The output file already exists. We will not overwrite the existing file.",
            err=True,
        )
        sys.exit(1)

    stable_download = ChunkedDownload(url=url)
    if stable_download.status_code != 200:
        click.echo("We cannot find this artifact for you.", err=True)
        sys.exit(1)
    click.echo("Downloading the artifact has started...")
    stable_download.download(output_file=output_path)
    click.echo("We have succesfully downloaded the artifact.")
    click.echo(f"The artifact is saved in: {output_path}")
Beispiel #5
0
    def test_setup_download(self):
        url_artifact_zip = "https://cdn.askanna.eu/v1/artifact/test-setup-download.zip"
        content_lenght = 213243556  # 21 chunks of 20 MiB per chunk

        self.responses.add(
            responses.HEAD,
            url=url_artifact_zip,
            headers={"Content-Length": str(content_lenght), "Accept-Ranges": "bytes"},
            content_type="application/zip",
            status=200,
        )

        download = ChunkedDownload(url=url_artifact_zip)
        download.setup_download()

        self.assertEqual(download.status_code, 200)
        self.assertEqual(len(download.download_queue), 21)
        self.assertEqual(download.download_queue[-1][-1], content_lenght)
Beispiel #6
0
 def test_init_download_file_not_exist(self):
     download = ChunkedDownload(url=self.base_url + "artifact/wxyz-wxyz-wxyz-wxyz/")
     self.assertEqual(download.status_code, 404)
Beispiel #7
0
 def test_init_download(self):
     download = ChunkedDownload(url=self.base_url + "artifact/abcd-abcd-abcd-abcd/")
     self.assertEqual(download.content_type, "application/zip")
     self.assertEqual(download.status_code, 200)