Browse Source

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.
Tamir Duberstein 1 year ago
parent
commit
23bea22ac1

+ 3 - 0
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;

+ 8 - 0
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;

+ 2 - 2
test/integration-test/tests/bpf_probe_read.rs → 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);

+ 0 - 0
test/integration-test/tests/btf_relocations.rs → test/integration-test/src/tests/btf_relocations.rs


+ 1 - 1
test/integration-test/tests/elf.rs → 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");
     }

+ 7 - 7
test/integration-test/tests/load.rs → 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();
 

+ 1 - 1
test/integration-test/tests/log.rs → 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();

+ 2 - 2
test/integration-test/tests/rbpf.rs → 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<u64>; 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!(

+ 2 - 2
test/integration-test/tests/relocations.rs → 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();

+ 3 - 6
test/integration-test/tests/smoke.rs → 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();
 }