summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Samuelsson <msamuelsson@storvix.eu>2023-05-28 14:09:02 +0200
committerMartin Samuelsson <msamuelsson@storvix.eu>2023-05-28 23:16:59 +0200
commitf185b97d60f76a025f55429a0d1711c39568e187 (patch)
tree7100b11e56db1b30b728027d952664aba79641fd
parent421aedc55865ed9075f4e2df1dfbb71e7987424a (diff)
downloadlibsyslog-rs-f185b97d60f76a025f55429a0d1711c39568e187.zip
Add unit tests and repair doc-tests
-rw-r--r--libsyslog/src/lib.rs7
-rw-r--r--libsyslog/src/tests.rs35
2 files changed, 41 insertions, 1 deletions
diff --git a/libsyslog/src/lib.rs b/libsyslog/src/lib.rs
index 904f3ea..7c4d942 100644
--- a/libsyslog/src/lib.rs
+++ b/libsyslog/src/lib.rs
@@ -13,7 +13,8 @@
//! Typical use: (A.k.a. recommended snippet to copy'n'paste to the start of
//! `main()` of your daemon.)
//! ```
-//! # fn main() -> Result<()> {
+//! # use log::error;
+//! # fn main() -> Result<(), log::SetLoggerError> {
//! libsyslog::Syslog::builder()
//! .module_level(std::module_path!(), log::LevelFilter::Info)
//! .level(log::LevelFilter::Off)
@@ -21,6 +22,7 @@
//! .init()?;
//!
//! error!("Hello libsyslog. I've failed you.");
+//! # Ok(())
//! # }
//! ```
//!
@@ -56,6 +58,9 @@ mod facility;
mod logopt;
mod syslog;
+#[cfg(test)]
+mod tests;
+
#[cfg(feature = "bitflags")]
pub use logopt::*;
diff --git a/libsyslog/src/tests.rs b/libsyslog/src/tests.rs
new file mode 100644
index 0000000..844641e
--- /dev/null
+++ b/libsyslog/src/tests.rs
@@ -0,0 +1,35 @@
+use crate::Syslog;
+
+#[test]
+fn call_init_twice() {
+ // 0. First test that a first call to initialize works.
+ if Syslog::builder().build().init().is_err() {
+ panic!("First request to initialize Syslog failed.");
+ }
+
+ // 1. Then assure that another attempt fails, as there can only be one logger.
+ //
+ // Reminder to self: Since this init() already returns a Result, there is no real need to
+ // deal with implementing a separate check with `std::sync::Once`. The log crate practically
+ // covers our ass here.
+ if Syslog::builder().build().init().is_ok() {
+ panic!("Second request to initialize Syslog unexpectedly succeeded.");
+ }
+}
+
+// NOTE It would be neat to have higher test coverage, including some of the actual logging, but
+// the question is how to obtain that in a meaningful way. Stubbing out `libsyslog-sys` would
+// make the tests mostly pointless, right? Targetting the host's `/dev/log` through `syslog()`
+// practically leads to nothing but unverifiable side-effects. Running with a custom syslogd
+// look-a-like, in a chroot or by other conceivable means, would require priviledges not
+// suitable for unit testing.
+//
+// On a more practical level, multiple tests can not run in threads of the same test runner
+// due to the singleton nature of Syslog, plus the fact that the foreign functions openlog()
+// and closelog() should really only be called once per process.
+//
+// There is https://github.com/rust-lang/rust/issues/47506 which points out this problem, but
+// the closest to a solution mentioned is a recommendation to use the `rusty-fork` crate. A
+// crate that seems to need a bit more love than anyone appears to be able to give it. By the
+// look of things, https://github.com/AltSysrq/rusty-fork/pull/10 is approaching three years
+// of waiting in a believed-to-be-mergable state.