Skip to content

Commit a38c626

Browse files
author
Alan Christie
committed
test: Fix instance creation
1 parent 8ffb3af commit a38c626

File tree

3 files changed

+46
-31
lines changed

3 files changed

+46
-31
lines changed

tests/instance_launcher.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,18 @@ def launch(self, *, launch_parameters: LaunchParameters) -> LaunchResult:
8282
<= launch_parameters.total_number_of_replicas
8383
)
8484

85+
# Create an Instance record (and dummy Task ID)
86+
response = self._api_adapter.create_instance()
87+
instance_id = response["id"]
88+
task_id = "task-00000000-0000-0000-0000-000000000001"
89+
8590
# Create a running workflow step
8691
assert launch_parameters.running_workflow_id
8792
assert launch_parameters.step_name
8893
response, _ = self._api_adapter.create_running_workflow_step(
8994
running_workflow_id=launch_parameters.running_workflow_id,
9095
step=launch_parameters.step_name,
96+
instance_id=instance_id,
9197
replica=launch_parameters.step_replication_number,
9298
replicas=launch_parameters.total_number_of_replicas,
9399
)
@@ -99,10 +105,11 @@ def launch(self, *, launch_parameters: LaunchParameters) -> LaunchResult:
99105
running_workflow_step_id=rwfs_id, variables=launch_parameters.variables
100106
)
101107

102-
# Create an Instance record (and dummy Task ID)
103-
response = self._api_adapter.create_instance(running_workflow_step_id=rwfs_id)
104-
instance_id = response["id"]
105-
task_id = "task-00000000-0000-0000-0000-000000000001"
108+
# Now add the running workflow ID ot the instance record.
109+
self._api_adapter.set_instance_running_workflow_id(
110+
instance_id=instance_id,
111+
running_workflow_step_id=rwfs_id,
112+
)
106113

107114
# Get the job defitnion.
108115
# This is expected to exist in the tests/job-definitions directory.

tests/test_test_wapi_adapter.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from tests.config import TEST_PROJECT_ID
66
from tests.wapi_adapter import UnitTestWorkflowAPIAdapter
77

8+
_I_ID: str = "instance-00000000-0000-0000-0000-000000000000"
9+
810

911
def test_get_nop_job():
1012
# Arrange
@@ -177,7 +179,7 @@ def test_create_running_workflow_step():
177179

178180
# Act
179181
response, _ = utaa.create_running_workflow_step(
180-
running_workflow_id=response["id"], step="step-1"
182+
running_workflow_id=response["id"], step="step-1", instance_id=_I_ID
181183
)
182184

183185
# Assert
@@ -195,7 +197,7 @@ def test_set_running_workflow_step_variables():
195197
variables={},
196198
)
197199
response, _ = utaa.create_running_workflow_step(
198-
running_workflow_id=response["id"], step="step-1"
200+
running_workflow_id=response["id"], step="step-1", instance_id=_I_ID
199201
)
200202
rwfsid = response["id"]
201203

@@ -220,7 +222,7 @@ def test_set_running_workflow_step_done_when_success():
220222
variables={},
221223
)
222224
response, _ = utaa.create_running_workflow_step(
223-
running_workflow_id=response["id"], step="step-1"
225+
running_workflow_id=response["id"], step="step-1", instance_id=_I_ID
224226
)
225227
rwfsid = response["id"]
226228

@@ -247,7 +249,7 @@ def test_set_running_workflow_step_done_when_failed():
247249
variables={},
248250
)
249251
response, _ = utaa.create_running_workflow_step(
250-
running_workflow_id=response["id"], step="step-1"
252+
running_workflow_id=response["id"], step="step-1", instance_id=_I_ID
251253
)
252254
rwfsid = response["id"]
253255

@@ -277,7 +279,7 @@ def test_get_running_workflow_step():
277279
)
278280
rwfid = response["id"]
279281
response, _ = utaa.create_running_workflow_step(
280-
running_workflow_id=rwfid, step="step-1"
282+
running_workflow_id=rwfid, step="step-1", instance_id=_I_ID
281283
)
282284
rwfsid = response["id"]
283285

@@ -306,7 +308,7 @@ def test_get_running_workflow_step_with_prior_step():
306308
response, _ = utaa.create_running_workflow_step(
307309
running_workflow_id=rwfid,
308310
step="step-1",
309-
prior_running_workflow_step_id="r-workflow-step-111",
311+
instance_id=_I_ID,
310312
)
311313
rwfsid = response["id"]
312314

@@ -317,8 +319,6 @@ def test_get_running_workflow_step_with_prior_step():
317319
assert response["name"] == "step-1"
318320
assert not response["done"]
319321
assert response["running_workflow"]["id"] == rwfid
320-
assert "prior_running_workflow_step" in response
321-
assert response["prior_running_workflow_step"]["id"] == "r-workflow-step-111"
322322

323323

324324
def test_create_instance():
@@ -332,7 +332,7 @@ def test_create_instance():
332332
variables={},
333333
)
334334
response, _ = utaa.create_running_workflow_step(
335-
running_workflow_id=response["id"], step="step-1"
335+
running_workflow_id=response["id"], step="step-1", instance_id=_I_ID
336336
)
337337
rwfs_id = response["id"]
338338

