Add test fixture for setting base image

pull/909/head
YuviPanda 2022-07-23 09:17:30 -07:00
rodzic f35a948e75
commit 446e67820e
9 zmienionych plików z 43 dodań i 32 usunięć

Wyświetl plik

@ -100,6 +100,14 @@ def run_repo2docker():
return run_test
@pytest.fixture()
def base_image():
"""
Base ubuntu image to use when testing specific BuildPacks
"""
return "buildpack-deps:bionic"
def _add_content_to_git(repo_dir):
"""Add content to file 'test' in git repository and commit."""
# use append mode so this can be called multiple times

Wyświetl plik

@ -6,21 +6,21 @@ from repo2docker import buildpacks
@pytest.mark.parametrize("binder_dir", ["binder", ".binder", ""])
def test_binder_dir(tmpdir, binder_dir):
def test_binder_dir(tmpdir, binder_dir, base_image):
tmpdir.chdir()
if binder_dir:
os.mkdir(binder_dir)
bp = buildpacks.BuildPack()
bp = buildpacks.BuildPack(base_image)
assert binder_dir == bp.binder_dir
assert bp.binder_path("foo.yaml") == os.path.join(binder_dir, "foo.yaml")
def test_exclusive_binder_dir(tmpdir):
def test_exclusive_binder_dir(tmpdir, base_image):
tmpdir.chdir()
os.mkdir("./binder")
os.mkdir("./.binder")
bp = buildpacks.BuildPack()
bp = buildpacks.BuildPack(base_image)
with pytest.raises(RuntimeError):
_ = bp.binder_dir

Wyświetl plik

@ -5,36 +5,37 @@ import pytest
from repo2docker.buildpacks import LegacyBinderDockerBuildPack, PythonBuildPack
from repo2docker.utils import chdir
from tests.conftest import base_image
def test_legacy_raises():
def test_legacy_raises(base_image):
# check legacy buildpack raises on a repo that triggers it
with TemporaryDirectory() as repodir:
with open(pjoin(repodir, "Dockerfile"), "w") as d:
d.write("FROM andrewosh/binder-base")
with chdir(repodir):
bp = LegacyBinderDockerBuildPack()
bp = LegacyBinderDockerBuildPack(base_image)
with pytest.raises(RuntimeError):
bp.detect()
def test_legacy_doesnt_detect():
def test_legacy_doesnt_detect(base_image):
# check legacy buildpack doesn't trigger
with TemporaryDirectory() as repodir:
with open(pjoin(repodir, "Dockerfile"), "w") as d:
d.write("FROM andrewosh/some-image")
with chdir(repodir):
bp = LegacyBinderDockerBuildPack()
bp = LegacyBinderDockerBuildPack(base_image)
assert not bp.detect()
def test_legacy_on_repo_without_dockerfile():
def test_legacy_on_repo_without_dockerfile(base_image):
# check legacy buildpack doesn't trigger on a repo w/o Dockerfile
with TemporaryDirectory() as repodir:
with chdir(repodir):
bp = LegacyBinderDockerBuildPack()
bp = LegacyBinderDockerBuildPack(base_image)
assert not bp.detect()

Wyświetl plik

