aboutsummaryrefslogtreecommitdiff
blob: ad3b288ef4c688f9ab59670c8c130cba4ae6804f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Copyright 2019, 2023 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

import errno
import os
import platform

import pytest

import portage.process
from portage.const import BASH_BINARY
from portage.tests import TestCase


CLONE_NEWNET = 0x40000000
UNSHARE_NET_TEST_SCRIPT = """
ping -c 1 -W 1 127.0.0.1 || exit 1
ping -c 1 -W 1 10.0.0.1 || exit 1
[[ -n ${IPV6} ]] || exit 0
ping -c 1 -W 1 ::1 || exit 1
ping -c 1 -W 1 fd::1 || exit 1
"""


class UnshareNetTestCase(TestCase):
    def setUp(self):
        """
        Initialize ABILITY_TO_UNSHARE in setUp so that _unshare_validate
        uses the correct PORTAGE_MULTIPROCESSING_START_METHOD setup
        from super().setUp().
        """
        super().setUp()
        self.ABILITY_TO_UNSHARE = portage.process._unshare_validate(CLONE_NEWNET)

    @pytest.mark.skipif(
        portage.process.find_binary("ping") is None, reason="ping not found"
    )
    @pytest.mark.skipif(platform.system() != "Linux", reason="not Linux")
    def testUnshareNet(self):
        if self.ABILITY_TO_UNSHARE != 0:
            pytest.skip(
                f"Unable to unshare: {errno.errorcode.get(self.ABILITY_TO_UNSHARE, '?')}"
            )
        env = os.environ.copy()
        env["IPV6"] = "1" if portage.process._has_ipv6() else ""
        self.assertEqual(
            portage.process.spawn(
                [BASH_BINARY, "-c", UNSHARE_NET_TEST_SCRIPT], unshare_net=True, env=env
            ),
            0,
        )