|
| 1 | +import builtins |
| 2 | +import json |
| 3 | +import ntpath |
| 4 | +import os.path |
| 5 | +import posixpath |
1 | 6 | from unittest import mock |
2 | 7 |
|
| 8 | +import pytest |
| 9 | + |
3 | 10 | from pre_commit.languages import docker |
4 | 11 |
|
5 | 12 |
|
6 | 13 | def test_docker_fallback_user(): |
7 | 14 | def invalid_attribute(): |
8 | 15 | raise AttributeError |
| 16 | + |
9 | 17 | with mock.patch.multiple( |
10 | | - 'os', create=True, |
11 | | - getuid=invalid_attribute, |
12 | | - getgid=invalid_attribute, |
| 18 | + 'os', create=True, |
| 19 | + getuid=invalid_attribute, |
| 20 | + getgid=invalid_attribute, |
13 | 21 | ): |
14 | 22 | assert docker.get_docker_user() == () |
| 23 | + |
| 24 | + |
| 25 | +def test_in_docker_no_file(): |
| 26 | + with mock.patch.object(builtins, 'open', side_effect=FileNotFoundError): |
| 27 | + assert docker._is_in_docker() is False |
| 28 | + |
| 29 | + |
| 30 | +def _mock_open(data): |
| 31 | + return mock.patch.object( |
| 32 | + builtins, |
| 33 | + 'open', |
| 34 | + new_callable=mock.mock_open, |
| 35 | + read_data=data, |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +def test_in_docker_docker_in_file(): |
| 40 | + docker_cgroup_example = b'''\ |
| 41 | +12:hugetlb:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 |
| 42 | +11:blkio:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 |
| 43 | +10:freezer:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 |
| 44 | +9:cpu,cpuacct:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 |
| 45 | +8:pids:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 |
| 46 | +7:rdma:/ |
| 47 | +6:net_cls,net_prio:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 |
| 48 | +5:cpuset:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 |
| 49 | +4:devices:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 |
| 50 | +3:memory:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 |
| 51 | +2:perf_event:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 |
| 52 | +1:name=systemd:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7 |
| 53 | +0::/system.slice/containerd.service |
| 54 | +''' # noqa: E501 |
| 55 | + with _mock_open(docker_cgroup_example): |
| 56 | + assert docker._is_in_docker() is True |
| 57 | + |
| 58 | + |
| 59 | +def test_in_docker_docker_not_in_file(): |
| 60 | + non_docker_cgroup_example = b'''\ |
| 61 | +12:perf_event:/ |
| 62 | +11:hugetlb:/ |
| 63 | +10:devices:/ |
| 64 | +9:blkio:/ |
| 65 | +8:rdma:/ |
| 66 | +7:cpuset:/ |
| 67 | +6:cpu,cpuacct:/ |
| 68 | +5:freezer:/ |
| 69 | +4:memory:/ |
| 70 | +3:pids:/ |
| 71 | +2:net_cls,net_prio:/ |
| 72 | +1:name=systemd:/init.scope |
| 73 | +0::/init.scope |
| 74 | +''' |
| 75 | + with _mock_open(non_docker_cgroup_example): |
| 76 | + assert docker._is_in_docker() is False |
| 77 | + |
| 78 | + |
| 79 | +def test_get_docker_path_not_in_docker_returns_same(): |
| 80 | + with mock.patch.object(docker, '_is_in_docker', return_value=False): |
| 81 | + assert docker._get_docker_path('abc') == 'abc' |
| 82 | + |
| 83 | + |
| 84 | +@pytest.fixture |
| 85 | +def in_docker(): |
| 86 | + with mock.patch.object(docker, '_is_in_docker', return_value=True): |
| 87 | + yield |
| 88 | + |
| 89 | + |
| 90 | +def _linux_commonpath(): |
| 91 | + return mock.patch.object(os.path, 'commonpath', posixpath.commonpath) |
| 92 | + |
| 93 | + |
| 94 | +def _nt_commonpath(): |
| 95 | + return mock.patch.object(os.path, 'commonpath', ntpath.commonpath) |
| 96 | + |
| 97 | + |
| 98 | +def _docker_output(out): |
| 99 | + ret = (0, out, b'') |
| 100 | + return mock.patch.object(docker, 'cmd_output_b', return_value=ret) |
| 101 | + |
| 102 | + |
| 103 | +def test_get_docker_path_in_docker_no_binds_same_path(in_docker): |
| 104 | + docker_out = json.dumps([{'Mounts': []}]).encode() |
| 105 | + |
| 106 | + with _docker_output(docker_out): |
| 107 | + assert docker._get_docker_path('abc') == 'abc' |
| 108 | + |
| 109 | + |
| 110 | +def test_get_docker_path_in_docker_binds_path_equal(in_docker): |
| 111 | + binds_list = [{'Source': '/opt/my_code', 'Destination': '/project'}] |
| 112 | + docker_out = json.dumps([{'Mounts': binds_list}]).encode() |
| 113 | + |
| 114 | + with _linux_commonpath(), _docker_output(docker_out): |
| 115 | + assert docker._get_docker_path('/project') == '/opt/my_code' |
| 116 | + |
| 117 | + |
| 118 | +def test_get_docker_path_in_docker_binds_path_complex(in_docker): |
| 119 | + binds_list = [{'Source': '/opt/my_code', 'Destination': '/project'}] |
| 120 | + docker_out = json.dumps([{'Mounts': binds_list}]).encode() |
| 121 | + |
| 122 | + with _linux_commonpath(), _docker_output(docker_out): |
| 123 | + path = '/project/test/something' |
| 124 | + assert docker._get_docker_path(path) == '/opt/my_code/test/something' |
| 125 | + |
| 126 | + |
| 127 | +def test_get_docker_path_in_docker_no_substring(in_docker): |
| 128 | + binds_list = [{'Source': '/opt/my_code', 'Destination': '/project'}] |
| 129 | + docker_out = json.dumps([{'Mounts': binds_list}]).encode() |
| 130 | + |
| 131 | + with _linux_commonpath(), _docker_output(docker_out): |
| 132 | + path = '/projectSuffix/test/something' |
| 133 | + assert docker._get_docker_path(path) == path |
| 134 | + |
| 135 | + |
| 136 | +def test_get_docker_path_in_docker_binds_path_many_binds(in_docker): |
| 137 | + binds_list = [ |
| 138 | + {'Source': '/something_random', 'Destination': '/not-related'}, |
| 139 | + {'Source': '/opt/my_code', 'Destination': '/project'}, |
| 140 | + {'Source': '/something-random-2', 'Destination': '/not-related-2'}, |
| 141 | + ] |
| 142 | + docker_out = json.dumps([{'Mounts': binds_list}]).encode() |
| 143 | + |
| 144 | + with _linux_commonpath(), _docker_output(docker_out): |
| 145 | + assert docker._get_docker_path('/project') == '/opt/my_code' |
| 146 | + |
| 147 | + |
| 148 | +def test_get_docker_path_in_docker_windows(in_docker): |
| 149 | + binds_list = [{'Source': r'c:\users\user', 'Destination': r'c:\folder'}] |
| 150 | + docker_out = json.dumps([{'Mounts': binds_list}]).encode() |
| 151 | + |
| 152 | + with _nt_commonpath(), _docker_output(docker_out): |
| 153 | + path = r'c:\folder\test\something' |
| 154 | + expected = r'c:\users\user\test\something' |
| 155 | + assert docker._get_docker_path(path) == expected |
0 commit comments