Skip to content

Commit

Permalink
integration-test: Remove integration-test-macros
Browse files Browse the repository at this point in the history
This doesn't add any value; use `cargo build --tests` with
`--message-format=json` instead; parse the output using `cargo_metadata`
to discover the location of the test binary.

Move test/integration-test/src/tests -> test/integration-test/tests to
conform to
https://doc.rust-lang.org/book/ch11-03-test-organization.html#integration-tests.
  • Loading branch information
tamird committed Jul 7, 2023
1 parent ecc03ec commit 9ca0af1
Show file tree
Hide file tree
Showing 19 changed files with 190 additions and 215 deletions.
29 changes: 25 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
[workspace]
members = [
"aya", "aya-obj", "aya-tool", "aya-log", "aya-log-common", "aya-log-parser", "test/integration-test", "test/integration-test-macros", "xtask",
"aya",
"aya-obj",
"aya-tool",
"aya-log",
"aya-log-common",
"aya-log-parser",
"test/integration-test",
"xtask",

# macros
"aya-bpf-macros", "aya-log-ebpf-macros",
"aya-bpf-macros",
"aya-log-ebpf-macros",

# ebpf crates
"bpf/aya-bpf", "bpf/aya-bpf-bindings", "bpf/aya-log-ebpf", "test/integration-ebpf"
"bpf/aya-bpf",
"bpf/aya-bpf-bindings",
"bpf/aya-log-ebpf",
"test/integration-ebpf",
]

default-members = [
"aya",
"aya-obj",
"aya-tool",
"aya-log",
"aya-bpf-macros",
"aya-log-ebpf-macros",
]
default-members = ["aya", "aya-obj", "aya-tool", "aya-log", "aya-bpf-macros", "aya-log-ebpf-macros"]

[profile.dev]
panic = "abort"
Expand Down
21 changes: 11 additions & 10 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,20 @@ cargo xtask integration-test --libbpf-dir /path/to/libbpf

### Virtualized


```
./test/run.sh /path/to/libbpf
```
### Writing a test

### Writing an integration test

Tests should follow these guidelines:

- Rust eBPF code should live in `integration-ebpf/${NAME}.rs` and included in `integration-ebpf/Cargo.toml`
- C eBPF code should live in `integration-test/src/bpf/${NAME}.bpf.c`. It's automatically compiled and made available as `${OUT_DIR}/${NAME}.bpf.o`.
- Any bytecode should be included in the integration test binary using `include_bytes_aligned!`
- Tests should be added to `integration-test/src/test`
- You may add a new module, or use an existing one
- Integration tests must use the `#[integration_test]` macro to be included in the build
- Test functions should return `anyhow::Result<()>` since this allows the use of `?` to return errors.
- You may either `panic!` when an assertion fails or `bail!`. The former is preferred since the stack trace will point directly to the failed line.
- Rust eBPF code should live in `integration-ebpf/${NAME}.rs` and included in
`integration-ebpf/Cargo.toml`.
- C eBPF code should live in `integration-ebpf/src/bpf/${NAME}.bpf.c`. It's automatically compiled
and made available as `${OUT_DIR}/${NAME}.bpf.o`.
- Any bytecode should be included in the integration test binary using `include_bytes_aligned!`.
- Tests should be added to `integration-test/tests`.
- You may add a new module, or use an existing one.
- Test functions should not return `anyhow::Result<()>` since this produces errors without stack
traces. Prefer to `panic!` instead.
13 changes: 0 additions & 13 deletions test/integration-test-macros/Cargo.toml

This file was deleted.

46 changes: 0 additions & 46 deletions test/integration-test-macros/src/lib.rs

This file was deleted.

