From e2696fbe5f3fe716c10dbe1718f401363f4f4cbe Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Sat, 20 Jan 2018 08:38:51 -0500 Subject: [PATCH] Write a test on the build job --- ofborg/src/tasks/build.rs | 84 ++++++++++++++++++++++++++++++--------- ofborg/src/worker.rs | 5 ++- 2 files changed, 68 insertions(+), 21 deletions(-) diff --git a/ofborg/src/tasks/build.rs b/ofborg/src/tasks/build.rs index d02946e..c8a1f93 100644 --- a/ofborg/src/tasks/build.rs +++ b/ofborg/src/tasks/build.rs @@ -2,7 +2,7 @@ extern crate amqp; extern crate env_logger; use std::collections::VecDeque; - +use std::slice::Iter; use std::fs::File; use std::io::BufReader; use std::io::BufRead; @@ -102,7 +102,6 @@ impl<'a, 'b> JobActions<'a, 'b> { Some("build.log".to_owned()), &msg )); - self.tell(worker::Action::Ack); } pub fn build_finished(&mut self, success: bool, lines: Vec) { @@ -233,6 +232,7 @@ mod tests { use ofborg::message::{Pr,Repo}; use notifyworker::SimpleNotifyWorker; use std::process::{Command, Stdio}; + use std::vec::IntoIter; fn nix() -> nix::Nix { nix::Nix::new("x86_64-linux".to_owned(), "daemon".to_owned(), 1800) @@ -242,6 +242,34 @@ mod tests { return Path::new(env!("CARGO_MANIFEST_DIR")).join(component); } + fn scratch_dir() -> PathBuf { + tpath("./test-scratch") + } + + fn cleanup_scratch() { + Command::new("rm") + .arg("-rf") + .arg(&scratch_dir()) + .status() + .expect("cleanup of test-scratch should work"); + } + + fn make_worker() -> BuildWorker { + cleanup_scratch(); + + // pub fn new(cloner: checkout::CachedCloner, nix: nix::Nix, system: String, identity: String) -> BuildWorker { + let cloner = checkout::cached_cloner(&scratch_dir()); + let nix = nix(); + let worker = BuildWorker::new( + cloner, + nix, + "x86_64-linux".to_owned(), + "cargo-test-build".to_owned() + ); + + return worker; + } + fn make_pr_repo() -> String{ let output = Command::new("./make-pr.sh") .current_dir(tpath("./test-srcs")) @@ -253,23 +281,34 @@ mod tests { return hash.trim().to_owned(); } + fn assert_contains_job(actions: &mut IntoIter, text_to_match: &str) { + println!("\n\nSearching for {:?}", text_to_match); + actions.position(|job| + match job { + worker::Action::Publish(ref body) => { + let mystr = String::from_utf8(body.content.clone()).unwrap(); + if mystr.contains(text_to_match) { + println!(" Matched: {:?}", mystr); + return true; + } else { + println!(" miss: {:?}", mystr); + return false; + } + } + e => { + println!(" notPublish: {:?}", e); + return false; + } + } + ).expect( + &format!("Actions should contain a job matching {:?}, after the previous check", + text_to_match) + ); + } + #[test] pub fn test_simple_build() { - Command::new("rm") - .arg("-rf") - .arg(&tpath("./test-scratch")) - .status() - .expect("cleanup of test-scratch should work"); - - // pub fn new(cloner: checkout::CachedCloner, nix: nix::Nix, system: String, identity: String) -> BuildWorker { - let cloner = checkout::cached_cloner(&tpath("./test-scratch")); - let nix = nix(); - let worker = BuildWorker::new( - cloner, - nix, - "x86_64-linux".to_owned(), - "cargo-test-build".to_owned() - ); + let worker = make_worker(); let job = buildjob::BuildJob{ attrs: vec!["success".to_owned()], @@ -291,8 +330,15 @@ mod tests { worker.consumer(&job, &mut dummyreceiver); - let actions = dummyreceiver.actions.iter(); + println!("Total actions: {:?}", dummyreceiver.actions.len()); + let mut actions = dummyreceiver.actions.into_iter(); - println!("{:?}", actions); + assert_contains_job(&mut actions, "output\":\"hi"); + assert_contains_job(&mut actions, "output\":\"1"); + assert_contains_job(&mut actions, "output\":\"2"); + assert_contains_job(&mut actions, "output\":\"3"); + assert_contains_job(&mut actions, "output\":\"4"); + assert_contains_job(&mut actions, "success\":true"); + assert_eq!(actions.next(), Some(worker::Action::Ack)); } } diff --git a/ofborg/src/worker.rs b/ofborg/src/worker.rs index e1e8070..c8c9e83 100644 --- a/ofborg/src/worker.rs +++ b/ofborg/src/worker.rs @@ -4,6 +4,7 @@ use amqp::protocol::basic::{Deliver,BasicProperties}; use std::marker::Send; use serde::Serialize; use serde_json; +use std::cmp::PartialEq; pub struct Worker { internal: T @@ -14,7 +15,7 @@ pub struct Response { pub type Actions = Vec; -#[derive(Debug)] +#[derive(Debug,PartialEq)] pub enum Action { Ack, NackRequeue, @@ -22,7 +23,7 @@ pub enum Action { Publish(QueueMsg), } -#[derive(Debug)] +#[derive(Debug,PartialEq)] pub struct QueueMsg { pub exchange: Option, pub routing_key: Option,