forked from PrefectHQ/prefect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_coiled.py
More file actions
186 lines (145 loc) · 5.37 KB
/
test_coiled.py
File metadata and controls
186 lines (145 loc) · 5.37 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
from unittest.mock import AsyncMock, MagicMock, patch
from uuid import UUID
import pytest
from prefect.blocks.core import Block
from prefect.client.orchestration import PrefectClient
from prefect.infrastructure.provisioners.coiled import CoiledPushProvisioner
@pytest.fixture(autouse=True)
async def coiled_credentials_block_cls():
class MockCoiledCredentials(Block):
_block_type_name = "Coiled Credentials"
api_token: str
await MockCoiledCredentials.register_type_and_schema()
return MockCoiledCredentials
@pytest.fixture
async def coiled_credentials_block_id(coiled_credentials_block_cls: Block):
block_doc_id = await coiled_credentials_block_cls(api_token="existing_token").save(
"work-pool-name-coiled-credentials", overwrite=True
)
return block_doc_id
@pytest.fixture
def mock_run_process():
with patch("prefect.infrastructure.provisioners.coiled.run_process") as mock:
yield mock
@pytest.fixture
def mock_coiled(monkeypatch):
mock = MagicMock()
monkeypatch.setattr("prefect.infrastructure.provisioners.coiled.coiled", mock)
yield mock
@pytest.fixture
def mock_importlib():
with patch("prefect.infrastructure.provisioners.coiled.importlib") as mock:
yield mock
@pytest.fixture
def mock_confirm():
with patch("prefect.infrastructure.provisioners.coiled.Confirm") as mock:
yield mock
@pytest.fixture
def mock_dask_config():
with patch(
"prefect.infrastructure.provisioners.coiled.CoiledPushProvisioner._get_coiled_creds"
) as mock:
mock.return_value = "local-api-token-from-dask-config", "my-workspace"
yield mock
async def test_provision(
prefect_client: PrefectClient,
mock_run_process: AsyncMock,
mock_coiled: MagicMock,
mock_dask_config: MagicMock,
mock_confirm: MagicMock,
mock_importlib: MagicMock,
):
"""
Test provision from a clean slate:
- Coiled is not installed
- Coiled token does not exist
- CoiledCredentials block does not exist
"""
provisioner = CoiledPushProvisioner()
provisioner.console.is_interactive = True
mock_confirm.ask.side_effect = [
True,
True,
True,
] # confirm provision, install coiled, create new token
mock_importlib.import_module.side_effect = [
ModuleNotFoundError,
mock_coiled,
mock_coiled,
]
# simulate coiled token creation
mock_coiled.config.Config.return_value.get.side_effect = [
None,
None,
"mock_token",
]
work_pool_name = "work-pool-name"
base_job_template = {"variables": {"properties": {"credentials": {}}}}
result = await provisioner.provision(
work_pool_name, base_job_template, client=prefect_client
)
# Check if the block document exists and has expected values
block_document = await prefect_client.read_block_document_by_name(
"work-pool-name-coiled-credentials", "coiled-credentials"
)
assert block_document.data["api_token"], str == "mock_token"
# Check if the base job template was updated
assert result["variables"]["properties"]["credentials"] == {
"default": {"$ref": {"block_document_id": str(block_document.id)}},
}
async def test_provision_existing_coiled_credentials_block(
prefect_client: PrefectClient,
coiled_credentials_block_id: UUID,
mock_run_process: AsyncMock,
):
"""
Test provision with an existing CoiledCredentials block.
"""
provisioner = CoiledPushProvisioner()
work_pool_name = "work-pool-name"
base_job_template = {"variables": {"properties": {"credentials": {}}}}
result = await provisioner.provision(
work_pool_name, base_job_template, client=prefect_client
)
# Check if the base job template was updated
assert result["variables"]["properties"]["credentials"] == {
"default": {"$ref": {"block_document_id": str(coiled_credentials_block_id)}},
}
mock_run_process.assert_not_called()
async def test_provision_existing_coiled_credentials(
prefect_client: PrefectClient,
mock_run_process: AsyncMock,
mock_coiled: MagicMock,
mock_dask_config: MagicMock,
mock_confirm: MagicMock,
mock_importlib: MagicMock,
):
"""
Test provision where the user has coiled installed and an existing Coiled configuration.
"""
provisioner = CoiledPushProvisioner()
mock_confirm.ask.side_effect = [
True,
] # confirm provision
mock_importlib.import_module.side_effect = [
mock_coiled,
mock_coiled,
] # coiled is already installed
mock_coiled.config.Config.return_value.get.side_effect = [
"mock_token",
] # coiled config exists
work_pool_name = "work-pool-name"
base_job_template = {"variables": {"properties": {"credentials": {}}}}
result = await provisioner.provision(
work_pool_name, base_job_template, client=prefect_client
)
# Check if the block document exists and has expected values
block_document = await prefect_client.read_block_document_by_name(
"work-pool-name-coiled-credentials", "coiled-credentials"
)
assert block_document.data["api_token"], str == "mock_token"
# Check if the base job template was updated
assert result["variables"]["properties"]["credentials"] == {
"default": {"$ref": {"block_document_id": str(block_document.id)}},
}
mock_run_process.assert_not_called()