-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathtest_rest_plugin.py
More file actions
206 lines (181 loc) · 7.82 KB
/
test_rest_plugin.py
File metadata and controls
206 lines (181 loc) · 7.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import json
import os
from contextlib import nullcontext as does_not_raise
from unittest.mock import Mock
import pytest
import pytest_asyncio
import requests
from pydantic import parse_obj_as
from sqlalchemy.ext.asyncio import AsyncSession
from dstack._internal.core.errors import ServerClientError, ServerError
from dstack._internal.core.models.backends.base import BackendType
from dstack._internal.core.models.configurations import ServiceConfiguration
from dstack._internal.core.models.fleets import FleetConfiguration, FleetSpec
from dstack._internal.core.models.gateways import GatewayConfiguration, GatewaySpec
from dstack._internal.core.models.profiles import Profile
from dstack._internal.core.models.resources import Range
from dstack._internal.core.models.runs import RunSpec
from dstack._internal.core.models.volumes import VolumeSpec
from dstack._internal.server.models import ProjectModel
from dstack._internal.server.services import encryption as encryption
from dstack._internal.server.testing.common import (
create_project,
create_repo,
create_user,
get_fleet_spec,
get_run_spec,
get_volume_configuration,
)
from dstack.plugins.builtin.rest_plugin import PLUGIN_SERVICE_URI_ENV_VAR_NAME, CustomApplyPolicy
async def create_run_spec(
session: AsyncSession,
project: ProjectModel,
replicas: str = 1,
) -> RunSpec:
repo = await create_repo(session=session, project_id=project.id)
run_name = "test-run"
profile = Profile(name="test-profile")
spec = get_run_spec(
repo_id=repo.name,
run_name=run_name,
profile=profile,
configuration=ServiceConfiguration(
commands=["echo hello"], port=8000, replicas=parse_obj_as(Range[int], replicas)
),
)
return spec
async def create_fleet_spec():
name = "test-fleet-spec"
fleet_conf = FleetConfiguration(name=name)
return get_fleet_spec(conf=fleet_conf)
async def create_volume_spec():
return VolumeSpec(configuration=get_volume_configuration())
async def create_gateway_spec():
configuration = GatewayConfiguration(
name="test-gateway-config",
backend=BackendType.AWS,
region="us-central",
)
return GatewaySpec(configuration=configuration)
@pytest_asyncio.fixture
async def project(session):
return await create_project(session=session)
@pytest_asyncio.fixture
async def user(session):
return await create_user(session=session)
@pytest_asyncio.fixture
async def spec(request, session, project):
if request.param == "run_spec":
return await create_run_spec(session, project)
elif request.param == "fleet_spec":
return await create_fleet_spec()
elif request.param == "volume_spec":
return await create_volume_spec()
elif request.param == "gateway_spec":
return await create_gateway_spec()
else:
raise ValueError(f"Unknown spec fixture: {request.param}")
class TestRESTPlugin:
@pytest.mark.asyncio
async def test_on_run_apply_plugin_service_uri_not_set(self):
with pytest.raises(ServerError):
CustomApplyPolicy()
@pytest.mark.asyncio
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
@pytest.mark.parametrize(
"spec", ["run_spec", "fleet_spec", "volume_spec", "gateway_spec"], indirect=True
)
async def test_on_apply_plugin_service_returns_mutated_spec(
self, mocker, test_db, user, project, spec
):
mocker.patch.dict(os.environ, {PLUGIN_SERVICE_URI_ENV_VAR_NAME: "http://mock"})
policy = CustomApplyPolicy()
mock_response = Mock()
response_dict = {"spec": spec.dict(), "error": None}
if isinstance(spec, (RunSpec, FleetSpec)):
response_dict["spec"]["profile"]["tags"] = {"env": "test", "team": "qa"}
else:
response_dict["spec"]["configuration_path"] = "/path/to/something"
mock_response.text = json.dumps(response_dict)
mock_response.raise_for_status = Mock()
mocker.patch("requests.post", return_value=mock_response)
result = policy.on_apply(user=user.name, project=project.name, spec=spec)
assert result == type(spec)(**response_dict["spec"])
@pytest.mark.asyncio
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
@pytest.mark.parametrize(
"spec", ["run_spec", "fleet_spec", "volume_spec", "gateway_spec"], indirect=True
)
async def test_on_apply_plugin_service_call_fails(self, mocker, test_db, user, project, spec):
mocker.patch.dict(os.environ, {PLUGIN_SERVICE_URI_ENV_VAR_NAME: "http://mock"})
policy = CustomApplyPolicy()
mocker.patch("requests.post", side_effect=requests.RequestException("fail"))
with pytest.raises(ServerClientError):
policy.on_apply(user=user.name, project=project.name, spec=spec)
@pytest.mark.asyncio
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
@pytest.mark.parametrize(
"spec", ["run_spec", "fleet_spec", "volume_spec", "gateway_spec"], indirect=True
)
async def test_on_apply_plugin_service_connection_fails(
self, mocker, test_db, user, project, spec
):
mocker.patch.dict(os.environ, {PLUGIN_SERVICE_URI_ENV_VAR_NAME: "http://mock"})
policy = CustomApplyPolicy()
mocker.patch("requests.post", side_effect=requests.ConnectionError("Failed to connect"))
with pytest.raises(ServerClientError):
policy.on_apply(user=user.name, project=project.name, spec=spec)
@pytest.mark.asyncio
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
@pytest.mark.parametrize(
"spec", ["run_spec", "fleet_spec", "volume_spec", "gateway_spec"], indirect=True
)
async def test_on_apply_plugin_service_returns_invalid_spec(
self, mocker, test_db, user, project, spec
):
mocker.patch.dict(os.environ, {PLUGIN_SERVICE_URI_ENV_VAR_NAME: "http://mock"})
policy = CustomApplyPolicy()
mock_response = Mock()
mock_response.text = json.dumps({"invalid-key": "abc"})
mock_response.raise_for_status = Mock()
mocker.patch("requests.post", return_value=mock_response)
with pytest.raises(ServerClientError):
policy.on_apply(user.name, project=project.name, spec=spec)
@pytest.mark.asyncio
@pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True)
@pytest.mark.parametrize(
"spec", ["run_spec", "fleet_spec", "volume_spec", "gateway_spec"], indirect=True
)
@pytest.mark.parametrize(
("error", "expectation"),
[
pytest.param(None, does_not_raise(), id="error_none"),
pytest.param(
"",
pytest.raises(
ServerClientError, match="Plugin service returned an invalid response"
),
id="error_empty_str",
),
pytest.param(
"validation failed",
pytest.raises(
ServerClientError, match="Apply request rejected: validation failed"
),
id="error_non_empty_str",
),
],
)
async def test_on_apply_plugin_service_error_handling(
self, mocker, test_db, user, project, spec, error, expectation
):
mocker.patch.dict(os.environ, {PLUGIN_SERVICE_URI_ENV_VAR_NAME: "http://mock"})
policy = CustomApplyPolicy()
mock_response = Mock()
response_dict = {"spec": spec.dict(), "error": error}
mock_response.text = json.dumps(response_dict)
mock_response.raise_for_status = Mock()
mocker.patch("requests.post", return_value=mock_response)
with expectation:
result = policy.on_apply(user=user.name, project=project.name, spec=spec)
assert result == type(spec)(**response_dict["spec"])