Skip to content
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

Refactor Reclass constructor classmethods #104

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,26 @@ impl Reclass {
Ok(r)
}

/// Creates a `Reclass` instance for the provided `inventory_path` and loads config options
/// from the provided config file. The value of `config_file` is interpreted relative to
/// `inventory_path`.
///
/// Returns a `Reclass` instance or raises a `ValueError`
#[classmethod]
fn from_config(_cls: &PyType, inventory_path: &str, config_file: &str) -> PyResult<Self> {
fn from_config_file(cls: &PyType, inventory_path: &str, config_file: &str) -> PyResult<Self> {
let mut c = Config::new(Some(inventory_path), None, None, None)
.map_err(|e| PyValueError::new_err(format!("{e}")))?;
c.load_from_file(config_file)
.map_err(|e| PyValueError::new_err(format!("{e}")))?;
let r = Self::new_from_config(c).map_err(|e| PyValueError::new_err(format!("{e}")))?;
Self::from_config(cls, c)
}

/// Creates a `Reclass` instance from the provided `Config` instance.
///
/// Returns a `Reclass` instance or raises a `ValueError`
#[classmethod]
fn from_config(_cls: &PyType, config: Config) -> PyResult<Self> {
let r = Self::new_from_config(config).map_err(|e| PyValueError::new_err(format!("{e}")))?;
Ok(r)
}

Expand Down
4 changes: 2 additions & 2 deletions tests/test_compose_node_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_no_compose_node_name():


def test_compose_node_name_compat():
r = reclass_rs.Reclass.from_config(
r = reclass_rs.Reclass.from_config_file(
"./tests/inventory-compose-node-name", "reclass-config.yml"
)
r.set_compat_flag(reclass_rs.CompatFlag.ComposeNodeNameLiteralDots)
Expand Down Expand Up @@ -63,7 +63,7 @@ def test_compose_node_name_compat():


def test_compose_node_name():
r = reclass_rs.Reclass.from_config(
r = reclass_rs.Reclass.from_config_file(
"./tests/inventory-compose-node-name", "reclass-config.yml"
)
assert r.config.compose_node_name
Expand Down
6 changes: 3 additions & 3 deletions tests/test_ignore_class_notfound_regexp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def test_ignore_regexp_render_n1():
r = reclass_rs.Reclass.from_config(
r = reclass_rs.Reclass.from_config_file(
"./tests/inventory-class-notfound-regexp", "reclass-config.yml"
)
assert r.config.ignore_class_notfound_regexp == ["service\\..*", ".*missing.*"]
Expand All @@ -15,7 +15,7 @@ def test_ignore_regexp_render_n1():


def test_ignore_regexp_render_n2():
r = reclass_rs.Reclass.from_config(
r = reclass_rs.Reclass.from_config_file(
"./tests/inventory-class-notfound-regexp", "reclass-config.yml"
)
assert r.config.ignore_class_notfound_regexp == ["service\\..*", ".*missing.*"]
Expand All @@ -27,7 +27,7 @@ def test_ignore_regexp_render_n2():


def test_ignore_regexp_update_config_render_n2():
r = reclass_rs.Reclass.from_config(
r = reclass_rs.Reclass.from_config_file(
"./tests/inventory-class-notfound-regexp", "reclass-config.yml"
)
r.set_ignore_class_notfound_regexp([".*"])
Expand Down
36 changes: 35 additions & 1 deletion tests/test_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@


def test_inventory():
r = reclass_rs.Reclass.from_config("./tests/inventory", "reclass-config.yml")
r = reclass_rs.Reclass.from_config_file("./tests/inventory", "reclass-config.yml")

assert set(r.nodes.keys()) == expected_nodes
included_classes = set(expected_classes.keys())
Expand Down Expand Up @@ -112,3 +112,37 @@ def test_inventory_as_dict():

assert "__reclass__" in inv
assert set(inv["__reclass__"].keys()) == set(["timestamp"])


def test_reclass_from_config():
config_options = {
"nodes_uri": "targets",
"classes_uri": "classes",
"ignore_class_notfound": True,
"compose_node_name": True,
}
c = reclass_rs.Config.from_dict("./tests/inventory", config_options)
assert c is not None

r = reclass_rs.Reclass.from_config(c)
assert r is not None

assert set(r.nodes.keys()) == expected_nodes
included_classes = set(expected_classes.keys())
all_classes = set(r.classes.keys())
# discovered classes which aren't shown with their resolved names in the output:
assert all_classes - included_classes == {
"${baz}", # appears as `\\${baz}`
"cluster.foo", # appears as `cluster.${dist}`
"config_symlink", # doesn't appear
"foo.bar", # appears as `${tenant}.${cluster}`
}
# class includes which aren't shown in their resolved form:
assert included_classes - all_classes == {
"${cls9}", # resolved as `cls9` for n15
"${qux}", # resolved as `cls1` for n4
"${tenant}.${cluster}", # resolved as `foo.bar` for n16
"\\${baz}", # resolved as `${baz}` for n17
"cluster.${dist}", # resolved as `cluster.foo` for n19
"nonexisting", # skipped because ignore_class_notfound=True
}
Loading