summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2024-02-02 17:31:53 +0200
committerLars Wirzenius <liw@liw.fi>2024-02-02 17:39:30 +0200
commitc468331c10aee5b848230c38799a88685bf1936d (patch)
tree6fba53d17d62f6a3bbc4a37f16fc802bb677f505
parentfbd1911e2fa6d97e8efa66afb14b162c00d59700 (diff)
downloadradicle-ci-broker-c468331c10aee5b848230c38799a88685bf1936d.tar.gz
feat(src/test.rs): add module with test helpers
Signed-off-by: Lars Wirzenius <liw@liw.fi>
-rw-r--r--src/test.rs93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/test.rs b/src/test.rs
new file mode 100644
index 0000000..f1e1bdd
--- /dev/null
+++ b/src/test.rs
@@ -0,0 +1,93 @@
+use std::{
+ fs::{metadata, set_permissions, write},
+ os::unix::fs::PermissionsExt,
+ path::Path,
+};
+
+use crate::adapter::Adapter;
+use crate::event::BrokerEvent;
+use crate::msg::Request;
+use radicle::crypto::ssh::Keystore;
+use radicle::crypto::test::signer::MockSigner;
+use radicle::crypto::Signer;
+use radicle::git::RefString;
+use radicle::profile::{Config, Home};
+use radicle::storage::ReadRepository;
+use radicle::test::setup::Node;
+use radicle::Profile;
+
+use tempfile::{tempdir, TempDir};
+
+pub type TestResult<T> = Result<T, Box<dyn std::error::Error>>;
+
+pub struct MockNode {
+ // This field must exist while the mock node exists.
+ #[allow(dead_code)]
+ tmp: TempDir,
+
+ alias: String,
+ node: Node,
+}
+
+impl MockNode {
+ pub fn new() -> TestResult<Self> {
+ let tmp = tempdir()?;
+ let signer = MockSigner::default();
+ let alias = "broker_test_alias".to_string();
+ let node = Node::new(tmp.path(), signer, &alias);
+ Ok(Self { tmp, alias, node })
+ }
+
+ pub fn alias(&self) -> &str {
+ &self.alias
+ }
+
+ pub fn node(&self) -> &Node {
+ &self.node
+ }
+
+ pub fn profile(&self) -> Result<Profile, Box<dyn std::error::Error>> {
+ let node = self.node();
+ Ok(Profile {
+ home: Home::new(node.root.clone())?,
+ storage: node.storage.clone(),
+ keystore: Keystore::new(&node.root),
+ public_key: node.signer.public_key().to_owned(),
+ config: Config::new(self.alias().parse()?),
+ })
+ }
+}
+
+pub fn trigger_request() -> TestResult<Request> {
+ let mock_node = MockNode::new()?;
+ let profile = mock_node.profile()?;
+ let node = mock_node.node();
+
+ let project = node.project();
+ let repo_head = project.repo.head()?.1;
+ let cmt =
+ radicle::test::fixtures::commit("my test commit", &[repo_head.into()], &project.backend);
+
+ let be = BrokerEvent::RefChanged {
+ rid: project.id,
+ name: RefString::try_from(
+ "refs/namespaces/$nid/refs/heads/master".replace("$nid", &profile.id().to_string()),
+ )?,
+ oid: cmt,
+ old: Some(repo_head),
+ };
+
+ Ok(Request::trigger(&profile, &be)?)
+}
+
+pub fn mock_adapter(filename: &Path, script: &str) -> TestResult<Adapter> {
+ const EXECUTABLE: u32 = 0o100;
+
+ write(filename, script)?;
+ let meta = metadata(filename)?;
+ let mut perms = meta.permissions();
+ perms.set_mode(perms.mode() | EXECUTABLE);
+ set_permissions(filename, perms)?;
+
+ Ok(Adapter::new(filename))
+}