6 changes: 0 additions & 6 deletions test/integration-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@ anyhow = "1"
aya = { path = "../../aya" }
aya-log = { path = "../../aya-log" }
aya-obj = { path = "../../aya-obj" }
clap = { version = "4", features = ["derive"] }
env_logger = "0.10"
futures-core = "0.3"
inventory = "0.3"
integration-test-macros = { path = "../integration-test-macros" }
libc = { version = "0.2.105" }
log = "0.4"
object = { version = "0.31", default-features = false, features = ["std", "read_core", "elf"] }
rbpf = "0.2.0"
regex = "1"
tempfile = "3.3.0"
libtest-mimic = "0.6.0"
tokio = { version = "1.24", features = ["rt", "rt-multi-thread", "sync", "time"] }
1 change: 1 addition & 0 deletions test/integration-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

21 changes: 0 additions & 21 deletions test/integration-test/src/main.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use aya::{
programs::{ProgramError, UProbe},
Bpf,
};
use integration_test_macros::integration_test;

const RESULT_BUF_LEN: usize = 1024;

Expand All @@ -20,53 +19,53 @@ struct TestResult {

unsafe impl aya::Pod for TestResult {}

#[integration_test]
#[test]
fn bpf_probe_read_user_str_bytes() {
let bpf = set_user_buffer(b"foo\0", RESULT_BUF_LEN);
assert_eq!(result_bytes(&bpf), b"foo");
}

#[integration_test]
#[test]
fn bpf_probe_read_user_str_bytes_truncate() {
let s = vec![b'a'; RESULT_BUF_LEN];
let bpf = set_user_buffer(&s, RESULT_BUF_LEN);
// The kernel truncates the string and the last byte is the null terminator
assert_eq!(result_bytes(&bpf), &s[..RESULT_BUF_LEN - 1]);
}

#[integration_test]
#[test]
fn bpf_probe_read_user_str_bytes_empty_string() {
let bpf = set_user_buffer(b"\0", RESULT_BUF_LEN);
assert_eq!(result_bytes(&bpf), b"");
}

#[integration_test]
#[test]
fn bpf_probe_read_user_str_bytes_empty_dest() {
let bpf = set_user_buffer(b"foo\0", 0);
assert_eq!(result_bytes(&bpf), b"");
}

#[integration_test]
#[test]
fn bpf_probe_read_kernel_str_bytes() {
let bpf = set_kernel_buffer(b"foo\0", RESULT_BUF_LEN);
assert_eq!(result_bytes(&bpf), b"foo");
}

#[integration_test]
#[test]
fn bpf_probe_read_kernel_str_bytes_truncate() {
let s = vec![b'a'; RESULT_BUF_LEN];
let bpf = set_kernel_buffer(&s, RESULT_BUF_LEN);
// The kernel truncates the string and the last byte is the null terminator
assert_eq!(result_bytes(&bpf), &s[..RESULT_BUF_LEN - 1]);
}

#[integration_test]
#[test]
fn bpf_probe_read_kernel_str_bytes_empty_string() {
let bpf = set_kernel_buffer(b"\0", RESULT_BUF_LEN);
assert_eq!(result_bytes(&bpf), b"");
}

#[integration_test]
#[test]
fn bpf_probe_read_kernel_str_bytes_empty_dest() {
let bpf = set_kernel_buffer(b"foo\0", 0);
assert_eq!(result_bytes(&bpf), b"");
Expand All @@ -76,7 +75,7 @@ fn set_user_buffer(bytes: &[u8], dest_len: usize) -> Bpf {
let bpf = load_and_attach_uprobe(
"test_bpf_probe_read_user_str_bytes",
"trigger_bpf_probe_read_user",
include_bytes_aligned!("../../../../target/bpfel-unknown-none/release/bpf_probe_read"),
include_bytes_aligned!("../../../target/bpfel-unknown-none/release/bpf_probe_read"),
);
trigger_bpf_probe_read_user(bytes.as_ptr(), dest_len);
bpf
Expand All @@ -86,7 +85,7 @@ fn set_kernel_buffer(bytes: &[u8], dest_len: usize) -> Bpf {
let mut bpf = load_and_attach_uprobe(
"test_bpf_probe_read_kernel_str_bytes",
"trigger_bpf_probe_read_kernel",
include_bytes_aligned!("../../../../target/bpfel-unknown-none/release/bpf_probe_read"),
include_bytes_aligned!("../../../target/bpfel-unknown-none/release/bpf_probe_read"),
);
set_kernel_buffer_element(&mut bpf, bytes);
trigger_bpf_probe_read_kernel(dest_len);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ use tempfile::TempDir;

use aya::{maps::Array, programs::TracePoint, BpfLoader, Btf, Endianness};

use super::integration_test;

// In the tests below we often use values like 0xAAAAAAAA or -0x7AAAAAAA. Those values have no
// special meaning, they just have "nice" bit patterns that can be helpful while debugging.

#[integration_test]
#[test]
fn relocate_field() {
let test = RelocationTest {
local_definition: r#"
Expand Down Expand Up @@ -40,7 +38,7 @@ fn relocate_field() {
assert_eq!(test.run_no_btf().unwrap(), 3);
}

#[integration_test]
#[test]
fn relocate_enum() {
let test = RelocationTest {
local_definition: r#"
Expand All @@ -60,7 +58,7 @@ fn relocate_enum() {
assert_eq!(test.run_no_btf().unwrap(), 0xAAAAAAAA);
}

#[integration_test]
#[test]
fn relocate_enum_signed() {
let test = RelocationTest {
local_definition: r#"
Expand All @@ -80,7 +78,7 @@ fn relocate_enum_signed() {
assert_eq!(test.run_no_btf().unwrap() as i64, -0x7AAAAAAAi64);
}

#[integration_test]
#[test]
fn relocate_enum64() {
let test = RelocationTest {
local_definition: r#"
Expand All @@ -100,7 +98,7 @@ fn relocate_enum64() {
assert_eq!(test.run_no_btf().unwrap(), 0xAAAAAAAABBBBBBBB);
}

#[integration_test]
#[test]
fn relocate_enum64_signed() {
let test = RelocationTest {
local_definition: r#"
Expand All @@ -120,7 +118,7 @@ fn relocate_enum64_signed() {
assert_eq!(test.run_no_btf().unwrap() as i64, -0xAAAAAAABBBBBBBBi64);
}

#[integration_test]
#[test]
fn relocate_pointer() {
let test = RelocationTest {
local_definition: r#"
Expand All @@ -143,7 +141,7 @@ fn relocate_pointer() {
assert_eq!(test.run_no_btf().unwrap(), 42);
}

#[integration_test]
#[test]
fn relocate_struct_flavors() {
let definition = r#"
struct foo {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,7 @@ use libc::{uname, utsname};
use regex::Regex;
use std::{cell::OnceCell, ffi::CStr, mem};

pub mod bpf_probe_read;
pub mod btf_relocations;
pub mod elf;
pub mod load;
pub mod log;
pub mod rbpf;
pub mod relocations;
pub mod smoke;

pub use integration_test_macros::{integration_test, tokio_integration_test};

#[derive(Debug)]
pub struct IntegrationTest {
pub name: &'static str,
pub test_fn: fn(),
}

pub(crate) fn kernel_version() -> anyhow::Result<(u8, u8, u8)> {
pub fn kernel_version() -> anyhow::Result<(u8, u8, u8)> {
static mut RE: OnceCell<Regex> = OnceCell::new();
let re =
unsafe { &mut RE }.get_or_init(|| Regex::new(r"^([0-9]+)\.([0-9]+)\.([0-9]+)").unwrap());
Expand All @@ -38,5 +21,3 @@ pub(crate) fn kernel_version() -> anyhow::Result<(u8, u8, u8)> {
bail!("no kernel version found");
}
}

inventory::collect!(IntegrationTest);
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use super::integration_test;

use aya::include_bytes_aligned;
use object::{Object, ObjectSymbol};

#[integration_test]
#[test]
fn test_maps() {
let bytes = include_bytes_aligned!("../../../../target/bpfel-unknown-none/release/map_test");
let bytes = include_bytes_aligned!("../../../target/bpfel-unknown-none/release/map_test");
let obj_file = object::File::parse(bytes).unwrap();
if obj_file.section_by_name("maps").is_none() {
panic!("No 'maps' ELF section");
Expand Down
Loading

0 comments on commit 9ca0af1

Please sign in to comment.