Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions test/commands/exec_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ def queue_test_success_res(sessionId="testSessionId"):
return {"success": True, "sessionId": sessionId}


def app_config_res():
return {"hostManifest": {"lab": {"workcell": "something.bar.foo"}}}


def mock_api_endpoint():
return "foo.bar.baz"
return "foo.bar.baz/lab/workcell"


def mockget(*args, **kwargs):
return MockResponse(0, app_config_res(), json.dumps(app_config_res()))


@pytest.fixture
Expand All @@ -39,12 +47,13 @@ def mockpost(*args, **kwargs):
)

monkeypatch.setattr(requests, "post", mockpost)
monkeypatch.setattr(requests, "get", mockget)
result = cli_test_runner.invoke(
cli, ["exec", str(ap_file), "-a", mock_api_endpoint()]
)
assert result.exit_code == 0
assert (
f"Success. View {mock_api_endpoint()}/dashboard?sessionId=testSessionId to see the scheduling outcome."
f"Success. View http://{mock_api_endpoint()}/dashboard to see the scheduling outcome."
in result.output
)

Expand Down Expand Up @@ -81,6 +90,7 @@ def mockpost(*args, **kwargs):
return MockResponse(0, "not-json", "not-json")

monkeypatch.setattr(requests, "post", mockpost)
monkeypatch.setattr(requests, "get", mockget)
result = cli_test_runner.invoke(
cli, ["exec", str(ap_file), "-a", mock_api_endpoint()]
)
Expand All @@ -95,22 +105,23 @@ def mockpost(*args, **kwargs):
)

monkeypatch.setattr(requests, "post", mockpost)
monkeypatch.setattr(requests, "get", mockget)
result = cli_test_runner.invoke(
cli, ["exec", str(ap_file), "-a", mock_api_endpoint(), "-w", "wc3"]
)
assert result.exit_code == 0
assert (
f"Success. View {mock_api_endpoint()}/dashboard?sessionId=testSessionId to see the scheduling outcome."
f"Success. View http://{mock_api_endpoint()}/dashboard to see the scheduling outcome."
in result.output
)


def test_bad_workcell(cli_test_runner, ap_file):
def test_bad_workcell(cli_test_runner, monkeypatch, ap_file):
result = cli_test_runner.invoke(
cli, ["exec", str(ap_file), "-a", mock_api_endpoint(), "-w", "bad-workcell-id"]
cli, ["exec", str(ap_file), "-a", mock_api_endpoint(), "-w", "hello.world"]
)
assert result.exit_code != 0
assert "Workcell id must be like wcN but was bad-workcell-id" in result.stderr
assert "Error: " in result.stderr


def test_session_id(cli_test_runner, monkeypatch, ap_file):
Expand All @@ -124,6 +135,7 @@ def mockpost(*args, **kwargs):
)

monkeypatch.setattr(requests, "post", mockpost)
monkeypatch.setattr(requests, "get", mockget)
result = cli_test_runner.invoke(
cli,
[
Expand All @@ -137,7 +149,7 @@ def mockpost(*args, **kwargs):
)
assert result.exit_code == 0
assert (
f"Success. View {mock_api_endpoint()}/dashboard?sessionId={sessionId} to see the scheduling outcome."
f"Success. View http://{mock_api_endpoint()}/dashboard to see the scheduling outcome."
in result.output
)

Expand Down
2 changes: 1 addition & 1 deletion transcriptic/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ def format_cmd(manifest):
@click.option(
"--workcell-id",
"-w",
help="The workcell id to use for the device set. This is not permitted along with the `device-set` or `session-id` option.",
help="The workcell id to use for the device set (wc4-mcx1, tst-01-mcx-01, etc.). This is not permitted along with the `device-set` or `session-id` option.",
)
@click.option(
"--device-set",
Expand Down
58 changes: 46 additions & 12 deletions transcriptic/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,30 @@ def execute(
partition_horizon,
partitioning_swap_device_id,
):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good. You could also support (or not?) https. I don't think we have the requirement to support it, but you could also warn if the user accidentally uses https protocol.

# Clean api end point
if api.startswith("http://"):
clean_api = api[7:]
elif api.startswith("https://"):
click.echo("HTTPS endpoint is not supported, falling back to HTTP.")
clean_api = api[8:]
else:
clean_api = api
if clean_api[-1] == "/":
clean_api = clean_api[0:-1] # remove trailing slash

# Validate api
path_tokens = clean_api.split("/")
if len(path_tokens) != 3:
click.echo(
f"Invalid api target, expects http://base/facility/workcell.", err=True
)
return

clean_api = f"http://{clean_api}"
path_base = f"http://{path_tokens[0]}"
path_lab = path_tokens[1]
path_workcell = path_tokens[2]

# Define the initial payload
payload = {"timeLimit": f"{time_limit}:second"}

Expand Down Expand Up @@ -1527,9 +1551,9 @@ def execute(
in_use.append("--device-set")

if workcell_id:
if not re.search("^wc[a-z,0-9]+$", workcell_id):
raise BadParameter(f"Workcell id must be like wcN but was {workcell_id}")
payload["workcellIdForDeviceSet"] = f"{workcell_id}-mcx1"
if "." in workcell_id:
raise BadParameter(f"Workcell id can't have '.' but was {workcell_id}")
payload["workcellIdForDeviceSet"] = workcell_id
in_use.append("--workcell-id")

if session_id is not None:
Expand All @@ -1553,27 +1577,37 @@ def execute(
if partitioning_swap_device_id is not None:
payload["partitioningSwapDeviceId"] = partitioning_swap_device_id

# Clean api end point
if api[-1] == "/":
clean_api = api[0:-1] # remove trailing slash
else:
clean_api = api
res = requests.get(f"{path_base}/app-config")
try:
res_json = json.loads(res.text)
if (
res_json["hostManifest"]
and res_json["hostManifest"][path_lab]
and res_json["hostManifest"][path_lab][path_workcell]
):
frontend_node_address = res_json["hostManifest"][path_lab][path_workcell]
else:
click.echo(f"Error when get frontend node address: {res_json}", err=True)
return
except json.decoder.JSONDecodeError:
click.echo(f"Error when get frontend node address: {res.text}", err=True)
return

# POST to workcell
test_run_endpoint = f"{clean_api}/testRun"
click.echo("Sending request...")
test_run_endpoint = f"http://{frontend_node_address}/testRun"
click.echo(f"Sending request to {frontend_node_address}")
res = requests.post(test_run_endpoint, json=payload)
try:
res_json = json.loads(res.text)
if res_json["success"]:
click.echo(
f"Success. View {clean_api}/dashboard?sessionId={res_json['sessionId']} to see the scheduling outcome."
f"Success. View {clean_api}/dashboard to see the scheduling outcome."
)
else:
click.echo(f"Error: {res_json['message']}", err=True)
if "sessionId" in res_json:
click.echo(
f"Dashboard can be seen at: {clean_api}/dashboard?sessionId={res_json['sessionId']}",
f"Dashboard can be seen at: {clean_api}/dashboard",
err=True,
)
except json.decoder.JSONDecodeError:
Expand Down