@@ -354,7 +354,7 @@ def test_create_and_get_instance():
354354
variables={},
355355
)
356356
response, _ = utaa.create_running_workflow_step(
357-
running_workflow_id=response["id"], step="step-1"
357+
running_workflow_id=response["id"], step="step-1", instance_id=_I_ID
358358
)
359359
rwfs_id = response["id"]
360360
response = utaa.create_instance(running_workflow_step_id=rwfs_id)
@@ -378,7 +378,7 @@ def test_create_instance_and_get_step_instance_directory():
378378
variables={},
379379
)
380380
response, _ = utaa.create_running_workflow_step(
381-
running_workflow_id=response["id"], step="step-1"
381+
running_workflow_id=response["id"], step="step-1", instance_id=_I_ID
382382
)
383383
rwfs_id = response["id"]
384384
response = utaa.create_instance(running_workflow_step_id=rwfs_id)
@@ -405,7 +405,7 @@ def test_create_instance_and_get_step_instance_directory_by_name():
405405
)
406406
rwf_id = response["id"]
407407
response, _ = utaa.create_running_workflow_step(
408-
running_workflow_id=rwf_id, step="step-1"
408+
running_workflow_id=rwf_id, step="step-1", instance_id=_I_ID
409409
)
410410
rwfs_id = response["id"]
411411
response = utaa.create_instance(running_workflow_step_id=rwfs_id)
@@ -438,7 +438,7 @@ def test_get_running_workflow_step_by_name():
438438
)
439439
rwf_id = response["id"]
440440
response, _ = utaa.create_running_workflow_step(
441-
running_workflow_id=rwf_id, step="step-2"
441+
running_workflow_id=rwf_id, step="step-2", instance_id=_I_ID
442442
)
443443
rwfs_id = response["id"]
444444

@@ -464,7 +464,7 @@ def test_mock_get_running_workflow_step_output_values_for_output():
464464
variables={},
465465
)
466466
response, _ = utaa.create_running_workflow_step(
467-
running_workflow_id=response["id"], step="step-1"
467+
running_workflow_id=response["id"], step="step-1", instance_id=_I_ID
468468
)
469469

470470
# Act
@@ -494,7 +494,7 @@ def test_basic_get_running_workflow_step_output_values_for_output_when_step_vari
494494
variables={},
495495
)
496496
response, _ = utaa.create_running_workflow_step(
497-
running_workflow_id=response["id"], step="step-1"
497+
running_workflow_id=response["id"], step="step-1", instance_id=_I_ID
498498
)
499499

500500
# Act

tests/wapi_adapter.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ def create_running_workflow_step(
159159
*,
160160
running_workflow_id: str,
161161
step: str,
162+
instance_id: str,
162163
replica: int = 0,
163164
replicas: int = 1,
164-
prior_running_workflow_step_id: str | None = None,
165165
) -> tuple[dict[str, Any], int]:
166166
assert replica >= 0
167167
assert replicas > replica
@@ -182,11 +182,8 @@ def create_running_workflow_step(
182182
"replicas": replicas,
183183
"variables": {},
184184
"running_workflow": {"id": running_workflow_id},
185+
"instance_id": instance_id,
185186
}
186-
if prior_running_workflow_step_id:
187-
record["prior_running_workflow_step"] = {
188-
"id": prior_running_workflow_step_id
189-
}
190187
running_workflow_step[running_workflow_step_id] = record
191188

192189
with open(_RUNNING_WORKFLOW_STEP_PICKLE_FILE, "wb") as pickle_file:
@@ -347,17 +344,30 @@ def create_running_workflow(
347344

348345
return {"id": running_workflow_id}
349346

350-
def create_instance(self, *, running_workflow_step_id: str) -> dict[str, Any]:
347+
def create_instance(self) -> dict[str, Any]:
351348
UnitTestWorkflowAPIAdapter.lock.acquire()
352349
with open(_INSTANCE_PICKLE_FILE, "rb") as pickle_file:
353350
instances = Unpickler(pickle_file).load()
354351

355352
next_id: int = len(instances) + 1
356353
instance_id: str = _INSTANCE_ID_FORMAT.format(id=next_id)
357-
record = {
358-
"running_workflow_step_id": running_workflow_step_id,
359-
}
360-
instances[instance_id] = record
354+
355+
with open(_INSTANCE_PICKLE_FILE, "wb") as pickle_file:
356+
Pickler(pickle_file).dump(instances)
357+
358+
UnitTestWorkflowAPIAdapter.lock.release()
359+
360+
return {"id": instance_id}
361+
362+
def set_instance_running_workflow_id(
363+
self, *, instance_id: str, running_workflow_step_id: str
364+
) -> None:
365+
UnitTestWorkflowAPIAdapter.lock.acquire()
366+
with open(_INSTANCE_PICKLE_FILE, "rb") as pickle_file:
367+
instances = Unpickler(pickle_file).load()
368+
369+
assert instance_id in instances
370+
instances["running_workflow_step_id"] = running_workflow_step_id
361371

362372
with open(_INSTANCE_PICKLE_FILE, "wb") as pickle_file:
363373
Pickler(pickle_file).dump(instances)
@@ -374,8 +384,6 @@ def create_instance(self, *, running_workflow_step_id: str) -> dict[str, Any]:
374384

375385
UnitTestWorkflowAPIAdapter.lock.release()
376386

377-
return {"id": instance_id}
378-
379387
def get_running_workflow_steps(self, *, running_workflow_id: str) -> dict[str, Any]:
380388
UnitTestWorkflowAPIAdapter.lock.acquire()
381389
with open(_RUNNING_WORKFLOW_STEP_PICKLE_FILE, "rb") as pickle_file:

0 commit comments

Comments
 (0)