Skip to content

Commit 2802deb

Browse files
committed
tests/e2e: don't rely DNS to get host's IP for container access
Signed-off-by: Jens Langhammer <[email protected]>
1 parent 3eccef8 commit 2802deb

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

tests/e2e/test_provider_oauth2_github.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ def setUp(self):
4747
"GF_AUTH_GITHUB_AUTH_URL": self.url(
4848
"authentik_providers_oauth2_root:github-authorize"
4949
),
50-
"GF_AUTH_GITHUB_TOKEN_URL": self.url(
50+
"GF_AUTH_GITHUB_TOKEN_URL": self.host_url(
5151
"authentik_providers_oauth2_root:github-access-token"
5252
),
53-
"GF_AUTH_GITHUB_API_URL": self.url("authentik_providers_oauth2_root:github-user"),
53+
"GF_AUTH_GITHUB_API_URL": self.host_url(
54+
"authentik_providers_oauth2_root:github-user"
55+
),
5456
"GF_LOG_LEVEL": "debug",
5557
},
5658
)

tests/e2e/test_provider_oauth2_grafana.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ def setUp(self):
5353
"GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET": self.client_secret,
5454
"GF_AUTH_GENERIC_OAUTH_SCOPES": "openid email profile",
5555
"GF_AUTH_GENERIC_OAUTH_AUTH_URL": self.url("authentik_providers_oauth2:authorize"),
56-
"GF_AUTH_GENERIC_OAUTH_TOKEN_URL": self.url("authentik_providers_oauth2:token"),
57-
"GF_AUTH_GENERIC_OAUTH_API_URL": self.url("authentik_providers_oauth2:userinfo"),
56+
"GF_AUTH_GENERIC_OAUTH_TOKEN_URL": self.host_url(
57+
"authentik_providers_oauth2:token"
58+
),
59+
"GF_AUTH_GENERIC_OAUTH_API_URL": self.host_url(
60+
"authentik_providers_oauth2:userinfo"
61+
),
5862
"GF_AUTH_SIGNOUT_REDIRECT_URL": self.url(
5963
"authentik_providers_oauth2:end-session",
6064
application_slug=self.app_slug,

tests/e2e/utils.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import json
44
import os
5-
import socket
65
from collections.abc import Callable
76
from functools import lru_cache, wraps
87
from os import environ
@@ -51,13 +50,6 @@ def get_docker_tag() -> str:
5150
return f"gh-{branch_name}"
5251

5352

54-
def get_local_ip() -> str:
55-
"""Get the local machine's IP"""
56-
hostname = socket.gethostname()
57-
ip_addr = socket.gethostbyname(hostname)
58-
return ip_addr
59-
60-
6153
class DockerTestCase(TestCase):
6254
"""Mixin for dealing with containers"""
6355

@@ -113,6 +105,9 @@ def run_container(self, **specs: dict[str, Any]) -> Container:
113105
specs["network"] = self.__network.name
114106
specs["labels"] = self.docker_labels
115107
specs["detach"] = True
108+
specs["extra_hosts"] = {
109+
"host.docker.internal": "host-gateway",
110+
}
116111
if hasattr(self, "live_server_url"):
117112
specs.setdefault("environment", {})
118113
specs["environment"]["AUTHENTIK_HOST"] = self.live_server_url
@@ -155,7 +150,6 @@ def tearDown(self):
155150
class SeleniumTestCase(DockerTestCase, StaticLiveServerTestCase):
156151
"""StaticLiveServerTestCase which automatically creates a Webdriver instance"""
157152

158-
host = get_local_ip()
159153
wait_timeout: int
160154
user: User
161155

@@ -210,6 +204,15 @@ def wait_for_url(self, desired_url):
210204
f"URL {self.driver.current_url} doesn't match expected URL {desired_url}",
211205
)
212206

207+
def host_url(self, view, query: dict | None = None, **kwargs) -> str:
208+
"""reverse `view` with `**kwargs` into full URL using live_server_url"""
209+
url = f"http://host.docker.internal:{self.server_thread.port}" + reverse(
210+
view, kwargs=kwargs
211+
)
212+
if query:
213+
return url + "?" + urlencode(query)
214+
return url
215+
213216
def url(self, view, query: dict | None = None, **kwargs) -> str:
214217
"""reverse `view` with `**kwargs` into full URL using live_server_url"""
215218
url = self.live_server_url + reverse(view, kwargs=kwargs)

0 commit comments

Comments
 (0)