test_etcd_bindings.py 1.77 KB
Newer Older
1
2
3
4
5
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

import asyncio

Neelay Shah's avatar
Neelay Shah committed
6
7
8
9
from dynamo._core import DistributedRuntime

# Todo add support for launching etcd
# pytestmark = pytest.mark.pre_merge
10
11
12
13
14


async def test_simple_put_get():
    # Initialize runtime
    loop = asyncio.get_running_loop()
15
    runtime = DistributedRuntime(loop, False)
16
17

    # Get etcd client
18
    etcd = runtime.do_not_use_etcd_client()
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

    # Write some key-value pairs
    test_keys = {
        "test/key1": b"value1",
        "test/key2": b"value2",
        "test/nested/key3": b"value3",
    }
    # Write each key-value pair
    for key, value in test_keys.items():
        print(f"Writing {key} = {value!r}")
        await etcd.kv_create_or_validate(key, value, None)

    print("Successfully wrote all keys to etcd")

    # Test kv_put
    put_key = "test/put_key"
    put_value = b"put_value"
    test_keys[put_key] = put_value
    print(f"Using kv_put to write {put_key} = {put_value!r}")
    await etcd.kv_put(put_key, put_value, None)

    # Test kv_get_prefix to read all keys
    print("\nReading all keys with prefix 'test/':")
    keys_values = await etcd.kv_get_prefix("test/")
    for item in keys_values:
        print(f"Retrieved {item['key']} = {item['value']!r}")
        assert test_keys[item["key"]] == item["value"]

    # Verify prefix filtering works
    print("\nReading keys with prefix 'test/nested/':")
    nested_keys_values = await etcd.kv_get_prefix("test/nested/")
    for item in nested_keys_values:
        print(f"Retrieved {item['key']} = {item['value']!r}")
        assert test_keys[item["key"]] == item["value"]

    # Shutdown runtime
    runtime.shutdown()


if __name__ == "__main__":
    asyncio.run(test_simple_put_get())