@ -30,7 +30,7 @@ def test_cache_from_base(tmpdir):
assert called_kwargs["cache_from"] == cache_from
def test_cache_from_docker(tmpdir):
def test_cache_from_docker(tmpdir, base_image):
cache_from = ["image-1:latest"]
fake_log_value = {"stream": "fake"}
fake_client = MagicMock(spec=docker.APIClient)
@ -42,7 +42,7 @@ def test_cache_from_docker(tmpdir):
with tmpdir.join("Dockerfile").open("w") as f:
f.write("FROM scratch\n")
for line in DockerBuildPack().build(
for line in DockerBuildPack(base_image).build(
fake_client, "image-2", 100, {}, cache_from, extra_build_kwargs
):
assert line == fake_log_value

Wyświetl plik

@ -9,20 +9,20 @@ import pytest
from repo2docker import buildpacks
def test_empty_env_yml(tmpdir):
def test_empty_env_yml(tmpdir, base_image):
tmpdir.chdir()
p = tmpdir.join("environment.yml")
p.write("")
bp = buildpacks.CondaBuildPack()
bp = buildpacks.CondaBuildPack(base_image)
py_ver = bp.python_version
# If the environment.yml is empty python_version will get an empty string
assert py_ver == ""
def test_no_dict_env_yml(tmpdir):
def test_no_dict_env_yml(tmpdir, base_image):
tmpdir.chdir()
q = tmpdir.join("environment.yml")
q.write("numpy\n " "matplotlib\n")
bq = buildpacks.CondaBuildPack()
bq = buildpacks.CondaBuildPack(base_image)
with pytest.raises(TypeError):
py_ver = bq.python_version

Wyświetl plik

@ -12,8 +12,8 @@ from repo2docker.buildpacks import BuildPack
URL = "https://github.com/binderhub-ci-repos/repo2docker-ci-clone-depth"
def test_buildpack_labels_rendered():
bp = BuildPack()
def test_buildpack_labels_rendered(base_image):
bp = BuildPack(base_image)
assert "LABEL" not in bp.render()
bp.labels["first_label"] = "firstlabel"
assert 'LABEL first_label="firstlabel"\n' in bp.render()

Wyświetl plik

@ -48,14 +48,16 @@ def test_memory_limit_enforced(tmpdir):
@pytest.mark.parametrize("BuildPack", [BaseImage, DockerBuildPack])
def test_memlimit_argument_type(BuildPack):
def test_memlimit_argument_type(BuildPack, base_image):
# check that an exception is raised when the memory limit isn't an int
fake_log_value = {"stream": "fake"}
fake_client = MagicMock(spec=docker.APIClient)
fake_client.build.return_value = iter([fake_log_value])
with pytest.raises(ValueError) as exc_info:
for line in BuildPack().build(fake_client, "image-2", "10Gi", {}, [], {}):
for line in BuildPack(base_image).build(
fake_client, "image-2", "10Gi", {}, [], {}
):
pass
assert "The memory limit has to be specified as an" in str(exc_info.value)

Wyświetl plik

@ -6,7 +6,7 @@ from repo2docker import buildpacks
@pytest.mark.parametrize("binder_dir", ["", ".binder", "binder"])
def test_combine_preassemble_steps(tmpdir, binder_dir):
def test_combine_preassemble_steps(tmpdir, binder_dir, base_image):
tmpdir.chdir()
if binder_dir:
os.mkdir(binder_dir)
@ -19,7 +19,7 @@ def test_combine_preassemble_steps(tmpdir, binder_dir):
with open(os.path.join(binder_dir, "runtime.txt"), "w") as f:
f.write("r-2019-01-30")
bp = buildpacks.RBuildPack()
bp = buildpacks.RBuildPack(base_image)
files = bp.get_preassemble_script_files()
assert len(files) == 2

Wyświetl plik

@ -10,7 +10,7 @@ from repo2docker import buildpacks
@pytest.mark.parametrize(
"runtime_version, expected", [("", "4.2"), ("3.6", "3.6"), ("3.5.1", "3.5")]
)
def test_version_specification(tmpdir, runtime_version, expected):
def test_version_specification(tmpdir, runtime_version, expected, base_image):
tmpdir.chdir()
with open("runtime.txt", "w") as f:
@ -18,17 +18,17 @@ def test_version_specification(tmpdir, runtime_version, expected):
runtime_version += "-"
f.write(f"r-{runtime_version}2019-01-01")
r = buildpacks.RBuildPack()
r = buildpacks.RBuildPack(base_image)
assert r.r_version.startswith(expected)
def test_version_completion(tmpdir):
def test_version_completion(tmpdir, base_image):
tmpdir.chdir()
with open("runtime.txt", "w") as f:
f.write(f"r-3.6-2019-01-01")
r = buildpacks.RBuildPack()
r = buildpacks.RBuildPack(base_image)
assert r.r_version == "3.6.3"
@ -40,17 +40,17 @@ def test_version_completion(tmpdir):
("r-3.5-2019-01-01", (2019, 1, 1)),
],
)
def test_mran_date(tmpdir, runtime, expected):
def test_mran_date(tmpdir, runtime, expected, base_image):
tmpdir.chdir()
with open("runtime.txt", "w") as f:
f.write(runtime)
r = buildpacks.RBuildPack()
r = buildpacks.RBuildPack(base_image)
assert r.checkpoint_date == date(*expected)
def test_snapshot_rspm_date():
def test_snapshot_rspm_date(base_image):
test_dates = {
# Even though there is no snapshot specified in the interface at https://packagemanager.rstudio.com/client/#/repos/1/overview
# For 2021 Oct 22, the API still returns a valid URL that one can install
@ -61,7 +61,7 @@ def test_snapshot_rspm_date():
date(2022, 1, 1): date(2022, 1, 1),
}
r = buildpacks.RBuildPack()
r = buildpacks.RBuildPack(base_image)
for requested, expected in test_dates.items():
snapshot_url = r.get_rspm_snapshot_url(requested)
assert snapshot_url.startswith(
@ -75,7 +75,7 @@ def test_snapshot_rspm_date():
@pytest.mark.parametrize("expected", [date(2019, 12, 29), date(2019, 12, 26)])
@pytest.mark.parametrize("requested", [date(2019, 12, 31)])
def test_snapshot_mran_date(requested, expected):
def test_snapshot_mran_date(requested, expected, base_image):
def mock_request_head(url):
r = Response()
if url == "https://mran.microsoft.com/snapshot/" + expected.isoformat():
@ -86,7 +86,7 @@ def test_snapshot_mran_date(requested, expected):
return r
with patch("requests.head", side_effect=mock_request_head):
r = buildpacks.RBuildPack()
r = buildpacks.RBuildPack(base_image)
assert (
r.get_mran_snapshot_url(requested)
== f"https://mran.microsoft.com/snapshot/{expected.isoformat()}"