Use a p2p dbus connection during tests

This removes the requirement of having the DBus daemon running locally,
which is particularly helpful e.g. in a container or for CI.
This commit is contained in:
Matteo Settenvini 2022-09-12 00:38:01 +02:00
parent 256267c213
commit 20072cf1ea
Signed by: matteo
GPG key ID: 8576CC1AD97D42DF
5 changed files with 101 additions and 64 deletions

View file

@ -36,14 +36,30 @@ pub async fn restrictions_for(user: Uid) -> anyhow::Result<Vec<Restriction>, any
#[cfg(feature = "integration_test")]
let proxy = {
use tokio::net::UnixStream;
// During integration testing, we want to connect to a private
// bus name to avoid clashes with existing system services.
let connection = zbus::Connection::session().await?;
let dbus_name = std::env::var("TEST_DBUS_SERVICE_NAME")
.expect("The test has not set the TEST_DBUS_SERVICE_NAME environment variable to the private bus name prior to attempting name resolution");
let socket_path = std::env::var("TEST_DBUS_SOCKET")
.expect("The test has not set the TEST_DBUS_SOCKET environment variable to the unix socket to connect to");
let socket = loop {
match UnixStream::connect(&socket_path).await {
Ok(stream) => break stream,
Err(e) if e.kind() == std::io::ErrorKind::ConnectionRefused => {
tokio::task::yield_now().await;
continue;
}
Err(e) => anyhow::bail!(e),
}
};
let connection = zbus::ConnectionBuilder::unix_stream(socket)
.p2p()
.build()
.await?;
MalcontentDnsProxy::builder(&connection)
.destination(zbus_names::UniqueName::try_from(dbus_name).unwrap())
.unwrap()
.build()
.await
.expect("Unable to build DBus proxy object")