all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Christian Ebner <c.ebner@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox 6/6] s3 client: add basic regression tests for response parsing
Date: Mon,  4 Aug 2025 18:09:37 +0200	[thread overview]
Message-ID: <20250804160937.660470-7-c.ebner@proxmox.com> (raw)
In-Reply-To: <20250804160937.660470-1-c.ebner@proxmox.com>

Adds regression tests for methods parsing the http response body
by using responses as found in the AWS documentation for the
respective api method.

Requires to derive PartialEq on S3ObjectKey and LastModifiedTimestamp
structs in order to be able to easily compare the resulting contents.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
 proxmox-s3-client/src/object_key.rs      |   2 +-
 proxmox-s3-client/src/response_reader.rs | 113 ++++++++++++++++++++++-
 proxmox-s3-client/src/timestamps.rs      |   2 +-
 3 files changed, 113 insertions(+), 4 deletions(-)

diff --git a/proxmox-s3-client/src/object_key.rs b/proxmox-s3-client/src/object_key.rs
index 327e8ac7..a68a588f 100644
--- a/proxmox-s3-client/src/object_key.rs
+++ b/proxmox-s3-client/src/object_key.rs
@@ -4,7 +4,7 @@ use anyhow::{bail, Error};
 /// See https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
 const S3_OBJECT_KEY_MAX_LENGTH: usize = 1024;
 
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, PartialEq)]
 /// S3 Object Key
 pub enum S3ObjectKey {
     /// Object key which will not be prefixed any further by the client
diff --git a/proxmox-s3-client/src/response_reader.rs b/proxmox-s3-client/src/response_reader.rs
index da76ec3f..f895db19 100644
--- a/proxmox-s3-client/src/response_reader.rs
+++ b/proxmox-s3-client/src/response_reader.rs
@@ -79,7 +79,7 @@ impl ListObjectsV2ResponseBody {
     }
 }
 
-#[derive(Deserialize, Debug)]
+#[derive(Deserialize, Debug, PartialEq)]
 #[serde(rename_all = "PascalCase")]
 /// Subset of contents used to deserialize the listed object contents of a list objects v2 respsonse.
 /// https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html#API_ListObjectsV2_ResponseSyntax
@@ -231,7 +231,7 @@ pub struct Buckets {
     bucket: Vec<Bucket>,
 }
 
-#[derive(Deserialize, Debug)]
+#[derive(Deserialize, Debug, PartialEq)]
 #[serde(rename_all = "PascalCase")]
 /// Subset of contents used to deserialize individual buckets for response of a list buckets api
 /// call.
@@ -511,3 +511,112 @@ impl ResponseReader {
         Ok(value)
     }
 }
