Skip to content

Commit

Permalink
fix execute exp. profile test
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Sep 13, 2024
1 parent dace2e0 commit 5dacd71
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 163 deletions.
4 changes: 2 additions & 2 deletions pioreactor/actions/leader/experiment_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ def _callable() -> None:
condition_met = evaluate_bool_expression(condition, env)
except MQTTValueError:
condition_met = False

if condition_met:
for action in actions:
schedule.enter(
Expand Down Expand Up @@ -637,8 +638,7 @@ def _callable() -> None:
logger.info(f"Dry-run: Stopping {job_name} on {unit}.")
else:
patch_into_leader(
f"/api/workers/{unit}/jobs/update/job_name/{job_name}/experiments/{experiment}",
json={"settings": {"$state": "disconnected"}},
f"/api/workers/{unit}/jobs/stop/job_name/{job_name}/experiments/{experiment}",
)
else:
logger.debug(f"Action's `if` condition, `{if_}`, evaluated False. Skipping action.")
Expand Down
25 changes: 18 additions & 7 deletions pioreactor/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import re
from unittest.mock import MagicMock
from unittest.mock import patch
from urllib.parse import urlparse

import pytest

from pioreactor.mureq import Response
from pioreactor.pubsub import publish


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -57,26 +59,23 @@ def active_workers_in_cluster():

@pytest.fixture(autouse=True)
def mock_external_leader_webserver_apis(mocker, active_workers_in_cluster):
# used mostly in pioreactor.config.py
def mock_get_response(endpoint):
mm = MagicMock()
if endpoint.endswith("/api/workers"):
mm = MagicMock()
mm.json.return_value = [
{"pioreactor_unit": unit, "is_active": 1} for unit in active_workers_in_cluster
] + [{"pioreactor_unit": "notactiveworker", "is_active": 0}]
return mm
elif re.search("/api/experiments/.*/workers", endpoint):
mm = MagicMock()
mm.json.return_value = [
{"pioreactor_unit": unit, "is_active": 1} for unit in active_workers_in_cluster
]
return mm
elif re.search("/api/workers/.*/experiment", endpoint):
mm = MagicMock()
mm.json.return_value = {"experiment": "_testing_experiment"}
return mm
else:
raise ValueError(f"{endpoint} not mocked")
raise ValueError(f"TODO: {endpoint} not mocked")

mock_get = mocker.patch(
"pioreactor.cluster_management.get_from_leader", autospec=True, side_effect=mock_get_response
Expand All @@ -86,11 +85,16 @@ def mock_get_response(endpoint):


class CapturedRequest:
def __init__(self, method, url, headers, body):
def __init__(self, method, url, headers, body, json):
self.method = method
self.url = url
self.headers = headers
self.body = body
self.json = json

r = urlparse(url)

self.path = r.path


@contextlib.contextmanager
Expand All @@ -101,7 +105,14 @@ def mock_request(method, url, **kwargs):
# Capture the request details
headers = kwargs.get("headers")
body = kwargs.get("body", None)
bucket.append(CapturedRequest(method, url, headers, body))
json = kwargs.get("json", None)
bucket.append(CapturedRequest(method, url, headers, body, json))

if re.search("/api/workers/.*/jobs/update/job_name/.*/experiments/.*", url):
# fire a mqtt too
r = re.search("/api/workers/(.*)/jobs/update/job_name/(.*)/experiments/(.*)", url)
for setting, v in json["settings"].items():
publish(f"pioreactor/{r.groups()[0]}/{r.groups()[2]}/{r.groups()[1]}/{setting}/set", v)

# Return a mock response object
return Response(url, 200, {}, b'{"mocked": "response"}')
Expand Down
Loading

0 comments on commit 5dacd71

Please sign in to comment.