Skip to content

Commit

Permalink
Add arch-specific tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwlewis committed May 15, 2024
1 parent 5513996 commit 187fc50
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ jobs:
uses: Swatinem/[email protected]
- name: Install Pack CLI
uses: buildpacks/github-actions/[email protected]
- name: Pull builder image
run: docker pull ${{ matrix.builder }}
- name: Run integration tests
env:
INTEGRATION_TEST_BUILDER: ${{ matrix.builder }}
# Runs only tests annotated with the `ignore` attribute (which in this repo, are the integration tests).
run: cargo test --locked -- --ignored
run: cargo test --locked -- --ignored --test-threads 16
73 changes: 59 additions & 14 deletions buildpacks/go/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,51 @@
#![allow(unused_crate_dependencies)]

use libcnb_test::{assert_contains, assert_not_contains, BuildConfig, ContainerConfig, TestRunner};
use std::time::Duration;
use std::{env::consts, time::Duration};

const DEFAULT_BUILDER: &str = "heroku/builder:24";

fn test_go_fixture(fixture: &str, expect_loglines: &[&str], refute_loglines: &[&str]) {
let builder = std::env::var("INTEGRATION_TEST_BUILDER").unwrap_or(DEFAULT_BUILDER.to_string());
// TODO: Once Pack build supports `--platform` and libcnb-test adjusted accordingly, change this
// to allow configuring the target arch independently of the builder name (eg via env var).
let target_triple = match builder.as_str() {
// Compile the buildpack for ARM64 iff the builder supports multi-arch and the host is ARM64.
"heroku/builder:24" if cfg!(target_arch = "aarch64") => "aarch64-unknown-linux-musl",
_ => "x86_64-unknown-linux-musl",
};
struct IntegrationTestConfig {
target: String,
builder: String,
fixture: String,
}

let mut build_config = BuildConfig::new(builder, format!("tests/fixtures/{fixture}"));
build_config.target_triple(target_triple);
TestRunner::default().build(build_config, |ctx| {
impl IntegrationTestConfig {
fn new<S: Into<String>>(fixture: S) -> Self {
let builder =
std::env::var("INTEGRATION_TEST_BUILDER").unwrap_or(DEFAULT_BUILDER.to_string());
let target = match (builder.as_str(), consts::ARCH) {
// Compile the buildpack for arm64 if the builder supports multi-arch and the host is ARM64.
// This happens in CI and on developer machines with Apple silicon.
("heroku/builder:24", "aarch64") => "aarch64-unknown-linux-musl".to_string(),
// Compile the buildpack for arm64 if an arm64-specific builder is chosen.
// Used to run cross-arch integration tests from machines with Intel silicon.
(b, _) if b.ends_with("arm64") => "aarch64-unknown-linux-musl".to_string(),
(_, _) => "x86_64-unknown-linux-musl".to_string(),
};
let fixture = format!("tests/fixtures/{}", fixture.into());
Self {
builder,
target,
fixture,
}
}
}

impl From<IntegrationTestConfig> for BuildConfig {
fn from(integration_test_config: IntegrationTestConfig) -> BuildConfig {
let mut build_config = BuildConfig::new(
integration_test_config.builder,
integration_test_config.fixture,
);
build_config.target_triple(integration_test_config.target);
build_config
}
}

fn test_go_fixture(fixture: &str, expect_loglines: &[&str], refute_loglines: &[&str]) {
TestRunner::default().build(&IntegrationTestConfig::new(fixture).into(), |ctx| {
let logs = format!("{}\n{}", ctx.pack_stdout, ctx.pack_stderr);
for expect_line in expect_loglines {
assert_contains!(logs, expect_line);
Expand Down Expand Up @@ -143,7 +171,7 @@ fn test_basic_http_122() {
#[ignore = "integration test"]
fn test_go_artifact_caching() {
TestRunner::default().build(
BuildConfig::new("heroku/builder:22", "tests/fixtures/basic_http_116"),
&IntegrationTestConfig::new("basic_http_116").into(),
|ctx| {
assert_contains!(ctx.pack_stdout, "Installing go1.16.",);
let config = ctx.config.clone();
Expand All @@ -153,3 +181,20 @@ fn test_go_artifact_caching() {
},
);
}

#[test]
#[ignore = "integration test"]
fn test_go_binary_arch() {
let integration_config = IntegrationTestConfig::new("basic_http_122");
let (contains, not_contain) = match integration_config.target.as_str() {
"aarch64-unknown-linux-musl" => (["(linux-arm64)", "linux-arm64.tar.gz"], "amd64"),
_ => (["(linux-amd64)", "linux-amd64.tar.gz"], "arm64"),
};

TestRunner::default().build(&integration_config.into(), |ctx| {
for contain in contains {
assert_contains!(ctx.pack_stdout, contain);
}
assert_not_contains!(ctx.pack_stdout, not_contain)
});
}

0 comments on commit 187fc50

Please sign in to comment.