Skip to content

Commit

Permalink
add integration test for twoliter update
Browse files Browse the repository at this point in the history
  • Loading branch information
cbgbt committed Jul 17, 2024
1 parent 98ca177 commit 1aeb91e
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 9 deletions.
47 changes: 47 additions & 0 deletions .github/actions/install-crane/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: "Install crane"
description: "Installs crane for use in testing."
inputs:
crane-version:
description: "Version of crane to install"
required: false
default: latest
install-dir:
description: "Directory to install crane"
required: false
default: $HOME/.crane

runs:
using: "composite"
steps:
- shell: bash
run: |
gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"
mkdir -p ${{ inputs.install-dir }}
VERSION=${{ inputs.crane-version }}
if [[ "${VERSION}" == "latest" ]]; then
VERSION=$(gh release list \
--exclude-pre-releases \
-R google/go-containerregistry \
--json name \
| jq -r '.[0].name')
fi
case ${{ runner.arch }} in
X64)
ARCH=x86_64
;;
ARM64)
ARCH=arm64
;;
esac
ARTIFACT_NAME="go-containerregistry_Linux_${ARCH}.tar.gz"
gh release download "${VERSION}" \
-R google/go-containerregistry \
-p "${ARTIFACT_NAME}" \
--output - \
| tar -zxvf - -C "${{ inputs.install-dir }}" crane
echo "${{ inputs.install-dir }}" >> "${GITHUB_PATH}"
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ jobs:
- uses: actions/checkout@v3
with:
submodules: recursive
- name: Install crane for testing
uses: ./.github/actions/install-crane
- name: Install cargo-dist
run: |
cargo install --locked \
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ jobs:
labels: bottlerocket_ubuntu-latest_16-core
steps:
- uses: actions/checkout@v3
- name: Install crane for testing
uses: ./.github/actions/install-crane
- run: cargo install cargo-deny --locked
- run: cargo install cargo-make --locked
- run: make build
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ members = [
"tools/unplug",
"tools/update-metadata",
"twoliter",

"tests/integration-tests",
]

[profile.release]
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ fmt:
test:
cargo test --release --locked

.PHONY: integ
integ:
cargo test --manifest-path tests/integration-tests/Cargo.toml -- --include-ignored

.PHONY: check
check: fmt clippy deny test
check: fmt clippy deny test integ

.PHONY: build
build: check
Expand Down
9 changes: 9 additions & 0 deletions tests/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "integration-tests"
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"

[dev-dependencies]
tokio = { version = "1", default-features = false, features = ["process", "fs", "rt-multi-thread"] }
twoliter = { version = "0", path = "../../twoliter", artifact = [ "bin:twoliter" ] }
44 changes: 44 additions & 0 deletions tests/integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#![cfg(test)]

use std::ffi::OsStr;
use std::path::PathBuf;
use tokio::process::Command;

mod twoliter_update;

pub const TWOLITER_PATH: &'static str = env!("CARGO_BIN_FILE_TWOLITER");

pub fn test_projects_dir() -> PathBuf {
let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
p.pop();
p.join("projects")
}

pub async fn run_command<I, S, E>(cmd: S, args: I, env: E) -> std::process::Output
where
I: IntoIterator<Item = S>,
E: IntoIterator<Item = (S, S)>,
S: AsRef<OsStr>,
{
let args: Vec<S> = args.into_iter().collect();

println!(
"Executing '{}' with args [{}]",
cmd.as_ref().to_string_lossy(),
args.iter()
.map(|arg| format!("'{}'", arg.as_ref().to_string_lossy()))
.collect::<Vec<_>>()
.join(", ")
);

let output = Command::new(cmd)
.args(args.into_iter())
.envs(env.into_iter())
.output()
.await
.expect("failed to execute process");

println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
output
}
76 changes: 76 additions & 0 deletions tests/integration-tests/src/twoliter_update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use super::{run_command, test_projects_dir, TWOLITER_PATH};

const EXPECTED_LOCKFILE: &str = r#"schema-version = 1
release-version = "1.0.0"
digest = "m/6DbBacnIBHMo34GCuzA4pAHzrnQJ2G/XJMMguZXjw="
[sdk]
name = "bottlerocket-sdk"
version = "0.42.0"
vendor = "bottlerocket"
source = "public.ecr.aws/bottlerocket/bottlerocket-sdk:v0.42.0"
digest = "myHHKE41h9qfeyR6V6HB0BfiLPwj3QEFLUFy4TXcR10="
[[kit]]
name = "bottlerocket-core-kit"
version = "2.0.0"
vendor = "custom-vendor"
source = "public.ecr.aws/bottlerocket/bottlerocket-core-kit:v2.0.0"
digest = "vlTsAAbSCzXFZofVmw8pLLkRjnG/y8mtb2QsQBSz1zk="
"#;

#[tokio::test]
#[ignore]
/// Generates a Twoliter.lock file for the `external-kit` project using docker
async fn test_twoliter_update_docker() {
let external_kit = test_projects_dir().join("external-kit");

let lockfile = external_kit.join("Twoliter.lock");
tokio::fs::remove_file(&lockfile).await.ok();

let output = run_command(
TWOLITER_PATH,
[
"update",
"--project-path",
external_kit.join("Twoliter.toml").to_str().unwrap(),
],
[("TWOLITER_KIT_IMAGE_TOOL", "docker")],
)
.await;

assert!(output.status.success());

let lock_contents = tokio::fs::read_to_string(&lockfile).await.unwrap();
assert_eq!(lock_contents, EXPECTED_LOCKFILE);

tokio::fs::remove_file(&lockfile).await.ok();
}

#[tokio::test]
#[ignore]
/// Generates a Twoliter.lock file for the `external-kit` project using crane
async fn test_twoliter_update_crane() {
let external_kit = test_projects_dir().join("external-kit");

let lockfile = external_kit.join("Twoliter.lock");
tokio::fs::remove_file(&lockfile).await.ok();

let output = run_command(
TWOLITER_PATH,
[
"update",
"--project-path",
external_kit.join("Twoliter.toml").to_str().unwrap(),
],
[("TWOLITER_KIT_IMAGE_TOOL", "crane")],
)
.await;

assert!(output.status.success());

let lock_contents = tokio::fs::read_to_string(&lockfile).await.unwrap();
assert_eq!(lock_contents, EXPECTED_LOCKFILE);

tokio::fs::remove_file(&lockfile).await.ok();
}
10 changes: 2 additions & 8 deletions tests/projects/external-kit/Twoliter.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
schema-version = 1
release-version = "1.0.0"

[sdk]
name = "bottlerocket-sdk"
vendor = "bottlerocket"
version = "0.41.0"

[vendor.bottlerocket]
registry = "public.ecr.aws/bottlerocket"

[vendor.custom-vendor]
# We need to figure out how we can do integration testing with this
registry = "public.ecr.aws/bottlerocket"

[[kit]]
name = "core-kit"
version = "0.1.0"
name = "bottlerocket-core-kit"
version = "2.0.0"
vendor = "custom-vendor"

0 comments on commit 1aeb91e

Please sign in to comment.