test docker and docker_image directly

This commit is contained in:
Anthony Sottile
2023-02-04 16:50:40 -05:00
parent b609368ca5
commit 0afb95ccca
9 changed files with 46 additions and 88 deletions

View File

@@ -138,9 +138,8 @@ def run_hook(
entry_exe, *cmd_rest = helpers.hook_cmd(entry, args)
entry_tag = ('--entrypoint', entry_exe, docker_tag(prefix))
cmd = (*docker_cmd(), *entry_tag, *cmd_rest)
return helpers.run_xargs(
cmd,
(*docker_cmd(), *entry_tag, *cmd_rest),
file_args,
require_serial=require_serial,
color=color,

View File

@@ -20,9 +20,10 @@ def run_language(
prefix = Prefix(str(path))
version = version or language.get_default_version()
language.install_environment(prefix, version, deps)
health_error = language.health_check(prefix, version)
assert health_error is None, health_error
if language.ENVIRONMENT_DIR is not None:
language.install_environment(prefix, version, deps)
health_error = language.health_check(prefix, version)
assert health_error is None, health_error
with language.in_env(prefix, version):
ret, out = language.run_hook(
prefix,

View File

@@ -1,17 +0,0 @@
- id: docker-hook
name: Docker test hook
entry: echo
language: docker
files: \.txt$
- id: docker-hook-arg
name: Docker test hook
entry: echo -n
language: docker
files: \.txt$
- id: docker-hook-failing
name: Docker test hook with nonzero exit code
entry: bork
language: docker
files: \.txt$

View File

@@ -1,3 +0,0 @@
FROM ubuntu:focal
CMD ["echo", "This is overwritten by the .pre-commit-hooks.yaml 'entry'"]

View File

@@ -1,8 +0,0 @@
- id: echo-entrypoint
name: echo (via --entrypoint)
language: docker_image
entry: --entrypoint echo ubuntu:focal
- id: echo-cmd
name: echo (via cmd)
language: docker_image
entry: ubuntu:focal echo

View File

@@ -6,24 +6,13 @@ import subprocess
import pytest
from pre_commit.util import CalledProcessError
from pre_commit.util import cmd_output
from pre_commit.util import cmd_output_b
from testing.auto_namedtuple import auto_namedtuple
TESTING_DIR = os.path.abspath(os.path.dirname(__file__))
def docker_is_running() -> bool: # pragma: win32 no cover
try:
cmd_output_b('docker', 'ps')
except CalledProcessError: # pragma: no cover
return False
else:
return True
def get_resource_path(path):
return os.path.join(TESTING_DIR, 'resources', path)
@@ -41,10 +30,6 @@ def cmd_output_mocked_pre_commit_home(
return ret, out.replace('\r\n', '\n'), None
skipif_cant_run_docker = pytest.mark.skipif(
os.name == 'nt' or not docker_is_running(),
reason="Docker isn't running or can't be accessed",
)
xfailif_windows = pytest.mark.xfail(os.name == 'nt', reason='windows')

View File

@@ -0,0 +1,27 @@
from __future__ import annotations
from pre_commit.languages import docker_image
from testing.language_helpers import run_language
from testing.util import xfailif_windows
@xfailif_windows # pragma: win32 no cover
def test_docker_image_hook_via_entrypoint(tmp_path):
ret = run_language(
tmp_path,
docker_image,
'--entrypoint echo ubuntu:22.04',
args=('hello hello world',),
)
assert ret == (0, b'hello hello world\n')
@xfailif_windows # pragma: win32 no cover
def test_docker_image_hook_via_args(tmp_path):
ret = run_language(
tmp_path,
docker_image,
'ubuntu:22.04 echo',
args=('hello hello world',),
)
assert ret == (0, b'hello hello world\n')

View File

@@ -11,6 +11,8 @@ import pytest
from pre_commit.languages import docker
from pre_commit.util import CalledProcessError
from testing.language_helpers import run_language
from testing.util import xfailif_windows
DOCKER_CGROUP_EXAMPLE = b'''\
12:hugetlb:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
@@ -181,3 +183,15 @@ def test_get_docker_path_in_docker_docker_in_docker(in_docker):
err = CalledProcessError(1, (), b'', b'')
with mock.patch.object(docker, 'cmd_output_b', side_effect=err):
assert docker._get_docker_path('/project') == '/project'
@xfailif_windows # pragma: win32 no cover
def test_docker_hook(tmp_path):
dockerfile = '''\
FROM ubuntu:22.04
CMD ["echo", "This is overwritten by the entry"']
'''
tmp_path.joinpath('Dockerfile').write_text(dockerfile)
ret = run_language(tmp_path, docker, 'echo hello hello world')
assert ret == (0, b'hello hello world\n')

View File

@@ -30,7 +30,6 @@ from testing.fixtures import make_repo
from testing.fixtures import modify_manifest
from testing.util import cwd
from testing.util import get_resource_path
from testing.util import skipif_cant_run_docker
def _norm_out(b):
@@ -163,45 +162,6 @@ def test_language_versioned_python_hook(tempdir_factory, store):
)
@skipif_cant_run_docker # pragma: win32 no cover
def test_run_a_docker_hook(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'docker_hooks_repo',
'docker-hook',
['Hello World from docker'], b'Hello World from docker\n',
)
@skipif_cant_run_docker # pragma: win32 no cover
def test_run_a_docker_hook_with_entry_args(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'docker_hooks_repo',
'docker-hook-arg',
['Hello World from docker'], b'Hello World from docker',
)
@skipif_cant_run_docker # pragma: win32 no cover
def test_run_a_failing_docker_hook(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'docker_hooks_repo',
'docker-hook-failing',
['Hello World from docker'],
mock.ANY, # an error message about `bork` not existing
expected_return_code=127,
)
@skipif_cant_run_docker # pragma: win32 no cover
@pytest.mark.parametrize('hook_id', ('echo-entrypoint', 'echo-cmd'))
def test_run_a_docker_image_hook(tempdir_factory, store, hook_id):
_test_hook_repo(
tempdir_factory, store, 'docker_image_hooks_repo',
hook_id,
['Hello World from docker'], b'Hello World from docker\n',
)
def test_system_hook_with_spaces(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'system_hook_with_spaces_repo',