|
| 1 | +# |
| 2 | +# Copyright (c) 2021 Project CHIP Authors |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +""" |
| 18 | +Handles linux-specific functionality for running test cases |
| 19 | +""" |
| 20 | + |
| 21 | +import logging |
| 22 | +import os |
| 23 | +import subprocess |
| 24 | +import sys |
| 25 | +import time |
| 26 | + |
| 27 | +from .test_definition import ApplicationPaths |
| 28 | + |
| 29 | +test_environ = os.environ.copy() |
| 30 | + |
| 31 | + |
| 32 | +def EnsureNetworkNamespaceAvailability(): |
| 33 | + if os.getuid() == 0: |
| 34 | + logging.debug("Current user is root") |
| 35 | + logging.warn("Running as root and this will change global namespaces.") |
| 36 | + return |
| 37 | + |
| 38 | + os.execvpe("unshare", ["unshare", "--map-root-user", "-n", "-m", "python3", |
| 39 | + sys.argv[0], '--internal-inside-unshare'] + sys.argv[1:], test_environ) |
| 40 | + |
| 41 | + |
| 42 | +def EnsurePrivateState(): |
| 43 | + logging.info("Ensuring /run is privately accessible") |
| 44 | + |
| 45 | + logging.debug("Making / private") |
| 46 | + if os.system("mount --make-private /") != 0: |
| 47 | + logging.error("Failed to make / private") |
| 48 | + logging.error("Are you using --privileged if running in docker?") |
| 49 | + sys.exit(1) |
| 50 | + |
| 51 | + logging.debug("Remounting /run") |
| 52 | + if os.system("mount -t tmpfs tmpfs /run") != 0: |
| 53 | + logging.error("Failed to mount /run as a temporary filesystem") |
| 54 | + logging.error("Are you using --privileged if running in docker?") |
| 55 | + sys.exit(1) |
| 56 | + |
| 57 | + |
| 58 | +def CreateNamespacesForAppTest(): |
| 59 | + """ |
| 60 | + Creates appropriate namespaces for a tool and app binaries in a simulated |
| 61 | + isolated network. |
| 62 | + """ |
| 63 | + COMMANDS = [ |
| 64 | + # 2 virtual hosts: for app and for the tool |
| 65 | + "ip netns add app", |
| 66 | + "ip netns add tool", |
| 67 | + |
| 68 | + # create links for switch to net connections |
| 69 | + "ip link add eth-app type veth peer name eth-app-switch", |
| 70 | + "ip link add eth-tool type veth peer name eth-tool-switch", |
| 71 | + |
| 72 | + # link the connections together |
| 73 | + "ip link set eth-app netns app", |
| 74 | + "ip link set eth-tool netns tool", |
| 75 | + |
| 76 | + "ip link add name br1 type bridge", |
| 77 | + "ip link set br1 up", |
| 78 | + "ip link set eth-app-switch master br1", |
| 79 | + "ip link set eth-tool-switch master br1", |
| 80 | + |
| 81 | + # mark connections up |
| 82 | + "ip netns exec app ip addr add 10.10.10.1/24 dev eth-app", |
| 83 | + "ip netns exec app ip link set dev eth-app up", |
| 84 | + "ip netns exec app ip link set dev lo up", |
| 85 | + "ip link set dev eth-app-switch up", |
| 86 | + |
| 87 | + "ip netns exec tool ip addr add 10.10.10.2/24 dev eth-tool", |
| 88 | + "ip netns exec tool ip link set dev eth-tool up", |
| 89 | + "ip netns exec tool ip link set dev lo up", |
| 90 | + "ip link set dev eth-tool-switch up", |
| 91 | + |
| 92 | + # Force IPv6 to use ULAs that we control |
| 93 | + "ip netns exec tool ip -6 addr flush eth-tool", |
| 94 | + "ip netns exec app ip -6 addr flush eth-app", |
| 95 | + "ip netns exec tool ip -6 a add fd00:0:1:1::2/64 dev eth-tool", |
| 96 | + "ip netns exec app ip -6 a add fd00:0:1:1::3/64 dev eth-app", |
| 97 | + ] |
| 98 | + |
| 99 | + for command in COMMANDS: |
| 100 | + logging.debug("Executing '%s'" % command) |
| 101 | + if os.system(command) != 0: |
| 102 | + logging.error("Failed to execute '%s'" % command) |
| 103 | + logging.error("Are you using --privileged if running in docker?") |
| 104 | + sys.exit(1) |
| 105 | + |
| 106 | + # IPv6 does Duplicate Address Detection even though |
| 107 | + # we know ULAs provided are isolated. Wait for 'tenative' address to be gone |
| 108 | + |
| 109 | + logging.info('Waiting for IPv6 DaD to complete (no tentative addresses)') |
| 110 | + for i in range(100): # wait at most 10 seconds |
| 111 | + output = subprocess.check_output(['ip', 'addr']) |
| 112 | + if b'tentative' not in output: |
| 113 | + logging.info('No more tentative addresses') |
| 114 | + break |
| 115 | + time.sleep(0.1) |
| 116 | + else: |
| 117 | + logging.warn("Some addresses look to still be tentative") |
| 118 | + |
| 119 | + |
| 120 | +def PrepareNamespacesForTestExecution(in_unshare: bool): |
| 121 | + if not in_unshare: |
| 122 | + EnsureNetworkNamespaceAvailability() |
| 123 | + elif in_unshare: |
| 124 | + EnsurePrivateState() |
| 125 | + |
| 126 | + CreateNamespacesForAppTest() |
| 127 | + |
| 128 | + |
| 129 | +def PathsWithNetworkNamespaces(paths: ApplicationPaths) -> ApplicationPaths: |
| 130 | + """ |
| 131 | + Returns a copy of paths with updated command arrays to invoke the |
| 132 | + commands in an appropriate network namespace. |
| 133 | + """ |
| 134 | + return ApplicationPaths( |
| 135 | + chip_tool='ip netns exec tool'.split() + paths.chip_tool, |
| 136 | + all_clusters_app='ip netns exec app'.split() + paths.all_clusters_app, |
| 137 | + tv_app='ip netns exec app'.split() + paths.tv_app, |
| 138 | + ) |
0 commit comments