Skip to content

Commit 648425c

Browse files
authored
Merge pull request #85 from DataKitchen/tg-base-url
feat: set TG_UI_BASE_URL env var in TestGen compose file
2 parents 97262e9 + 533b72d commit 648425c

File tree

3 files changed

+89
-5
lines changed

3 files changed

+89
-5
lines changed

dk-installer.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@
8989
#
9090

9191

92+
def _get_tg_base_url(args):
93+
protocol = "https" if args.ssl_cert_file and args.ssl_key_file else "http"
94+
return f"{protocol}://localhost:{args.port}"
95+
96+
9297
def collect_images_digest(action, images, env=None):
9398
if images:
9499
action.run_cmd(
@@ -1661,6 +1666,7 @@ def __init__(self):
16611666
self.update_version = False
16621667
self.update_analytics = False
16631668
self.update_token = False
1669+
self.update_base_url = False
16641670
super().__init__()
16651671

16661672
def pre_execute(self, action, args):
@@ -1720,12 +1726,19 @@ def pre_execute(self, action, args):
17201726

17211727
self.update_token = "TG_JWT_HASHING_KEY" not in contents
17221728

1723-
if not any((self.update_version, self.update_analytics, self.update_token)):
1729+
self.update_base_url = "TG_UI_BASE_URL" not in contents
1730+
if self.update_base_url:
1731+
port_match = re.search(r"- (\d+):8501", contents)
1732+
port = port_match.group(1) if port_match else str(TESTGEN_DEFAULT_PORT)
1733+
protocol = "https" if "SSL_CERT_FILE" in contents else "http"
1734+
self._base_url = f"{protocol}://localhost:{port}"
1735+
1736+
if not any((self.update_version, self.update_analytics, self.update_token, self.update_base_url)):
17241737
CONSOLE.msg("No changes will be applied.")
17251738
raise AbortAction
17261739

17271740
def execute(self, action, args):
1728-
if not any((self.update_version, self.update_analytics, self.update_token)):
1741+
if not any((self.update_version, self.update_analytics, self.update_token, self.update_base_url)):
17291742
raise SkipStep
17301743

17311744
contents = action.get_compose_file_path(args).read_text()
@@ -1755,6 +1768,11 @@ def execute(self, action, args):
17551768
var = f"\n{match.group(1)}TG_JWT_HASHING_KEY: {str(base64.b64encode(random.randbytes(32)), 'ascii')}"
17561769
contents = contents[0 : match.end()] + match.group(1) + var + contents[match.end() :]
17571770

1771+
if self.update_base_url:
1772+
match = re.search(r"^([ \t]+)TG_METADATA_DB_HOST:.*$", contents, flags=re.M)
1773+
var = f"\n{match.group(1)}TG_UI_BASE_URL: {self._base_url}"
1774+
contents = contents[0 : match.end()] + var + contents[match.end() :]
1775+
17581776
action.get_compose_file_path(args).write_text(contents)
17591777

17601778

@@ -1787,10 +1805,9 @@ def pre_execute(self, action, args):
17871805

17881806
def on_action_success(self, action, args):
17891807
super().on_action_success(action, args)
1790-
protocol = "https" if args.ssl_cert_file and args.ssl_key_file else "http"
17911808
cred_file_path = action.data_folder.joinpath(CREDENTIALS_FILE.format(args.prod))
17921809
with CONSOLE.tee(cred_file_path) as console_tee:
1793-
console_tee(f"User Interface: {protocol}://localhost:{args.port}")
1810+
console_tee(f"User Interface: {_get_tg_base_url(args)}")
17941811
console_tee("CLI Access: docker compose exec engine bash")
17951812
console_tee("")
17961813
console_tee(f"Username: {self.username}")
@@ -1849,6 +1866,7 @@ def get_compose_file_contents(self, action, args):
18491866
TG_EXPORT_TO_OBSERVABILITY_VERIFY_SSL: no
18501867
TG_INSTANCE_ID: {action.analytics.get_instance_id()}
18511868
TG_ANALYTICS: {"yes" if args.send_analytics_data else "no"}
1869+
TG_UI_BASE_URL: {_get_tg_base_url(args)}
18521870
{ssl_variables}
18531871
18541872
services:

tests/test_tg_install.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,27 @@ def test_tg_create_compose_file_abort_args(arg_to_set, tg_install_action, stdout
8686
console_msg_mock.assert_any_msg_contains(
8787
"Both --ssl-cert-file and --ssl-key-file must be provided to use SSL certificates.",
8888
)
89+
90+
91+
@pytest.mark.integration
92+
def test_tg_compose_contains_base_url(tg_install_action, start_cmd_mock, stdout_mock, compose_path):
93+
tg_install_action.execute()
94+
contents = compose_path.read_text()
95+
assert "TG_UI_BASE_URL: http://localhost:8501" in contents
96+
97+
98+
@pytest.mark.integration
99+
def test_tg_compose_base_url_custom_port(tg_install_action, start_cmd_mock, stdout_mock, args_mock, compose_path):
100+
args_mock.port = 9000
101+
tg_install_action.execute()
102+
contents = compose_path.read_text()
103+
assert "TG_UI_BASE_URL: http://localhost:9000" in contents
104+
105+
106+
@pytest.mark.integration
107+
def test_tg_compose_base_url_ssl(tg_install_action, start_cmd_mock, stdout_mock, args_mock, compose_path):
108+
args_mock.ssl_cert_file = "/path/to/cert.crt"
109+
args_mock.ssl_key_file = "/path/to/cert.key"
110+
tg_install_action.execute()
111+
contents = compose_path.read_text()
112+
assert "TG_UI_BASE_URL: https://localhost:8501" in contents

tests/test_tg_upgrade.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ def test_tg_upgrade_abort(
125125
):
126126
args_mock.skip_verify = False
127127
set_version_check_mock(version_check_mock, "1.0.0")
128-
initial_compose_content = get_compose_content("TG_INSTANCE_ID: test-instance-id")
128+
initial_compose_content = get_compose_content(
129+
"TG_INSTANCE_ID: test-instance-id", "TG_UI_BASE_URL: http://localhost:8501"
130+
)
129131
compose_path.write_text(initial_compose_content)
130132

131133
with pytest.raises(AbortAction):
@@ -186,3 +188,43 @@ def test_tg_upgrade_disable_analytics(
186188
assert "TG_ANALYTICS: no" in compose_content
187189
assert "image: datakitchen/dataops-testgen:v2.14.5" in compose_content
188190
console_msg_mock.assert_any_msg_contains("Application is already up-to-date.")
191+
192+
193+
@pytest.mark.integration
194+
def test_tg_upgrade_adds_base_url(
195+
tg_upgrade_action,
196+
compose_path,
197+
start_cmd_mock,
198+
tg_upgrade_stdout_side_effect,
199+
args_mock,
200+
version_check_mock,
201+
):
202+
set_version_check_mock(version_check_mock, "1.0.0")
203+
compose_path.write_text(get_compose_content("TG_INSTANCE_ID: test-instance-id"))
204+
205+
tg_upgrade_action.execute(args_mock)
206+
207+
compose_content = compose_path.read_text()
208+
assert "TG_UI_BASE_URL: http://localhost:8501" in compose_content
209+
210+
211+
@pytest.mark.integration
212+
def test_tg_upgrade_preserves_existing_base_url(
213+
tg_upgrade_action,
214+
compose_path,
215+
start_cmd_mock,
216+
tg_upgrade_stdout_side_effect,
217+
args_mock,
218+
version_check_mock,
219+
):
220+
args_mock.skip_verify = True
221+
set_version_check_mock(version_check_mock, "1.1.0")
222+
compose_path.write_text(
223+
get_compose_content("TG_INSTANCE_ID: test-instance-id", "TG_UI_BASE_URL: https://custom.example.com")
224+
)
225+
226+
tg_upgrade_action.execute(args_mock)
227+
228+
compose_content = compose_path.read_text()
229+
assert "TG_UI_BASE_URL: https://custom.example.com" in compose_content
230+
assert compose_content.count("TG_UI_BASE_URL") == 1

0 commit comments

Comments
 (0)