+
+#[test]
+fn parse_list_objects_v2_response_test() {
+    let response_body = r#"<?xml version="1.0" encoding="UTF-8"?>
+        <ListBucketResult>
+            <Name>bucket0</Name>
+            <Prefix>.cnt</Prefix>
+            <KeyCount>2</KeyCount>
+            <MaxKeys>1000</MaxKeys>
+            <IsTruncated>false</IsTruncated>
+            <Contents>
+                <Key>.cnt/key0</Key>
+                <LastModified>2011-02-26T01:56:20.000Z</LastModified>
+                <ETag>"bf1d737a4d46a19f3bced6905cc8b902"</ETag>
+                <Size>10</Size>
+                <StorageClass>STANDARD</StorageClass>
+            </Contents>
+            <Contents>
+                <Key>.cnt/key1</Key>
+                <LastModified>2011-02-26T01:56:20.000Z</LastModified>
+                <ETag>"9b2cf535f27731c974343645a3985328"</ETag>
+                <Size>20</Size>
+                <StorageClass>STANDARD</StorageClass>
+            </Contents>
+        </ListBucketResult>
+    "#;
+    let result: ListObjectsV2ResponseBody = serde_xml_rs::from_str(&response_body).unwrap();
+    assert_eq!(result.name, "bucket0");
+    assert_eq!(result.prefix, ".cnt");
+    assert_eq!(result.key_count, 2);
+    assert_eq!(result.max_keys, 1000);
+    assert_eq!(result.is_truncated, false);
+    assert_eq!(
+        result.contents.unwrap(),
+        vec![
+            ListObjectsV2Contents {
+                key: S3ObjectKey::try_from("/.cnt/key0").unwrap(),
+                last_modified: LastModifiedTimestamp::from_str("2011-02-26T01:56:20.000Z").unwrap(),
+                e_tag: "\"bf1d737a4d46a19f3bced6905cc8b902\"".to_string(),
+                size: 10,
+                storage_class: "STANDARD".to_string(),
+            },
+            ListObjectsV2Contents {
+                key: S3ObjectKey::try_from("/.cnt/key1").unwrap(),
+                last_modified: LastModifiedTimestamp::from_str("2011-02-26T01:56:20.000Z").unwrap(),
+                e_tag: "\"9b2cf535f27731c974343645a3985328\"".to_string(),
+                size: 20,
+                storage_class: "STANDARD".to_string(),
+            },
+        ]
+    );
+}
+
+#[test]
+fn parse_copy_object_response_test() {
+    let response_body = r#"<?xml version="1.0" encoding="UTF-8"?>
+        <CopyObjectResult>
+            <LastModified>2009-10-12T17:50:30.000Z</LastModified>
+            <ETag>"9b2cf535f27731c974343645a3985328"</ETag>
+        </CopyObjectResult>
+    "#;
+    let result: CopyObjectResult = serde_xml_rs::from_str(&response_body).unwrap();
+    assert_eq!(
+        result.last_modified,
+        LastModifiedTimestamp::from_str("2009-10-12T17:50:30.000Z").unwrap()
+    );
+    assert_eq!(
+        result.e_tag,
+        "\"9b2cf535f27731c974343645a3985328\"".to_string()
+    );
+}
+
+#[test]
+fn parse_list_buckets_response_test() {
+    let response_body = r#"<?xml version="1.0" encoding="UTF-8"?>
+        <ListAllMyBucketsResult>
+            <Buckets>
+                <Bucket>
+                    <CreationDate>2019-12-11T23:32:47+00:00</CreationDate>
+                    <Name>bucket0</Name>
+                </Bucket>
+                <Bucket>
+                    <CreationDate>2019-11-10T23:32:13+00:00</CreationDate>
+                    <Name>bucket1</Name>
+                </Bucket>
+            </Buckets>
+        </ListAllMyBucketsResult>
+    "#;
+    let result: ListAllMyBucketsResult = serde_xml_rs::from_str(&response_body).unwrap();
+    assert_eq!(
+        result.buckets.unwrap().bucket,
+        vec![
+            Bucket {
+                name: "bucket0".to_string(),
+                creation_date: LastModifiedTimestamp::from_str("2019-12-11T23:32:47+00:00")
+                    .unwrap(),
+                bucket_arn: None,
+                bucket_region: None,
+            },
+            Bucket {
+                name: "bucket1".to_string(),
+                creation_date: LastModifiedTimestamp::from_str("2019-11-10T23:32:13+00:00")
+                    .unwrap(),
+                bucket_arn: None,
+                bucket_region: None,
+            },
+        ]
+    );
+}
diff --git a/proxmox-s3-client/src/timestamps.rs b/proxmox-s3-client/src/timestamps.rs
index 22330966..661e1fdf 100644
--- a/proxmox-s3-client/src/timestamps.rs
+++ b/proxmox-s3-client/src/timestamps.rs
@@ -5,7 +5,7 @@ const VALID_MONTHS: [&str; 12] = [
     "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
 ];
 
-#[derive(Debug)]
+#[derive(Debug, PartialEq)]
 /// Last modified timestamp as obtained from API response http headers.
 pub struct LastModifiedTimestamp {
     _datetime: iso8601::DateTime,
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


  parent reply	other threads:[~2025-08-04 16:08 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-04 16:09 [pbs-devel] [PATCH proxmox 0/6] followups and cleanups for s3 client Christian Ebner
2025-08-04 16:09 ` [pbs-devel] [PATCH proxmox 1/6] s3 client: fix formatting issues by `cargo fmt` Christian Ebner
2025-08-04 16:09 ` [pbs-devel] [PATCH proxmox 2/6] s3 client: refactor list buckets response status code matching Christian Ebner
2025-08-04 16:09 ` [pbs-devel] [PATCH proxmox 3/6] s3 client: refactor list buckets result list fetching Christian Ebner
2025-08-04 16:09 ` [pbs-devel] [PATCH proxmox 4/6] s3 client: add doc comments for response parser helper methods Christian Ebner
2025-08-04 16:09 ` [pbs-devel] [PATCH proxmox 5/6] s3 client: add and expand doc comments for response parsing objects Christian Ebner
2025-08-04 16:09 ` Christian Ebner [this message]
2025-08-04 20:18 ` [pbs-devel] [PATCH proxmox 0/6] followups and cleanups for s3 client Thomas Lamprecht
2025-08-04 20:18   ` [pve-devel] " Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250804160937.660470-7-c.ebner@proxmox.com \
    --to=c.ebner@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal