public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox 1/2] section config: add test for array schema
@ 2022-11-30 13:12 Fiona Ebner
  2022-11-30 13:12 ` [pbs-devel] [PATCH proxmox 2/2] section config: fix handling array schema in unknown sections Fiona Ebner
  2022-12-12 13:06 ` [pbs-devel] applied-series: [PATCH proxmox 1/2] section config: add test for array schema Wolfgang Bumiller
  0 siblings, 2 replies; 3+ messages in thread
From: Fiona Ebner @ 2022-11-30 13:12 UTC (permalink / raw)
  To: pbs-devel

where duplicate keys are allowed.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
 proxmox-section-config/src/lib.rs | 86 +++++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)

diff --git a/proxmox-section-config/src/lib.rs b/proxmox-section-config/src/lib.rs
index 91793a4..929ab92 100644
--- a/proxmox-section-config/src/lib.rs
+++ b/proxmox-section-config/src/lib.rs
@@ -1106,6 +1106,92 @@ token: asdf@pbs!asdftoken
     assert!(config.parse(filename, raw).is_err());
 }
 
+#[test]
+fn test_section_config_array() {
+    let filename = "sync.cfg";
+
+    const PROPERTIES: ObjectSchema = ObjectSchema::new(
+        "Dummy sync job properties",
+        &[
+            (
+                "group-filter",
+                true,
+                &ArraySchema::new(
+                    "Group filter array schema",
+                    &StringSchema::new("Group filter entry schema.").schema(),
+                )
+                .schema(),
+            ),
+            (
+                "schedule",
+                true,
+                &StringSchema::new("Remote schema.").schema(),
+            ),
+        ],
+    );
+
+    let plugin = SectionConfigPlugin::new("sync".to_string(), None, &PROPERTIES);
+
+    const ID_SCHEMA: Schema = StringSchema::new("ID schema.").min_length(3).schema();
+
+    let mut config = SectionConfig::new(&ID_SCHEMA);
+    config.register_plugin(plugin);
+
+    let raw = r"
+
+sync: s-4a1011e8-40e2
+        group-filter group:vm/144
+        schedule monthly
+
+sync: s-5b2122f9-51f3
+        group-filter group:vm/100
+        schedule hourly
+        group-filter group:vm/102
+
+sync: s-6c32330a-6204
+        group-filter group:vm/103
+        group-filter group:vm/104
+        group-filter group:vm/105
+";
+
+    let check = |res: SectionConfigData| {
+        let (_, second_section) = res.sections.get("s-5b2122f9-51f3").unwrap();
+        assert_eq!(*second_section.get("schedule").unwrap(), json!("hourly"));
+        assert_eq!(
+            *second_section.get("group-filter").unwrap(),
+            json!(["group:vm/100", "group:vm/102"]),
+        );
+
+        let (_, third_section) = res.sections.get("s-6c32330a-6204").unwrap();
+        assert_eq!(
+            *third_section.get("group-filter").unwrap(),
+            json!(["group:vm/103", "group:vm/104", "group:vm/105"]),
+        );
+        assert!(third_section.get("schedule").is_none());
+    };
+
+    let res = config.parse(filename, raw).unwrap();
+    println!("RES: {:?}", res);
+    let written = config.write(filename, &res).unwrap();
+    println!("CONFIG:\n{}", written);
+
+    check(res);
+
+    let res = config.parse(filename, &written).unwrap();
+    println!("RES (second time): {:?}", res);
+
+    check(res);
+
+    let raw = r"
+
+sync: fail
+        schedule hourly
+        schedule monthly
+";
+
+    assert!(config.parse(filename, raw).is_err());
+}
+
 /// Generate ReST Documentaion for ``SectionConfig``
 pub fn dump_section_config(config: &SectionConfig) -> String {
     let mut res = String::new();
-- 
2.30.2





^ permalink raw reply	[flat|nested] 3+ messages in thread

* [pbs-devel] [PATCH proxmox 2/2] section config: fix handling array schema in unknown sections
  2022-11-30 13:12 [pbs-devel] [PATCH proxmox 1/2] section config: add test for array schema Fiona Ebner
@ 2022-11-30 13:12 ` Fiona Ebner
  2022-12-12 13:06 ` [pbs-devel] applied-series: [PATCH proxmox 1/2] section config: add test for array schema Wolfgang Bumiller
  1 sibling, 0 replies; 3+ messages in thread
From: Fiona Ebner @ 2022-11-30 13:12 UTC (permalink / raw)
  To: pbs-devel

Mostly relevant when the config is written out again after parsing it
with unknown sections. Previously, with duplicate keys, only the last
value would be saved. Now, duplicate keys are assumed to be part of
an array schema and handled as such.

Because the unknown section parsing does not know if a certain
property does actually have an array schema, it's not possible to
detect duplicate keys for non-array-schema properties, and if a
property with array-schema shows up only once, it will not be saved as
a Value::Array, but a Value::String.

Writing, or to be precise the format_section_content methods, already
handle Value::Array, so don't need to be adapted.

Fixes: 0cd0d16 ("section config: support allowing unknown section types")
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
 proxmox-section-config/src/lib.rs | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/proxmox-section-config/src/lib.rs b/proxmox-section-config/src/lib.rs
index 929ab92..66eeacf 100644
--- a/proxmox-section-config/src/lib.rs
+++ b/proxmox-section-config/src/lib.rs
@@ -529,7 +529,14 @@ impl SectionConfig {
                                 continue;
                             }
                             if let Some((key, value)) = (self.parse_section_content)(line) {
-                                config[key] = json!(value);
+                                match &mut config[&key] {
+                                    Value::Null => config[key] = json!(value),
+                                    // Assume it's an array schema in order to handle actual array
+                                    // schemas as good as we can.
+                                    Value::String(current) => config[key] = json!([current, value]),
+                                    Value::Array(array) => array.push(json!(value)),
+                                    other => bail!("got unexpected Value {:?}", other),
+                                }
                             } else {
                                 bail!("syntax error (expected section properties)");
                             }
@@ -1137,6 +1144,8 @@ fn test_section_config_array() {
     let mut config = SectionConfig::new(&ID_SCHEMA);
     config.register_plugin(plugin);
 
+    let config_unknown = SectionConfig::new(&ID_SCHEMA).allow_unknown_sections(true);
+
     let raw = r"
 
 sync: s-4a1011e8-40e2
@@ -1182,6 +1191,15 @@ sync: s-6c32330a-6204
 
     check(res);
 
+    let res_unknown = config_unknown.parse(filename, raw).unwrap();
+    println!("RES (unknown): {:?}", res_unknown);
+    let written_unknown = config_unknown.write(filename, &res_unknown).unwrap();
+    println!("CONFIG (unknown):\n{}", written_unknown);
+
+    check(res_unknown);
+
+    assert_eq!(written, written_unknown);
+
     let raw = r"
 
 sync: fail
-- 
2.30.2





^ permalink raw reply	[flat|nested] 3+ messages in thread

* [pbs-devel] applied-series: [PATCH proxmox 1/2] section config: add test for array schema
  2022-11-30 13:12 [pbs-devel] [PATCH proxmox 1/2] section config: add test for array schema Fiona Ebner
  2022-11-30 13:12 ` [pbs-devel] [PATCH proxmox 2/2] section config: fix handling array schema in unknown sections Fiona Ebner
@ 2022-12-12 13:06 ` Wolfgang Bumiller
  1 sibling, 0 replies; 3+ messages in thread
From: Wolfgang Bumiller @ 2022-12-12 13:06 UTC (permalink / raw)
  To: Fiona Ebner; +Cc: pbs-devel

applied both patches, thanks




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-12-12 13:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-30 13:12 [pbs-devel] [PATCH proxmox 1/2] section config: add test for array schema Fiona Ebner
2022-11-30 13:12 ` [pbs-devel] [PATCH proxmox 2/2] section config: fix handling array schema in unknown sections Fiona Ebner
2022-12-12 13:06 ` [pbs-devel] applied-series: [PATCH proxmox 1/2] section config: add test for array schema Wolfgang Bumiller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal