|
| 1 | +#[cfg(test)] |
| 2 | +mod test { |
| 3 | + use std::path::PathBuf; |
| 4 | + use std::process::{Child, Command, Stdio}; |
| 5 | + use std::{env, io, thread, time}; |
| 6 | + |
| 7 | + #[cfg(not(target_os = "windows"))] |
| 8 | + fn get_wws_path() -> PathBuf { |
| 9 | + let path = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()); |
| 10 | + |
| 11 | + // Use release when it's available |
| 12 | + let wws_path = if path.join("target/release/wws").exists() { |
| 13 | + path.join("target/release/wws") |
| 14 | + } else { |
| 15 | + path.join("target/debug/wws") |
| 16 | + }; |
| 17 | + |
| 18 | + println!("[E2E] Running wws from {}", wws_path.display()); |
| 19 | + |
| 20 | + wws_path |
| 21 | + } |
| 22 | + |
| 23 | + #[cfg(target_os = "windows")] |
| 24 | + fn get_wws_path() -> PathBuf { |
| 25 | + let path = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()); |
| 26 | + |
| 27 | + // Use release when it's available |
| 28 | + let wws_path = if path.join("target/release/wws.exe").exists() { |
| 29 | + path.join("target/release/wws.exe") |
| 30 | + } else { |
| 31 | + path.join("target/debug/wws.exe") |
| 32 | + }; |
| 33 | + |
| 34 | + println!("[E2E] Running wws from {}", wws_path.display()); |
| 35 | + |
| 36 | + wws_path |
| 37 | + } |
| 38 | + |
| 39 | + fn run(example_path: &str) -> io::Result<Child> { |
| 40 | + let path = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()); |
| 41 | + let example_path = path.join("examples").join(example_path); |
| 42 | + let wws_path = get_wws_path(); |
| 43 | + |
| 44 | + // Install missing runtimes |
| 45 | + println!("[E2E] Installing missing runtimes"); |
| 46 | + Command::new(&wws_path) |
| 47 | + .current_dir(&example_path) |
| 48 | + .args(["runtimes", "install"]) |
| 49 | + .stdout(Stdio::inherit()) |
| 50 | + .stderr(Stdio::inherit()) |
| 51 | + .status()?; |
| 52 | + |
| 53 | + // Run the example |
| 54 | + println!("[E2E] Running the service"); |
| 55 | + Command::new(&wws_path).arg(&example_path).spawn() |
| 56 | + } |
| 57 | + |
| 58 | + fn sleep_for(seconds: u64) { |
| 59 | + thread::sleep(time::Duration::from_secs(seconds)); |
| 60 | + } |
| 61 | + |
| 62 | + fn request_body(url: &str) -> Result<String, reqwest::Error> { |
| 63 | + reqwest::blocking::get(url)?.text() |
| 64 | + } |
| 65 | + |
| 66 | + // Check the examples/js-json works |
| 67 | + fn run_end_to_end_test(example: &str, waiting_seconds: u64, url: &str, expected_text: &str) { |
| 68 | + println!("[E2E] Running example: {example}"); |
| 69 | + |
| 70 | + let mut child = run(example).expect("Failed to execute command"); |
| 71 | + |
| 72 | + sleep_for(waiting_seconds); |
| 73 | + |
| 74 | + let body = match request_body(url) { |
| 75 | + Ok(body) => body, |
| 76 | + Err(err) => { |
| 77 | + eprintln!("[E2E] Error getting the body from the request to {url}"); |
| 78 | + eprintln!("[E2E] Error: {}", err); |
| 79 | + String::new() |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + println!("[E2E] Body content: {body}"); |
| 84 | + |
| 85 | + println!("[E2E] Stopping wws process [{}]", &child.id()); |
| 86 | + child.kill().expect("Error stopping wws"); |
| 87 | + |
| 88 | + // Test |
| 89 | + assert!(body.contains(expected_text)); |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + // Use this approach to run tests sequentially |
| 94 | + fn test_end_to_end() { |
| 95 | + // Allow configuring waiting times. It avoids having long waiting times |
| 96 | + // in development, while making it configurable in the CI |
| 97 | + let global_timeout = |
| 98 | + env::var("E2E_WAITING_TIME").map_or(None, |str| str.parse::<u64>().ok()); |
| 99 | + |
| 100 | + // env (Result(String)) -> map () |
| 101 | + |
| 102 | + let tests = [ |
| 103 | + ( |
| 104 | + "rust-basic", |
| 105 | + global_timeout.unwrap_or(5), |
| 106 | + "http://localhost:8080/basic", |
| 107 | + "This page was generated by a Wasm module built from Rust", |
| 108 | + ), |
| 109 | + ( |
| 110 | + "rust-kv", |
| 111 | + global_timeout.unwrap_or(5), |
| 112 | + "http://localhost:8080/kv", |
| 113 | + "Counter: 0", |
| 114 | + ), |
| 115 | + ( |
| 116 | + "rust-params", |
| 117 | + global_timeout.unwrap_or(5), |
| 118 | + "http://localhost:8080/thisisatest", |
| 119 | + "thisisatest", |
| 120 | + ), |
| 121 | + ( |
| 122 | + "js-basic", |
| 123 | + global_timeout.unwrap_or(5), |
| 124 | + "http://localhost:8080", |
| 125 | + "This page was generated by a JavaScript file", |
| 126 | + ), |
| 127 | + ( |
| 128 | + "js-json", |
| 129 | + global_timeout.unwrap_or(5), |
| 130 | + "http://localhost:8080/handler", |
| 131 | + "This message comes from an environment variable", |
| 132 | + ), |
| 133 | + ( |
| 134 | + "js-params", |
| 135 | + global_timeout.unwrap_or(10), |
| 136 | + "http://localhost:8080/thisisatest", |
| 137 | + "thisisatest", |
| 138 | + ), |
| 139 | + ( |
| 140 | + "python-basic", |
| 141 | + global_timeout.unwrap_or(20), |
| 142 | + "http://localhost:8080/", |
| 143 | + "This page was generated by a Python script", |
| 144 | + ), |
| 145 | + ( |
| 146 | + "python-mount", |
| 147 | + global_timeout.unwrap_or(20), |
| 148 | + "http://localhost:8080/", |
| 149 | + "This page was loaded from a mounted file", |
| 150 | + ), |
| 151 | + ( |
| 152 | + "ruby-basic", |
| 153 | + global_timeout.unwrap_or(20), |
| 154 | + "http://localhost:8080/", |
| 155 | + "This page was generated by a Ruby script", |
| 156 | + ), |
| 157 | + ]; |
| 158 | + |
| 159 | + for (example, waiting_seconds, url, expected_text) in tests { |
| 160 | + run_end_to_end_test(example, waiting_seconds, url, expected_text); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments