Skip to content

Commit 813c313

Browse files
committed
fix: some glitches in tests
1 parent ad770f1 commit 813c313

File tree

5 files changed

+37
-32
lines changed

5 files changed

+37
-32
lines changed

src/ai/backend/manager/sokovan/scheduler/selectors/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,14 @@ def order_slots_by_priority(
6262
An ordered list of slot names
6363
"""
6464

65-
def get_slot_index(slot_name: str | SlotName, order_reference: list[str | SlotName]) -> int:
65+
def get_slot_index(
66+
slot_name: str | SlotName, order_reference: list[str | SlotName]
67+
) -> tuple[int, str]:
6668
try:
67-
return order_reference.index(slot_name)
69+
return (order_reference.index(slot_name), str(slot_name))
6870
except ValueError:
69-
return sys.maxsize
71+
# Slots missing in the priority order will be pushed back and sorted alphabetically there.
72+
return (sys.maxsize, str(slot_name))
7073

7174
# Fill missing concrete slot names, by inserting slot names after corresponding device names.
7275
priority_order_including_slot_names = [*priority_order]

tests/manager/conftest.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from ai.backend.common.lock import FileLock
4848
from ai.backend.common.plugin.hook import HookPluginContext
4949
from ai.backend.common.typed_validators import HostPortPair as HostPortPairModel
50+
from ai.backend.common.types import ResourceSlot
5051
from ai.backend.logging import LocalLogger, LogLevel
5152
from ai.backend.logging.config import ConsoleConfig, LogDriver, LoggingConfig
5253
from ai.backend.logging.types import LogFormat
@@ -566,8 +567,8 @@ async def database_fixture(
566567
extra_fixture_file_path = Path(extra_fixture_file.name)
567568

568569
def fixture_json_encoder(obj: Any):
569-
if isinstance(obj, Mapping):
570-
return dict(obj)
570+
if isinstance(obj, ResourceSlot):
571+
return obj.to_json()
571572
if isinstance(obj, uuid.UUID):
572573
return str(obj)
573574
if isinstance(obj, datetime):
@@ -576,7 +577,8 @@ def fixture_json_encoder(obj: Any):
576577
return obj.value
577578
if isinstance(obj, yarl.URL):
578579
return str(obj)
579-
580+
if isinstance(obj, Mapping):
581+
return dict(obj)
580582
raise TypeError(f'Fixture type "{type(obj)}" not serializable')
581583

582584
with open(extra_fixture_file_path, "w") as f:

tests/manager/services/session/actions/test_create_cluster.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ async def test_create_cluster_with_template(
8484
mock_create_cluster_rpc,
8585
processors: SessionProcessors,
8686
session_repository,
87-
):
87+
) -> None:
8888
# Create the action using template
8989
action = CreateClusterAction(
9090
session_name=cast(str, SESSION_FIXTURE_DATA.name),
@@ -168,7 +168,7 @@ async def test_create_cluster_with_gpu_template(
168168
mock_create_cluster_rpc,
169169
processors: SessionProcessors,
170170
session_repository,
171-
):
171+
) -> None:
172172
# Create the action using GPU template
173173
action = CreateClusterAction(
174174
session_name="gpu_test_session",

tests/manager/sokovan/scheduler/selectors/test_utils.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
class TestUtilityFunctions:
1515
"""Test utility functions used by selectors."""
1616

17-
def test_count_unutilized_capabilities_no_unused(self):
17+
def test_count_unutilized_capabilities_no_unused(self) -> None:
1818
"""Test counting when all requested resources are used."""
1919
agent = create_agent_info(
20-
available_slots={
20+
available_slots=ResourceSlot({
2121
"cpu": Decimal("8"),
2222
"mem": Decimal("16384"),
2323
"cuda.shares": Decimal("4"),
24-
}
24+
})
2525
)
2626
requested_slots = ResourceSlot({
2727
"cpu": Decimal("2"),
@@ -32,15 +32,15 @@ def test_count_unutilized_capabilities_no_unused(self):
3232
count = count_unutilized_capabilities(agent, requested_slots)
3333
assert count == 0
3434

35-
def test_count_unutilized_capabilities_with_unused(self):
35+
def test_count_unutilized_capabilities_with_unused(self) -> None:
3636
"""Test counting with some zero-requested resources."""
3737
agent = create_agent_info(
38-
available_slots={
38+
available_slots=ResourceSlot({
3939
"cpu": Decimal("8"),
4040
"mem": Decimal("16384"),
4141
"cuda.shares": Decimal("4"),
4242
"tpu": Decimal("2"),
43-
}
43+
})
4444
)
4545
requested_slots = ResourceSlot({
4646
"cpu": Decimal("2"),
@@ -52,19 +52,19 @@ def test_count_unutilized_capabilities_with_unused(self):
5252
count = count_unutilized_capabilities(agent, requested_slots)
5353
assert count == 2 # cuda.shares and tpu are unutilized
5454

55-
def test_count_unutilized_capabilities_unavailable_resources(self):
55+
def test_count_unutilized_capabilities_unavailable_resources(self) -> None:
5656
"""Test that unavailable resources are not counted."""
5757
agent = create_agent_info(
58-
available_slots={
58+
available_slots=ResourceSlot({
5959
"cpu": Decimal("8"),
6060
"mem": Decimal("16384"),
6161
"cuda.shares": Decimal("4"),
62-
},
63-
occupied_slots={
62+
}),
63+
occupied_slots=ResourceSlot({
6464
"cpu": Decimal("0"),
6565
"mem": Decimal("0"),
6666
"cuda.shares": Decimal("4"), # Fully occupied
67-
},
67+
}),
6868
)
6969
requested_slots = ResourceSlot({
7070
"cpu": Decimal("2"),
@@ -75,7 +75,7 @@ def test_count_unutilized_capabilities_unavailable_resources(self):
7575
count = count_unutilized_capabilities(agent, requested_slots)
7676
assert count == 0 # cuda.shares is zero-requested but not available
7777

78-
def test_order_slots_by_priority_basic(self):
78+
def test_order_slots_by_priority_basic(self) -> None:
7979
"""Test basic slot ordering by priority."""
8080
requested_slots = ResourceSlot({
8181
"cpu": Decimal("1"),
@@ -88,7 +88,7 @@ def test_order_slots_by_priority_basic(self):
8888
result = order_slots_by_priority(requested_slots, priority_order)
8989
assert result == ["mem", "cpu", "cuda.shares", "disk"]
9090

91-
def test_order_slots_by_priority_missing_priorities(self):
91+
def test_order_slots_by_priority_missing_priorities(self) -> None:
9292
"""Test ordering when some slots are not in priority list."""
9393
requested_slots = ResourceSlot({
9494
"cpu": Decimal("1"),
@@ -104,19 +104,19 @@ def test_order_slots_by_priority_missing_priorities(self):
104104
# Non-priority slots should be sorted alphabetically
105105
assert result[1:] == sorted(result[1:])
106106

107-
def test_order_slots_by_priority_empty_priority(self):
107+
def test_order_slots_by_priority_empty_priority(self) -> None:
108108
"""Test ordering with empty priority list."""
109109
requested_slots = ResourceSlot({
110110
"zebra": Decimal("1"),
111111
"alpha": Decimal("2"),
112112
"beta": Decimal("3"),
113113
})
114-
priority_order = []
114+
priority_order: list[str] = []
115115

116116
result = order_slots_by_priority(requested_slots, priority_order)
117117
assert result == ["alpha", "beta", "zebra"] # Alphabetical order
118118

119-
def test_order_slots_by_priority_nonexistent_priorities(self):
119+
def test_order_slots_by_priority_nonexistent_priorities(self) -> None:
120120
"""Test that non-existent priority slots are ignored."""
121121
requested_slots = ResourceSlot({
122122
"cpu": Decimal("1"),
@@ -127,17 +127,17 @@ def test_order_slots_by_priority_nonexistent_priorities(self):
127127
result = order_slots_by_priority(requested_slots, priority_order)
128128
assert result == ["cpu", "mem"] # Only requested slots appear
129129

130-
def test_agent_info_calculations(self):
130+
def test_agent_info_calculations(self) -> None:
131131
"""Test that AgentInfo correctly calculates available resources."""
132132
agent = create_agent_info(
133-
available_slots={
133+
available_slots=ResourceSlot({
134134
"cpu": Decimal("16"),
135135
"mem": Decimal("32768"),
136-
},
137-
occupied_slots={
136+
}),
137+
occupied_slots=ResourceSlot({
138138
"cpu": Decimal("10"),
139139
"mem": Decimal("20480"),
140-
},
140+
}),
141141
)
142142

143143
free_slots = agent.available_slots - agent.occupied_slots

tests/manager/test_registry.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ async def test_convert_resource_spec_to_resource_slot(
182182
},
183183
}
184184
converted_allocations = registry.convert_resource_spec_to_resource_slot(allocations)
185-
assert converted_allocations["cuda.shares"] == "4.5"
185+
assert converted_allocations["cuda.shares"] == Decimal("4.5")
186186
allocations = {
187187
"cpu": {
188188
SlotName("cpu"): {
@@ -198,5 +198,5 @@ async def test_convert_resource_spec_to_resource_slot(
198198
},
199199
}
200200
converted_allocations = registry.convert_resource_spec_to_resource_slot(allocations)
201-
assert converted_allocations["cpu"] == "4"
202-
assert converted_allocations["ram"] == str(Decimal(BinarySize.from_str("1g")) * 3)
201+
assert converted_allocations["cpu"] == Decimal("4")
202+
assert converted_allocations["ram"] == Decimal(BinarySize.from_str("1g")) * 3

0 commit comments

Comments
 (0)