Add tests for 'detect-external-ip' script of Docker image

This commit is contained in:
tyranron 2021-05-17 20:40:49 +03:00
parent e5af5a813f
commit 7ed7c438a7
No known key found for this signature in database
GPG Key ID: 762E144FB230A4F0

View File

@ -125,3 +125,76 @@
[ "$status" -eq 0 ]
[ ! "$output" = '' ]
}
@test "detect-external-ip is present" {
run docker run --rm --platform $PLATFORM --entrypoint sh $IMAGE -c \
'which detect-external-ip'
[ "$status" -eq 0 ]
}
@test "detect-external-ip runs ok" {
run docker run --rm --platform $PLATFORM --entrypoint sh $IMAGE -c \
'detect-external-ip'
[ "$status" -eq 0 ]
}
@test "detect-external-ip returns valid IPv4" {
run docker run --rm --platform $PLATFORM --entrypoint sh $IMAGE -c \
'detect-external-ip --ipv4'
[ "$status" -eq 0 ]
run validate_ipv4 "$output"
[ "$status" -eq 0 ]
}
@test "detect-external-ip returns valid IPv6" {
run docker run --rm --platform $PLATFORM --entrypoint sh $IMAGE -c \
'detect-external-ip --ipv6'
[ "$status" -eq 0 ]
run validate_ipv6 "$output"
[ "$status" -eq 0 ]
}
@test "detect-external-ip returns IPv4 by default" {
run docker run --rm --platform $PLATFORM --entrypoint sh $IMAGE -c \
'detect-external-ip --ipv4'
[ "$status" -eq 0 ]
run validate_ipv4 "$output"
[ "$status" -eq 0 ]
}
#
# Helpers
#
# Tests the IP address to be a valid IPv4 address.
function validate_ipv4() {
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
# Tests the IP address to be a valid IPv6 address.
function validate_ipv6() {
local ip=$1
local stat=1
if [[ $ip =~ ^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$ ]]; then
stat=0
fi
return $stat
}