utils.rs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /// A simple RAII guard that resets an env var after the test exits
  2. pub struct EnvVarGuard {
  3. key: String,
  4. original: Option<String>,
  5. }
  6. impl EnvVarGuard {
  7. pub fn new<K: Into<String>, V: Into<String>>(key: K, value: V) -> Self {
  8. let key = key.into();
  9. let original = std::env::var(&key).ok();
  10. std::env::set_var(&key, value.into());
  11. Self { key, original }
  12. }
  13. }
  14. impl Drop for EnvVarGuard {
  15. fn drop(&mut self) {
  16. if let Some(ref val) = self.original {
  17. std::env::set_var(&self.key, val);
  18. } else {
  19. std::env::remove_var(&self.key);
  20. }
  21. }
  22. }
  23. use std::fs;
  24. use std::path::PathBuf;
  25. use std::process::{Command, Output};
  26. /// Test environment setup for SBPF tests
  27. pub struct TestEnv {
  28. pub sbpf_bin: String,
  29. pub temp_dir: PathBuf,
  30. pub project_dir: PathBuf,
  31. pub _guard: EnvVarGuard,
  32. }
  33. impl TestEnv {
  34. /// Set up a test environment with SBPF binary and temporary directory
  35. pub fn new(project_name: &str) -> Self {
  36. let current_dir = std::env::current_dir().expect("Failed to get current directory");
  37. let sbpf_path = current_dir.join("target").join("debug").join("sbpf");
  38. let guard = EnvVarGuard::new("SBPF_BIN", sbpf_path.to_string_lossy());
  39. let sbpf_bin = std::env::var("SBPF_BIN").expect("SBPF_BIN not set");
  40. let temp_dir = std::env::temp_dir().join(format!("sbpf_test_{}_e2e", project_name));
  41. if temp_dir.exists() {
  42. fs::remove_dir_all(&temp_dir).expect("Failed to remove existing test directory");
  43. }
  44. fs::create_dir_all(&temp_dir).expect("Failed to create test directory");
  45. let project_dir = temp_dir.join(project_name);
  46. println!("Using test directory: {:?}", temp_dir);
  47. println!("Using project directory: {:?}", project_dir);
  48. Self {
  49. sbpf_bin,
  50. temp_dir,
  51. project_dir,
  52. _guard: guard,
  53. }
  54. }
  55. /// Clean up the test environment
  56. pub fn cleanup(self) {
  57. fs::remove_dir_all(&self.temp_dir).expect("Failed to remove test directory");
  58. }
  59. }
  60. /// Run a command and return the output, panicking on failure
  61. pub fn run_command(cmd: &mut Command, operation_name: &str) -> Output {
  62. let output = cmd
  63. .output()
  64. .unwrap_or_else(|_| panic!("Failed to execute {}", operation_name));
  65. if !output.status.success() {
  66. let stderr = String::from_utf8_lossy(&output.stderr);
  67. let stdout = String::from_utf8_lossy(&output.stdout);
  68. panic!(
  69. "{} failed:\nSTDOUT: {}\nSTDERR: {}",
  70. operation_name, stdout, stderr
  71. );
  72. }
  73. println!("✅ {} completed successfully", operation_name);
  74. println!("STDOUT: {}", String::from_utf8_lossy(&output.stdout));
  75. output
  76. }
  77. /// Initialize a new SBPF project
  78. pub fn init_project(env: &TestEnv, project_name: &str) {
  79. println!("Step 1: Initializing {} project...", project_name);
  80. let init_output = run_command(
  81. Command::new(&env.sbpf_bin)
  82. .current_dir(&env.temp_dir)
  83. .arg("init")
  84. .arg(project_name),
  85. &format!("target/debug/sbpf init {}", project_name),
  86. );
  87. println!("✅ Project initialized successfully");
  88. println!("STDOUT: {}", String::from_utf8_lossy(&init_output.stdout));
  89. }
  90. /// Verify that the project structure is correct
  91. pub fn verify_project_structure(env: &TestEnv, project_name: &str) {
  92. let project_dir = &env.project_dir;
  93. assert!(
  94. project_dir.join("src").exists(),
  95. "src directory should exist"
  96. );
  97. assert!(
  98. project_dir.join("deploy").exists(),
  99. "deploy directory should exist"
  100. );
  101. assert!(
  102. project_dir.join(format!("src/{}", project_name)).exists(),
  103. "src/{} directory should exist",
  104. project_name
  105. );
  106. assert!(
  107. project_dir
  108. .join(format!("src/{}/{}.s", project_name, project_name))
  109. .exists(),
  110. "src/{}/{}.s should exist",
  111. project_name,
  112. project_name
  113. );
  114. assert!(
  115. project_dir.join("src/lib.rs").exists(),
  116. "src/lib.rs should exist"
  117. );
  118. assert!(
  119. project_dir.join("Cargo.toml").exists(),
  120. "Cargo.toml should exist"
  121. );
  122. }
  123. /// Run build on the project
  124. pub fn run_build(env: &TestEnv) {
  125. println!("Step 3: Running build...");
  126. run_command(
  127. Command::new(&env.sbpf_bin)
  128. .current_dir(&env.project_dir)
  129. .arg("build"),
  130. "target/debug/sbpf build",
  131. );
  132. }
  133. /// Verify that .so files were created in the deploy directory
  134. pub fn verify_so_files(env: &TestEnv) {
  135. let deploy_dir = env.project_dir.join("deploy");
  136. assert!(deploy_dir.exists(), "deploy directory should exist");
  137. let so_files: Vec<_> = fs::read_dir(&deploy_dir)
  138. .expect("Failed to read deploy directory")
  139. .filter_map(|entry| entry.ok())
  140. .filter(|entry| {
  141. entry
  142. .path()
  143. .extension()
  144. .and_then(|ext| ext.to_str())
  145. .map(|ext| ext == "so")
  146. .unwrap_or(false)
  147. })
  148. .collect();
  149. assert!(
  150. !so_files.is_empty(),
  151. "At least one .so file should be created in deploy directory"
  152. );
  153. println!("Found {} .so file(s) in deploy directory", so_files.len());
  154. }
  155. /// Run tests on the project
  156. #[allow(dead_code)]
  157. pub fn run_tests(env: &TestEnv) {
  158. println!("Step 4: Running tests...");
  159. run_command(
  160. Command::new(&env.sbpf_bin)
  161. .current_dir(&env.project_dir)
  162. .arg("test"),
  163. "target/debug/sbpf test",
  164. );
  165. }
  166. /// Update the project's assembly file content
  167. pub fn update_assembly_file(env: &TestEnv, project_name: &str, content: &str) {
  168. let assembly_path = env
  169. .project_dir
  170. .join(format!("src/{}/{}.s", project_name, project_name));
  171. fs::write(&assembly_path, content)
  172. .unwrap_or_else(|_| panic!("Failed to write new {}.s content", project_name));
  173. println!("✅ Updated {}.s with specified content", project_name);
  174. }