|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +# contributor license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright ownership. |
| 4 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +# (the "License"); you may not use this file except in compliance with |
| 6 | +# the License. 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 | +load hadoop-functions_test_helper |
| 17 | + |
| 18 | +# Mock docker command |
| 19 | +docker () { |
| 20 | + if [ "$1" = "-v" ]; then |
| 21 | + shift |
| 22 | + echo Docker version ${DCKR_MOCK_VER:?} |
| 23 | + elif [ "$1" = run ]; then |
| 24 | + shift |
| 25 | + until [ $# -eq 0 ]; do |
| 26 | + if [ "$1" = -v ]; then |
| 27 | + shift |
| 28 | + echo "$1"|awk -F':' '{if (NF == 3 && $3 == "z") |
| 29 | + printf "Mounted %s with z option.\n", $1 |
| 30 | + else if (NF == 2) |
| 31 | + printf "Mounted %s without z option.\n", $1}' |
| 32 | + fi |
| 33 | + shift |
| 34 | + done |
| 35 | + fi |
| 36 | +} |
| 37 | +export -f docker |
| 38 | +export DCKR_MOCK_VER |
| 39 | + |
| 40 | +# Mock a SELinux enabled system |
| 41 | +enable_selinux () { |
| 42 | + mkdir -p "${TMP}/bin" |
| 43 | + echo true >"${TMP}/bin"/selinuxenabled |
| 44 | + chmod a+x "${TMP}/bin"/selinuxenabled |
| 45 | + if [ "${PATH#${TMP}/bin}" = "${PATH}" ]; then |
| 46 | + PATH="${TMP}/bin":"$PATH" |
| 47 | + fi |
| 48 | +} |
| 49 | + |
| 50 | +setup_user () { |
| 51 | + if [ -z "$(printenv USER)" ]; then |
| 52 | + if [ -z "$USER" ]; then |
| 53 | + USER=${HOME##*/} |
| 54 | + fi |
| 55 | + export USER |
| 56 | + fi |
| 57 | +} |
| 58 | + |
| 59 | +# Mock stat command as used in start-build-env.sh |
| 60 | +stat () { |
| 61 | + if [ "$1" = --printf='%C' -a $# -eq 2 ]; then |
| 62 | + printf 'mock_u:mock_r:mock_t:s0' |
| 63 | + else |
| 64 | + command stat "$@" |
| 65 | + fi |
| 66 | +} |
| 67 | +export -f stat |
| 68 | + |
| 69 | +# Verify that host directories get mounted without z option |
| 70 | +# and INFO messages get printed out |
| 71 | +@test "start-build-env.sh (Docker without z mount option)" { |
| 72 | + if [ "$(uname -s)" != "Linux" ]; then |
| 73 | + skip "Not on Linux platform" |
| 74 | + fi |
| 75 | + enable_selinux |
| 76 | + setup_user |
| 77 | + DCKR_MOCK_VER=1.4 |
| 78 | + run "${BATS_TEST_DIRNAME}/../../../../../start-build-env.sh" |
| 79 | + [ "$status" -eq 0 ] |
| 80 | + [[ ${lines[0]} == "INFO: SELinux is enabled." ]] |
| 81 | + [[ ${lines[1]} =~ \ |
| 82 | + "Mounted ".*" may not be accessible to the container." ]] |
| 83 | + [[ ${lines[2]} == \ |
| 84 | + "INFO: If so, on the host, run the following command:" ]] |
| 85 | + [[ ${lines[3]} =~ "# chcon -Rt svirt_sandbox_file_t " ]] |
| 86 | + [[ ${lines[-2]} =~ "Mounted ".*" without z option." ]] |
| 87 | + [[ ${lines[-1]} =~ "Mounted ".*" without z option." ]] |
| 88 | +} |
| 89 | + |
| 90 | +# Verify that host directories get mounted with z option |
| 91 | +@test "start-build-env.sh (Docker with z mount option)" { |
| 92 | + if [ "$(uname -s)" != "Linux" ]; then |
| 93 | + skip "Not on Linux platform" |
| 94 | + fi |
| 95 | + enable_selinux |
| 96 | + setup_user |
| 97 | + DCKR_MOCK_VER=1.7 |
| 98 | + run "${BATS_TEST_DIRNAME}/../../../../../start-build-env.sh" |
| 99 | + [ "$status" -eq 0 ] |
| 100 | + [[ ${lines[-2]} =~ "Mounted ".*" with z option." ]] |
| 101 | + [[ ${lines[-1]} =~ "Mounted ".*" with z option." ]] |
| 102 | +} |
0 commit comments