Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable problem matcher when tox seems to be running on container #114

Merged
merged 1 commit into from Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/tox_gh_actions/plugin.py
Expand Up @@ -55,8 +55,16 @@ def tox_configure(config):
config.envlist_default = config.envlist = envlist
verbosity1("overriding envlist with: {}".format(envlist))

verbosity2("enabling problem matcher")
print("::add-matcher::" + get_problem_matcher_file_path())
if is_running_on_container():
verbosity2(
"not enabling problem matcher as tox seems to be running on a container"
)
# Trying to add a problem matcher from a container without proper host mount can
# cause an error like the following:
# Unable to process command '::add-matcher::/.../matcher.json' successfully.
else:
verbosity2("enabling problem matcher")
print("::add-matcher::" + get_problem_matcher_file_path())

if not is_log_grouping_enabled(config):
verbosity2(
Expand Down Expand Up @@ -236,6 +244,21 @@ def is_running_on_actions():
return os.environ.get("GITHUB_ACTIONS") == "true"


def is_running_on_container():
# type: () -> bool
"""Check whether tox is running on a container or not

Only Linux containers are supported.
"""
cgroup_path = "/proc/1/cgroup"
if os.path.exists(cgroup_path):
with open(cgroup_path) as f:
for line in f:
if "containerd" in line:
return True
return False


def is_log_grouping_enabled(config):
# type: (Config) -> bool
"""Returns True when the plugin should enable log line grouping
Expand Down
43 changes: 43 additions & 0 deletions tests/test_plugin.py
Expand Up @@ -395,6 +395,49 @@ def test_is_running_on_actions(mocker, environ, expected):
assert plugin.is_running_on_actions() == expected


def is_running_on_container_returns_false_on_non_linux(mocker):
mocker.patch("tox_gh_actions.plugin.os.path.exists", False)
assert not plugin.is_running_on_container()


def is_running_on_container_returns_false_on_linux_host(mocker):
mocker.patch("tox_gh_actions.plugin.os.path.exists", True)
mock_open = mocker.mock_open(read_data="""12:perf_event:/
11:devices:/init.scope
10:cpu,cpuacct:/init.scope
9:freezer:/
8:pids:/init.scope
7:memory:/init.scope
6:cpuset:/
5:hugetlb:/
4:blkio:/init.scope
3:net_cls,net_prio:/
2:rdma:/
1:name=systemd:/init.scope
0::/init.scope""")
mocker.patch("tox_gh_actions.plugin.open", mock_open)
assert plugin.is_running_on_container()


def is_running_on_container_returns_true_on_linux_container(mocker):
mocker.patch("tox_gh_actions.plugin.os.path.exists", True)
mock_open = mocker.mock_open(read_data="""12:pids:/actions_job/3d81
11:net_cls,net_prio:/actions_job/3d81
10:devices:/actions_job/3d81
9:freezer:/actions_job/3d81
8:cpuset:/actions_job/3d81
7:cpu,cpuacct:/actions_job/3d81
6:memory:/actions_job/3d81
5:blkio:/actions_job/3d81
4:perf_event:/actions_job/3d81
3:rdma:/
2:hugetlb:/actions_job/3d81
1:name=systemd:/actions_job/3d81
0::/system.slice/containerd.service""")
mocker.patch("tox_gh_actions.plugin.open", mock_open)
assert plugin.is_running_on_container()


@pytest.mark.parametrize(
"environ,parallel,parallel_live,expected",
[
Expand Down