Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions tests/unit/test_bootstrap/test_default_flow_start.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
Unit tests for trustgraph.bootstrap.initialisers.DefaultFlowStart

Verifies the list/start timeouts are configurable and that the
configured values actually reach the flow-client request calls.
"""

from unittest.mock import AsyncMock, MagicMock

import pytest

from trustgraph.bootstrap.initialisers.default_flow_start import (
DefaultFlowStart,
)


def test_default_timeouts():
init = DefaultFlowStart(blueprint="bp")
assert init.list_timeout == 10
assert init.start_timeout == 30


def test_timeout_overrides_are_stored():
init = DefaultFlowStart(blueprint="bp", list_timeout=5, start_timeout=99)
assert init.list_timeout == 5
assert init.start_timeout == 99


@pytest.mark.asyncio
async def test_run_forwards_configured_timeouts():
Comment thread
cybermaggedon marked this conversation as resolved.
init = DefaultFlowStart(blueprint="bp", list_timeout=5, start_timeout=99)

# Flow client: list-flows returns no error + empty flow list,
# start-flow returns no error.
flow = MagicMock()
flow.start = AsyncMock()
flow.stop = AsyncMock()
flow.request = AsyncMock(side_effect=[
MagicMock(error=None, flow_ids=[]), # list-flows response
MagicMock(error=None), # start-flow response
])

# Context: workspace "default" exists, hands back our mock flow client.
ctx = MagicMock()
ctx.logger = MagicMock()
ctx.config.keys = AsyncMock(return_value=["default"])
ctx.make_flow_client = MagicMock(return_value=flow)

await init.run(ctx, None, "v1")

calls = flow.request.call_args_list
assert len(calls) == 2
assert calls[0].kwargs["timeout"] == 5
assert calls[1].kwargs["timeout"] == 99
13 changes: 13 additions & 0 deletions tests/unit/test_bootstrap/test_workspace_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Unit tests for trustgraph.bootstrap.initialisers.WorkspaceInit."""

from trustgraph.bootstrap.initialisers.workspace_init import WorkspaceInit


def test_default_iam_timeout():
init = WorkspaceInit()
assert init.iam_timeout == 10


def test_iam_timeout_override_is_stored():
init = WorkspaceInit(iam_timeout=42)
assert init.iam_timeout == 42
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
Human-readable description passed to flow-svc.
parameters : dict (optional)
Optional parameter overrides passed to start-flow.
list_timeout : int (default 10)
Timeout in seconds for the list-flows request.
start_timeout : int (default 30)
Timeout in seconds for the start-flow request.
"""

from trustgraph.schema import FlowRequest
Expand All @@ -34,6 +38,8 @@ def __init__(
blueprint=None,
description="Default",
parameters=None,
list_timeout=10,
start_timeout=30,
**kwargs,
):
super().__init__(**kwargs)
Expand All @@ -46,6 +52,8 @@ def __init__(
self.blueprint = blueprint
self.description = description
self.parameters = dict(parameters) if parameters else {}
self.list_timeout = list_timeout
self.start_timeout = start_timeout

async def run(self, ctx, old_flag, new_flag):

Expand All @@ -70,7 +78,7 @@ async def run(self, ctx, old_flag, new_flag):
FlowRequest(
operation="list-flows",
),
timeout=10,
timeout=self.list_timeout,
)
if list_resp.error:
raise RuntimeError(
Expand Down Expand Up @@ -99,7 +107,7 @@ async def run(self, ctx, old_flag, new_flag):
description=self.description,
parameters=self.parameters,
),
timeout=30,
timeout=self.start_timeout,
)
if resp.error:
raise RuntimeError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
Path to a JSON seed file with the same shape TemplateSeed consumes.
overwrite : bool (default False)
On re-run (flag change), if True overwrite all keys; if False,
upsert-missing-only (preserves in-workspace customisations).
upsert-missing-only (preserves in-workspace customisations)
iam_timeout : int (default 10)
Timeout in seconds for the IAM create-workspace request.

Raises (in ``run``)
-------------------
Expand All @@ -41,7 +43,9 @@ def __init__(
source="template",
seed_file=None,
overwrite=False,
iam_timeout=10,
**kwargs,

):
super().__init__(**kwargs)

Expand All @@ -59,6 +63,7 @@ def __init__(
self.source = source
self.seed_file = seed_file
self.overwrite = overwrite
self.iam_timeout = iam_timeout

async def run(self, ctx, old_flag, new_flag):
await self._create_workspace(ctx)
Expand Down Expand Up @@ -120,10 +125,10 @@ async def _create_workspace(self, ctx):
workspace_record=WorkspaceInput(
id=self.workspace,
name=self.workspace.title(),
enabled=True,
enabled=True,
),
),
timeout=10,
timeout=self.iam_timeout,
)
if resp.error:
if resp.error.type == "duplicate":
Expand Down
Loading