Testing code that uses RayDog

RayDog ships the simulated YellowDog platform that its own tests use, so that code which builds clusters can be tested without an account, without instances and without waiting.

from yellowdog_ray import ClusterSpec, NodeSpec, RayDogCluster
from yellowdog_ray.testing import FakePlatform

def test_my_pipeline_shuts_its_cluster_down():
    platform = FakePlatform()
    node = NodeSpec(compute_requirement_template="ns/any", start_script="ray start --block")
    spec = ClusterSpec(name="demo", namespace="ns", head=node)

    with RayDogCluster(spec, platform=platform, clock=platform.clock) as cluster:
        cluster.add_worker_pool("workers", node, count=3)
        cluster.build()
        platform.clock.advance(300)             # let the workers start
        assert platform.live_instances == 4

    platform.clock.advance(120)
    assert platform.live_instances == 0

FakePlatform behaves as the real platform was observed to: instances take time to boot, nodes register, tasks start, cancellation is asynchronous, pools grow to meet demand, names stay reserved after use. Nothing happens until its clock is advanced, and waiting inside RayDog (for a head node to start, say) advances the clock rather than sleeping, so tests run in milliseconds.

It can also misbehave on request:

platform.fail_next("provision_pool", PlatformError("no capacity"))   # the next call fails
platform.never_register("demo")           # instances run but nodes never appear
platform.node_boot_time(600)              # slow nodes
platform.fail_task(task_id)               # a start script exits with an error
platform.lose_node(task_id)               # an instance is reclaimed

Its docstring lists the few behaviours that are assumptions rather than observations.

An in-memory YellowDog, for testing RayDog and code built on it offline.

FakePlatform implements yellowdog_ray.core.platform.Platform as a simulation driven by a FakeClock: nothing happens until the clock is advanced, and then instances boot, nodes register, tasks start, aborts complete and idle nodes are released, in one-second steps. Its rules were taken from probes of the real platform and are checked against it by the contract tests in tests/contract.

Three of its rules were seen later, in runs made by hand and not by a contract test: a task that reaches its timeout becomes FAILED (the script is terminated, exit code 143); a task whose instance is lost becomes FAILED (“Task node allocation lost”); and a work requirement stays RUNNING after its head has failed or its lifetime has passed, until it is cancelled.

Rules that were not observed on the real platform, and are assumptions:

  • a task’s timeout runs from the moment it starts executing;

  • a pool with no instances and no tasks waiting shuts itself down after idle_pool_shutdown;

  • a pool’s target is silently capped at its max_nodes, at provisioning and on set_pool_target;

  • a work requirement stays RUNNING when every one of its task groups has ended (seen only with the head’s group failed);

  • a task group with finish_if_all_tasks_finished completes once it has at least one task and all its tasks have completed;

  • a task group that fails does so at once (the real platform passes through FAILING for about half a minute);

  • adding a task group to a work requirement that is not running is rejected.

Timings are attributes (instance_start, task_start and so on) and may be changed per test.

class yellowdog_ray.testing.fake_platform.FakePlatform(clock: FakeClock | None = None)[source]

See the module docstring.

add_image_family(namespace: str, name: str) → str[source]

Make an image family exist, as it would in the portal; returns its ID.

complete_task(task_id: str) → None[source]

As if the task’s script exited normally.

fail_next(method: str, error: Exception, times: int = 1) → None[source]

Make the next times calls of a Platform method raise error.

fail_task(task_id: str) → None[source]

As if the task’s script exited with an error.

lose_node(task_id: str) → None[source]

As if the instance running the task were reclaimed.

never_register(pool_name_contains: str) → None[source]

Instances of matching pools run but their nodes never register.

purge_terminal() → None[source]

Forget ended objects, as the platform does after 24 hours.

A clock that only moves when told to.