Skip to content
Snippets Groups Projects
Commit 326653d0 authored by Charlotte Hausman's avatar Charlotte Hausman
Browse files

misc. clean up

parent cfcec469
No related branches found
No related tags found
1 merge request!481misc. clean up
Pipeline #2788 passed
......@@ -4,15 +4,14 @@ import logging
import os
import sys
from pathlib import Path
# pylint: disable=C0301, E0401, W0622, W1203
from typing import Dict
from pycapo import CapoConfig
from carta_envoy.utilities import CAPO_UI_SETTINGS_KEY
from carta_envoy.launchers import CartaLauncher
# pylint: disable=C0301, E0401, W0622, W1203
"""
Setup and Launch CARTA via Workspaces
(modified from carta-valet)
......@@ -34,7 +33,7 @@ def _get_settings(location: str) -> Dict:
ws_carta = CapoConfig().settings("edu.nrao.workspaces.carta")
notification_url = CapoConfig().settings("edu.nrao.workspaces.NotificationSettings").serviceUrl
workflow_url = CapoConfig().settings("edu.nrao.workspaces.WorkflowSettings").serviceUrl
ui_url = CapoConfig().settings(CAPO_UI_SETTINGS_KEY).serviceUrl
ui_url = CapoConfig().settings("edu.nrao.workspaces.UISettings").serviceUrl
return {
"timeout": carta_settings.timeoutInMinutes,
......@@ -134,8 +133,7 @@ def make_arg_parser() -> argparse.ArgumentParser:
"--single",
action="store_true",
required=False,
help="allow CARTA envoy to launch with specified image"
" (Use when data must be retrieved for viewing)",
help="allow CARTA envoy to launch with specified image" " (Use when data must be retrieved for viewing)",
)
parser.add_argument(
"-wf",
......
......@@ -99,14 +99,10 @@ class CartaLauncher:
self.logger.info(f"file_browser_path: {file_browser_path}")
if not file_browser_path.exists():
self.logger.error(
f"ERROR: Invalid file browser path. {file_browser_path}: Directory does not exist."
)
self.logger.error(f"ERROR: Invalid file browser path. {file_browser_path}: Directory does not exist.")
return
elif not file_browser_path.is_dir():
self.logger.error(
f"ERROR: Invalid file browser path. {file_browser_path}: Path is not a directory."
)
self.logger.error(f"ERROR: Invalid file browser path. {file_browser_path}: Path is not a directory.")
return
if self.settings["useCarta"]:
......@@ -160,7 +156,7 @@ class CartaLauncher:
def notify_ready(self, carta_url: str, html_dir: Path):
"""
Sends URL notification to user and request handler
Sends URL notification to user or request handler
:param carta_url: URL to CARTA session
:param html_dir: location of CARTA HTML wrapper
......@@ -168,16 +164,14 @@ class CartaLauncher:
:return:
"""
self.logger.info("SENDING URL NOTIFICATION TO: ")
if self.settings["send_ready"] == "true":
self.logger.info(f"User Email")
self.logger.info("User Email")
self.notification.send_session_ready(carta_url)
elif self.settings["send_ready"] == "false":
self.logger.info(f"AAT Request Handler, with {carta_url}")
self.logger.info("AAT Request Handler")
self.archive_connect.send_carta_url_to_rh(carta_url)
def teardown(self):
"""
Deletes instance's Redis keys from the Redis server
......
......@@ -4,7 +4,6 @@ general helper methods shared between envoy modules
import random
import string
CAPO_UI_SETTINGS_KEY = "edu.nrao.workspaces.UISettings"
CARTA_URL_REPLACE_TEXT = "CARTA_URL_GOES_HERE"
CARTA_HTML_FILENAME = "carta_url_page.html"
CARTA_TEMPLATE = f"""
......
......@@ -28,7 +28,6 @@ LOCATION = "/fake/location/path"
class TestCarta:
"""Tests for carta module"""
def test_get_settings(self):
settings = carta.get_carta_settings(LOCATION)
for key in settings.keys():
......
"""
Tests for carta_envoy.launchers
"""
import logging
import shutil
import tempfile
from pathlib import Path
from unittest.mock import MagicMock, patch
# pylint: disable=E0401, R0201
import pytest
from carta_envoy.utilities import (
CARTA_URL_REPLACE_TEXT,
......@@ -17,10 +13,8 @@ from carta_envoy.utilities import (
)
from carta_envoy.launchers import CartaLauncher
logger = logging.getLogger("casa_envoy")
UI_URL = "http://localhost:4444/workspaces"
CARTA_URL = UI_URL + "/carta/requests/-1/html"
test_settings = {
"timeout": 1,
"carta_path": "/fake/path/to/nowhere",
......@@ -39,10 +33,8 @@ launcher = CartaLauncher(settings=test_settings)
SUBPROCESS_COMMAND_PATCH = "carta_envoy.launchers.subprocess.Popen"
CARTA_HTML_TEST_PATH = (
"/lustre/aoc/cluster/pipeline/docker/workspaces/carta/requests/-1/html"
)
FAKE_WRAPPER_URL = "http://localhost:4444/workspaces/carta/requests/-1/html"
CARTA_HTML_TEST_PATH = "/path/to/nowhere"
WRAPPER_URL = "http://localhost:4444/workspaces/carta/requests/-1/html"
BACK_END_PORT = 7777
FRONT_END_PORT = 6464
......@@ -163,7 +155,8 @@ class TestCartaLauncher:
assert mock_frame.call_count == 1
mock_notification_connect.send_session_ready.assert_called_with("carta_url")
def test_generates_carta_html(self):
@patch("pathlib.Path.exists")
def test_generates_carta_html(self, mock_path):
"""
Test that we can make a nice HTML page containing the CARTA URL in a frame
:return:
......@@ -174,38 +167,18 @@ class TestCartaLauncher:
mock_notification_connect = MagicMock()
launcher.notification = mock_notification_connect
carta_path = Path(tempfile.mkdtemp())
with patch(
"carta_envoy.launchers.CartaLauncher.create_frame_html"
) as mock_carta_html:
with patch("carta_envoy.launchers.CartaLauncher.create_frame_html") as mock_carta_html:
with patch(SUBPROCESS_COMMAND_PATCH):
launcher.run_carta(
path_to_carta=str(carta_path),
path_to_carta=test_settings["carta_path"],
timeout_minutes=1,
file_browser_path=Path(),
file_browser_path=Path(test_settings["data_location"]),
front_end_port=FRONT_END_PORT,
back_end_port=BACK_END_PORT,
carta_url=CARTA_URL,
carta_url="carta url",
)
mock_carta_html.assert_called()
shutil.rmtree(carta_path)
def test_generates_wrapper_url(self):
"""
Does the launcher generate the expected link to the CARTA URL wrapper page?
:return:
"""
expected_url = f"{UI_URL}/carta/requests/-1/html"
logger.info("Pretending to generate CARTA wrapper url...")
wrapper_url = generate_carta_frame_url(UI_URL, Path(CARTA_HTML_TEST_PATH))
assert wrapper_url == expected_url
logger.info(f"CARTA wrapper URL: {wrapper_url}")
assert mock_carta_html.call_count == 1
def test_serves_carta_wrapper(self):
"""
......@@ -232,7 +205,7 @@ class TestCartaLauncher:
file_browser_path=Path(CARTA_HTML_TEST_PATH),
front_end_port=FRONT_END_PORT,
back_end_port=BACK_END_PORT,
carta_url=CARTA_URL,
carta_url="carta url",
)
assert mock_subprocess.call_count == 1
mock_subprocess.assert_called_with(
......@@ -248,94 +221,9 @@ class TestCartaLauncher:
stdout=-1,
stderr=-1,
)
mock_notification_connect.send_session_ready.assert_called_with(CARTA_URL)
mock_notification_connect.send_session_ready.assert_called_with("carta url")
carta_html = Path(CARTA_HTML_TEST_PATH) / CARTA_HTML_FILENAME
expected_html = CARTA_TEMPLATE.replace(CARTA_URL_REPLACE_TEXT, CARTA_URL)
with patch("pathlib.Path.is_file", return_value=True):
assert carta_html.is_file()
expected_html = CARTA_TEMPLATE.replace(CARTA_URL_REPLACE_TEXT, WRAPPER_URL)
with patch("pathlib.Path.read_text", return_value=expected_html):
assert CARTA_URL in carta_html.read_text()
@pytest.mark.skip(
"works only locally -- not in the CI. retained for hysterical porpoises"
)
def test_serves_carta_wrapper_no_mock(self):
"""
Can we create HTML containing the CARTA URL in a frame and serve it up?
:return:
"""
# 1. make fake file_browser_path
html_dir = Path(CARTA_HTML_TEST_PATH)
html_dir.mkdir(parents=True, exist_ok=True)
assert html_dir.is_dir()
# 2. copy necessary files there
template_source = "/packages/apps/cli/executables/pexable/carta_envoy/carta_envoy/carta_url_template.html"
shutil.copy(template_source, CARTA_HTML_TEST_PATH)
test_data_dir = Path(
"/packages/apps/cli/executables/pexable/carta_envoy/test/carta_test_data"
)
assert test_data_dir.is_dir()
for file in test_data_dir.glob("*.fits"):
source = str(file)
shutil.copy(source, CARTA_HTML_TEST_PATH)
launcher.settings["useCarta"] = True
launcher.settings["send_ready"] = "true"
mock_notification_connect = MagicMock()
launcher.notification = mock_notification_connect
fake_carta_url = "fake_carta_url"
fake_carta_path = "fake_carta_path"
# 3. pretend to launch CARTA
with patch(SUBPROCESS_COMMAND_PATCH) as mock_subprocess:
launcher.run_carta(
path_to_carta=fake_carta_path,
timeout_minutes=1,
file_browser_path=test_data_dir,
front_end_port=FRONT_END_PORT,
back_end_port=BACK_END_PORT,
carta_url=fake_carta_url,
)
assert mock_subprocess.call_count == 1
mock_subprocess.assert_called_with(
[
fake_carta_path,
f"--port={BACK_END_PORT}",
f"--fport={FRONT_END_PORT}",
"--folder=" + str(test_data_dir),
"--root=" + str(test_data_dir),
],
preexec_fn=None,
stdin=-1,
stdout=-1,
stderr=-1,
)
mock_notification_connect.send_session_ready.assert_called_with(
fake_carta_url
)
carta_html = test_data_dir / "carta_url_page.html"
assert carta_html.is_file()
assert fake_carta_url in carta_html.read_text()
shutil.rmtree(html_dir)
def generate_carta_frame_url(base_url: str, carta_html_path: Path) -> str:
"""
Convert the CARTA URL into the URL of the page that contains link to CARTA session
in a frame.
:param base_url: CARTA service prefix
:param carta_html_path: location of the wrapper page
:return:
"""
wf_request_id = carta_html_path.parent.name
return f"{base_url}/carta/requests/{wf_request_id}/{carta_html_path.name}"
assert WRAPPER_URL in carta_html.read_text()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment