Jelajahi Sumber

tests: enable local testing on macOS / Darwin

Nicolas Duchon 5 bulan lalu
induk
melakukan
40309e2441

+ 3 - 0
test/compose.base.yml

@@ -4,3 +4,6 @@ services:
     container_name: nginx-proxy
     volumes:
       - /var/run/docker.sock:/tmp/docker.sock:ro
+    ports:
+      - "80:80"
+      - "443:443"

+ 13 - 4
test/conftest.py

@@ -2,6 +2,7 @@ import contextlib
 import logging
 import os
 import pathlib
+import platform
 import re
 import shlex
 import socket
@@ -219,8 +220,8 @@ def nginx_proxy_dns_resolver(domain_name: str) -> Optional[str]:
 
 def docker_container_dns_resolver(domain_name: str) -> Optional[str]:
     """
-    if domain name is of the form "XXX.container.docker" or "anything.XXX.container.docker", return the ip address of the docker container
-    named XXX.
+    if domain name is of the form "XXX.container.docker" or "anything.XXX.container.docker",
+    return the ip address of the docker container named XXX.
 
     :return: IP or None
     """
@@ -250,7 +251,10 @@ def monkey_patch_urllib_dns_resolver():
     """
     Alter the behavior of the urllib DNS resolver so that any domain name
     containing substring 'nginx-proxy' will resolve to the IP address
-    of the container created from image 'nginxproxy/nginx-proxy:test'.
+    of the container created from image 'nginxproxy/nginx-proxy:test',
+    or to 127.0.0.1 on Darwin.
+
+    see https://docs.docker.com/desktop/features/networking/#i-want-to-connect-to-a-container-from-the-host
     """
     prv_getaddrinfo = socket.getaddrinfo
     dns_cache = {}
@@ -264,7 +268,12 @@ def monkey_patch_urllib_dns_resolver():
             pytest.skip("This system does not support IPv6")
 
         # custom DNS resolvers
-        ip = nginx_proxy_dns_resolver(args[0])
+        ip = None
+        # Docker Desktop can't route traffic directly to Linux containers.
+        if platform.system() == "Darwin":
+            ip = "127.0.0.1"
+        if ip is None:
+            ip = nginx_proxy_dns_resolver(args[0])
         if ip is None:
             ip = docker_container_dns_resolver(args[0])
         if ip is not None:

+ 3 - 0
test/test_dockergen/test_dockergen.base.yml

@@ -8,6 +8,9 @@ services:
     container_name: nginx
     volumes:
       - nginx_conf:/etc/nginx/conf.d:ro
+    ports:
+      - "80:80"
+      - "443:443"
 
   nginx-proxy-dockergen:
     image: nginxproxy/docker-gen

+ 3 - 0
test/test_fallback/test_fallback.data/compose.base.yml

@@ -4,3 +4,6 @@ services:
     container_name: nginx-proxy
     volumes:
       - /var/run/docker.sock:/tmp/docker.sock:ro
+    ports:
+      - "80:80"
+      - "443:443"

+ 3 - 0
test/test_host-network-mode/test_proxy-host-network-mode.py

@@ -1,3 +1,6 @@
+# Note: on Docker Desktop, host networking must be manually enabled.
+# See https://docs.docker.com/engine/network/drivers/host/
+
 def test_forwards_to_host_network_container_1(docker_compose, nginxproxy):
     r = nginxproxy.get("http://host-network-1.nginx-proxy.tld:8888/port")
     assert r.status_code == 200

+ 2 - 0
test/test_http-port/test_http-port.yml

@@ -2,6 +2,8 @@ services:
   nginx-proxy:
     environment:
       HTTP_PORT: 8080
+    ports:
+      - "8080:8080"
 
   web1:
     image: web

+ 10 - 0
test/test_ipv6/test_ipv6.py

@@ -1,3 +1,13 @@
+import platform
+
+import pytest
+
+pytestmark = pytest.mark.skipif(
+    platform.system() == "Darwin",
+    reason="Those tests rely entirely on being able to directly contact container's IP"
+)
+
+
 def test_unknown_virtual_host_ipv4(docker_compose, nginxproxy):
     r = nginxproxy.get("http://nginx-proxy/port")
     assert r.status_code == 503

+ 6 - 0
test/test_nominal/test_nominal.py

@@ -1,3 +1,5 @@
+import platform
+
 import pytest
 from requests import ConnectionError
 
@@ -19,6 +21,10 @@ def test_forwards_to_web2(docker_compose, nginxproxy):
     assert r.text == "answer from port 82\n" 
 
 
+@pytest.mark.skipif(
+    platform.system() == "Darwin",
+    reason="This test rely on being able to directly contact the container's IP"
+)
 def test_ipv6_is_disabled_by_default(docker_compose, nginxproxy):
     with pytest.raises(ConnectionError):
         nginxproxy.get("http://nginx-proxy/port", ipv6=True)

+ 2 - 2
test/test_raw-ip-vhost/test_raw-ip-vhost.py

@@ -1,12 +1,12 @@
 def test_raw_ipv4_vhost_forwards_to_web1(docker_compose, nginxproxy):
-    r = nginxproxy.get("http://172.20.0.4")
+    r = nginxproxy.get("http://172.20.0.1")
     assert r.status_code == 200
     web1_container = docker_compose.containers.get("web1")
     assert r.text == f"I'm {web1_container.id[:12]}\n"
 
 
 def test_raw_ipv6_vhost_forwards_to_web2(docker_compose, nginxproxy):
-    r = nginxproxy.get("http://[fd00::4]", ipv6=True)
+    r = nginxproxy.get("http://[fd00::1]")
     assert r.status_code == 200
     web2_container = docker_compose.containers.get("web2")
     assert r.text == f"I'm {web2_container.id[:12]}\n"

+ 2 - 2
test/test_raw-ip-vhost/test_raw-ip-vhost.yml

@@ -22,7 +22,7 @@ services:
       - "81"
     environment:
       WEB_PORTS: "81"
-      VIRTUAL_HOST: "172.20.0.4"
+      VIRTUAL_HOST: "172.20.0.1"
     networks:
       net1:
         ipv4_address: 172.20.0.2
@@ -35,7 +35,7 @@ services:
       - "82"
     environment:
       WEB_PORTS: "82"
-      VIRTUAL_HOST: "[fd00::4]"
+      VIRTUAL_HOST: "[fd00::1]"
     networks:
       net1:
         ipv4_address: 172.20.0.3

+ 5 - 0
test/test_ssl/test_dhparam.py

@@ -1,3 +1,4 @@
+import platform
 import re
 import subprocess
 
@@ -7,6 +8,10 @@ import pytest
 
 docker_client = docker.from_env()
 
+pytestmark = pytest.mark.skipif(
+    platform.system() == "Darwin",
+    reason="Those tests rely entirely on being able to directly contact container's IP"
+)
 
 ###############################################################################
 #

+ 3 - 0
test/test_ssl/test_https-port.yml

@@ -3,6 +3,9 @@ services:
     environment:
       HTTP_PORT: 8080
       HTTPS_PORT: 8443
+    ports:
+      - "8080:8080"
+      - "8443:8443"
 
   web1:
     image: web