From 7ed7c438a7db43cad50fc071d7a9030b8a091990 Mon Sep 17 00:00:00 2001 From: tyranron Date: Mon, 17 May 2021 20:40:49 +0300 Subject: [PATCH] Add tests for 'detect-external-ip' script of Docker image --- docker/coturn/tests/main.bats | 73 +++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/docker/coturn/tests/main.bats b/docker/coturn/tests/main.bats index 73e238f..93a2aeb 100644 --- a/docker/coturn/tests/main.bats +++ b/docker/coturn/tests/main.bats @@ -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 +}