Skip to content

Commit

Permalink
Merge pull request #268 from bottlerocket-os/build-kit-test
Browse files Browse the repository at this point in the history
testing: build local kits
  • Loading branch information
webern authored Jun 4, 2024
2 parents 7d76c04 + cc44630 commit 7692d9f
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ We welcome ideas and requirements in the form of issues and comments!

This section includes information for maintainers about testing and releasing Twoliter.

## Testing
## Unit Tests and Integration Tests

If you run `cargo test` you will get the default features which includes the feature `integ-tests`.
These will be quite slow as some of them do complete builds.
If you don't have time for that, run `cargo test --no-default-features` to run only the fast tests.

## Testing the Binary in a Project

In general, if you have changes to Twoliter and want to try them out in a Twoliter project, it is as simple as building the Twoliter binary and using it in your project.
Different projects will have different ways of making sure the correct Twoliter binary is being used.
Expand Down
4 changes: 4 additions & 0 deletions twoliter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ tuftool = { version = "0.10", artifact = [ "bin:tuftool" ] }
bytes = "1"
flate2 = "1"
tar = "0.4"

[features]
default = ["integ-tests"]
integ-tests = []
121 changes: 121 additions & 0 deletions twoliter/src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,124 @@ impl BuildVariant {
res
}
}

#[cfg(feature = "integ-tests")]
#[cfg(test)]
mod test {
use super::*;
use std::path::Path;

const PROJECT: &str = "local-kit";

fn expect_kit(project_dir: &Path, name: &str, arch: &str, packages: &[&str]) {
let build = project_dir.join("build");
let kit_output_dir = build.join("kits").join(name).join(arch).join("Packages");
assert!(
kit_output_dir.is_dir(),
"Expected to find output dir for {} at {}",
name,
kit_output_dir.display()
);

for package in packages {
let rpm = kit_output_dir.join(&format!("bottlerocket-{package}-0.0-0.{arch}.rpm"));
assert!(
rpm.is_file(),
"Expected to find RPM for {}, for {} at {}",
package,
name,
rpm.display()
);
}
}

#[tokio::test]
async fn build_core_kit() {
let kit_name = "core-kit";
let arch = "aarch64";
let temp_dir = crate::test::copy_project_to_temp_dir(PROJECT);
let project_dir = temp_dir.path();
let project_path = project_dir.join("Twoliter.toml");

let command = BuildKit {
project_path: Some(project_path),
arch: arch.to_string(),
kit: kit_name.to_string(),
lookaside_cache: None,
upstream_source_fallback: false,
};

command.run().await.unwrap();
expect_kit(&project_dir, "core-kit", arch, &["pkg-a"]);
}

#[tokio::test]
async fn build_extra_1_kit() {
let kit_name = "extra-1-kit";
let arch = "x86_64";
let temp_dir = crate::test::copy_project_to_temp_dir(PROJECT);
let project_dir = temp_dir.path();
let project_path = project_dir.join("Twoliter.toml");

let command = BuildKit {
project_path: Some(project_path),
arch: arch.to_string(),
kit: kit_name.to_string(),
lookaside_cache: None,
upstream_source_fallback: false,
};

command.run().await.unwrap();
expect_kit(&project_dir, "core-kit", arch, &["pkg-a"]);
expect_kit(&project_dir, "extra-1-kit", arch, &["pkg-b", "pkg-d"]);
}

#[tokio::test]
async fn build_extra_2_kit() {
let kit_name = "extra-2-kit";
let arch = "aarch64";
let temp_dir = crate::test::copy_project_to_temp_dir(PROJECT);
let project_dir = temp_dir.path();
let project_path = project_dir.join("Twoliter.toml");

let command = BuildKit {
project_path: Some(project_path),
arch: arch.to_string(),
kit: kit_name.to_string(),
lookaside_cache: None,
upstream_source_fallback: false,
};

command.run().await.unwrap();
expect_kit(&project_dir, "core-kit", arch, &["pkg-a"]);
expect_kit(&project_dir, "extra-2-kit", arch, &["pkg-c"]);
}

#[tokio::test]
async fn build_extra_3_kit() {
let kit_name = "extra-3-kit";
let arch = "x86_64";
let temp_dir = crate::test::copy_project_to_temp_dir(PROJECT);
let project_dir = temp_dir.path();
let project_path = project_dir.join("Twoliter.toml");

let command = BuildKit {
project_path: Some(project_path),
arch: arch.to_string(),
kit: kit_name.to_string(),
lookaside_cache: None,
upstream_source_fallback: false,
};

command.run().await.unwrap();
expect_kit(&project_dir, "core-kit", arch, &["pkg-a"]);
expect_kit(&project_dir, "extra-1-kit", arch, &["pkg-b", "pkg-d"]);
expect_kit(&project_dir, "extra-2-kit", arch, &["pkg-c"]);
expect_kit(
&project_dir,
"extra-3-kit",
arch,
&["pkg-e", "pkg-f", "pkg-g"],
);
}
}
41 changes: 40 additions & 1 deletion twoliter/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ This directory and module are for tests, test data, and re-usable test code. Thi
be compiled for `cfg(test)`, which is accomplished at its declaration in `main.rs`.
!*/

#![allow(unused)]

#[cfg(feature = "integ-tests")]
mod cargo_make;

use std::path::PathBuf;
use std::fs;
use std::path::{Path, PathBuf};
use tempfile::TempDir;

/// Return the canonical path to the directory where we store test data.
pub(crate) fn data_dir() -> PathBuf {
Expand All @@ -23,3 +29,36 @@ pub(crate) fn projects_dir() -> PathBuf {
path.pop();
path.join("tests").join("projects").canonicalize().unwrap()
}

pub(crate) fn project_dir(name: &str) -> PathBuf {
let path = projects_dir().join(name);
path.canonicalize()
.expect(&format!("Unable to canonicalize '{}'", path.display()))
}

pub(crate) fn copy_project_to_temp_dir(project: &str) -> TempDir {
let temp_dir = TempDir::new().unwrap();
let src = project_dir(project);
let dst = temp_dir.path();
copy_most_dirs_recursively(&src, dst);
temp_dir
}

/// Copy dirs recursively except for some of the larger "ignoreable" dirs that may exist in the
/// user's checkout.
fn copy_most_dirs_recursively(src: &Path, dst: &Path) {
for entry in fs::read_dir(src).unwrap() {
fs::create_dir_all(&dst).unwrap();
let entry = entry.unwrap();
let file_type = entry.file_type().unwrap();
if file_type.is_dir() {
let name = entry.file_name().to_str().unwrap().to_string();
if matches!(name.as_ref(), "target" | "build" | ".gomodcache" | ".cargo") {
continue;
}
copy_most_dirs_recursively(&entry.path(), &dst.join(entry.file_name()));
} else {
fs::copy(entry.path(), dst.join(entry.file_name())).unwrap();
}
}
}

0 comments on commit 7692d9f

Please sign in to comment.