mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-18 16:44:42 +04:00
test docker and docker_image directly
This commit is contained in:
parent
b609368ca5
commit
0afb95ccca
9 changed files with 46 additions and 88 deletions
|
|
@ -138,9 +138,8 @@ def run_hook(
|
||||||
entry_exe, *cmd_rest = helpers.hook_cmd(entry, args)
|
entry_exe, *cmd_rest = helpers.hook_cmd(entry, args)
|
||||||
|
|
||||||
entry_tag = ('--entrypoint', entry_exe, docker_tag(prefix))
|
entry_tag = ('--entrypoint', entry_exe, docker_tag(prefix))
|
||||||
cmd = (*docker_cmd(), *entry_tag, *cmd_rest)
|
|
||||||
return helpers.run_xargs(
|
return helpers.run_xargs(
|
||||||
cmd,
|
(*docker_cmd(), *entry_tag, *cmd_rest),
|
||||||
file_args,
|
file_args,
|
||||||
require_serial=require_serial,
|
require_serial=require_serial,
|
||||||
color=color,
|
color=color,
|
||||||
|
|
|
||||||
|
|
@ -20,9 +20,10 @@ def run_language(
|
||||||
prefix = Prefix(str(path))
|
prefix = Prefix(str(path))
|
||||||
version = version or language.get_default_version()
|
version = version or language.get_default_version()
|
||||||
|
|
||||||
language.install_environment(prefix, version, deps)
|
if language.ENVIRONMENT_DIR is not None:
|
||||||
health_error = language.health_check(prefix, version)
|
language.install_environment(prefix, version, deps)
|
||||||
assert health_error is None, health_error
|
health_error = language.health_check(prefix, version)
|
||||||
|
assert health_error is None, health_error
|
||||||
with language.in_env(prefix, version):
|
with language.in_env(prefix, version):
|
||||||
ret, out = language.run_hook(
|
ret, out = language.run_hook(
|
||||||
prefix,
|
prefix,
|
||||||
|
|
|
||||||
|
|
@ -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$
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
FROM ubuntu:focal
|
|
||||||
|
|
||||||
CMD ["echo", "This is overwritten by the .pre-commit-hooks.yaml 'entry'"]
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -6,24 +6,13 @@ import subprocess
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit.util import CalledProcessError
|
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
from pre_commit.util import cmd_output_b
|
|
||||||
from testing.auto_namedtuple import auto_namedtuple
|
from testing.auto_namedtuple import auto_namedtuple
|
||||||
|
|
||||||
|
|
||||||
TESTING_DIR = os.path.abspath(os.path.dirname(__file__))
|
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):
|
def get_resource_path(path):
|
||||||
return os.path.join(TESTING_DIR, 'resources', 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
|
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')
|
xfailif_windows = pytest.mark.xfail(os.name == 'nt', reason='windows')
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
27
tests/languages/docker_image_test.py
Normal file
27
tests/languages/docker_image_test.py
Normal 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')
|
||||||
|
|
@ -11,6 +11,8 @@ import pytest
|
||||||
|
|
||||||
from pre_commit.languages import docker
|
from pre_commit.languages import docker
|
||||||
from pre_commit.util import CalledProcessError
|
from pre_commit.util import CalledProcessError
|
||||||
|
from testing.language_helpers import run_language
|
||||||
|
from testing.util import xfailif_windows
|
||||||
|
|
||||||
DOCKER_CGROUP_EXAMPLE = b'''\
|
DOCKER_CGROUP_EXAMPLE = b'''\
|
||||||
12:hugetlb:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
|
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'')
|
err = CalledProcessError(1, (), b'', b'')
|
||||||
with mock.patch.object(docker, 'cmd_output_b', side_effect=err):
|
with mock.patch.object(docker, 'cmd_output_b', side_effect=err):
|
||||||
assert docker._get_docker_path('/project') == '/project'
|
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')
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,6 @@ from testing.fixtures import make_repo
|
||||||
from testing.fixtures import modify_manifest
|
from testing.fixtures import modify_manifest
|
||||||
from testing.util import cwd
|
from testing.util import cwd
|
||||||
from testing.util import get_resource_path
|
from testing.util import get_resource_path
|
||||||
from testing.util import skipif_cant_run_docker
|
|
||||||
|
|
||||||
|
|
||||||
def _norm_out(b):
|
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):
|
def test_system_hook_with_spaces(tempdir_factory, store):
|
||||||
_test_hook_repo(
|
_test_hook_repo(
|
||||||
tempdir_factory, store, 'system_hook_with_spaces_repo',
|
tempdir_factory, store, 'system_hook_with_spaces_repo',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue