1
0
mirror of https://github.com/thib8956/nginx-proxy synced 2025-02-24 01:38:15 +00:00

tests: enable local testing on macOS / Darwin

This commit is contained in:
Nicolas Duchon 2024-12-30 13:41:47 +01:00
parent daa9449176
commit 40309e2441
12 changed files with 55 additions and 8 deletions

View File

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

View File

@ -2,6 +2,7 @@ import contextlib
import logging import logging
import os import os
import pathlib import pathlib
import platform
import re import re
import shlex import shlex
import socket 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]: 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 if domain name is of the form "XXX.container.docker" or "anything.XXX.container.docker",
named XXX. return the ip address of the docker container named XXX.
:return: IP or None :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 Alter the behavior of the urllib DNS resolver so that any domain name
containing substring 'nginx-proxy' will resolve to the IP address 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 prv_getaddrinfo = socket.getaddrinfo
dns_cache = {} dns_cache = {}
@ -264,6 +268,11 @@ def monkey_patch_urllib_dns_resolver():
pytest.skip("This system does not support IPv6") pytest.skip("This system does not support IPv6")
# custom DNS resolvers # custom DNS resolvers
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]) ip = nginx_proxy_dns_resolver(args[0])
if ip is None: if ip is None:
ip = docker_container_dns_resolver(args[0]) ip = docker_container_dns_resolver(args[0])

View File

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

View File

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

View File

@ -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): def test_forwards_to_host_network_container_1(docker_compose, nginxproxy):
r = nginxproxy.get("http://host-network-1.nginx-proxy.tld:8888/port") r = nginxproxy.get("http://host-network-1.nginx-proxy.tld:8888/port")
assert r.status_code == 200 assert r.status_code == 200

View File

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

View File

@ -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): def test_unknown_virtual_host_ipv4(docker_compose, nginxproxy):
r = nginxproxy.get("http://nginx-proxy/port") r = nginxproxy.get("http://nginx-proxy/port")
assert r.status_code == 503 assert r.status_code == 503

View File

@ -1,3 +1,5 @@
import platform
import pytest import pytest
from requests import ConnectionError from requests import ConnectionError
@ -19,6 +21,10 @@ def test_forwards_to_web2(docker_compose, nginxproxy):
assert r.text == "answer from port 82\n" 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): def test_ipv6_is_disabled_by_default(docker_compose, nginxproxy):
with pytest.raises(ConnectionError): with pytest.raises(ConnectionError):
nginxproxy.get("http://nginx-proxy/port", ipv6=True) nginxproxy.get("http://nginx-proxy/port", ipv6=True)

View File

@ -1,12 +1,12 @@
def test_raw_ipv4_vhost_forwards_to_web1(docker_compose, nginxproxy): 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 assert r.status_code == 200
web1_container = docker_compose.containers.get("web1") web1_container = docker_compose.containers.get("web1")
assert r.text == f"I'm {web1_container.id[:12]}\n" assert r.text == f"I'm {web1_container.id[:12]}\n"
def test_raw_ipv6_vhost_forwards_to_web2(docker_compose, nginxproxy): 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 assert r.status_code == 200
web2_container = docker_compose.containers.get("web2") web2_container = docker_compose.containers.get("web2")
assert r.text == f"I'm {web2_container.id[:12]}\n" assert r.text == f"I'm {web2_container.id[:12]}\n"

View File

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

View File

@ -1,3 +1,4 @@
import platform
import re import re
import subprocess import subprocess
@ -7,6 +8,10 @@ import pytest
docker_client = docker.from_env() 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"
)
############################################################################### ###############################################################################
# #

View File

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