Write a test on the build job

This commit is contained in:
Graham Christensen 2018-01-20 08:38:51 -05:00
parent 47f3ba4bef
commit e2696fbe5f
No known key found for this signature in database
GPG key ID: ACA1C1D120C83D5C
2 changed files with 68 additions and 21 deletions

View file

@ -2,7 +2,7 @@ extern crate amqp;
extern crate env_logger; extern crate env_logger;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::slice::Iter;
use std::fs::File; use std::fs::File;
use std::io::BufReader; use std::io::BufReader;
use std::io::BufRead; use std::io::BufRead;
@ -102,7 +102,6 @@ impl<'a, 'b> JobActions<'a, 'b> {
Some("build.log".to_owned()), Some("build.log".to_owned()),
&msg &msg
)); ));
self.tell(worker::Action::Ack);
} }
pub fn build_finished(&mut self, success: bool, lines: Vec<String>) { pub fn build_finished(&mut self, success: bool, lines: Vec<String>) {
@ -233,6 +232,7 @@ mod tests {
use ofborg::message::{Pr,Repo}; use ofborg::message::{Pr,Repo};
use notifyworker::SimpleNotifyWorker; use notifyworker::SimpleNotifyWorker;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
use std::vec::IntoIter;
fn nix() -> nix::Nix { fn nix() -> nix::Nix {
nix::Nix::new("x86_64-linux".to_owned(), "daemon".to_owned(), 1800) 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); 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{ fn make_pr_repo() -> String{
let output = Command::new("./make-pr.sh") let output = Command::new("./make-pr.sh")
.current_dir(tpath("./test-srcs")) .current_dir(tpath("./test-srcs"))
@ -253,23 +281,34 @@ mod tests {
return hash.trim().to_owned(); return hash.trim().to_owned();
} }
fn assert_contains_job(actions: &mut IntoIter<worker::Action>, 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] #[test]
pub fn test_simple_build() { pub fn test_simple_build() {
Command::new("rm") let worker = make_worker();
.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 job = buildjob::BuildJob{ let job = buildjob::BuildJob{
attrs: vec!["success".to_owned()], attrs: vec!["success".to_owned()],
@ -291,8 +330,15 @@ mod tests {
worker.consumer(&job, &mut dummyreceiver); 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));
} }
} }

View file

@ -4,6 +4,7 @@ use amqp::protocol::basic::{Deliver,BasicProperties};
use std::marker::Send; use std::marker::Send;
use serde::Serialize; use serde::Serialize;
use serde_json; use serde_json;
use std::cmp::PartialEq;
pub struct Worker<T: SimpleWorker> { pub struct Worker<T: SimpleWorker> {
internal: T internal: T
@ -14,7 +15,7 @@ pub struct Response {
pub type Actions = Vec<Action>; pub type Actions = Vec<Action>;
#[derive(Debug)] #[derive(Debug,PartialEq)]
pub enum Action { pub enum Action {
Ack, Ack,
NackRequeue, NackRequeue,
@ -22,7 +23,7 @@ pub enum Action {
Publish(QueueMsg), Publish(QueueMsg),
} }
#[derive(Debug)] #[derive(Debug,PartialEq)]
pub struct QueueMsg { pub struct QueueMsg {
pub exchange: Option<String>, pub exchange: Option<String>,
pub routing_key: Option<String>, pub routing_key: Option<String>,