summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/s3_fetch.py
blob: 241d2648b504a2b64758a0f2b9e62ddc4849802e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Copyright 2013-2023 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import os

import pytest

import spack.config as spack_config
import spack.error
import spack.fetch_strategy as spack_fs
import spack.stage as spack_stage


@pytest.mark.parametrize("_fetch_method", ["curl", "urllib"])
def test_s3fetchstrategy_sans_url(_fetch_method):
    """Ensure constructor with no URL fails."""
    with spack_config.override("config:url_fetch_method", _fetch_method):
        with pytest.raises(ValueError):
            spack_fs.S3FetchStrategy(None)


@pytest.mark.parametrize("_fetch_method", ["curl", "urllib"])
def test_s3fetchstrategy_bad_url(tmpdir, _fetch_method):
    """Ensure fetch with bad URL fails as expected."""
    testpath = str(tmpdir)

    with spack_config.override("config:url_fetch_method", _fetch_method):
        fetcher = spack_fs.S3FetchStrategy(url="file:///does-not-exist")
        assert fetcher is not None

        with spack_stage.Stage(fetcher, path=testpath) as stage:
            assert stage is not None
            assert fetcher.archive_file is None
            with pytest.raises(spack.error.FetchError):
                fetcher.fetch()


@pytest.mark.parametrize("_fetch_method", ["curl", "urllib"])
def test_s3fetchstrategy_downloaded(tmpdir, _fetch_method):
    """Ensure fetch with archive file already downloaded is a noop."""
    testpath = str(tmpdir)
    archive = os.path.join(testpath, "s3.tar.gz")

    with spack_config.override("config:url_fetch_method", _fetch_method):

        class Archived_S3FS(spack_fs.S3FetchStrategy):
            @property
            def archive_file(self):
                return archive

        url = "s3:///{0}".format(archive)
        fetcher = Archived_S3FS(url=url)
        with spack_stage.Stage(fetcher, path=testpath):
            fetcher.fetch()