-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from fhem/sidey79/issue175
fix auto detect DOCKER_GW and setting of DOCKER_HOST
- Loading branch information
Showing
11 changed files
with
220 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -273,7 +273,7 @@ function collectDockerInfo() { | |
export DOCKER_HOSTNETWORK=0 | ||
if ip -4 addr show docker0 >/dev/null 2>&1 ; then | ||
export DOCKER_HOSTNETWORK=1 | ||
unset DOCKER_HOST | ||
export DOCKER_HOST=127.0.0.1 | ||
unset DOCKER_GW | ||
fi | ||
echo $DOCKER_HOSTNETWORK > /docker.hostnetwork | ||
|
@@ -751,17 +751,7 @@ MACs [email protected],[email protected],umac-128-etm@op | |
END_OF_INLINE | ||
fi | ||
|
||
# Adding to local hosts file | ||
local -A hostAddr | ||
hostAddr[gateway.docker.internal]="${DOCKER_GW}" | ||
hostAddr[host.docker.internal]="${DOCKER_HOST:-127.0.127.2}" | ||
for theHost in "${!hostAddr[@]}" ; do | ||
[ -n "$(dig +short -t a ${theHost}.)" ] && continue | ||
[ -z "${hostAddr[$theHost]}" ] && continue | ||
grep -q -F "${hostAddr[$theHost]}" /etc/hosts && continue | ||
printfInfo "Adding ${theHost} to /etc/hosts \n" | ||
echo -e "${hostAddr[$theHost]}\t${theHost}" >> /etc/hosts | ||
done | ||
addDockerHosts | ||
|
||
# Key pinning for Docker host | ||
printfInfo "Pre-authorizing SSH to Docker host for user 'fhem' \n" | ||
|
@@ -817,6 +807,39 @@ function prepareFhemShellEnv() { | |
for theVar in $(env | awk -F= '/^NODE|^PERL|^PYTHON/{print $1}'); do export $theVar ; done | ||
} | ||
|
||
# Function add hosts entry to hosts file | ||
# | ||
# Usage: addDockerHosts | ||
# Global vars: DOCKER_GW | ||
# DOCKER_HOST | ||
# | ||
function addDockerHosts() | ||
{ | ||
|
||
printfInfo "Patching /etc/hosts file with DOCKER_HOST and DOCKER_GW'\n" | ||
|
||
# Adding to local hosts file | ||
local -A hostAddr | ||
hostAddr[gateway.docker.internal]="${DOCKER_GW}" | ||
hostAddr[host.docker.internal]="${DOCKER_HOST:-127.0.127.2}" | ||
declare -a hostLst | ||
|
||
# get changes and modify hosts file later | ||
for theHost in "${!hostAddr[@]}" ; do | ||
[ -n "$(dig +short -t a ${theHost}.)" ] && continue | ||
[ -z "${hostAddr[$theHost]}" ] && continue | ||
[ $DOCKER_HOSTNETWORK == 0 ] && grep -q -F "${hostAddr[$theHost]}" /etc/hosts && continue | ||
local hostEntry="${hostAddr[$theHost]}\t${theHost}" | ||
hostLst+=("$hostEntry") | ||
done | ||
|
||
# Write to hostsfle | ||
for hostEntry in "${hostLst[@]}"; do | ||
printfInfo "Adding ${hostEntry} to /etc/hosts \n" | ||
echo -e "${hostEntry}" | tee -a "/etc/hosts" | ||
done | ||
} | ||
|
||
|
||
#====================================================================================================================- | ||
#--- FHEM process functions ------------------------------------------------------------------------------------------ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
|
||
#!/usr/bin/env bats | ||
|
||
setup() { | ||
load '/opt/bats/test_helper/bats-support/load.bash' | ||
load '/opt/bats/test_helper/bats-assert/load.bash' | ||
load '/opt/bats/test_helper/bats-file/load.bash' | ||
load '/opt/bats/test_helper/bats-mock/load.bash' | ||
|
||
# Clean bevore every run | ||
declare -g DOCKER_GW= | ||
declare -g DOCKER_HOST= | ||
declare -g DOCKER_PRIVILEGED= | ||
|
||
# copy default hosts file before every test | ||
cp "${BATS_SUITE_TMPDIR}/hosts" "${HOSTS_FILE}" | ||
} | ||
|
||
|
||
setup_file() { | ||
[ -z ${GITHUB_RUN_ID+x} ] || echo '::group::Network Tests' >&3 | ||
export LOG_FILE="${BATS_SUITE_TMPDIR}/log" | ||
|
||
set -a | ||
source /entry.sh | ||
set +a | ||
|
||
export CAP_E_FILE='/docker.container.cap.e' | ||
export CAP_P_FILE='/docker.container.cap.p' | ||
export CAP_I_FILE='/docker.container.cap.i' | ||
export HOSTNETWORK_FILE='/docker.hostnetwork' | ||
export PRIVILEDGED_FILE='/docker.privileged' | ||
|
||
} | ||
|
||
teardown_file() { | ||
sleep 0 | ||
|
||
# Cleanup | ||
unset DOCKER_GW | ||
unset DOCKER_HOST | ||
unset DOCKER_PRIVILEGED | ||
cp "${BATS_SUITE_TMPDIR}/hosts" "${HOSTS_FILE}" | ||
[ -z ${GITHUB_RUN_ID+x} ] || echo '::endgroup::' >&3 | ||
} | ||
|
||
|
||
|
||
teardown() { | ||
rm -f ${CAP_E_FILE} ${CAP_P_FILE} ${CAP_I_FILE} ${HOSTNETWORK_FILE} ${PRIVILEDGED_FILE} | ||
} | ||
|
||
# bats test_tags=unitTest | ||
@test "check collectDockerInfo() - check cap files" { | ||
bats_require_minimum_version 1.5.0 | ||
collectDockerInfo | ||
|
||
assert_file_exists ${CAP_E_FILE} | ||
assert_file_exists ${CAP_P_FILE} | ||
assert_file_exists ${CAP_I_FILE} | ||
} | ||
|
||
# bats test_tags=unitTest | ||
@test "check collectDockerInfo() - HOSTNETWORK File in bridgeMode (default)" { | ||
collectDockerInfo | ||
|
||
assert_file_exists ${HOSTNETWORK_FILE} | ||
assert_file_contains ${HOSTNETWORK_FILE} "0" grep | ||
assert_file_not_contains ${HOSTNETWORK_FILE} "1" grep | ||
assert_equal ${DOCKER_HOSTNETWORK} '0' | ||
} | ||
|
||
# bats test_tags=hostMode,unitTest | ||
@test "check collectDockerInfo() - HOSTNETWORK File in hostMode" { | ||
collectDockerInfo | ||
|
||
assert_file_exists ${HOSTNETWORK_FILE} | ||
assert_file_contains ${HOSTNETWORK_FILE} "1" grep | ||
assert_file_not_contains ${HOSTNETWORK_FILE} "0" grep | ||
assert_equal ${DOCKER_HOSTNETWORK} '1' | ||
|
||
} | ||
|
||
|
||
# bats test_tags=unitTest | ||
@test "check collectDockerInfo() - PRIVILEDGED file " { | ||
collectDockerInfo | ||
|
||
assert_file_contains ${PRIVILEDGED_FILE} '0' grep | ||
assert_file_not_contains ${PRIVILEDGED_FILE} '1' grep | ||
assert_equal ${DOCKER_PRIVILEGED} '0' | ||
|
||
} | ||
|
||
# bats test_tags=hostMode,unitTest | ||
@test "check collectDockerInfo() - DOCKER_GW" { | ||
collectDockerInfo | ||
|
||
assert_equal ${DOCKER_GW} '' | ||
} | ||
|
||
# bats test_tags=hostMode,unitTest | ||
@test "check collectDockerInfo() - DOCKER_HOST" { | ||
collectDockerInfo | ||
|
||
assert_equal ${DOCKER_HOST} '127.0.0.1' | ||
} | ||
|
||
# bats test_tags=unitTest | ||
@test "check DOCKER_HOST in ${HOSTS_FILE}" { | ||
collectDockerInfo | ||
|
||
run addDockerHosts | ||
assert_output --partial "Adding" | ||
assert_file_contains ${HOSTS_FILE} "${DOCKER_HOST}" grep | ||
assert_file_contains ${HOSTS_FILE} "host.docker.internal" grep | ||
|
||
} | ||
|
||
|
||
# bats test_tags=unitTest | ||
@test "check DOCKER_GW in ${HOSTS_FILE}" { | ||
collectDockerInfo | ||
|
||
run addDockerHosts | ||
|
||
assert_file_contains ${HOSTS_FILE} "${DOCKER_GW}.*gateway.docker.internal" grep | ||
} | ||
|
||
# bats test_tags=hostMode,unitTest | ||
@test "check DOCKER_HOST in ${HOSTS_FILE} with hostMode" { | ||
collectDockerInfo | ||
|
||
run addDockerHosts | ||
assert_output --partial "Adding " | ||
cat ${HOSTS_FILE} | ||
assert_file_contains ${HOSTS_FILE} "${DOCKER_HOST}.*host.docker.internal" grep | ||
} | ||
|
||
|
||
# bats test_tags=hostMode,unitTest | ||
@test "check DOCKER_GW in ${HOSTS_FILE} with hostMode" { | ||
collectDockerInfo | ||
|
||
assert_equal "${DOCKER_GW}" "" | ||
|
||
run addDockerHosts | ||
|
||
cat "${HOSTS_FILE}" | ||
refute_output --partial "Adding gateway.docker.internal" | ||
assert_file_not_contains ${HOSTS_FILE} "${DOCKER_GW}.*gateway.docker.internal" grep | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
setup_suite() { | ||
export FHEM_DIR="/opt/fhem" | ||
export FHEM_CFG_FILE="${FHEM_DIR}/fhem.cfg" | ||
export HOSTS_FILE='/etc/hosts' | ||
|
||
cp ${HOSTS_FILE} ${BATS_SUITE_TMPDIR}/hosts | ||
mkdir -p /tmp/fhem/FHEM | ||
cp -r /fhem/FHEM/* /tmp/fhem/FHEM/ | ||
|
||
wget https://raw.githubusercontent.com/heinz-otto/fhemcl/master/fhemcl.sh -O /usr/local/bin/fhemcl.sh | ||
chmod +x /usr/local/bin/fhemcl.sh | ||
} | ||
|
||
|
||
teardown_suite() { | ||
sleep 0 | ||
rm /usr/local/bin/fhemcl.sh | ||
rm -r /tmp/fhem | ||
} | ||
|