Skip to content

Commit

Permalink
Add integration tests (#52)
Browse files Browse the repository at this point in the history
Starting with C++ binaries. This work leverages the nix build system to
build the test programs [0] and executes them while running the profiler
to ensure the traces are what we expect.

There are some future improvements such as testing other languages and
programs, as well as asserting metadata, and improving the assertion
helper to ensure that the frames are correct by changing it to be a
macro and not converting it to a string

Test Plan
=========
Added tests, CI

[0]: https://github.com/javierhonduco/lightswitch-testprogs
  • Loading branch information
javierhonduco authored Jul 21, 2024
1 parent fc8d6b7 commit 17978cc
Show file tree
Hide file tree
Showing 9 changed files with 423 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ impl Profiler<'_> {
pub fn profile_pids(&mut self, pids: Vec<i32>) {
for pid in pids {
self.filter_pids.insert(pid, true);
self.event_new_proc(pid);
}
}

Expand Down
5 changes: 0 additions & 5 deletions test-progs/src/main.rs

This file was deleted.

115 changes: 115 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
use std::io;
use std::io::Write;
use std::process::{Child, Command, Stdio};
use std::time::Duration;

use lightswitch::collector::Collector;
use lightswitch::profile::symbolize_profile;
use lightswitch::profiler::Profiler;
use lightswitch::profiler::SymbolizedAggregatedProfile;

/// Find the `nix` binary either in the $PATH or in the below hardcoded location.
fn nix_bin() -> String {
for path in ["nix", "/nix/var/nix/profiles/default/bin/nix"] {
if Command::new(path).arg("--help").output().is_ok() {
return path.into();
}
}

panic!("`nix` could not be found in $PATH or /nix/var/nix/profiles/default/bin/nix");
}

/// Builds the given test program and writes the resulting binaries under `target/nix` to prevent
/// clobbering artifacts from manual builds.
fn build_test_binary(target: &str) {
let output = Command::new(nix_bin())
.args([
"build",
&format!("./tests/testprogs#{}", target),
"--out-link",
"target/nix",
])
.output()
.expect("failed to execute process");

if !output.status.success() {
io::stdout().write_all(&output.stdout).unwrap();
io::stderr().write_all(&output.stderr).unwrap();
panic!("process exited with an error");
}
}

struct TestProcess {
child: Child,
}

/// Runs a test program and terminates it when the scope exits.
impl TestProcess {
fn new(target: &str) -> Self {
Self {
child: Command::new(format!("./target/nix/bin/{}", target))
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.unwrap(),
}
}

fn pid(&self) -> i32 {
self.child.id() as i32
}
}

impl Drop for TestProcess {
fn drop(&mut self) {
self.child.kill().unwrap();
}
}

fn assert_any_stack_contains(
symbolized_profile: &SymbolizedAggregatedProfile,
expected_stack: &[&str],
) -> bool {
for sample in symbolized_profile {
let stack_string = sample
.ustack
.iter()
.map(|e| e.name.clone())
.collect::<Vec<_>>()
.join("::");

if stack_string.contains(&expected_stack.join("::")) {
return true;
}
}

false
}

#[test]
fn test_integration() {
let bpf_test_debug = std::env::var("TEST_DEBUG_BPF").is_ok();

build_test_binary("cpp-progs");
let cpp_proc = TestProcess::new("main_cpp_clang_O1");

let collector = Collector::new();
let mut p = Profiler::new(bpf_test_debug, bpf_test_debug, Duration::from_secs(5), 999);
p.profile_pids(vec![cpp_proc.pid()]);
p.run(collector.clone());
let collector = collector.lock().unwrap();
let (raw_profile, procs, objs) = collector.finish();
let symbolized_profile = symbolize_profile(&raw_profile, procs, objs);

assert!(assert_any_stack_contains(
&symbolized_profile,
&[
"top2()",
"c2()",
"b2()",
"a2()",
"main",
"__libc_start_call_main",
],
));
}
1 change: 1 addition & 0 deletions tests/testprogs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/result
21 changes: 21 additions & 0 deletions tests/testprogs/LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 Francisco Javier Honduvilla Coto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
2 changes: 2 additions & 0 deletions tests/testprogs/build-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Testing wrapper to build all the x86_64 derivations and ensure they works.
nix flake show --json | jq '.packages."x86_64-linux" | keys[]' | xargs -I {} nix build .#{}
85 changes: 85 additions & 0 deletions tests/testprogs/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 17978cc

Please sign in to comment.