Merge pull request #517 from drager/binary-install-tests
test(binaryinstall): Add initial testsmaster
commit
0c452156d1
@ -0,0 +1,142 @@ |
||||
use binary_install::Cache; |
||||
use std::path::Path; |
||||
use utils; |
||||
|
||||
#[test] |
||||
fn it_returns_none_if_install_is_not_permitted() { |
||||
let binary_name = "wasm-pack"; |
||||
let binaries = vec![binary_name]; |
||||
|
||||
let dir = tempfile::TempDir::new().unwrap(); |
||||
let cache = Cache::at(dir.path()); |
||||
|
||||
let dl = cache.download( |
||||
false, |
||||
binary_name, |
||||
&binaries, |
||||
&format!("{}/{}.tar.gz", "", binary_name), |
||||
); |
||||
|
||||
assert!(dl.is_ok()); |
||||
assert!(dl.unwrap().is_none()) |
||||
} |
||||
|
||||
#[test] |
||||
fn it_downloads_tarball() { |
||||
let binary_name = "wasm-pack"; |
||||
let binaries = vec![binary_name]; |
||||
|
||||
// Create a temporary tarball.
|
||||
let tarball = utils::create_tarball(binary_name).ok(); |
||||
|
||||
// Spin up a local TcpListener.
|
||||
let server_port = utils::start_server(tarball, None).recv().unwrap(); |
||||
|
||||
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port); |
||||
|
||||
let dir = tempfile::TempDir::new().unwrap(); |
||||
let cache = Cache::at(dir.path()); |
||||
|
||||
let dl = cache.download( |
||||
true, |
||||
binary_name, |
||||
&binaries, |
||||
&format!("{}/{}.tar.gz", &url, binary_name), |
||||
); |
||||
|
||||
assert!(dl.is_ok()); |
||||
assert!(dl.unwrap().is_some()) |
||||
} |
||||
|
||||
#[test] |
||||
fn it_returns_error_when_it_failed_to_download() { |
||||
let server_port = 7881; |
||||
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port); |
||||
let binary_name = "wasm-pack"; |
||||
let binaries = vec![binary_name]; |
||||
|
||||
let dir = tempfile::TempDir::new().unwrap(); |
||||
let cache = Cache::at(dir.path()); |
||||
let full_url = &format!("{}/{}.tar.gz", &url, binary_name); |
||||
|
||||
let dl = cache.download(true, binary_name, &binaries, full_url); |
||||
|
||||
assert!(dl.is_err()); |
||||
assert_eq!( |
||||
&format!("failed to download from {}", full_url), |
||||
&format!("{}", dl.unwrap_err()) |
||||
); |
||||
} |
||||
|
||||
#[test] |
||||
fn it_returns_error_when_it_failed_to_extract_tarball() { |
||||
let binary_name = "wasm-pack"; |
||||
let binaries = vec![binary_name]; |
||||
|
||||
let dir = tempfile::TempDir::new().unwrap(); |
||||
let cache = Cache::at(dir.path()); |
||||
|
||||
// Spin up a local TcpListener.
|
||||
let server_port = utils::start_server(None, None).recv().unwrap(); |
||||
|
||||
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port); |
||||
let full_url = &format!("{}/{}.tar.gz", &url, binary_name); |
||||
|
||||
let dl = cache.download(true, binary_name, &binaries, full_url); |
||||
|
||||
assert!(dl.is_err()); |
||||
assert_eq!( |
||||
&format!("failed to extract tarball from {}", full_url), |
||||
&format!("{}", dl.unwrap_err()) |
||||
); |
||||
} |
||||
|
||||
#[test] |
||||
fn it_returns_error_when_it_failed_to_extract_zip() { |
||||
let binary_name = "wasm-pack"; |
||||
let binaries = vec![binary_name]; |
||||
|
||||
let dir = tempfile::TempDir::new().unwrap(); |
||||
let cache = Cache::at(dir.path()); |
||||
|
||||
// Spin up a local TcpListener.
|
||||
let server_port = utils::start_server(None, None).recv().unwrap(); |
||||
|
||||
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port); |
||||
let full_url = &format!("{}/{}.zip", &url, binary_name); |
||||
|
||||
let dl = cache.download(true, binary_name, &binaries, full_url); |
||||
|
||||
assert!(dl.is_err()); |
||||
assert_eq!( |
||||
&format!("failed to extract zip from {}", full_url), |
||||
&format!("{}", dl.unwrap_err()) |
||||
); |
||||
} |
||||
|
||||
#[test] |
||||
#[should_panic(expected = "don't know how to extract http://localhost:7884/wasm-pack.bin")] |
||||
fn it_panics_if_not_tarball_or_zip() { |
||||
let server_port = 7884; |
||||
let binary_name = "wasm-pack"; |
||||
let binaries = vec![binary_name]; |
||||
|
||||
let dir = tempfile::TempDir::new().unwrap(); |
||||
let cache = Cache::at(dir.path()); |
||||
|
||||
// Spin up a local TcpListener.
|
||||
utils::start_server(None, Some(server_port)).recv().unwrap(); |
||||
|
||||
let url = format!("http://{}:{}", utils::TEST_SERVER_HOST, server_port); |
||||
let full_url = &format!("{}/{}.bin", &url, binary_name); |
||||
|
||||
let _ = cache.download(true, binary_name, &binaries, full_url); |
||||
} |
||||
|
||||
#[test] |
||||
fn it_joins_path_with_destination() { |
||||
let dir = tempfile::TempDir::new().unwrap(); |
||||
let cache = Cache::at(dir.path()); |
||||
|
||||
assert_eq!(dir.path().join("hello"), cache.join(Path::new("hello"))); |
||||
} |
@ -0,0 +1,125 @@ |
||||
use binary_install::Download; |
||||
use std::fs::OpenOptions; |
||||
|
||||
#[test] |
||||
#[cfg(unix)] |
||||
fn it_returns_binary_name_for_unix() { |
||||
use std::os::unix::fs::OpenOptionsExt; |
||||
|
||||
let binary_name = "wasm-pack"; |
||||
|
||||
let dir = tempfile::TempDir::new().unwrap(); |
||||
let download = Download::at(dir.path()); |
||||
|
||||
let full_path = dir.path().join(binary_name); |
||||
|
||||
let mut options = OpenOptions::new(); |
||||
options.create(true); |
||||
options.write(true); |
||||
|
||||
// Make the "binary" an executable.
|
||||
options.mode(0o755); |
||||
|
||||
options.open(&full_path).unwrap(); |
||||
|
||||
let binary = download.binary(binary_name); |
||||
|
||||
assert!(binary.is_ok()); |
||||
assert_eq!(full_path, binary.unwrap()); |
||||
} |
||||
|
||||
#[test] |
||||
#[cfg(not(windows))] |
||||
fn it_bails_if_not_file_for_unix() { |
||||
let binary_name = "wasm-pack"; |
||||
|
||||
let dir = tempfile::TempDir::new().unwrap(); |
||||
let download = Download::at(dir.path()); |
||||
|
||||
let full_path = dir.path().join(binary_name); |
||||
|
||||
let mut options = OpenOptions::new(); |
||||
options.create(true); |
||||
options.write(true); |
||||
|
||||
let binary = download.binary(binary_name); |
||||
|
||||
assert!(binary.is_err()); |
||||
assert_eq!( |
||||
format!("{} binary does not exist", full_path.to_str().unwrap()), |
||||
binary.unwrap_err().to_string() |
||||
); |
||||
} |
||||
|
||||
#[test] |
||||
#[cfg(windows)] |
||||
fn it_bails_if_not_file_for_windows() { |
||||
let binary_name = "wasm-pack.exe"; |
||||
|
||||
let dir = tempfile::TempDir::new().unwrap(); |
||||
let download = Download::at(dir.path()); |
||||
|
||||
let full_path = dir.path().join(binary_name); |
||||
|
||||
let mut options = OpenOptions::new(); |
||||
options.create(true); |
||||
options.write(true); |
||||
|
||||
let binary = download.binary(binary_name); |
||||
|
||||
assert!(binary.is_err()); |
||||
assert_eq!( |
||||
format!("{} binary does not exist", full_path.to_str().unwrap()), |
||||
binary.unwrap_err().to_string() |
||||
); |
||||
} |
||||
|
||||
#[test] |
||||
#[cfg(not(windows))] |
||||
fn it_bails_if_not_executable_for_unix() { |
||||
let binary_name = "wasm-pack"; |
||||
|
||||
let dir = tempfile::TempDir::new().unwrap(); |
||||
let download = Download::at(dir.path()); |
||||
|
||||
let full_path = dir.path().join(binary_name); |
||||
|
||||
let mut options = OpenOptions::new(); |
||||
options.create(true); |
||||
options.write(true); |
||||
|
||||
options.open(&full_path).unwrap(); |
||||
|
||||
let binary = download.binary(binary_name); |
||||
|
||||
assert!(binary.is_err()); |
||||
assert_eq!( |
||||
format!("{} is not executable", full_path.to_str().unwrap()), |
||||
binary.unwrap_err().to_string() |
||||
); |
||||
} |
||||
|
||||
#[test] |
||||
#[cfg(windows)] |
||||
fn it_bails_if_not_executable_for_windows() { |
||||
let binary_name = "wasm-pack.exe"; |
||||
|
||||
let dir = tempfile::TempDir::new().unwrap(); |
||||
let download = Download::at(dir.path()); |
||||
|
||||
let full_path = dir.path().join(binary_name); |
||||
|
||||
let mut options = OpenOptions::new(); |
||||
options.create(true); |
||||
options.write(true); |
||||
|
||||
options.open(&full_path).unwrap(); |
||||
|
||||
let binary = download.binary(binary_name); |
||||
|
||||
assert!(binary.is_err()); |
||||
assert_eq!( |
||||
format!("{} is not executable", full_path.to_str().unwrap()), |
||||
binary.unwrap_err().to_string() |
||||
); |
||||
} |
@ -0,0 +1,7 @@ |
||||
extern crate binary_install; |
||||
extern crate flate2; |
||||
extern crate tar; |
||||
|
||||
mod cache; |
||||
mod download; |
||||
mod utils; |
@ -0,0 +1,79 @@ |
||||
use flate2::write::GzEncoder; |
||||
use flate2::Compression; |
||||
use std::fs::{File, OpenOptions}; |
||||
use std::io::{self, Read, Write}; |
||||
use std::net::TcpListener; |
||||
use std::sync::mpsc::{channel, Receiver}; |
||||
use std::thread; |
||||
|
||||
pub const TEST_SERVER_HOST: &'static str = "localhost"; |
||||
|
||||
pub fn start_server(tarball: Option<Vec<u8>>, server_port: Option<u16>) -> Receiver<u16> { |
||||
let (sender, receiver) = channel(); |
||||
|
||||
thread::spawn(move || { |
||||
TcpListener::bind(format!( |
||||
"{}:{}", |
||||
TEST_SERVER_HOST, |
||||
server_port.unwrap_or_else(|| 0) |
||||
)) |
||||
.map(|listener| { |
||||
sender.send(listener.local_addr().unwrap().port()).unwrap(); |
||||
|
||||
for stream in listener.incoming() { |
||||
let mut stream = stream.unwrap(); |
||||
|
||||
let mut buffer = [0; 512]; |
||||
|
||||
stream.read(&mut buffer).unwrap(); |
||||
|
||||
let response = "HTTP/1.1 200 OK\r\n\r\n"; |
||||
|
||||
stream.write(response.as_bytes()).unwrap(); |
||||
|
||||
match tarball.to_owned() { |
||||
Some(tar) => { |
||||
stream.write(tar.as_ref()).unwrap(); |
||||
} |
||||
None => {} |
||||
} |
||||
|
||||
stream.flush().unwrap(); |
||||
} |
||||
}) |
||||
.unwrap(); |
||||
}); |
||||
|
||||
receiver |
||||
} |
||||
|
||||
pub fn create_tarball(binary_name: &str) -> Result<Vec<u8>, io::Error> { |
||||
let temp_dir = tempfile::TempDir::new().unwrap(); |
||||
let full_path = temp_dir.path().join(binary_name.to_owned() + ".tar.gz"); |
||||
|
||||
let tar = OpenOptions::new() |
||||
.create(true) |
||||
.read(true) |
||||
.write(true) |
||||
.open(&full_path)?; |
||||
|
||||
let mut file = OpenOptions::new() |
||||
.create(true) |
||||
.read(true) |
||||
.write(true) |
||||
.open(temp_dir.path().join(binary_name))?; |
||||
|
||||
let mut encoder = GzEncoder::new(tar, Compression::default()); |
||||
{ |
||||
let mut archive = tar::Builder::new(&mut encoder); |
||||
archive.append_file(binary_name, &mut file)?; |
||||
} |
||||
|
||||
let mut contents = vec![]; |
||||
|
||||
encoder.finish()?; |
||||
|
||||
File::open(temp_dir.path().join(&full_path))?.read_to_end(&mut contents)?; |
||||
|
||||
Ok(contents) |
||||
} |
Loading…
Reference in new issue