From: Lukas Wagner <l.wagner@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [PATCH datacenter-manager v2 14/20] tests: add helpers for building API-handler-level integration tests
Date: Thu, 20 Aug 2026 16:52:14 +0200 [thread overview]
Message-ID: <20260820145220.418032-15-l.wagner@proxmox.com> (raw)
In-Reply-To: <20260820145220.418032-1-l.wagner@proxmox.com>
Unfortunately, some subsystems still require static initialization,
namely access control and worker tasks. For those we need a setup
function that is ensure to be only called once in each test binary.
Worker tasks especially require a directory in which it can place task
logs. For this we create a temporary directory, which we clean up using
libc::atexit. Unfortunately, tempfile::TempDir does not work for this
case, since we'd need to put it in a static variable, and `drop` is
never called for those.
TestApplication is essentially a builder that can be used to produce a
PdmApplication suitable for tests.
Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
Notes:
Changes since v1:
- don't .unwrap() in read_captured_responses
server/tests/common/environment.rs | 25 +++
server/tests/common/mod.rs | 62 +++++
server/tests/common/test_application.rs | 287 ++++++++++++++++++++++++
3 files changed, 374 insertions(+)
create mode 100644 server/tests/common/environment.rs
create mode 100644 server/tests/common/mod.rs
create mode 100644 server/tests/common/test_application.rs
diff --git a/server/tests/common/environment.rs b/server/tests/common/environment.rs
new file mode 100644
index 00000000..5dad7b21
--- /dev/null
+++ b/server/tests/common/environment.rs
@@ -0,0 +1,25 @@
+use proxmox_router::RpcEnvironment;
+
+pub struct TestRpcEnvironment;
+
+impl RpcEnvironment for TestRpcEnvironment {
+ fn result_attrib_mut(&mut self) -> &mut serde_json::Value {
+ unimplemented!()
+ }
+
+ fn result_attrib(&self) -> &serde_json::Value {
+ unimplemented!()
+ }
+
+ fn env_type(&self) -> proxmox_router::RpcEnvironmentType {
+ unimplemented!()
+ }
+
+ fn set_auth_id(&mut self, _user: Option<String>) {
+ unimplemented!()
+ }
+
+ fn get_auth_id(&self) -> Option<String> {
+ Some("root@pam".to_string())
+ }
+}
diff --git a/server/tests/common/mod.rs b/server/tests/common/mod.rs
new file mode 100644
index 00000000..07a07208
--- /dev/null
+++ b/server/tests/common/mod.rs
@@ -0,0 +1,62 @@
+use std::{
+ path::{Path, PathBuf},
+ sync::{Once, OnceLock},
+};
+
+use anyhow::Context;
+use serde::de::DeserializeOwned;
+
+use proxmox_sys::fs::CreateOptions;
+
+mod environment;
+mod test_application;
+
+pub use environment::TestRpcEnvironment;
+pub use test_application::*;
+
+/// Read and deserialize a previously captured API response.
+pub async fn read_captured_response<T: DeserializeOwned, P: AsRef<Path>>(
+ path: P,
+) -> Result<T, proxmox_client::Error> {
+ let s = tokio::fs::read_to_string(path.as_ref())
+ .await
+ .with_context(|| format!("could not read from {path}", path = path.as_ref().display()))
+ .map_err(proxmox_client::Error::Anyhow)?;
+
+ serde_json::from_str(&s).map_err(|e| proxmox_client::Error::Anyhow(e.into()))
+}
+
+static STATIC_TEMP_DIR: OnceLock<PathBuf> = OnceLock::new();
+
+extern "C" fn cleanup() {
+ if let Some(dir) = STATIC_TEMP_DIR.get() {
+ let _ = std::fs::remove_dir_all(dir);
+ }
+}
+
+/// Run commonly-used setup steps that must only run *once*.
+///
+/// Be sure to call this at the start of every test.
+pub fn test_setup() {
+ static INIT: Once = Once::new();
+
+ INIT.call_once(|| {
+ let file_opts = CreateOptions::new();
+
+ let dir = proxmox_sys::fs::make_tmp_dir("/tmp", None).unwrap();
+ STATIC_TEMP_DIR.set(dir.clone()).unwrap();
+
+ proxmox_rest_server::init_worker_tasks(dir.clone(), file_opts).unwrap();
+ proxmox_access_control::init::init(&pdm_api_types::AccessControlConfig, dir)
+ .expect("failed to setup access control config");
+
+ unsafe {
+ libc::atexit(cleanup);
+ }
+ });
+}
+
+/// Create a [`TestRpcEnvironment`] for the use in a test.
+pub fn rpcenv() -> TestRpcEnvironment {
+ TestRpcEnvironment
+}
diff --git a/server/tests/common/test_application.rs b/server/tests/common/test_application.rs
new file mode 100644
index 00000000..8d21b261
--- /dev/null
+++ b/server/tests/common/test_application.rs
@@ -0,0 +1,287 @@
+use std::collections::HashMap;
+use std::sync::Arc;
+use std::sync::Mutex;
+
+use anyhow::{Context, Error, bail};
+use serde::{Serialize, de::DeserializeOwned};
+
+use nix::unistd::User;
+use proxmox_client::Client;
+use proxmox_section_config::typed::SectionConfigData;
+
+use pbs_api_types::Authid;
+use pdm_api_types::{
+ ConfigDigest,
+ remotes::{Remote, RemoteType},
+};
+
+use pdm_config::remotes::RemoteConfig;
+
+use server::context::product_config::ProductConfig;
+use server::{
+ connection::{ClientFactory, PveClient},
+ context::ContextFactory,
+ pbs_client::PbsClient,
+};
+
+/// Shared, clonable storage for arbitrary data recorded by a [`PveTestRemote`]'s client
+/// implementation.
+#[derive(Clone, Default)]
+pub struct TestRemoteState(Arc<Mutex<HashMap<String, serde_json::Value>>>);
+
+impl TestRemoteState {
+ /// Record `value` under `key`, overwriting any previous value stored there.
+ pub fn set<T: Serialize>(&self, key: impl Into<String>, value: &T) {
+ let value = serde_json::to_value(value).expect("failed to serialize test state value");
+ self.0.lock().unwrap().insert(key.into(), value);
+ }
+
+ /// Retrieve the value previously stored under `key`, if any.
+ pub fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T> {
+ self.0.lock().unwrap().get(key).cloned().map(|value| {
+ serde_json::from_value(value).expect("failed to deserialize test state value")
+ })
+ }
+}
+
+/// A PVE remote registered in a [`TestApplication`], passed to the client factory whenever a
+/// client for it is created.
+#[derive(Clone)]
+pub struct PveTestRemote {
+ pub name: String,
+ pub nodes: u32,
+ pub state: TestRemoteState,
+}
+
+#[derive(Clone)]
+struct PveTestRemoteWithClientMaker {
+ remote: PveTestRemote,
+ make_client: Arc<dyn Fn(PveTestRemote) -> Result<Arc<PveClient>, Error> + Send + Sync>,
+}
+
+impl PveTestRemote {
+ /// Build the [`Remote`] configuration entry describing this test remote.
+ pub fn to_remote(&self) -> Remote {
+ Remote {
+ ty: RemoteType::Pve,
+ id: self.name.clone(),
+ nodes: Vec::new(),
+ authid: Authid::root_auth_id().clone(),
+ token: "".into(),
+ web_url: None,
+ }
+ }
+}
+
+/// A [`ContextFactory`] implementation for tests, backed by mocked remotes and a temporary
+/// directory for config, caches and runtime directories.
+///
+/// Make sure to *not* drops this while running the test cast, otherwise the temporary directory
+/// will be dropped.
+#[derive(Clone)]
+pub struct TestApplication {
+ pve_remotes: HashMap<String, PveTestRemoteWithClientMaker>,
+ base_dir: Arc<tempfile::TempDir>,
+}
+
+impl Default for TestApplication {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl TestApplication {
+ /// Create a test application without any registered remotes.
+ pub fn new() -> Self {
+ Self {
+ pve_remotes: HashMap::new(),
+ base_dir: Arc::new(tempfile::tempdir().expect("could not create temporary directory")),
+ }
+ }
+
+ /// Register a PVE remote called `name`, using `f` to create its client implementation.
+ ///
+ /// The client is constructed for every request, so any data that should outlive a single
+ /// client has to be kept in `state`.
+ pub fn with_pve_remote<S, C, F>(mut self, name: S, state: TestRemoteState, f: F) -> Self
+ where
+ S: Into<String>,
+ C: pve_api_types::client::PveClient + Send + Sync + 'static,
+ F: Fn(PveTestRemote) -> C + Send + Sync + 'static,
+ {
+ let name = name.into();
+ let make_client = Arc::new(move |remote| Ok(Arc::new(f(remote)) as Arc<PveClient>));
+
+ self.pve_remotes.insert(
+ name.clone(),
+ PveTestRemoteWithClientMaker {
+ make_client,
+ remote: PveTestRemote {
+ name,
+ nodes: 1,
+ state,
+ },
+ },
+ );
+
+ self
+ }
+}
+
+impl ContextFactory for TestApplication {
+ fn make_client_factory(&self) -> Result<Arc<dyn ClientFactory + Send + Sync>, Error> {
+ Ok(Arc::new(self.clone()))
+ }
+
+ fn make_remote_config(&self) -> Result<Box<dyn RemoteConfig + Send + Sync>, Error> {
+ Ok(Box::new(self.clone()))
+ }
+
+ fn make_product_config(&self) -> Result<ProductConfig, Error> {
+ let user = User::from_uid(nix::unistd::getuid())
+ .ok()
+ .flatten()
+ .context("could not look up user")?;
+
+ let base = self.base_dir.path();
+
+ let config_dir = base.join("config");
+ let state_dir = base.join("state");
+ let run_dir = base.join("run");
+ let cache_dir = base.join("cache");
+
+ std::fs::create_dir(&config_dir)?;
+ std::fs::create_dir(&state_dir)?;
+ std::fs::create_dir(&run_dir)?;
+ std::fs::create_dir(&cache_dir)?;
+
+ ProductConfig::builder()
+ .api_user(user.clone())
+ .priv_user(user)
+ .config_dir(config_dir)
+ .state_dir(state_dir)
+ .run_dir(run_dir)
+ .cache_dir(cache_dir)
+ .build()
+ }
+
+ // Override any other methods here if needed.
+}
+
+impl RemoteConfig for TestApplication {
+ fn read(&self) -> Result<(SectionConfigData<Remote>, ConfigDigest), anyhow::Error> {
+ let mut sections = SectionConfigData::default();
+
+ for pve_remote in self.pve_remotes.values() {
+ sections.insert(
+ pve_remote.remote.name.clone(),
+ pve_remote.remote.to_remote(),
+ );
+ }
+
+ Ok((sections, ConfigDigest::from_slice([])))
+ }
+
+ fn read_secret_token(
+ &self,
+ _remote: &pdm_api_types::remotes::Remote,
+ ) -> Result<String, anyhow::Error> {
+ unimplemented!()
+ }
+
+ fn lock(&self) -> Result<proxmox_product_config::ApiLockGuard, anyhow::Error> {
+ unimplemented!()
+ }
+
+ fn write(
+ &self,
+ _remotes: proxmox_section_config::typed::SectionConfigData<pdm_api_types::remotes::Remote>,
+ ) -> Result<(), anyhow::Error> {
+ unimplemented!()
+ }
+}
+
+#[async_trait::async_trait]
+impl ClientFactory for TestApplication {
+ fn make_pve_client(&self, remote: &Remote) -> Result<Arc<PveClient>, Error> {
+ if let Some(a) = self.pve_remotes.get(&remote.id) {
+ (a.make_client)(a.remote.clone())
+ } else {
+ bail!("remote not registered")
+ }
+ }
+
+ fn make_pve_client_with_endpoint(
+ &self,
+ _remote: &Remote,
+ _target_endpoint: Option<&str>,
+ ) -> Result<Arc<PveClient>, Error> {
+ bail!("not implemented")
+ }
+
+ fn make_pbs_client(&self, _remote: &Remote) -> Result<Box<PbsClient>, Error> {
+ bail!("not implemented")
+ }
+
+ fn make_raw_client(&self, _remote: &Remote) -> Result<Box<proxmox_client::Client>, Error> {
+ bail!("not implemented")
+ }
+
+ async fn make_pve_client_and_login(&self, _remote: &Remote) -> Result<Arc<PveClient>, Error> {
+ bail!("not implemented")
+ }
+
+ async fn make_pbs_client_and_login(
+ &self,
+ _remote: &Remote,
+ ) -> Result<Box<PbsClient<Client>>, Error> {
+ bail!("not implemented")
+ }
+}
+
+macro_rules! test_pve_client {
+ ($ty:ident { $($overrides:tt)* }) => {
+
+ pub struct $ty(crate::common::PveTestRemote);
+
+ #[async_trait::async_trait]
+ impl pve_api_types::client::PveClient for $ty {
+ async fn list_nodes(
+ &self,
+ ) -> Result<Vec<pve_api_types::ClusterNodeIndexResponse>, proxmox_client::Error> {
+ let mut nodes = Vec::new();
+
+ for i in 0..self.0.nodes {
+ nodes.push(ClusterNodeIndexResponse {
+ cpu: Some(0.0),
+ level: None,
+ maxcpu: Some(4),
+ maxmem: Some(4096),
+ mem: Some(1000),
+ node: format!("{}-node-{i}", self.0.name),
+ ssl_fingerprint: None,
+ status: pve_api_types::ClusterNodeIndexResponseStatus::Online,
+ uptime: Some(1000),
+ });
+ }
+
+ Ok(nodes)
+ }
+ $($overrides)*
+ }
+
+ impl $ty {
+ /// Name of the remote this client was created for.
+ pub fn remote(&self) -> &str {
+ &self.0.name
+ }
+
+ /// Shared state of the remote this client was created for.
+ pub fn state(&self) -> crate::common::TestRemoteState {
+ self.0.state.clone()
+ }
+ }
+ };
+}
+
+pub(crate) use test_pve_client;
--
2.47.3
next prev parent reply other threads:[~2026-08-20 14:53 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-20 14:52 [PATCH datacenter-manager/proxmox v2 00/20] inject application context via API macro for easier integration testing Lukas Wagner
2026-08-20 14:52 ` [PATCH proxmox v2 01/20] router: introduce shared state Lukas Wagner
2026-08-20 14:52 ` [PATCH proxmox v2 02/20] rest-server: allow to inject " Lukas Wagner
2026-08-20 14:52 ` [PATCH proxmox v2 03/20] api-macro: support shared state extraction type Lukas Wagner
2026-08-20 14:52 ` [PATCH datacenter-manager v2 04/20] context: promote context to a dir-style module Lukas Wagner
2026-08-20 14:52 ` [PATCH datacenter-manager v2 05/20] pdm-config: remotes: rename trait methods to read/write/lock Lukas Wagner
2026-08-20 14:52 ` [PATCH datacenter-manager v2 06/20] pdm-config: subscriptions: " Lukas Wagner
2026-08-20 14:52 ` [PATCH datacenter-manager v2 07/20] remote iterator: pass remote config reader explicitly Lukas Wagner
2026-08-20 14:52 ` [PATCH datacenter-manager v2 08/20] context: introduce a ContextFactory to build application context Lukas Wagner
2026-08-20 14:52 ` [PATCH datacenter-manager v2 09/20] context: establish PdmApplication object Lukas Wagner
2026-08-21 9:52 ` Thomas Ellmenreich
2026-08-21 12:26 ` Lukas Wagner
2026-08-20 14:52 ` [PATCH datacenter-manager v2 10/20] context: register PdmApplication in router Lukas Wagner
2026-08-20 14:52 ` [PATCH datacenter-manager v2 11/20] parallel fetcher: pass arguments to closure in a single type Lukas Wagner
2026-08-20 14:52 ` [PATCH datacenter-manager v2 12/20] parallel fetcher: support a custom client factory Lukas Wagner
2026-08-20 14:52 ` [PATCH datacenter-manager v2 13/20] api: sdn: use PdmApplication handle for accessing remotes Lukas Wagner
2026-08-20 14:52 ` Lukas Wagner [this message]
2026-08-21 9:57 ` [PATCH datacenter-manager v2 14/20] tests: add helpers for building API-handler-level integration tests Thomas Ellmenreich
2026-08-21 12:25 ` Lukas Wagner
2026-08-20 14:52 ` [PATCH datacenter-manager v2 15/20] tests: add example tests for SDN API routes Lukas Wagner
2026-08-20 14:52 ` [PATCH datacenter-manager v2 16/20] api-cache: add wrapper type Lukas Wagner
2026-08-20 14:52 ` [PATCH datacenter-manager v2 17/20] context: provide api-cache on the app object Lukas Wagner
2026-08-20 14:52 ` [PATCH datacenter-manager v2 18/20] api: subscriptions: use PdmApplication instead of globals Lukas Wagner
2026-08-20 14:52 ` [PATCH datacenter-manager v2 19/20] pdm-config: subscriptions: drop unused accessor functions Lukas Wagner
2026-08-20 14:52 ` [PATCH datacenter-manager v2 20/20] tests: add example tests for remote subscription management 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=20260820145220.418032-15-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.