Repository navigation
feat(bug-finding): warn users if resource config conflicts with machi… #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tkqdldk
wants to merge
8
commits into
ossf:main
Choose a base branch
from
tkqdldk:feat/resource-config-warn
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+282
−4
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
782576a
feat(bug-finding): warn users if resource config conflicts with machi…
tkqdldk 2880aca
fix(bug-finding): correct off-by-one in cpuset validation and machine…
tkqdldk 8f89db8
feat(bug-finding): add dynamic testing for machine resources check
tkqdldk f50b49d
fix: run resource check before build-target
tkqdldk e487694
fix: apply ruff format
tkqdldk d4e142f
fix: trigger error and termination from resource validation failure
tkqdldk a15644b
refactor: move resource check earlier in build_target phase
tkqdldk 44f6826
fix(tests): reduce test memory allocation to avoid rounding error due…
tkqdldk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| """Unit tests for _validate_machine_resources step in CRSCompose.""" | ||
|
|
||
| from typing import cast | ||
|
|
||
| import pytest | ||
|
|
||
| from oss_crs.src.config.crs_compose import CRSComposeConfig | ||
| from oss_crs.src.crs_compose import CRSCompose | ||
|
|
||
| MOCK_CPU_COUNT = 12 | ||
| MOCK_MEMORY = 32 * (1024**3) | ||
|
|
||
|
|
||
| class _FakeResource: | ||
| def __init__(self, cpuset: str, memory: str): | ||
| self.cpuset = cpuset | ||
| self.memory = memory | ||
|
|
||
|
|
||
| class _FakeComposeConfig: | ||
| def __init__(self, infra: _FakeResource, crs_entries: dict): | ||
| self.oss_crs_infra = infra | ||
| self.crs_entries = crs_entries | ||
|
|
||
|
|
||
| def _make_compose( | ||
| infra_cpuset: str, | ||
| infra_memory: str = "4G", | ||
| crs_entries: dict | None = None, | ||
| ) -> CRSCompose: | ||
| compose = object.__new__(CRSCompose) | ||
| compose.config = cast( | ||
| CRSComposeConfig, | ||
| _FakeComposeConfig( | ||
| _FakeResource(infra_cpuset, infra_memory), | ||
| crs_entries or {}, | ||
| ), | ||
| ) | ||
| return compose | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def patched_resources(monkeypatch): | ||
| """Patch machine resource detection with standard mock values. | ||
|
|
||
| Returns a tuple of (warnings, errors) lists. | ||
| """ | ||
| warnings = [] | ||
| errors = [] | ||
| monkeypatch.setattr("oss_crs.src.crs_compose.os.cpu_count", lambda: MOCK_CPU_COUNT) | ||
| monkeypatch.setattr("oss_crs.src.crs_compose.get_host_memory", lambda: MOCK_MEMORY) | ||
| monkeypatch.setattr( | ||
| "oss_crs.src.crs_compose.log_warning", lambda msg: warnings.append(msg) | ||
| ) | ||
| monkeypatch.setattr( | ||
| "oss_crs.src.crs_compose.log_error", lambda msg: errors.append(msg) | ||
| ) | ||
| return warnings, errors | ||
|
|
||
|
|
||
| ## CPU and memory checks | ||
|
|
||
|
|
||
| def test_cpu_within_bounds_no_warning(patched_resources): | ||
| warnings, errors = patched_resources | ||
| compose = _make_compose(f"0-{MOCK_CPU_COUNT - 2}") | ||
| assert compose._validate_machine_resources() is True | ||
| assert len(errors) == 0 | ||
|
|
||
|
|
||
| def test_cpu_at_max_valid_id_no_warning(patched_resources): | ||
| warnings, errors = patched_resources | ||
| compose = _make_compose(f"0-{MOCK_CPU_COUNT - 1}") | ||
| assert compose._validate_machine_resources() is True | ||
| assert len(errors) == 0 | ||
|
|
||
|
|
||
| def test_cpu_equals_machine_count_errors(patched_resources): | ||
| warnings, errors = patched_resources | ||
| compose = _make_compose(f"0-{MOCK_CPU_COUNT}") | ||
| assert compose._validate_machine_resources() is False | ||
| assert len(errors) == 1 | ||
| assert "does not have adequate resources" in errors[0] | ||
|
|
||
|
|
||
| def test_cpu_exceeds_machine_count_errors(patched_resources): | ||
| warnings, errors = patched_resources | ||
| compose = _make_compose(f"0-{MOCK_CPU_COUNT + 1}") | ||
| assert compose._validate_machine_resources() is False | ||
| assert len(errors) == 1 | ||
| assert "does not have adequate resources" in errors[0] | ||
|
|
||
|
|
||
| def test_memory_at_exact_limit_no_warning(patched_resources): | ||
| warnings, errors = patched_resources | ||
| exact_memory = f"{MOCK_MEMORY // (1024**3)}G" | ||
| compose = _make_compose("0-3", exact_memory) | ||
| assert compose._validate_machine_resources() is True | ||
| assert len(errors) == 0 | ||
|
|
||
|
|
||
| def test_memory_over_limit_errors(patched_resources): | ||
| warnings, errors = patched_resources | ||
| over_memory = f"{(MOCK_MEMORY // (1024**3)) + 1}G" | ||
| compose = _make_compose("0-3", over_memory) | ||
| assert compose._validate_machine_resources() is False | ||
| assert len(errors) == 1 | ||
| assert "does not have adequate resources" in errors[0] | ||
|
|
||
|
|
||
| def test_crs_entry_exceeds_cpu_count_errors(patched_resources): | ||
| warnings, errors = patched_resources | ||
| compose = _make_compose( | ||
| "0-3", | ||
| crs_entries={"crs-a": _FakeResource(f"0-{MOCK_CPU_COUNT}", "4G")}, | ||
| ) | ||
| assert compose._validate_machine_resources() is False | ||
| assert len(errors) == 1 | ||
| assert "does not have adequate resources" in errors[0] | ||
|
|
||
|
|
||
| def test_combined_memory_across_entries_errors(patched_resources): | ||
| warnings, errors = patched_resources | ||
| half_memory = f"{MOCK_MEMORY // (1024**3) // 2 + 1}G" | ||
| compose = _make_compose( | ||
| "0-3", | ||
| infra_memory=half_memory, | ||
| crs_entries={ | ||
| "crs-a": _FakeResource("0-3", half_memory), | ||
| }, | ||
| ) | ||
| assert compose._validate_machine_resources() is False | ||
| assert len(errors) == 1 | ||
| assert "does not have adequate resources" in errors[0] | ||
|
|
||
|
|
||
| def test_both_adequate_no_warning(patched_resources): | ||
| warnings, errors = patched_resources | ||
| compose = _make_compose("0-3", "4G") | ||
| assert compose._validate_machine_resources() is True | ||
| assert len(errors) == 0 | ||
|
|
||
|
|
||
| ## undetectable machine resources checks | ||
|
|
||
|
|
||
| def test_undetectable_cpu_skips_check(monkeypatch): | ||
| warnings = [] | ||
| errors = [] | ||
| monkeypatch.setattr("oss_crs.src.crs_compose.os.cpu_count", lambda: None) | ||
| monkeypatch.setattr("oss_crs.src.crs_compose.get_host_memory", lambda: MOCK_MEMORY) | ||
| monkeypatch.setattr( | ||
| "oss_crs.src.crs_compose.log_warning", lambda msg: warnings.append(msg) | ||
| ) | ||
| monkeypatch.setattr( | ||
| "oss_crs.src.crs_compose.log_error", lambda msg: errors.append(msg) | ||
| ) | ||
|
|
||
| compose = _make_compose("0-3") | ||
| assert compose._validate_machine_resources() is True | ||
| assert len(warnings) == 1 | ||
| assert "Could not determine machine resources" in warnings[0] | ||
| assert len(errors) == 0 | ||
|
|
||
|
|
||
| def test_undetectable_memory_skips_check(monkeypatch): | ||
| warnings = [] | ||
| errors = [] | ||
| monkeypatch.setattr("oss_crs.src.crs_compose.os.cpu_count", lambda: MOCK_CPU_COUNT) | ||
| monkeypatch.setattr("oss_crs.src.crs_compose.get_host_memory", lambda: None) | ||
| monkeypatch.setattr( | ||
| "oss_crs.src.crs_compose.log_warning", lambda msg: warnings.append(msg) | ||
| ) | ||
| monkeypatch.setattr( | ||
| "oss_crs.src.crs_compose.log_error", lambda msg: errors.append(msg) | ||
| ) | ||
|
|
||
| compose = _make_compose("0-3") | ||
| assert compose._validate_machine_resources() is True | ||
| assert len(warnings) == 1 | ||
| assert "Could not determine machine resources" in warnings[0] | ||
| assert len(errors) == 0 | ||
|
|
||
|
|
||
| # parsing error checks | ||
|
|
||
|
|
||
| def test_invalid_cpuset_warns(patched_resources): | ||
| warnings, errors = patched_resources | ||
| compose = _make_compose("not-a-cpuset", "4G") | ||
| compose._validate_machine_resources() | ||
| assert any("Failed to validate cpuset" in w for w in warnings) | ||
|
|
||
|
|
||
| def test_invalid_memory_string_warns(patched_resources): | ||
| warnings, errors = patched_resources | ||
| compose = _make_compose("0-3", "notmemory") | ||
| compose._validate_machine_resources() | ||
| assert any("Failed to parse memory" in w for w in warnings) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think pyright is complaining about this line?
is_builder