From b005ca57c5a1b87af9e74a1972cd75bb6ce08658 Mon Sep 17 00:00:00 2001 From: Sayan Nandan Date: Mon, 21 Mar 2022 11:02:08 +0530 Subject: [PATCH] Add file name tests for harness --- harness/src/build.rs | 2 +- harness/src/bundle.rs | 2 +- harness/src/linuxpkg.rs | 2 +- harness/src/main.rs | 2 + harness/src/test.rs | 14 ++++-- harness/src/tests.rs | 108 ++++++++++++++++++++++++++++++++++++++++ harness/src/util.rs | 12 ++++- 7 files changed, 133 insertions(+), 9 deletions(-) create mode 100644 harness/src/tests.rs diff --git a/harness/src/build.rs b/harness/src/build.rs index 2de5726b..66d0685a 100644 --- a/harness/src/build.rs +++ b/harness/src/build.rs @@ -59,7 +59,7 @@ impl ToString for BuildMode { /// Returns the paths of the files for the given target folder pub fn get_files_index(target_folder: &PathBuf) -> Vec { - let mut paths = Vec::with_capacity(3); + let mut paths = Vec::with_capacity(BINARIES.len()); for binary in BINARIES { paths.push(util::concat_path(binary, target_folder)); } diff --git a/harness/src/bundle.rs b/harness/src/bundle.rs index 257da7b1..b35e47ca 100644 --- a/harness/src/bundle.rs +++ b/harness/src/bundle.rs @@ -37,7 +37,7 @@ use std::path::PathBuf; use zip::{write::FileOptions, ZipWriter}; /// Returns the bundle name -fn get_bundle_name() -> String { +pub fn get_bundle_name() -> String { let mut filename = format!("sky-bundle-v{VERSION}"); match util::get_var(util::VAR_ARTIFACT) { Some(artifact) => { diff --git a/harness/src/linuxpkg.rs b/harness/src/linuxpkg.rs index 4d8b2aef..dbac7546 100644 --- a/harness/src/linuxpkg.rs +++ b/harness/src/linuxpkg.rs @@ -44,7 +44,7 @@ impl LinuxPackageType { } } /// Returns the file name for the package - fn get_file_name(&self) -> String { + pub fn get_file_name(&self) -> String { let mut filename = format!("skytable-v{VERSION}"); match util::get_var(util::VAR_ARTIFACT) { Some(artifact) => { diff --git a/harness/src/main.rs b/harness/src/main.rs index 054bb38e..1871761b 100644 --- a/harness/src/main.rs +++ b/harness/src/main.rs @@ -35,6 +35,8 @@ mod error; mod linuxpkg; mod presetup; mod test; +#[cfg(test)] +mod tests; use crate::{ cli::HarnessWhat, error::{HarnessError, HarnessResult}, diff --git a/harness/src/test.rs b/harness/src/test.rs index d50bc77f..970f7c4e 100644 --- a/harness/src/test.rs +++ b/harness/src/test.rs @@ -24,7 +24,11 @@ * */ -use crate::{build::BuildMode, util, HarnessError, HarnessResult}; +use crate::{ + build::BuildMode, + util::{self, SLEEP_FOR_STARTUP, SLEEP_FOR_TERMINATION}, + HarnessError, HarnessResult, +}; use openssl::{ asn1::Asn1Time, bn::{BigNum, MsbOption}, @@ -73,18 +77,19 @@ pub fn get_run_server_cmd(server_id: &'static str, target_folder: impl AsRef HarnessResult<(Child, Child)> { info!("Starting server1 ..."); let s1 = util::get_child("start server1", s1_cmd)?; - util::sleep_sec(10); + util::sleep_sec(SLEEP_FOR_STARTUP); info!("Starting server2 ..."); let s2 = util::get_child("start server2", s2_cmd)?; - util::sleep_sec(10); + util::sleep_sec(SLEEP_FOR_STARTUP); Ok((s1, s2)) } /// Kill the servers (run the command and then sleep for 10s) fn kill_servers() -> HarnessResult<()> { + info!("Terminating server instances ..."); kill_servers_inner()?; // sleep - util::sleep_sec(20); + util::sleep_sec(SLEEP_FOR_TERMINATION); Ok(()) } @@ -114,6 +119,7 @@ pub fn run_test() -> HarnessResult<()> { } // clean up + info!("Cleaning up test directories ..."); fs::remove_dir_all("server1").map_err(|e| { HarnessError::Other(format!("Failed to remove dir `server1` with error: {e}")) })?; diff --git a/harness/src/tests.rs b/harness/src/tests.rs new file mode 100644 index 00000000..02c2d945 --- /dev/null +++ b/harness/src/tests.rs @@ -0,0 +1,108 @@ +/* + * Created on Mon Mar 21 2022 + * + * This file is a part of Skytable + * Skytable (formerly known as TerrabaseDB or Skybase) is a free and open-source + * NoSQL database written by Sayan Nandan ("the Author") with the + * vision to provide flexibility in data modelling without compromising + * on performance, queryability or scalability. + * + * Copyright (c) 2022, Sayan Nandan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * +*/ + +use crate::util::WORKSPACE_ROOT; +use crate::{ + build::{self, BuildMode}, + bundle, linuxpkg, util, +}; +use libsky::VERSION; +use std::env; +use std::path::PathBuf; + +#[test] +fn file_names() { + const ARTIFACT: &str = "x86_64-linux-gnu"; + const TARGET: &str = "x86_64-unknown-linux-gnu"; + // without a target + // check bundle name + let bundle_name = bundle::get_bundle_name(); + assert_eq!(bundle_name, format!("sky-bundle-v{VERSION}.zip")); + // check target folder + let target_folder_debug = util::get_target_folder(BuildMode::Debug); + assert_eq!( + target_folder_debug.to_string_lossy(), + format!("{WORKSPACE_ROOT}target/debug") + ); + let target_folder_release = util::get_target_folder(BuildMode::Release); + assert_eq!( + target_folder_release.to_string_lossy(), + format!("{WORKSPACE_ROOT}target/release") + ); + // check files index + // files index for debug + let files_index = build::get_files_index(&target_folder_debug); + let expected_files_index: Vec = build::BINARIES + .iter() + .map(|bin| format!("{WORKSPACE_ROOT}target/debug/{bin}").into()) + .collect(); + assert_eq!(files_index, expected_files_index); + // files index for release + let files_index = build::get_files_index(&target_folder_release); + let expected_files_index: Vec = build::BINARIES + .iter() + .map(|bin| format!("{WORKSPACE_ROOT}target/release/{bin}").into()) + .collect(); + assert_eq!(files_index, expected_files_index); + // linux package name + let name = linuxpkg::LinuxPackageType::Deb.get_file_name(); + assert_eq!(name, format!("skytable-v{VERSION}.deb")); + + // with a target + env::set_var(util::VAR_ARTIFACT, ARTIFACT); // check bundle name + env::set_var(util::VAR_TARGET, TARGET); + let bundle_name = bundle::get_bundle_name(); + assert_eq!(bundle_name, format!("sky-bundle-v{VERSION}-{ARTIFACT}.zip")); + // check target folder + let target_folder_debug = util::get_target_folder(BuildMode::Debug); + assert_eq!( + target_folder_debug.to_string_lossy(), + format!("{WORKSPACE_ROOT}target/{TARGET}/debug") + ); + let target_folder_release = util::get_target_folder(BuildMode::Release); + assert_eq!( + target_folder_release.to_string_lossy(), + format!("{WORKSPACE_ROOT}target/{TARGET}/release") + ); + // check files index + // files index for debug + let files_index = build::get_files_index(&target_folder_debug); + let expected_files_index: Vec = build::BINARIES + .iter() + .map(|bin| format!("{WORKSPACE_ROOT}target/{TARGET}/debug/{bin}").into()) + .collect(); + assert_eq!(files_index, expected_files_index); + // files index for release + let files_index = build::get_files_index(&target_folder_release); + let expected_files_index: Vec = build::BINARIES + .iter() + .map(|bin| format!("{WORKSPACE_ROOT}target/{TARGET}/release/{bin}").into()) + .collect(); + assert_eq!(files_index, expected_files_index); + // linux package name + let name = linuxpkg::LinuxPackageType::Deb.get_file_name(); + assert_eq!(name, format!("skytable-v{VERSION}-{ARTIFACT}.deb")); +} diff --git a/harness/src/util.rs b/harness/src/util.rs index 9743abb2..11be1a01 100644 --- a/harness/src/util.rs +++ b/harness/src/util.rs @@ -34,9 +34,17 @@ use std::process::Child; use std::process::Command; pub type ExitCode = Option; +#[cfg(not(test))] pub const VAR_TARGET: &str = "TARGET"; +#[cfg(test)] +pub const VAR_TARGET: &str = "TARGET_TESTSUITE"; +#[cfg(not(test))] pub const VAR_ARTIFACT: &str = "ARTIFACT"; +#[cfg(test)] +pub const VAR_ARTIFACT: &str = "ARTIFACT_TESTSUITE"; pub const WORKSPACE_ROOT: &str = env!("ROOT_DIR"); +pub const SLEEP_FOR_STARTUP: u64 = 15; +pub const SLEEP_FOR_TERMINATION: u64 = 20; pub fn get_var(var: &str) -> Option { env::var_os(var).map(|v| v.to_string_lossy().to_string()) @@ -77,8 +85,8 @@ pub fn sleep_sec(secs: u64) { pub fn get_target_folder(mode: BuildMode) -> PathBuf { match env::var_os(VAR_TARGET).map(|v| v.to_string_lossy().to_string()) { - Some(target) => format!("{WORKSPACE_ROOT}/target/{target}/{}", mode.to_string()).into(), - None => format!("{WORKSPACE_ROOT}/target/{}", mode.to_string()).into(), + Some(target) => format!("{WORKSPACE_ROOT}target/{target}/{}", mode.to_string()).into(), + None => format!("{WORKSPACE_ROOT}target/{}", mode.to_string()).into(), } }