-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c90b2e
commit ff66c22
Showing
9 changed files
with
245 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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,54 @@ | ||
import logging | ||
import pathlib | ||
from typing import Optional | ||
|
||
from packaging.requirements import SpecifierSet | ||
from pydantic import BaseModel, ConfigDict, field_validator | ||
|
||
from _nebari._version import __version__ | ||
from _nebari.utils import yaml | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ConfigSetMetadata(BaseModel): | ||
model_config: ConfigDict = ConfigDict(extra="allow", arbitrary_types_allowed=True) | ||
name: str # for use with guided init | ||
description: Optional[str] = None | ||
nebari_version: str | SpecifierSet | ||
|
||
@field_validator("nebari_version") | ||
@classmethod | ||
def validate_version_requirement(cls, version_req): | ||
if isinstance(version_req, str): | ||
version_req = SpecifierSet(version_req, prereleases=True) | ||
|
||
return version_req | ||
|
||
def check_version(self, version): | ||
if not self.nebari_version.contains(version, prereleases=True): | ||
raise ValueError( | ||
f'Nebari version "{version}" is not compatible with ' | ||
f'version requirement {self.nebari_version} for "{self.name}" config set.' | ||
) | ||
|
||
|
||
class ConfigSet(BaseModel): | ||
metadata: ConfigSetMetadata | ||
config: dict | ||
|
||
|
||
def read_config_set(config_set_filepath: str): | ||
"""Read a config set from a config file.""" | ||
|
||
filename = pathlib.Path(config_set_filepath) | ||
|
||
with filename.open() as f: | ||
config_set_yaml = yaml.load(f) | ||
|
||
config_set = ConfigSet(**config_set_yaml) | ||
|
||
# validation | ||
config_set.metadata.check_version(__version__) | ||
|
||
return config_set |
This file contains 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 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 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 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 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,73 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from packaging.requirements import SpecifierSet | ||
|
||
from _nebari.config_set import ConfigSetMetadata, read_config_set | ||
|
||
test_version = "2024.12.2" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"version_input,test_version,should_pass", | ||
[ | ||
# Standard version tests | ||
(">=2024.12.0,<2025.0.0", "2024.12.2", True), | ||
(SpecifierSet(">=2024.12.0,<2025.0.0"), "2024.12.2", True), | ||
# Pre-release version requirement tests | ||
(">=2024.12.0rc1,<2025.0.0", "2024.12.0rc1", True), | ||
(SpecifierSet(">=2024.12.0rc1"), "2024.12.0rc2", True), | ||
# Pre-release test version against standard requirement | ||
(">=2024.12.0,<2025.0.0", "2024.12.1rc1", True), | ||
(SpecifierSet(">=2024.12.0,<2025.0.0"), "2024.12.1rc1", True), | ||
# Failing cases | ||
(">=2025.0.0", "2024.12.2rc1", False), | ||
(SpecifierSet(">=2025.0.0rc1"), "2024.12.2", False), | ||
], | ||
) | ||
def test_version_requirement(version_input, test_version, should_pass): | ||
metadata = ConfigSetMetadata(name="test-config", nebari_version=version_input) | ||
|
||
if should_pass: | ||
metadata.check_version(test_version) | ||
else: | ||
with pytest.raises(ValueError) as exc_info: | ||
metadata.check_version(test_version) | ||
assert "Nebari version" in str(exc_info.value) | ||
|
||
|
||
def test_read_config_set_valid(tmp_path): | ||
config_set_yaml = """ | ||
metadata: | ||
name: test-config | ||
nebari_version: ">=2024.12.0" | ||
config: | ||
key: value | ||
""" | ||
config_set_filepath = tmp_path / "config_set.yaml" | ||
config_set_filepath.write_text(config_set_yaml) | ||
with patch("_nebari.config_set.__version__", "2024.12.2"): | ||
config_set = read_config_set(str(config_set_filepath)) | ||
assert config_set.metadata.name == "test-config" | ||
assert config_set.config["key"] == "value" | ||
|
||
|
||
def test_read_config_set_invalid_version(tmp_path): | ||
config_set_yaml = """ | ||
metadata: | ||
name: test-config | ||
nebari_version: ">=2025.0.0" | ||
config: | ||
key: value | ||
""" | ||
config_set_filepath = tmp_path / "config_set.yaml" | ||
config_set_filepath.write_text(config_set_yaml) | ||
|
||
with patch("_nebari.config_set.__version__", "2024.12.2"): | ||
with pytest.raises(ValueError) as exc_info: | ||
read_config_set(str(config_set_filepath)) | ||
assert "Nebari version" in str(exc_info.value) | ||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main() |
This file contains 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 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 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