all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Lukas Wagner <l.wagner@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [pdm-devel] [PATCH proxmox-datacenter-manager 11/25] metric collection: add tests for the fetch_remotes function
Date: Tue, 11 Feb 2025 13:05:27 +0100	[thread overview]
Message-ID: <20250211120541.163621-12-l.wagner@proxmox.com> (raw)
In-Reply-To: <20250211120541.163621-1-l.wagner@proxmox.com>

Since we have the option to have mocked remote clients and a mocked
remote config, as well as due to the decoupling of the metric collection
and RRD task, it is now viable to write a unit test for one of the core
pieces of the metric collection system.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 .../src/metric_collection/collection_task.rs  | 232 ++++++++++++++++++
 server/src/metric_collection/state.rs         |  16 +-
 2 files changed, 236 insertions(+), 12 deletions(-)

diff --git a/server/src/metric_collection/collection_task.rs b/server/src/metric_collection/collection_task.rs
index f0742ea..b07a933 100644
--- a/server/src/metric_collection/collection_task.rs
+++ b/server/src/metric_collection/collection_task.rs
@@ -433,3 +433,235 @@ pub(super) fn load_state() -> Result<MetricCollectionState, Error> {
         file_options,
     ))
 }
+
+#[cfg(test)]
+pub(super) mod tests {
+    use std::{collections::HashMap, sync::Once};
+
+    use anyhow::bail;
+    use http::StatusCode;
+
+    use pdm_api_types::Authid;
+    use pve_api_types::{client::PveClient, ClusterMetrics, ClusterMetricsData};
+
+    use crate::{
+        connection::ClientFactory, metric_collection::rrd_task::RrdStoreResult,
+        pbs_client::PbsClient, test_support::temp::NamedTempFile,
+    };
+
+    use super::*;
+
+    pub(crate) fn get_create_options() -> CreateOptions {
+        CreateOptions::new()
+            .owner(nix::unistd::Uid::effective())
+            .group(nix::unistd::Gid::effective())
+            .perm(nix::sys::stat::Mode::from_bits_truncate(0o600))
+    }
+
+    struct TestClientFactory {
+        now: i64,
+    }
+
+    #[async_trait::async_trait]
+    impl ClientFactory for TestClientFactory {
+        fn make_pve_client(
+            &self,
+            remote: &Remote,
+        ) -> Result<Box<dyn PveClient + Send + Sync>, Error> {
+            Ok(Box::new(TestPveClient {
+                fail: remote.id.contains("fail"),
+                now: self.now,
+            }))
+        }
+        /// Create a new API client for PVE remotes, but with a specific endpoint.
+        fn make_pve_client_with_endpoint(
+            &self,
+            _remote: &Remote,
+            _target_endpoint: Option<&str>,
+        ) -> Result<Box<dyn PveClient + Send + Sync>, Error> {
+            bail!("not implemented")
+        }
+
+        fn make_pbs_client(&self, _remote: &Remote) -> Result<Box<PbsClient>, Error> {
+            bail!("not implemented")
+        }
+
+        async fn make_pve_client_and_login(
+            &self,
+            _remote: &Remote,
+        ) -> Result<Box<dyn PveClient + Send + Sync>, Error> {
+            bail!("not implemented")
+        }
+
+        async fn make_pbs_client_and_login(
+            &self,
+            _remote: &Remote,
+        ) -> Result<Box<PbsClient>, Error> {
+            bail!("not implemented")
+        }
+    }
+
+    struct TestPveClient {
+        now: i64,
+        fail: bool,
+    }
+
+    #[async_trait::async_trait]
+    impl PveClient for TestPveClient {
+        /// Retrieve metrics of the cluster.
+        async fn cluster_metrics_export(
+            &self,
+            _history: Option<bool>,
+            _local_only: Option<bool>,
+            start_time: Option<i64>,
+        ) -> Result<ClusterMetrics, proxmox_client::Error> {
+            if self.fail {
+                return Err(proxmox_client::Error::Api(
+                    StatusCode::INTERNAL_SERVER_ERROR,
+                    "internal server error".into(),
+                ));
+            }
+
+            let mut time = start_time.unwrap_or(0);
+            time = time.max(self.now - (30 * 60));
+            let mut data = Vec::new();
+
+            use pve_api_types::ClusterMetricsDataType::*;
+
+            while time < self.now {
+                let point = |id: &str, metric: &str, timestamp, ty| ClusterMetricsData {
+                    id: id.into(),
+                    metric: metric.into(),
+                    timestamp,
+                    ty,
+                    value: 0.1,
+                };
+
+                for i in 0..5 {
+                    let id = format!("node/node-{i}");
+                    data.push(point(&id, "cpu_current", time, Gauge));
+                }
+
+                // Advance time by 10 seconds
+                time += 10;
+            }
+
+            Ok(ClusterMetrics { data })
+        }
+    }
+
+    fn make_remote_config() -> SectionConfigData<Remote> {
+        let mut order = Vec::new();
+        let mut sections = HashMap::new();
+
+        for i in 0..4 {
+            let status = if i >= 2 { "fail" } else { "pass" };
+            let name = format!("pve-{i}-{status}");
+
+            sections.insert(
+                name.clone(),
+                Remote {
+                    ty: pdm_api_types::remotes::RemoteType::Pve,
+                    id: name.clone(),
+                    nodes: Vec::new(),
+                    authid: Authid::root_auth_id().clone(),
+                    token: "".into(),
+                    web_url: None,
+                },
+            );
+
+            order.push(name);
+        }
+
+        SectionConfigData { sections, order }
+    }
+
+    async fn fake_rrd_task(mut rx: Receiver<RrdStoreRequest>) -> u32 {
+        let mut number_of_requests = 0;
+
+        while let Some(request) = rx.recv().await {
+            number_of_requests += 1;
+
+            match a {
+                RrdStoreRequest::Pve {
+                    remote: _,
+                    metrics,
+                    channel,
+                } => {
+                    let most_recent_timestamp =
+                        metrics.data.iter().fold(0, |acc, e| acc.max(e.timestamp));
+
+                    let _ = channel.send(RrdStoreResult {
+                        most_recent_timestamp,
+                    });
+                }
+                _ => {}
+            }
+        }
+
+        number_of_requests
+    }
+
+    static START: Once = Once::new();
+
+    fn test_init() -> i64 {
+        let now = 10000;
+        START.call_once(|| {
+            // TODO: the client factory is currently stored in a OnceLock -
+            // we can only set it from one test... Ideally we'd like to have the
+            // option to set it in every single test if needed - task/thread local?
+            connection::init(Box::new(TestClientFactory { now }));
+        });
+
+        now
+    }
+
+    #[tokio::test]
+    async fn test_fetch_remotes_updates_state() {
+        // Arrange
+        let now = test_init();
+
+        let (tx, rx) = tokio::sync::mpsc::channel(10);
+        let handle = tokio::task::spawn(fake_rrd_task(rx));
+
+        let settings = CollectionSettings::default();
+        let config = make_remote_config();
+
+        let state_file = NamedTempFile::new(get_create_options()).unwrap();
+        let state = MetricCollectionState::new(state_file.path().into(), get_create_options());
+
+        let (_control_tx, control_rx) = tokio::sync::mpsc::channel(10);
+
+        let mut task = MetricCollectionTask {
+            state,
+            settings,
+            metric_data_tx: tx,
+            control_message_rx: control_rx,
+        };
+
+        // Act
+        task.fetch_remotes(&config, &config.order).await;
+
+        // Assert
+        for remote in config.order {
+            let status = task.state.get_status(&remote).unwrap();
+
+            // Our faked PVE client will return an error if the remote name contains
+            // 'fail'.
+            if remote.contains("fail") {
+                assert!(status
+                    .error
+                    .as_ref()
+                    .unwrap()
+                    .contains("internal server error"));
+                assert_eq!(status.last_collection, None);
+            } else {
+                assert!(now - status.most_recent_datapoint <= 10);
+                assert!(status.error.is_none());
+            }
+        }
+
+        drop(task);
+        assert_eq!(handle.await.unwrap(), 2);
+    }
+}
diff --git a/server/src/metric_collection/state.rs b/server/src/metric_collection/state.rs
index 4e88a45..a9f712d 100644
--- a/server/src/metric_collection/state.rs
+++ b/server/src/metric_collection/state.rs
@@ -89,23 +89,15 @@ impl MetricCollectionState {
 
 #[cfg(test)]
 mod tests {
-    use proxmox_sys::fs::CreateOptions;
-
-    use super::*;
-
+    use crate::metric_collection::collection_task::tests::get_create_options;
     use crate::test_support::temp::NamedTempFile;
 
-    fn get_options() -> CreateOptions {
-        CreateOptions::new()
-            .owner(nix::unistd::Uid::effective())
-            .group(nix::unistd::Gid::effective())
-            .perm(nix::sys::stat::Mode::from_bits_truncate(0o600))
-    }
+    use super::*;
 
     #[test]
     fn save_and_load() -> Result<(), Error> {
-        let file = NamedTempFile::new(get_options())?;
-        let options = get_options();
+        let file = NamedTempFile::new(get_create_options())?;
+        let options = get_create_options();
         let mut state = MetricCollectionState::new(file.path().into(), options.clone());
 
         state.set_status(
-- 
2.39.5



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


  parent reply	other threads:[~2025-02-11 12:06 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-11 12:05 [pdm-devel] [PATCH proxmox-datacenter-manager 00/25] metric collection improvements (concurrency, config, API, CLI) Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 01/25] test support: add NamedTempFile helper Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 02/25] test support: add NamedTempDir helper Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 03/25] pdm-api-types: add CollectionSettings type Lukas Wagner
2025-02-11 14:18   ` Maximiliano Sandoval
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 04/25] pdm-config: add functions for reading/writing metric collection settings Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 05/25] metric collection: split top_entities split into separate module Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 06/25] metric collection: save metric data to RRD in separate task Lukas Wagner
2025-02-12 13:59   ` Wolfgang Bumiller
2025-02-12 14:32     ` Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 07/25] metric collection: rework metric poll task Lukas Wagner
2025-02-11 12:58   ` Lukas Wagner
2025-02-12 15:57   ` Wolfgang Bumiller
2025-02-13 12:31     ` Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 08/25] metric collection: persist state after metric collection Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 09/25] metric collection: skip if last_collection < MIN_COLLECTION_INTERVAL Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 10/25] metric collection: collect overdue metrics on startup/timer change Lukas Wagner
2025-02-13  8:55   ` Wolfgang Bumiller
2025-02-13 13:50     ` Lukas Wagner
2025-02-13 14:19       ` Wolfgang Bumiller
2025-02-13 15:21         ` Lukas Wagner
2025-02-13 15:34           ` Wolfgang Bumiller
2025-02-11 12:05 ` Lukas Wagner [this message]
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 12/25] metric collection: add test for fetch_overdue Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 13/25] metric collection: pass rrd cache instance as function parameter Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 14/25] metric collection: add test for rrd task Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 15/25] metric collection: wrap rrd_cache::Cache in a struct Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 16/25] metric collection: record remote response time in metric database Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 17/25] metric collection: save time needed for collection run to RRD Lukas Wagner
2025-02-13 11:53   ` Wolfgang Bumiller
2025-02-13 12:12     ` Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 18/25] metric collection: periodically clean removed remotes from statefile Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 19/25] api: add endpoint for updating metric collection settings Lukas Wagner
2025-02-13 12:09   ` Wolfgang Bumiller
2025-02-13 12:15     ` Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 20/25] api: add endpoint to trigger metric collection Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 21/25] api: remotes: trigger immediate metric collection for newly added nodes Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 22/25] api: add api for querying metric collection RRD data Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 23/25] api: metric-collection: add status endpoint Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 24/25] pdm-client: add metric collection API methods Lukas Wagner
2025-02-13 12:10   ` Wolfgang Bumiller
2025-02-13 13:52     ` Lukas Wagner
2025-02-11 12:05 ` [pdm-devel] [PATCH proxmox-datacenter-manager 25/25] cli: add commands for metric-collection settings, trigger, status Lukas Wagner
2025-02-13 12:14   ` Wolfgang Bumiller
2025-02-13 14:17     ` Lukas Wagner
2025-02-13 14:56       ` Wolfgang Bumiller
2025-02-13 14:58         ` Lukas Wagner
2025-02-13 15:11           ` Lukas Wagner
2025-02-14 13:08 ` [pdm-devel] [PATCH proxmox-datacenter-manager 00/25] metric collection improvements (concurrency, config, API, CLI) Lukas Wagner

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=20250211120541.163621-12-l.wagner@proxmox.com \
    --to=l.wagner@proxmox.com \
    --cc=pdm-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