Skip to content

Commit

Permalink
Merge pull request #177 from fhem/sidey79/issue175
Browse files Browse the repository at this point in the history
fix auto detect DOCKER_GW and setting of DOCKER_HOST
  • Loading branch information
sidey79 authored Apr 3, 2024
2 parents d43306b + 7ef943e commit 022376b
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 31 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,14 @@ jobs:
L_VCS_URL=${{ github.server_url }}/${{ github.repository }}/
L_AUTHORS=${{ github.server_url }}/${{ github.repository }}/graphs/contributors
- name: Run bats unittetss
- name: Run bats unit and integration tests
run: |
docker run --rm -v "${PWD}/src/tests/bats:/code" bats-withfhem:latest -t .
echo '::group::network = bridge mode'
docker run --rm -e GITHUB_RUN_ID=$GITHUB_RUN_ID -v "${PWD}/src/tests/bats:/code" bats-withfhem:latest -T -t . --filter-tags '!hostMode'
echo '::endgroup::'
echo '::group::netwwork = hostmode'
docker run --rm --net=host -e GITHUB_RUN_ID=$GITHUB_RUN_ID -v "${PWD}/src/tests/bats:/code" bats-withfhem:latest -T -t . --filter-tags 'hostMode'
echo '::endgroup::'
- name: Build for test fhem, python and nodejs layer added for amd64
uses: docker/build-push-action@v5
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile-bullseye
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ RUN <<EOF
libimlib2-dev \
python-dev \
libusb-dev \
bash
bash \
iproute2
LC_ALL=C apt-get autoremove -qqy && LC_ALL=C apt-get clean
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* ~/.[^.] ~/.??* ~/*
EOF
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile-threaded-bullseye
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ RUN <<EOF
libimlib2-dev \
python-dev \
libusb-dev \
bash
bash \
iproute2
LC_ALL=C apt-get autoremove -qqy && LC_ALL=C apt-get clean
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* ~/.[^.] ~/.??* ~/*
EOF
Expand Down
47 changes: 35 additions & 12 deletions src/entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 ------------------------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions src/tests/bats/aptInstall.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ setup() {


setup_file() {
[ -z ${GITHUB_RUN_ID+x} ] || echo '::group::aptInstall Tests' >&3

export BATS_TEST_TIMEOUT=60
export LOG_FILE="${BATS_SUITE_TMPDIR}/log"

Expand All @@ -20,6 +22,8 @@ setup_file() {

teardown_file() {
sleep 0
[ -z ${GITHUB_RUN_ID+x} ] || echo '::endgroup::' >&3

}


Expand Down
2 changes: 2 additions & 0 deletions src/tests/bats/entry.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ setup() {


setup_file() {
[ -z ${GITHUB_RUN_ID+x} ] || echo '::group::function tests' >&3
export BATS_TEST_TIMEOUT=60
export LOG_FILE="${BATS_SUITE_TMPDIR}/log"

Expand All @@ -26,6 +27,7 @@ teardown_file() {
sleep 0
rm -f /tmp/log
rm -rf /opt/fhem/*
[ -z ${GITHUB_RUN_ID+x} ] || echo '::endgroup::' >&3
}


Expand Down
2 changes: 2 additions & 0 deletions src/tests/bats/health-check.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ setup() {
}

setup_file() {
[ -z ${GITHUB_RUN_ID+x} ] || echo '::group::Healtcheck Tests' >&3
export LOG_FILE="${BATS_SUITE_TMPDIR}/log"
}

teardown_file() {
mkdir -p /fhem/FHEM
cp /tmp/fhem/FHEM/* /fhem/FHEM/
[ -z ${GITHUB_RUN_ID+x} ] || echo '::endgroup::' >&3
}

teardown() {
Expand Down
9 changes: 4 additions & 5 deletions src/tests/bats/logfile.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ setup() {
}

setup_file() {
[ -z ${GITHUB_RUN_ID+x} ] || echo '::group::Logfile Tests' >&3

export BATS_TEST_TIMEOUT=60
export LOG_FILE="${BATS_SUITE_TMPDIR}/log"
export CONFIGTYPE="fhem.cfg"

set -a
source /entry.sh
set +a

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_file() {
sleep 0
rm -f /tmp/log
rm /usr/local/bin/fhemcl.sh
[ -z ${GITHUB_RUN_ID+x} ] || echo '::endgroup::' >&3
}

teardown() {
Expand Down
152 changes: 152 additions & 0 deletions src/tests/bats/network.bats
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
}
13 changes: 4 additions & 9 deletions src/tests/bats/pidfile.bats
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,30 @@ setup() {
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'


#export -f printfDebug
#export -f printfInfo


# Sometimes perl or grep does not terminate, we will clean up
#pkill tail || true
#pkill grep || true

#pkill grep || true
}

setup_file() {
[ -z ${GITHUB_RUN_ID+x} ] || echo '::group::aptInstall Tests' >&3
export BATS_TEST_TIMEOUT=60
export LOG_FILE="${BATS_SUITE_TMPDIR}/log"
export CONFIGTYPE="fhem.cfg"

set -a
source /entry.sh
set +a

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_file() {
sleep 0
rm -f /tmp/log
rm /usr/local/bin/fhemcl.sh
[ -z ${GITHUB_RUN_ID+x} ] || echo '::endgroup::' >&3
}

teardown() {
Expand Down
7 changes: 6 additions & 1 deletion src/tests/bats/setup_suite.bash
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
}

0 comments on commit 022376b

Please sign in to comment.