From e230e12e34036a35af1393060be1f03159be76d5 Mon Sep 17 00:00:00 2001
From: Konstantin Pavlov <thresh@nginx.com>
Date: Tue, 6 Feb 2024 14:00:30 -0800
Subject: [PATCH] CI: fixed templates-resolver-ipv6 tests.

---
 .../expected-std-out.txt                      |  2 ++
 .test/tests/templates-resolver-ipv6/run.sh    | 29 ++++++++++++++++---
 .../server.conf.template                      |  1 +
 3 files changed, 28 insertions(+), 4 deletions(-)
 create mode 100644 .test/tests/templates-resolver-ipv6/expected-std-out.txt

diff --git a/.test/tests/templates-resolver-ipv6/expected-std-out.txt b/.test/tests/templates-resolver-ipv6/expected-std-out.txt
new file mode 100644
index 0000000..5309ac8
--- /dev/null
+++ b/.test/tests/templates-resolver-ipv6/expected-std-out.txt
@@ -0,0 +1,2 @@
+example.com - OK
+ipv6 nameserver(s) present
diff --git a/.test/tests/templates-resolver-ipv6/run.sh b/.test/tests/templates-resolver-ipv6/run.sh
index 8ebbabf..a7bc333 100755
--- a/.test/tests/templates-resolver-ipv6/run.sh
+++ b/.test/tests/templates-resolver-ipv6/run.sh
@@ -21,7 +21,10 @@ fi
 
 # Create a new Docker network
 nid="$(docker network create --ipv6 --subnet fd0c:7e57::/64 nginx-test-ipv6-network)"
-trap "docker network rm -f $nid > /dev/null" EXIT
+
+_network_exit_handler() {
+    docker network rm -f $nid > /dev/null
+}
 
 # Create an instance of the container-under-test
 serverImage="$("$HOME/oi/test/tests/image-name.sh" librarytest/nginx-template "$image")"
@@ -30,7 +33,14 @@ FROM $image
 COPY dir/server.conf.template /etc/nginx/templates/server.conf.template
 EOD
 cid="$(docker run -d --network $nid -e NGINX_ENTRYPOINT_LOCAL_RESOLVERS=true -e NGINX_MY_SERVER_NAME=example.com "$serverImage")"
-trap "docker rm -vf $cid > /dev/null" EXIT
+
+_container_exit_handler() {
+    docker rm -vf $cid > /dev/null
+}
+_exit_handler() { _container_exit_handler; _network_exit_handler; }
+trap "_exit_handler" EXIT
+
+ipv6cid="$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.GlobalIPv6Address}}{{end}}' $cid)"
 
 _request() {
 	local method="$1"
@@ -49,12 +59,23 @@ _request() {
 	fi
 
 	docker run --rm \
-		--link "$cid":nginx \
+		--network "$nid" \
 		"$clientImage" \
-		curl -fsSL -X"$method" --connect-to '::nginx:' "$@" "$proto://example.com/$url"
+		curl -fsSL -X"$method" --connect-to "::[$ipv6cid]:" "$@" "$proto://example.com/$url"
 }
 
 . "$HOME/oi/test/retry.sh" '[ "$(_request GET / --output /dev/null || echo $?)" != 7 ]'
 
 # Check that we can request /
 _request GET http '/resolver-templates' | grep 'example.com - OK'
+
+result="$(docker exec $cid grep resolver /etc/nginx/conf.d/server.conf)"
+
+case "$result" in
+    resolver*\[*\]*)
+        echo "ipv6 nameserver(s) present"
+        ;;
+    *)
+        echo "no ipv6 nameserver(s) present"
+        ;;
+esac
diff --git a/.test/tests/templates-resolver-ipv6/server.conf.template b/.test/tests/templates-resolver-ipv6/server.conf.template
index 04a0c08..7083556 100644
--- a/.test/tests/templates-resolver-ipv6/server.conf.template
+++ b/.test/tests/templates-resolver-ipv6/server.conf.template
@@ -2,6 +2,7 @@ resolver ${NGINX_LOCAL_RESOLVERS};
 
 server {
     listen 80;
+    listen [::]:80;
     server_name ${NGINX_MY_SERVER_NAME};
     default_type text/plain;
     location = / { return 200 'OK\n'; }
-- 
GitLab