Skip to content

Commit c7afd3a

Browse files
authored
Minor updates to mock server behavior (#3314)
* Minor updates to mock server behavior * Fix tests that assert on servicer.deployed_apps directly
1 parent e50878f commit c7afd3a

3 files changed

Lines changed: 36 additions & 36 deletions

File tree

test/app_test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,16 @@ def test_create_object_invalid_exception(servicer, client):
9191
def test_deploy_falls_back_to_app_name(servicer, client):
9292
named_app = App(name="foo_app")
9393
named_app.deploy(client=client)
94-
assert "foo_app" in servicer.deployed_apps
94+
app_names = {app_name for (_, app_name) in servicer.deployed_apps}
95+
assert "foo_app" in app_names
9596

9697

9798
def test_deploy_uses_deployment_name_if_specified(servicer, client):
9899
named_app = App(name="foo_app")
99100
named_app.deploy(name="bar_app", client=client)
100-
assert "bar_app" in servicer.deployed_apps
101-
assert "foo_app" not in servicer.deployed_apps
101+
app_names = {app_name for (_, app_name) in servicer.deployed_apps}
102+
assert "bar_app" in app_names
103+
assert "foo_app" not in app_names
102104

103105

104106
def test_run_function_without_app_error():

test/cli_test.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,16 @@ def test_app_deploy_success(servicer, mock_dir, set_env_client):
7676
# Deploy as a script with an absolute path
7777
_run(["deploy", os.path.abspath("myapp.py")])
7878

79-
assert "my_app" in servicer.deployed_apps
79+
app_names = {app_name for (_, app_name) in servicer.deployed_apps}
80+
assert "my_app" in app_names
8081

8182

8283
def test_app_deploy_with_name(servicer, mock_dir, set_env_client):
8384
with mock_dir({"myapp.py": dummy_app_file, "other_module.py": dummy_other_module_file}):
8485
_run(["deploy", "myapp.py", "--name", "my_app_foo"])
8586

86-
assert "my_app_foo" in servicer.deployed_apps
87+
app_names = {app_name for (_, app_name) in servicer.deployed_apps}
88+
assert "my_app_foo" in app_names
8789

8890

8991
def test_secret_create_list_delete(servicer, set_env_client):
@@ -987,7 +989,7 @@ def test_app_history(servicer, mock_dir, set_env_client):
987989
with mock_dir({"myapp.py": dummy_app_file, "other_module.py": dummy_other_module_file}):
988990
_run(["deploy", "myapp.py", "--name", "my_app_foo"])
989991

990-
app_id = servicer.deployed_apps.get("my_app_foo")
992+
app_id = servicer.deployed_apps.get(("main", "my_app_foo"))
991993

992994
servicer.app_deployment_history[app_id][-1]["commit_info"] = api_pb2.CommitInfo(
993995
vcs="git", branch="main", commit_hash="abc123"
@@ -1027,11 +1029,11 @@ def test_app_rollback(servicer, mock_dir, set_env_client):
10271029
for _ in range(4):
10281030
_run(["deploy", "myapp.py", "--name", "my_app"])
10291031
_run(["app", "rollback", "my_app"])
1030-
app_id = servicer.deployed_apps.get("my_app")
1032+
app_id = servicer.deployed_apps.get(("main", "my_app"))
10311033
assert servicer.app_deployment_history[app_id][-1]["rollback_version"] == 3
10321034

10331035
_run(["app", "rollback", "my_app", "v2"])
1034-
app_id = servicer.deployed_apps.get("my_app")
1036+
app_id = servicer.deployed_apps.get(("main", "my_app"))
10351037
assert servicer.app_deployment_history[app_id][-1]["rollback_version"] == 2
10361038

10371039
_run(["app", "rollback", "my_app", "2"], expected_exit_code=2)

test/conftest.py

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,8 @@ def __init__(self, blob_host, blobs, blocks, files_sha2data, credentials, port):
186186
self.fc_data_in = defaultdict(lambda: asyncio.Queue()) # unbounded
187187
self.fc_data_out = defaultdict(lambda: asyncio.Queue()) # unbounded
188188
self.queue: dict[bytes, list[bytes]] = {b"": []}
189-
self.deployed_apps = {
190-
client_mount_name(): "ap-x",
191-
}
189+
self.deployed_apps: dict[tuple[str, str], str] = {}
190+
self.app_environments: dict[str, str] = {}
192191
self.app_deployment_history: defaultdict[str, list[dict[str, Any]]] = defaultdict(list)
193192
self.app_deployment_history["ap-x"] = [
194193
{
@@ -474,6 +473,11 @@ def get_object_metadata(self, object_id) -> api_pb2.Object:
474473
res.object_id = object_id
475474
return res
476475

476+
def get_environment(self, environment_name: Optional[str] = None) -> str:
477+
if environment_name is None:
478+
return next(iter(self.environments)) # Use first environment as default
479+
return environment_name
480+
477481
def mounts_excluding_published_client(self):
478482
return {
479483
mount_id: content
@@ -513,6 +517,7 @@ async def AppCreate(self, stream):
513517
self.n_apps += 1
514518
app_id = f"ap-{self.n_apps}"
515519
self.app_state_history[app_id].append(api_pb2.APP_STATE_INITIALIZING)
520+
self.app_environments[app_id] = self.get_environment(request.environment_name)
516521
await stream.send_message(
517522
api_pb2.AppCreateResponse(app_id=app_id, app_page_url="https://modaltest.com/apps/ap-123")
518523
)
@@ -582,13 +587,6 @@ async def AppRollback(self, stream):
582587
self.app_state_history[request.app_id].append(api_pb2.APP_STATE_DEPLOYED)
583588
await stream.send_message(Empty())
584589

585-
async def AppDeploy(self, stream):
586-
request: api_pb2.AppDeployRequest = await stream.recv_message()
587-
self.deployed_apps[request.name] = request.app_id
588-
self.app_state_history[request.app_id].append(api_pb2.APP_STATE_DEPLOYED)
589-
590-
await stream.send_message(api_pb2.AppDeployResponse(url="http://test.modal.com/foo/bar"))
591-
592590
async def AppPublish(self, stream):
593591
request: api_pb2.AppPublishRequest = await stream.recv_message()
594592
for key, val in request.definition_ids.items():
@@ -599,7 +597,8 @@ async def AppPublish(self, stream):
599597
self.app_objects[request.app_id] = {**request.function_ids, **request.class_ids}
600598
self.app_state_history[request.app_id].append(request.app_state)
601599
if request.app_state == api_pb2.AppState.APP_STATE_DEPLOYED:
602-
self.deployed_apps[request.name] = request.app_id
600+
app_key = (self.app_environments[request.app_id], request.name)
601+
self.deployed_apps[app_key] = request.app_id
603602
await stream.send_message(api_pb2.AppPublishResponse(url="http://test.modal.com/foo/bar"))
604603
else:
605604
await stream.send_message(api_pb2.AppPublishResponse())
@@ -623,7 +622,9 @@ async def AppPublish(self, stream):
623622

624623
async def AppGetByDeploymentName(self, stream):
625624
request: api_pb2.AppGetByDeploymentNameRequest = await stream.recv_message()
626-
await stream.send_message(api_pb2.AppGetByDeploymentNameResponse(app_id=self.deployed_apps.get(request.name)))
625+
app_key = (self.get_environment(request.environment_name), request.name)
626+
app_id = self.deployed_apps.get(app_key)
627+
await stream.send_message(api_pb2.AppGetByDeploymentNameResponse(app_id=app_id))
627628

628629
async def AppHeartbeat(self, stream):
629630
request: api_pb2.AppHeartbeatRequest = await stream.recv_message()
@@ -656,9 +657,12 @@ async def AppDeploymentHistory(self, stream):
656657
)
657658

658659
async def AppList(self, stream):
659-
await stream.recv_message()
660+
req = await stream.recv_message()
660661
apps = []
661-
for app_name, app_id in self.deployed_apps.items():
662+
requested_environment = self.get_environment(req.environment_name)
663+
for (environment_name, app_name), app_id in self.deployed_apps.items():
664+
if environment_name != requested_environment:
665+
continue
662666
apps.append(
663667
api_pb2.AppListResponse.AppListItem(
664668
name=app_name,
@@ -757,7 +761,8 @@ async def ClassCreate(self, stream):
757761

758762
async def ClassGet(self, stream):
759763
request: api_pb2.ClassGetRequest = await stream.recv_message()
760-
if not (app_id := self.deployed_apps.get(request.app_name)):
764+
app_key = (self.get_environment(request.environment_name), request.app_name)
765+
if not (app_id := self.deployed_apps.get(app_key)):
761766
raise GRPCError(Status.NOT_FOUND, f"can't find app {request.app_name}")
762767
app_objects = self.app_objects[app_id]
763768
object_id = app_objects.get(request.object_tag)
@@ -867,7 +872,6 @@ async def DictGetOrCreate(self, stream):
867872
dict_id = f"di-{len(self.dicts)}"
868873
self.dicts[dict_id] = {entry.key: entry.value for entry in request.data}
869874
self.deployed_dicts[k] = dict_id
870-
self.deployed_apps[request.deployment_name] = f"ap-{dict_id}"
871875
elif request.object_creation_type == api_pb2.OBJECT_CREATION_TYPE_EPHEMERAL:
872876
dict_id = f"di-{len(self.dicts)}"
873877
self.dicts[dict_id] = {entry.key: entry.value for entry in request.data}
@@ -900,11 +904,7 @@ async def DictLen(self, stream):
900904
await stream.send_message(api_pb2.DictLenResponse(len=len(self.dicts[request.dict_id])))
901905

902906
async def DictList(self, stream):
903-
dicts = [
904-
api_pb2.DictListResponse.DictInfo(name=name, created_at=1)
905-
for name, _ in self.deployed_dicts
906-
if name in self.deployed_apps
907-
]
907+
dicts = [api_pb2.DictListResponse.DictInfo(name=name, created_at=1) for name, _ in self.deployed_dicts]
908908
await stream.send_message(api_pb2.DictListResponse(dicts=dicts))
909909

910910
async def DictUpdate(self, stream):
@@ -1140,7 +1140,8 @@ async def FunctionCreate(self, stream):
11401140

11411141
async def FunctionGet(self, stream):
11421142
request: api_pb2.FunctionGetRequest = await stream.recv_message()
1143-
if not (app_id := self.deployed_apps.get(request.app_name)):
1143+
app_key = (self.get_environment(request.environment_name), request.app_name)
1144+
if not (app_id := self.deployed_apps.get(app_key)):
11441145
raise GRPCError(Status.NOT_FOUND, f"can't find app {request.app_name}")
11451146

11461147
app_objects = self.app_objects[app_id]
@@ -1514,7 +1515,6 @@ async def QueueGetOrCreate(self, stream):
15141515
self.n_queues += 1
15151516
queue_id = f"qu-{self.n_queues}"
15161517
self.deployed_queues[k] = queue_id
1517-
self.deployed_apps[request.deployment_name] = f"ap-{queue_id}"
15181518
elif request.object_creation_type == api_pb2.OBJECT_CREATION_TYPE_EPHEMERAL:
15191519
self.n_queues += 1
15201520
queue_id = f"qu-{self.n_queues}"
@@ -1562,11 +1562,7 @@ async def QueueLen(self, stream):
15621562
async def QueueList(self, stream):
15631563
# TODO Note that the actual self.queue holding the data assumes we have a single queue
15641564
# So there is a mismatch and I am not implementing a mock for the num_partitions / total_size
1565-
queues = [
1566-
api_pb2.QueueListResponse.QueueInfo(name=name, created_at=1)
1567-
for name, _ in self.deployed_queues
1568-
if name in self.deployed_apps
1569-
]
1565+
queues = [api_pb2.QueueListResponse.QueueInfo(name=name, created_at=1) for name, _ in self.deployed_queues]
15701566
await stream.send_message(api_pb2.QueueListResponse(queues=queues))
15711567

15721568
async def QueueNextItems(self, stream):

0 commit comments

Comments
 (0)