Skip to content

Commit

Permalink
Use cross for building in CI
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Prendes <[email protected]>
  • Loading branch information
jprendes committed Oct 18, 2023
1 parent 1ea4b02 commit 19601f1
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 26 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/action-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ on:
runtime:
required: true
type: string
slug:
required: true
type: string

jobs:
build:
Expand All @@ -26,6 +29,9 @@ jobs:
RUST_CACHE_KEY_OS: ${{ inputs.os }}
with:
rustflags: '' #Disable. By default this action sets environment variable is set to -D warnings. We manage this in the Makefile
- name: Setup cross-rs
if: runner.os == 'Linux'
run: ./scripts/setup-cross.sh
- name: Build
run: make build-${{ inputs.runtime }}
- name: Validate docs
Expand All @@ -41,13 +47,13 @@ jobs:
make dist-${{ inputs.runtime }}
# Check if there's any files to archive as tar fails otherwise
if stat dist/bin/* >/dev/null 2>&1; then
tar -czf dist/containerd-shim-${{ inputs.runtime }}-${{ inputs.os }}.tar.gz -C dist/bin .
tar -czf dist/containerd-shim-${{ inputs.runtime }}-${{ inputs.slug }}.tar.gz -C dist/bin .
else
tar -czf dist/containerd-shim-${{ inputs.runtime }}-${{ inputs.os }}.tar.gz -T /dev/null
tar -czf dist/containerd-shim-${{ inputs.runtime }}-${{ inputs.slug }}.tar.gz -T /dev/null
fi
- name: Upload artifacts
if: ${{ inputs.runtime != 'common' }}
uses: actions/upload-artifact@master
with:
name: containerd-shim-${{ inputs.runtime }}-${{ inputs.os }}
path: dist/containerd-shim-${{ inputs.runtime }}-${{ inputs.os }}.tar.gz
name: containerd-shim-${{ inputs.runtime }}-${{ inputs.slug }}
path: dist/containerd-shim-${{ inputs.runtime }}-${{ inputs.slug }}.tar.gz
4 changes: 2 additions & 2 deletions .github/workflows/action-test-k3s.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ jobs:
- name: Download artifacts
uses: actions/download-artifact@master
with:
name: containerd-shim-${{ inputs.runtime }}-${{ inputs.os }}
name: containerd-shim-${{ inputs.runtime }}-linux
path: dist
- name: Unpack artifats
shell: bash
run: |
mkdir -p dist/bin
tar -xzf dist/containerd-shim-${{ inputs.runtime }}-${{ inputs.os }}.tar.gz -C dist/bin
tar -xzf dist/containerd-shim-${{ inputs.runtime }}-linux.tar.gz -C dist/bin
- name: Download test image
uses: actions/download-artifact@master
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/action-test-kind.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ jobs:
- name: Download artifacts
uses: actions/download-artifact@master
with:
name: containerd-shim-${{ inputs.runtime }}-${{ inputs.os }}
name: containerd-shim-${{ inputs.runtime }}-linux
path: dist
- name: Unpack artifats
shell: bash
run: |
mkdir -p dist/bin
tar -xzf dist/containerd-shim-${{ inputs.runtime }}-${{ inputs.os }}.tar.gz -C dist/bin
tar -xzf dist/containerd-shim-${{ inputs.runtime }}-linux.tar.gz -C dist/bin
- name: Download test image
uses: actions/download-artifact@master
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/action-test-smoke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ jobs:
- name: Download artifacts
uses: actions/download-artifact@master
with:
name: containerd-shim-${{ inputs.runtime }}-${{ inputs.os }}
name: containerd-shim-${{ inputs.runtime }}-linux
path: dist
- name: Unpack artifats
shell: bash
run: |
mkdir -p dist/bin
tar -xzf dist/containerd-shim-${{ inputs.runtime }}-${{ inputs.os }}.tar.gz -C dist/bin
tar -xzf dist/containerd-shim-${{ inputs.runtime }}-linux.tar.gz -C dist/bin
- name: Download test image
uses: actions/download-artifact@master
with:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ jobs:
name: ${{ matrix.runtime }}
strategy:
matrix:
os: ["ubuntu-20.04", "ubuntu-22.04"]
os: ["ubuntu-22.04"]
runtime: ["common", "wasmtime", "wasmedge", "wasmer"]
uses: ./.github/workflows/action-build.yml
with:
os: ${{ matrix.os }}
runtime: ${{ matrix.runtime }}
slug: "linux"

build-windows:
name: ${{ matrix.runtime }}
Expand All @@ -49,6 +50,7 @@ jobs:
with:
os: ${{ matrix.os }}
runtime: ${{ matrix.runtime }}
slug: "windows"

smoke-tests:
name: ${{ matrix.runtime }}
Expand Down
36 changes: 26 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,21 @@ TEST_IMG_NAME ?= wasmtest:latest
RUNTIMES ?= wasmedge wasmtime wasmer
CONTAINERD_NAMESPACE ?= default

TARGET ?=
ifeq ($(CARGO),cross)
# Set the default target as defined in Cross.toml
TARGET ?= $(shell uname -m)-unknown-linux-musl
# When using `cross` we need to run the tests outside the `cross` container.
# We stop `cargo test` from running the tests with the `--no-run` flag.
# We then need to run the generate test binary manually.
# For that we use `--message-format=json` and `jq` to find the name of the binary, `xargs` and execute it.
TEST_ARGS_SEP= --no-run --color=always --message-format=json | \
jq -R '. as $$line | try (fromjson | .executable | strings) catch ($$line+"\n" | stderr | empty)' -r | \
xargs -I_ sudo -E ./_
else
TARGET ?= $(shell rustc --version -v | sed -En 's/host: (.*)/\1/p')
TEST_ARGS_SEP= --
endif

OPT_PROFILE ?= debug
RELEASE_FLAG :=
ifeq ($(OPT_PROFILE),release)
Expand Down Expand Up @@ -47,23 +61,23 @@ check: check-wasm $(RUNTIMES:%=check-%);

check-common: check-wasm;
check-wasm:
$(CARGO) +nightly fmt $(TARGET_FLAG) -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test-modules -- --check
CARGO= $(CARGO) +nightly fmt -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test-modules -- --check
$(CARGO) clippy $(TARGET_FLAG) $(FEATURES_wasm) -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test-modules -- $(WARNINGS)

check-%:
$(CARGO) +nightly fmt $(TARGET_FLAG) -p containerd-shim-$* -- --check
CARGO= $(CARGO) +nightly fmt -p containerd-shim-$* -- --check
$(CARGO) clippy $(TARGET_FLAG) $(FEATURES_$*) -p containerd-shim-$* -- $(WARNINGS)

.PHONY: fix fix-common fix-wasm fix-%
fix: fix-wasm $(RUNTIMES:%=fix-%);

fix-common: fix-wasm;
fix-wasm:
$(CARGO) +nightly fmt $(TARGET_FLAG) -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test-modules
CARGO= $(CARGO) +nightly fmt -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test-modules
$(CARGO) clippy $(TARGET_FLAG) $(FEATURES_wasm) --fix -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test-modules -- $(WARNINGS)

fix-%:
$(CARGO) +nightly fmt $(TARGET_FLAG) -p containerd-shim-$*
CARGO= $(CARGO) +nightly fmt -p containerd-shim-$*
$(CARGO) clippy $(TARGET_FLAG) $(FEATURES_$*) --fix -p containerd-shim-$* -- $(WARNINGS)

.PHONY: test test-common test-wasm test-wasmedge test-%
Expand All @@ -72,26 +86,28 @@ test: test-wasm $(RUNTIMES:%=test-%);
test-common: test-wasm;
test-wasm:
# oci-tar-builder and wasi-demo-app have no tests
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-wasm $(FEATURES_wasm) --verbose -- --nocapture --test-threads=1
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-wasm $(FEATURES_wasm) --verbose $(TEST_ARGS_SEP) --nocapture --test-threads=1

test-wasmedge:
# run tests in one thread to prevent paralellism
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-wasmedge $(FEATURES_wasmedge) --lib --verbose -- --nocapture --test-threads=1
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-wasmedge $(FEATURES_wasmedge) --lib --verbose $(TEST_ARGS_SEP) --nocapture --test-threads=1
ifneq ($(OS), Windows_NT)
ifneq ($(patsubst %-musl,,xx_$(TARGET)),)
# run wasmedge test without the default `static` feature
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-wasmedge --no-default-features --features standalone --lib --verbose -- --nocapture --test-threads=1
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-wasmedge --no-default-features --features standalone --lib --verbose $(TEST_ARGS_SEP) --nocapture --test-threads=1
endif
endif

test-%:
# run tests in one thread to prevent paralellism
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-$* $(FEATURES_$*) --lib --verbose -- --nocapture --test-threads=1
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-$* $(FEATURES_$*) --lib --verbose $(TEST_ARGS_SEP) --nocapture --test-threads=1

.PHONY: install install-%
install: $(RUNTIMES:%=install-%);

install-%: build-%
mkdir -p $(PREFIX)/bin
$(INSTALL) target/$(OPT_PROFILE)/containerd-shim-$*-v1 $(PREFIX)/bin/
$(INSTALL) target/$(TARGET)/$(OPT_PROFILE)/containerd-shim-$*-v1 $(PREFIX)/bin/
$(LN) ./containerd-shim-$*-v1 $(PREFIX)/bin/containerd-shim-$*d-v1
$(LN) ./containerd-shim-$*-v1 $(PREFIX)/bin/containerd-$*d

Expand Down
11 changes: 6 additions & 5 deletions crates/containerd-shim-wasmedge/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,32 +105,33 @@ fn test_has_default_devices() -> anyhow::Result<()> {
// Get the path to binary where the `WasmEdge_VersionGet` C ffi symbol is defined.
// If wasmedge is dynamically linked, this will be the path to the `.so`.
// If wasmedge is statically linked, this will be the path to the current executable.
fn get_wasmedge_binary_path() -> Option<std::path::PathBuf> {
fn get_wasmedge_binary_path() -> std::path::PathBuf {
use std::os::unix::prelude::OsStrExt;

let f = wasmedge_sys::ffi::WasmEdge_VersionGet;
let mut info = unsafe { std::mem::zeroed() };
if unsafe { libc::dladdr(f as *const libc::c_void, &mut info) } == 0 {
None
// no dladdr support, must be a static binary
std::env::current_exe().unwrap_or_default()
} else {
let fname = unsafe { std::ffi::CStr::from_ptr(info.dli_fname) };
let fname = std::ffi::OsStr::from_bytes(fname.to_bytes());
Some(std::path::PathBuf::from(fname))
std::path::PathBuf::from(fname)
}
}

#[cfg(feature = "static")]
#[test]
fn check_static_linking() {
let wasmedge_path = get_wasmedge_binary_path().unwrap().canonicalize().unwrap();
let wasmedge_path = get_wasmedge_binary_path().canonicalize().unwrap();
let current_exe = std::env::current_exe().unwrap().canonicalize().unwrap();
assert!(wasmedge_path == current_exe);
}

#[cfg(not(feature = "static"))]
#[test]
fn check_dynamic_linking() {
let wasmedge_path = get_wasmedge_binary_path().unwrap().canonicalize().unwrap();
let wasmedge_path = get_wasmedge_binary_path().canonicalize().unwrap();
let current_exe = std::env::current_exe().unwrap().canonicalize().unwrap();
assert!(wasmedge_path != current_exe);
}
3 changes: 3 additions & 0 deletions scripts/setup-cross.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
cargo install cross --git https://github.com/cross-rs/cross
echo "CARGO=cross" >> ${GITHUB_ENV}

0 comments on commit 19601f1

Please sign in to comment.