Skip to content

Commit

Permalink
Move tarball and http functions to utils.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
jbesraa committed Jan 21, 2025
1 parent 690ee38 commit 94f2895
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 48 deletions.
54 changes: 6 additions & 48 deletions roles/tests-integration/lib/template_provider.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,9 @@
use bitcoind::{bitcoincore_rpc::RpcApi, BitcoinD, Conf};
use flate2::read::GzDecoder;
use std::{
env,
fs::{create_dir_all, File},
io::{BufReader, Read},
path::{Path, PathBuf},
};
use tar::Archive;
use std::{env, fs::create_dir_all, path::PathBuf};

const VERSION_TP: &str = "0.1.13";

fn download_bitcoind_tarball(download_url: &str) -> Vec<u8> {
let response = minreq::get(download_url)
.send()
.unwrap_or_else(|_| panic!("Cannot reach URL: {}", download_url));
assert_eq!(
response.status_code, 200,
"URL {} didn't return 200",
download_url
);
response.as_bytes().to_vec()
}

fn read_tarball_from_file(path: &str) -> Vec<u8> {
let file = File::open(path).unwrap_or_else(|_| {
panic!(
"Cannot find {:?} specified with env var BITCOIND_TARBALL_FILE",
path
)
});
let mut reader = BufReader::new(file);
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).unwrap();
buffer
}
use crate::utils::{http, tarball};

fn unpack_tarball(tarball_bytes: &[u8], destination: &Path) {
let decoder = GzDecoder::new(tarball_bytes);
let mut archive = Archive::new(decoder);
for mut entry in archive.entries().unwrap().flatten() {
if let Ok(file) = entry.path() {
if file.ends_with("bitcoind") {
entry.unpack_in(destination).unwrap();
}
}
}
}
const VERSION_TP: &str = "0.1.13";

fn get_bitcoind_filename(os: &str, arch: &str) -> String {
match (os, arch) {
Expand Down Expand Up @@ -94,7 +52,7 @@ impl TemplateProvider {

if !bitcoin_exe_home.exists() {
let tarball_bytes = match env::var("BITCOIND_TARBALL_FILE") {
Ok(path) => read_tarball_from_file(&path),
Ok(path) => tarball::read_from_file(&path),
Err(_) => {
let download_endpoint =
env::var("BITCOIND_DOWNLOAD_ENDPOINT").unwrap_or_else(|_| {
Expand All @@ -104,15 +62,15 @@ impl TemplateProvider {
"{}/sv2-tp-{}/{}",
download_endpoint, VERSION_TP, download_filename
);
download_bitcoind_tarball(&url)
http::make_get_request(&url)
}
};

if let Some(parent) = bitcoin_exe_home.parent() {
create_dir_all(parent).unwrap();
}

unpack_tarball(&tarball_bytes, &temp_dir);
tarball::unpack(&tarball_bytes, &temp_dir);

if os == "macos" {
let bitcoind_binary = bitcoin_exe_home.join("bitcoind");
Expand Down
45 changes: 45 additions & 0 deletions roles/tests-integration/lib/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,48 @@ fn get_available_port() -> u16 {
}
}
}

pub mod http {
pub fn make_get_request(url: &str) -> Vec<u8> {
let response = minreq::get(url)
.send()
.unwrap_or_else(|_| panic!("Cannot reach URL: {}", url));
assert_eq!(response.status_code, 200, "URL {} didn't return 200", url);
response.as_bytes().to_vec()
}
}

pub mod tarball {
use flate2::read::GzDecoder;
use std::{
fs::File,
io::{BufReader, Read},
path::Path,
};
use tar::Archive;

pub fn read_from_file(path: &str) -> Vec<u8> {
let file = File::open(path).unwrap_or_else(|_| {
panic!(
"Cannot find {:?} specified with env var BITCOIND_TARBALL_FILE",
path
)
});
let mut reader = BufReader::new(file);
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).unwrap();
buffer
}

pub fn unpack(tarball_bytes: &[u8], destination: &Path) {
let decoder = GzDecoder::new(tarball_bytes);
let mut archive = Archive::new(decoder);
for mut entry in archive.entries().unwrap().flatten() {
if let Ok(file) = entry.path() {
if file.ends_with("bitcoind") {
entry.unpack_in(destination).unwrap();
}
}
}
}
}

0 comments on commit 94f2895

Please sign in to comment.