Skip to content

Commit cb6a540

Browse files
committed
tests(compute-static): use a background HTTP server with Python for e2e tests to test the whole flow in a simple case
1 parent 4a9c49e commit cb6a540

File tree

4 files changed

+82
-9
lines changed

4 files changed

+82
-9
lines changed

terragrunt/modules/crates-io/compute-static/README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,30 @@ fastly compute build
1919

2020
## Testing
2121

22-
Testing requires [cargo-nextest](https://nexte.st/docs/installation/pre-built-binaries/), as specified in the [Fastly documentation](https://www.fastly.com/documentation/guides/compute/developer-guides/rust/). You can install it from source with binstall:
22+
Testing requires [cargo-nextest](https://nexte.st/docs/installation/pre-built-binaries/), as specified in
23+
the [Fastly documentation](https://www.fastly.com/documentation/guides/compute/developer-guides/rust/). You can install
24+
it from source with binstall:
25+
2326
```shell
2427
cargo install cargo-binstall
2528
cargo binstall cargo-nextest --secure
2629
```
2730

2831
Then, install [Viceroy](https://github.com/fastly/Viceroy) to run the edge function locally:
32+
2933
```shell
3034
cargo install --locked viceroy
3135
```
3236

33-
Now you can run the tests with:
37+
Due to the fact Viceroy does not allow easily mocking HTTP requests being sent (
38+
see [issue](https://github.com/fastly/Viceroy/issues/442)), some tests use a small Python HTTP
39+
server to work.
40+
For this reason, a wrapper bash script is provided that runs `cargo nextest run` with the test server active in
41+
background. You can therefore run the tests with:
42+
:
43+
3444
```shell
35-
cargo nextest run
45+
./run_tests.sh
3646
```
3747

3848
## Deployment
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
# Start the Python HTTP server in the background, which will act as primary host for requests sent from Rust tests
4+
python3 scripts/test_http_server.py &
5+
SERVER_PID=$!
6+
7+
echo "HTTP server started with PID: $SERVER_PID"
8+
9+
# Run the tests while the HTTP server is active in background
10+
cargo nextest run
11+
CARGO_EXIT_CODE=$?
12+
13+
kill $SERVER_PID
14+
echo "HTTP server stopped"
15+
16+
exit $CARGO_EXIT_CODE
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from http.server import BaseHTTPRequestHandler, HTTPServer
2+
3+
4+
class MockHandler(BaseHTTPRequestHandler):
5+
def do_GET(self):
6+
if self.path == "/crates/libgit2-sys/libgit2-sys-0.12.25%2B1.3.0.crate":
7+
self.send_response(200)
8+
self.end_headers()
9+
# The written data is used in Rust tests to verify whether the primary host (this) has actually been queried
10+
self.wfile.write(b'test_data')
11+
else:
12+
self.send_response(404)
13+
self.end_headers()
14+
15+
16+
HTTPServer(("127.0.0.1", 8080), MockHandler).serve_forever()

terragrunt/modules/crates-io/compute-static/src/main.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ fn add_cors_headers(response: &mut Result<Response, Error>) {
210210
}
211211
}
212212

213-
214213
/// Finalize the builder and log the line
215214
fn build_and_send_log(log_line: LogLineV1Builder, config: &Config) {
216215
match log_line.build() {
@@ -246,10 +245,13 @@ fn http_version_to_string(version: Version) -> Option<String> {
246245
#[cfg(test)]
247246
mod tests {
248247
use super::*;
248+
use fastly::experimental::BackendExt;
249+
use fastly::Backend;
249250

250251
fn new_test_config() -> Config {
251252
Config {
252-
primary_host: "test_backend".to_string(),
253+
// For tests sending requests to S3, ensure this points at the web server config defined in scripts/test_http_server.py.
254+
primary_host: "127.0.0.1:8080".to_string(),
253255
fallback_host: "fallback_host".to_string(),
254256
static_ttl: 0,
255257
cloudfront_url: "cloudfront_url".to_string(),
@@ -291,11 +293,16 @@ mod tests {
291293
#[test]
292294
fn test_plus_encoding() {
293295
// Example taken from the GitHub issue above
294-
let mut client_req = Request::get("https://static.crates.io/crates/libgit2-sys/libgit2-sys-0.12.25+1.3.0.crate");
296+
let mut client_req = Request::get(
297+
"https://static.crates.io/crates/libgit2-sys/libgit2-sys-0.12.25+1.3.0.crate",
298+
);
295299
let config = new_test_config();
296300

297301
rewrite_request(&config, &mut client_req);
298-
assert_eq!(client_req.get_path(), "/crates/libgit2-sys/libgit2-sys-0.12.25%2B1.3.0.crate");
302+
assert_eq!(
303+
client_req.get_path(),
304+
"/crates/libgit2-sys/libgit2-sys-0.12.25%2B1.3.0.crate"
305+
);
299306
}
300307

301308
/// Ensures visiting version-downloads with and without a trailing slash properly redirects to the index.html
@@ -304,10 +311,34 @@ mod tests {
304311
let mut client_req = Request::get("https://static.crates.io/archive/version-downloads/");
305312
let config = new_test_config();
306313
rewrite_request(&config, &mut client_req);
307-
assert_eq!(client_req.get_path(), "/archive/version-downloads/index.html");
314+
assert_eq!(
315+
client_req.get_path(),
316+
"/archive/version-downloads/index.html"
317+
);
308318

309319
let mut client_req = Request::get("https://static.crates.io/archive/version-downloads");
310320
rewrite_request(&config, &mut client_req);
311-
assert_eq!(client_req.get_path(), "/archive/version-downloads/index.html");
321+
assert_eq!(
322+
client_req.get_path(),
323+
"/archive/version-downloads/index.html"
324+
);
325+
}
326+
327+
/// Ensures that the request is forwarded to the primary host for normal queries.
328+
/// Assumes the tests are being executed with the test HTTP Python server active in background (see README.md).
329+
#[test]
330+
fn test_handle_request() {
331+
let config = new_test_config();
332+
Backend::builder(&config.primary_host, &config.primary_host)
333+
.finish()
334+
.unwrap();
335+
let client_req = Request::get(
336+
"https://static.crates.io/crates/libgit2-sys/libgit2-sys-0.12.25+1.3.0.crate",
337+
);
338+
let mut res = handle_request(&config, client_req).unwrap();
339+
assert_eq!(res.get_status(), StatusCode::OK);
340+
// Assuming the function sent a request to the primary host, verify whether the body is the one set in the test server
341+
let body = res.take_body_str();
342+
assert_eq!(body, "test_data");
312343
}
313344
}

0 commit comments

Comments
 (0)