mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-30 00:47:13 +00:00
test(targets,notify,audit): share a builder MockTarget testkit (#6717)
* test(targets): ship a builder MockTarget testkit and retire the in-crate Target mocks Adds crates/targets/src/testkit.rs with a builder-style MockTarget implementing Target<E> for every E: PluginEvent, with orthogonal off-by-default knobs: disabled/active override, health delay plus health-started signal plus a drop-guard counter proving a cancelled probe future was dropped, an init failure budget (usize::MAX = always fail) plus blocking init plus an init counter, a close counter/signal/semaphore gate with a runtime block toggle, a save counter plus save failure budget, caller-supplied store and failed-store handles, and a shared final-failure counter. Clones and clone_dyn share all counters, so an observer clone keeps watching a target after it is boxed into a runtime. The module is gated as #[cfg(any(test, feature = "test-support"))]: in-crate unit tests get it via cfg(test), and downstream test suites opt in through the new off-by-default test-support cargo feature (test-support = [], activating no dependencies). Migrates the five in-crate duplicate mocks onto it: the plugin.rs registry-factory TestTarget, the runtime/adapter.rs lifecycle TestTarget (init/close/store knobs), the runtime/mod.rs TestTarget plus HealthDropGuard (close gating and health-probe tests), the target/mod.rs MoveTestTarget (folded into the test_support helper constructors used by the NATS JetStream failed-store tests), and the target/mod.rs StoreBackedTarget (the default send_from_store purge test; the mock deliberately does not override send_from_store or handle_terminal_failure). The forced init failure now uses TargetError::Initialization instead of the old adapter mock's Configuration; the adapter derives its redacted failure summary from the target id alone, so the migrated assertions are unchanged. Leak guard: testkit unit tests assert the crate manifest still declares default = [] and that test-support = [] stays a pure cfg gate, complementing the compile-level cfg gate that keeps the mock out of production builds. Part of rustfs/backlog#1846 (cluster 3, step 1). * test(notify,audit): migrate the Target mocks onto the shared testkit Retires the hand-written Target mocks in crates/notify and crates/audit in favor of rustfs_targets::testkit::MockTarget: notify's notifier.rs TestTarget/DeferredTestTarget/ClosableTestTarget, lifecycle.rs BlockingInitTarget/RetryInitTarget (rebuilt as observed MockTarget templates cloned by their plugin-descriptor factories, sharing the init signal, close counter, and single-failure init budget across generations), runtime_view.rs TestTarget, runtime_facade.rs TestTarget, and audit's pipeline.rs MockTarget, system.rs TestTarget, registry.rs CloseTestTarget, plus TestTarget and FailingTarget in audit/tests/pipeline_layer_test.rs. Removing the two integration-test mocks also removes their respelled PluginEvent bound (E: Send + Sync + 'static + Clone + Serialize + DeserializeOwned), which the plugin-contract rules require to be spelled only via PluginEvent. lifecycle.rs ReplayTarget stays bespoke on purpose: its generation tags, mpsc observation channels, gated send_raw delivery, and ObservedQueueStore model the replay pipeline itself and would contort a general-purpose mock. The other bespoke mocks named out of scope in PR-3a (ProgrammedTarget, ClassifyingTarget, the ReloadableTargetTls fakes) are likewise untouched. New testkit knobs, each defaulted off and unit-tested: with_id (rename a clone while keeping the shared counters, for factory templates), with_first_save_gate (the first save notifies entered and waits on release; several mocks may share one pair), with_health_gate (is_active waits on a release handle after notifying health_started), with_delivery_snapshot (fixed snapshot overriding the store-derived default), with_close_failures (close-failure budget, default TargetError::Storage) with with_close_failure_error to shape the variant (audit's registry test pins TargetError::Unknown), and an always-on is_enabled call counter exposed as enabled_call_count (notifier's generation tests count dispatcher selections through it). Both crates enable the testkit through a dev-dependency on rustfs-targets with the test-support feature; the feature stays out of default and activates no dependencies, so production builds are unchanged. Part of rustfs/backlog#1846 (cluster 3, step 2).
This commit is contained in:
@@ -960,76 +960,29 @@ pub(crate) fn ensure_rustls_provider_installed() {
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod test_support {
|
||||
use super::{EntityTarget, QueuedPayload, QueuedPayloadMeta};
|
||||
use crate::arn::TargetID;
|
||||
use crate::store::{FailedEventStore, Key, QueueStore, Store};
|
||||
use crate::{StoreError, Target, TargetError};
|
||||
use async_trait::async_trait;
|
||||
use super::{QueuedPayload, QueuedPayloadMeta};
|
||||
use crate::Target;
|
||||
use crate::store::QueueStore;
|
||||
use crate::testkit::MockTarget;
|
||||
use rustfs_s3_types::EventName;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use uuid::Uuid;
|
||||
|
||||
/// A minimal target for failed-store move tests: every delivery method succeeds, the optional
|
||||
/// store backs the store and failed-store accessors, and final failures land on a shared counter.
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct MoveTestTarget {
|
||||
pub(crate) id: TargetID,
|
||||
pub(crate) store: Option<Arc<QueueStore<QueuedPayload>>>,
|
||||
pub(crate) failed: Arc<AtomicU64>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Target<String> for MoveTestTarget {
|
||||
fn id(&self) -> TargetID {
|
||||
self.id.clone()
|
||||
}
|
||||
async fn is_active(&self) -> Result<bool, TargetError> {
|
||||
Ok(true)
|
||||
}
|
||||
async fn save(&self, _event: Arc<EntityTarget<String>>) -> Result<(), TargetError> {
|
||||
Ok(())
|
||||
}
|
||||
async fn send_raw_from_store(&self, _key: Key, _body: Vec<u8>, _meta: QueuedPayloadMeta) -> Result<(), TargetError> {
|
||||
Ok(())
|
||||
}
|
||||
async fn close(&self) -> Result<(), TargetError> {
|
||||
Ok(())
|
||||
}
|
||||
fn store(&self) -> Option<&(dyn Store<QueuedPayload, Error = StoreError, Key = Key> + Send + Sync)> {
|
||||
self.store
|
||||
.as_deref()
|
||||
.map(|store| store as &(dyn Store<_, Error = StoreError, Key = Key> + Send + Sync))
|
||||
}
|
||||
fn failed_store(&self) -> Option<&dyn FailedEventStore> {
|
||||
self.store.as_deref().map(|store| store as &dyn FailedEventStore)
|
||||
}
|
||||
fn clone_dyn(&self) -> Box<dyn Target<String> + Send + Sync> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
fn is_enabled(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn record_final_failure(&self) {
|
||||
self.failed.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
/// A minimal target for failed-store move tests: every delivery method succeeds, no store is
|
||||
/// attached, and final failures land on the mock's shared counter.
|
||||
pub(crate) fn move_test_target() -> Arc<dyn Target<String> + Send + Sync> {
|
||||
Arc::new(MoveTestTarget {
|
||||
id: TargetID::new("target-a".to_string(), "nats".to_string()),
|
||||
store: None,
|
||||
failed: Arc::new(AtomicU64::new(0)),
|
||||
})
|
||||
Arc::new(MockTarget::new("target-a", "nats"))
|
||||
}
|
||||
|
||||
/// Like [`move_test_target`], but the given store backs both the store and failed-store
|
||||
/// accessors, matching a target whose live queue also parks terminal failures.
|
||||
pub(crate) fn move_test_target_with_store(store: Arc<QueueStore<QueuedPayload>>) -> Arc<dyn Target<String> + Send + Sync> {
|
||||
Arc::new(MoveTestTarget {
|
||||
id: TargetID::new("target-a".to_string(), "nats".to_string()),
|
||||
store: Some(store),
|
||||
failed: Arc::new(AtomicU64::new(0)),
|
||||
})
|
||||
Arc::new(
|
||||
MockTarget::new("target-a", "nats")
|
||||
.with_store(store.clone())
|
||||
.with_failed_store(store),
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn failed_store_dir(name: &str) -> PathBuf {
|
||||
@@ -1472,47 +1425,6 @@ mod tests {
|
||||
assert!(dir.starts_with("rustfs-redis-tenant_alpha-"), "unexpected subdir: {dir}");
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct StoreBackedTarget {
|
||||
id: TargetID,
|
||||
store: QueueStore<QueuedPayload>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Target<String> for StoreBackedTarget {
|
||||
fn id(&self) -> TargetID {
|
||||
self.id.clone()
|
||||
}
|
||||
|
||||
async fn is_active(&self) -> Result<bool, TargetError> {
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
async fn save(&self, _event: Arc<EntityTarget<String>>) -> Result<(), TargetError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn send_raw_from_store(&self, _key: Key, _body: Vec<u8>, _meta: QueuedPayloadMeta) -> Result<(), TargetError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn close(&self) -> Result<(), TargetError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn store(&self) -> Option<&(dyn Store<QueuedPayload, Error = StoreError, Key = Key> + Send + Sync)> {
|
||||
Some(&self.store)
|
||||
}
|
||||
|
||||
fn clone_dyn(&self) -> Box<dyn Target<String> + Send + Sync> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn is_enabled(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn send_from_store_purges_missing_or_empty_entry() {
|
||||
let dir = std::env::temp_dir().join(format!("rustfs-send-from-store-{}", Uuid::new_v4()));
|
||||
@@ -1541,10 +1453,10 @@ mod tests {
|
||||
.expect("event file should exist");
|
||||
std::fs::write(&event_file, b"").unwrap();
|
||||
|
||||
let target = StoreBackedTarget {
|
||||
id: TargetID::new("primary".to_string(), "webhook".to_string()),
|
||||
store: store.clone(),
|
||||
};
|
||||
// The default send_from_store implementation is under test here, so the mock must not
|
||||
// override it; it only supplies the backing store.
|
||||
let target: Box<dyn Target<String> + Send + Sync> =
|
||||
Box::new(crate::testkit::MockTarget::new("primary", "webhook").with_store(Arc::new(store.clone())));
|
||||
|
||||
// A NotFound/empty entry must be purged (index + file) rather than
|
||||
// silently skipped and replayed forever.
|
||||
|
||||
Reference in New Issue
Block a user