From 23bea22ac12289b75eb1052bafd2c19f019eb38f Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Wed, 12 Jul 2023 15:22:11 -0400 Subject: [PATCH] integration-test: build one binary instead of N "integration tests" as defined by Cargo produce a binary per file in the tests directory. This is really not what we want and has a number of downsides, but the main one is binary size. Before: tamird@pc:~/src/aya$ cargo xtask build-integration-test | xargs ls -lah Finished dev [unoptimized + debuginfo] target(s) in 0.05s Running `target/debug/xtask build-integration-test` Compiling integration-test v0.1.0 (/home/tamird/src/aya/test/integration-test) Finished dev [unoptimized + debuginfo] target(s) in 0.68s -rwxrwxr-x 1 tamird tamird 34M Jul 12 15:21 /home/tamird/src/aya/target/debug/deps/bpf_probe_read-e03eb905a5e6209c -rwxrwxr-x 1 tamird tamird 35M Jul 12 15:21 /home/tamird/src/aya/target/debug/deps/btf_relocations-57a4fbb38bf06064 -rwxrwxr-x 1 tamird tamird 31M Jul 12 15:21 /home/tamird/src/aya/target/debug/deps/elf-98b7a32d6d04effb -rwxrwxr-x 1 tamird tamird 6.9M Jul 12 15:21 /home/tamird/src/aya/target/debug/deps/integration_test-0dd55ce96bfdad77 -rwxrwxr-x 1 tamird tamird 34M Jul 12 15:21 /home/tamird/src/aya/target/debug/deps/load-0718562e85b86d03 -rwxrwxr-x 1 tamird tamird 40M Jul 12 15:21 /home/tamird/src/aya/target/debug/deps/log-dbf355f9ea34068a -rwxrwxr-x 1 tamird tamird 36M Jul 12 15:21 /home/tamird/src/aya/target/debug/deps/rbpf-89a1bb848fa5cc3c -rwxrwxr-x 1 tamird tamird 34M Jul 12 15:21 /home/tamird/src/aya/target/debug/deps/relocations-cfe655c3bb413d8b -rwxrwxr-x 1 tamird tamird 34M Jul 12 15:21 /home/tamird/src/aya/target/debug/deps/smoke-ccd3974180a3fd29 After: tamird@pc:~/src/aya$ cargo xtask build-integration-test | xargs ls -lah Finished dev [unoptimized + debuginfo] target(s) in 0.05s Running `target/debug/xtask build-integration-test` Compiling integration-test v0.1.0 (/home/tamird/src/aya/test/integration-test) Finished dev [unoptimized + debuginfo] target(s) in 0.90s -rwxrwxr-x 1 tamird tamird 47M Jul 12 15:21 /home/tamird/src/aya/target/debug/deps/integration_test-0dd55ce96bfdad77 Since we plan to run these tests in a VM, copying 10x fewer bytes seems like a win. --- test/integration-test/src/lib.rs | 3 +++ test/integration-test/src/tests.rs | 8 ++++++++ .../{ => src}/tests/bpf_probe_read.rs | 4 ++-- .../{ => src}/tests/btf_relocations.rs | 0 test/integration-test/{ => src}/tests/elf.rs | 2 +- test/integration-test/{ => src}/tests/load.rs | 14 +++++++------- test/integration-test/{ => src}/tests/log.rs | 2 +- test/integration-test/{ => src}/tests/rbpf.rs | 4 ++-- .../{ => src}/tests/relocations.rs | 4 ++-- test/integration-test/{ => src}/tests/smoke.rs | 9 +++------ 10 files changed, 29 insertions(+), 21 deletions(-) create mode 100644 test/integration-test/src/tests.rs rename test/integration-test/{ => src}/tests/bpf_probe_read.rs (97%) rename test/integration-test/{ => src}/tests/btf_relocations.rs (100%) rename test/integration-test/{ => src}/tests/elf.rs (85%) rename test/integration-test/{ => src}/tests/load.rs (93%) rename test/integration-test/{ => src}/tests/log.rs (98%) rename test/integration-test/{ => src}/tests/rbpf.rs (96%) rename test/integration-test/{ => src}/tests/relocations.rs (86%) rename test/integration-test/{ => src}/tests/smoke.rs (82%) diff --git a/test/integration-test/src/lib.rs b/test/integration-test/src/lib.rs index 39a87ba2..7d571104 100644 --- a/test/integration-test/src/lib.rs +++ b/test/integration-test/src/lib.rs @@ -15,3 +15,6 @@ pub const TEST: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/test") pub const RELOCATIONS: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/relocations")); pub const BPF_PROBE_READ: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/bpf_probe_read")); + +#[cfg(test)] +mod tests; diff --git a/test/integration-test/src/tests.rs b/test/integration-test/src/tests.rs new file mode 100644 index 00000000..dd8565b0 --- /dev/null +++ b/test/integration-test/src/tests.rs @@ -0,0 +1,8 @@ +mod bpf_probe_read; +mod btf_relocations; +mod elf; +mod load; +mod log; +mod rbpf; +mod relocations; +mod smoke; diff --git a/test/integration-test/tests/bpf_probe_read.rs b/test/integration-test/src/tests/bpf_probe_read.rs similarity index 97% rename from test/integration-test/tests/bpf_probe_read.rs rename to test/integration-test/src/tests/bpf_probe_read.rs index 528c6a8d..20e6a134 100644 --- a/test/integration-test/tests/bpf_probe_read.rs +++ b/test/integration-test/src/tests/bpf_probe_read.rs @@ -68,7 +68,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", - integration_test::BPF_PROBE_READ, + crate::BPF_PROBE_READ, ); trigger_bpf_probe_read_user(bytes.as_ptr(), dest_len); bpf @@ -78,7 +78,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", - integration_test::BPF_PROBE_READ, + crate::BPF_PROBE_READ, ); set_kernel_buffer_element(&mut bpf, bytes); trigger_bpf_probe_read_kernel(dest_len); diff --git a/test/integration-test/tests/btf_relocations.rs b/test/integration-test/src/tests/btf_relocations.rs similarity index 100% rename from test/integration-test/tests/btf_relocations.rs rename to test/integration-test/src/tests/btf_relocations.rs diff --git a/test/integration-test/tests/elf.rs b/test/integration-test/src/tests/elf.rs similarity index 85% rename from test/integration-test/tests/elf.rs rename to test/integration-test/src/tests/elf.rs index a51733d7..3ebbdf58 100644 --- a/test/integration-test/tests/elf.rs +++ b/test/integration-test/src/tests/elf.rs @@ -2,7 +2,7 @@ use object::{Object, ObjectSymbol}; #[test] fn test_maps() { - let obj_file = object::File::parse(integration_test::MAP_TEST).unwrap(); + let obj_file = object::File::parse(crate::MAP_TEST).unwrap(); if obj_file.section_by_name("maps").is_none() { panic!("No 'maps' ELF section"); } diff --git a/test/integration-test/tests/load.rs b/test/integration-test/src/tests/load.rs similarity index 93% rename from test/integration-test/tests/load.rs rename to test/integration-test/src/tests/load.rs index db9f6482..ede471e2 100644 --- a/test/integration-test/tests/load.rs +++ b/test/integration-test/src/tests/load.rs @@ -15,7 +15,7 @@ const RETRY_DURATION_MS: u64 = 10; #[test] fn long_name() { - let mut bpf = Bpf::load(integration_test::NAME_TEST).unwrap(); + let mut bpf = Bpf::load(crate::NAME_TEST).unwrap(); let name_prog: &mut Xdp = bpf .program_mut("ihaveaverylongname") .unwrap() @@ -31,7 +31,7 @@ fn long_name() { #[test] fn multiple_btf_maps() { - let mut bpf = Bpf::load(integration_test::MULTIMAP_BTF).unwrap(); + let mut bpf = Bpf::load(crate::MULTIMAP_BTF).unwrap(); let map_1: Array<_, u64> = bpf.take_map("map_1").unwrap().try_into().unwrap(); let map_2: Array<_, u64> = bpf.take_map("map_2").unwrap().try_into().unwrap(); @@ -67,7 +67,7 @@ macro_rules! assert_loaded { #[test] fn unload_xdp() { - let mut bpf = Bpf::load(integration_test::TEST).unwrap(); + let mut bpf = Bpf::load(crate::TEST).unwrap(); let prog: &mut Xdp = bpf .program_mut("test_unload_xdp") .unwrap() @@ -96,7 +96,7 @@ fn unload_xdp() { #[test] fn unload_kprobe() { - let mut bpf = Bpf::load(integration_test::TEST).unwrap(); + let mut bpf = Bpf::load(crate::TEST).unwrap(); let prog: &mut KProbe = bpf .program_mut("test_unload_kpr") .unwrap() @@ -131,7 +131,7 @@ fn pin_link() { return; } - let mut bpf = Bpf::load(integration_test::TEST).unwrap(); + let mut bpf = Bpf::load(crate::TEST).unwrap(); let prog: &mut Xdp = bpf .program_mut("test_unload_xdp") .unwrap() @@ -168,7 +168,7 @@ fn pin_lifecycle() { // 1. Load Program and Pin { - let mut bpf = Bpf::load(integration_test::PASS).unwrap(); + let mut bpf = Bpf::load(crate::PASS).unwrap(); let prog: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap(); prog.load().unwrap(); prog.pin("/sys/fs/bpf/aya-xdp-test-prog").unwrap(); @@ -202,7 +202,7 @@ fn pin_lifecycle() { // 4. Load a new version of the program, unpin link, and atomically replace old program { - let mut bpf = Bpf::load(integration_test::PASS).unwrap(); + let mut bpf = Bpf::load(crate::PASS).unwrap(); let prog: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap(); prog.load().unwrap(); diff --git a/test/integration-test/tests/log.rs b/test/integration-test/src/tests/log.rs similarity index 98% rename from test/integration-test/tests/log.rs rename to test/integration-test/src/tests/log.rs index f4134e98..b3d92893 100644 --- a/test/integration-test/tests/log.rs +++ b/test/integration-test/src/tests/log.rs @@ -89,7 +89,7 @@ impl Log for TestingLogger { #[tokio::test] async fn log() { - let mut bpf = Bpf::load(integration_test::LOG).unwrap(); + let mut bpf = Bpf::load(crate::LOG).unwrap(); let (logger, captured_logs) = TestingLogger::with_capacity(5); BpfLogger::init_with_logger(&mut bpf, logger).unwrap(); diff --git a/test/integration-test/tests/rbpf.rs b/test/integration-test/src/tests/rbpf.rs similarity index 96% rename from test/integration-test/tests/rbpf.rs rename to test/integration-test/src/tests/rbpf.rs index 59765cb6..1f2fe2cb 100644 --- a/test/integration-test/tests/rbpf.rs +++ b/test/integration-test/src/tests/rbpf.rs @@ -5,7 +5,7 @@ use aya_obj::{generated::bpf_insn, Object, ProgramSection}; #[test] fn run_with_rbpf() { - let object = Object::parse(integration_test::PASS).unwrap(); + let object = Object::parse(crate::PASS).unwrap(); assert_eq!(object.programs.len(), 1); matches::assert_matches!(object.programs["pass"].section, ProgramSection::Xdp { .. }); @@ -32,7 +32,7 @@ static mut MULTIMAP_MAPS: [*mut Vec; 2] = [null_mut(), null_mut()]; #[test] fn use_map_with_rbpf() { - let mut object = Object::parse(integration_test::MULTIMAP_BTF).unwrap(); + let mut object = Object::parse(crate::MULTIMAP_BTF).unwrap(); assert_eq!(object.programs.len(), 1); matches::assert_matches!( diff --git a/test/integration-test/tests/relocations.rs b/test/integration-test/src/tests/relocations.rs similarity index 86% rename from test/integration-test/tests/relocations.rs rename to test/integration-test/src/tests/relocations.rs index 52b113b8..8642dc4b 100644 --- a/test/integration-test/tests/relocations.rs +++ b/test/integration-test/src/tests/relocations.rs @@ -4,7 +4,7 @@ use aya::{programs::UProbe, Bpf}; #[test] fn relocations() { - let bpf = load_and_attach("test_64_32_call_relocs", integration_test::RELOCATIONS); + let bpf = load_and_attach("test_64_32_call_relocs", crate::RELOCATIONS); trigger_relocations_program(); std::thread::sleep(Duration::from_millis(100)); @@ -17,7 +17,7 @@ fn relocations() { #[test] fn text_64_64_reloc() { - let mut bpf = load_and_attach("test_text_64_64_reloc", integration_test::TEXT_64_64_RELOC); + let mut bpf = load_and_attach("test_text_64_64_reloc", crate::TEXT_64_64_RELOC); let mut m = aya::maps::Array::<_, u64>::try_from(bpf.map_mut("RESULTS").unwrap()).unwrap(); m.set(0, 1, 0).unwrap(); diff --git a/test/integration-test/tests/smoke.rs b/test/integration-test/src/tests/smoke.rs similarity index 82% rename from test/integration-test/tests/smoke.rs rename to test/integration-test/src/tests/smoke.rs index 8748cbce..8b09ac92 100644 --- a/test/integration-test/tests/smoke.rs +++ b/test/integration-test/src/tests/smoke.rs @@ -12,7 +12,7 @@ fn xdp() { return; } - let mut bpf = Bpf::load(integration_test::PASS).unwrap(); + let mut bpf = Bpf::load(crate::PASS).unwrap(); let dispatcher: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap(); dispatcher.load().unwrap(); dispatcher.attach("lo", XdpFlags::default()).unwrap(); @@ -25,15 +25,12 @@ fn extension() { eprintln!("skipping test on kernel {kernel_version:?}, XDP uses netlink"); return; } - let mut bpf = Bpf::load(integration_test::MAIN).unwrap(); + let mut bpf = Bpf::load(crate::MAIN).unwrap(); let pass: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap(); pass.load().unwrap(); pass.attach("lo", XdpFlags::default()).unwrap(); - let mut bpf = BpfLoader::new() - .extension("drop") - .load(integration_test::EXT) - .unwrap(); + let mut bpf = BpfLoader::new().extension("drop").load(crate::EXT).unwrap(); let drop_: &mut Extension = bpf.program_mut("drop").unwrap().try_into().unwrap(); drop_.load(pass.fd().unwrap(), "xdp_pass").unwrap(); }