11from __future__ import annotations
22
3+ import pytest
34from pytest import MonkeyPatch
45
56from porter_sandbox ._config import Config
67
7- DEFAULT_BASE_URL = "http://sandbox-api.porter-sandbox-system.svc.cluster.local:8080"
8+ IN_CLUSTER_BASE_URL = "http://sandbox-api.porter-sandbox-system.svc.cluster.local:8080"
9+
10+ RESOLUTION_ENV_VARS = (
11+ "PORTER_SANDBOX_BASE_URL" ,
12+ "PORTER_SANDBOX_API_KEY" ,
13+ "PORTER_PROJECT_ID" ,
14+ "PORTER_CLUSTER_ID" ,
15+ "KUBERNETES_SERVICE_HOST" ,
16+ )
17+
18+
19+ @pytest .fixture (autouse = True )
20+ def clean_env (monkeypatch : MonkeyPatch ) -> None :
21+ for var in RESOLUTION_ENV_VARS :
22+ monkeypatch .delenv (var , raising = False )
823
924
1025def test_config_falls_back_to_in_cluster_sandbox_api_url (monkeypatch : MonkeyPatch ) -> None :
11- monkeypatch .delenv ( "PORTER_SANDBOX_BASE_URL " , raising = False )
26+ monkeypatch .setenv ( "KUBERNETES_SERVICE_HOST " , "10.0.0.1" )
1227
13- assert Config .resolve ().base_url == DEFAULT_BASE_URL
28+ assert Config .resolve ().base_url == IN_CLUSTER_BASE_URL
1429
1530
16- def test_config_uses_environment_base_url_when_no_base_url_is_passed (monkeypatch : MonkeyPatch ) -> None :
31+ def test_config_uses_environment_base_url_when_no_base_url_is_passed (
32+ monkeypatch : MonkeyPatch ,
33+ ) -> None :
1734 monkeypatch .setenv ("PORTER_SANDBOX_BASE_URL" , "https://sandbox.example/" )
1835
1936 assert Config .resolve ().base_url == "https://sandbox.example"
@@ -22,4 +39,77 @@ def test_config_uses_environment_base_url_when_no_base_url_is_passed(monkeypatch
2239def test_config_prefers_explicit_base_url_over_environment (monkeypatch : MonkeyPatch ) -> None :
2340 monkeypatch .setenv ("PORTER_SANDBOX_BASE_URL" , "https://sandbox.example" )
2441
25- assert Config .resolve (base_url = "https://sandbox.override/" ).base_url == "https://sandbox.override"
42+ assert (
43+ Config .resolve (base_url = "https://sandbox.override/" ).base_url == "https://sandbox.override"
44+ )
45+
46+
47+ def test_config_builds_external_url_from_project_and_cluster_ids (monkeypatch : MonkeyPatch ) -> None :
48+ monkeypatch .setenv ("PORTER_PROJECT_ID" , "123" )
49+ monkeypatch .setenv ("PORTER_CLUSTER_ID" , "456" )
50+ monkeypatch .setenv ("PORTER_SANDBOX_API_KEY" , "token" )
51+
52+ assert (
53+ Config .resolve ().base_url
54+ == "https://dashboard.porter.run/api/v2/alpha/projects/123/clusters/456"
55+ )
56+
57+
58+ def test_config_prefers_project_and_cluster_ids_over_in_cluster_detection (
59+ monkeypatch : MonkeyPatch ,
60+ ) -> None :
61+ monkeypatch .setenv ("PORTER_PROJECT_ID" , "123" )
62+ monkeypatch .setenv ("PORTER_CLUSTER_ID" , "456" )
63+ monkeypatch .setenv ("PORTER_SANDBOX_API_KEY" , "token" )
64+ monkeypatch .setenv ("KUBERNETES_SERVICE_HOST" , "10.0.0.1" )
65+
66+ assert (
67+ Config .resolve ().base_url
68+ == "https://dashboard.porter.run/api/v2/alpha/projects/123/clusters/456"
69+ )
70+
71+
72+ def test_config_prefers_environment_base_url_over_project_and_cluster_ids (
73+ monkeypatch : MonkeyPatch ,
74+ ) -> None :
75+ monkeypatch .setenv ("PORTER_PROJECT_ID" , "123" )
76+ monkeypatch .setenv ("PORTER_CLUSTER_ID" , "456" )
77+ monkeypatch .setenv ("PORTER_SANDBOX_BASE_URL" , "https://sandbox.example" )
78+
79+ assert Config .resolve ().base_url == "https://sandbox.example"
80+
81+
82+ def test_config_requires_api_key_for_external_url (monkeypatch : MonkeyPatch ) -> None :
83+ monkeypatch .setenv ("PORTER_PROJECT_ID" , "123" )
84+ monkeypatch .setenv ("PORTER_CLUSTER_ID" , "456" )
85+
86+ with pytest .raises (ValueError , match = "PORTER_SANDBOX_API_KEY" ):
87+ Config .resolve ()
88+
89+
90+ def test_config_accepts_api_key_argument_for_external_url (monkeypatch : MonkeyPatch ) -> None :
91+ monkeypatch .setenv ("PORTER_PROJECT_ID" , "123" )
92+ monkeypatch .setenv ("PORTER_CLUSTER_ID" , "456" )
93+
94+ config = Config .resolve (api_key = "token" )
95+
96+ assert config .base_url == "https://dashboard.porter.run/api/v2/alpha/projects/123/clusters/456"
97+
98+
99+ def test_config_rejects_project_id_without_cluster_id (monkeypatch : MonkeyPatch ) -> None :
100+ monkeypatch .setenv ("PORTER_PROJECT_ID" , "123" )
101+
102+ with pytest .raises (ValueError , match = "PORTER_CLUSTER_ID is not" ):
103+ Config .resolve ()
104+
105+
106+ def test_config_rejects_cluster_id_without_project_id (monkeypatch : MonkeyPatch ) -> None :
107+ monkeypatch .setenv ("PORTER_CLUSTER_ID" , "456" )
108+
109+ with pytest .raises (ValueError , match = "PORTER_PROJECT_ID is not" ):
110+ Config .resolve ()
111+
112+
113+ def test_config_raises_when_base_url_cannot_be_determined () -> None :
114+ with pytest .raises (ValueError , match = "Could not determine the sandbox API base URL" ):
115+ Config .resolve ()
0 commit comments