From 220962979c5d6d80a516fcb76060103b138f6ea3 Mon Sep 17 00:00:00 2001 From: Leo Romanovsky Date: Sun, 24 Nov 2024 17:21:46 -0800 Subject: [PATCH 1/6] Create fastly-edge-assignments integration tests. --- .github/workflows/ci.yml | 2 - .github/workflows/fastly-edge-assignments.yml | 66 +++++++++++++++++++ Makefile | 5 +- fastly-edge-assignments/.cargo/config.toml | 3 + fastly-edge-assignments/Makefile | 31 +++++++++ fastly-edge-assignments/README.md | 30 +++++++++ fastly-edge-assignments/src/main.rs | 18 ++++- 7 files changed, 148 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/fastly-edge-assignments.yml create mode 100644 fastly-edge-assignments/Makefile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a1a76073..d7bfa17e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,8 +27,6 @@ jobs: - run: rustup target add wasm32-wasi # Build non-WASM targets - run: cargo build --verbose --all-targets --workspace --exclude fastly-edge-assignments - # Build WASM target separately - - run: cargo build --verbose -p fastly-edge-assignments --target wasm32-wasi # Run tests (excluding WASM package) - run: cargo test --verbose --workspace --exclude fastly-edge-assignments - run: cargo doc --verbose diff --git a/.github/workflows/fastly-edge-assignments.yml b/.github/workflows/fastly-edge-assignments.yml new file mode 100644 index 00000000..c9c4a0df --- /dev/null +++ b/.github/workflows/fastly-edge-assignments.yml @@ -0,0 +1,66 @@ +name: Fastly Edge Assignments + +on: + push: + branches: + - main + release: + types: [published] + pull_request: + workflow_dispatch: + +permissions: + contents: read + +jobs: + cargo_build_and_test: + runs-on: ubuntu-latest + defaults: + run: + working-directory: fastly-edge-assignments + steps: + - uses: actions/checkout@v4 + with: + submodules: true + + # Cache Rust toolchain and dependencies + - uses: actions/cache@v3 + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + ~/.rustup/ + target/ + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + restore-keys: | + ${{ runner.os }}-cargo- + + - name: Setup Rust + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + target: wasm32-wasi + + # Install tools only if not cached + - name: Install Tools + run: | + if ! command -v cargo-nextest &> /dev/null; then + cargo install cargo-nextest + fi + if ! command -v fastly &> /dev/null; then + wget https://github.com/fastly/cli/releases/download/v10.17.0/fastly_10.17.0_linux_amd64.deb + sudo apt install ./fastly_10.17.0_linux_amd64.deb + fi + if ! command -v viceroy &> /dev/null; then + cargo install viceroy + fi + + # Build WASM target + - run: make build + + # Run unit and integration tests + - run: make test diff --git a/Makefile b/Makefile index dc53cf6d..d10bda1b 100644 --- a/Makefile +++ b/Makefile @@ -38,10 +38,9 @@ workspace-test: # Build only the `fastly-edge-assignments` package for WASM .PHONY: fastly-edge-assignments-build fastly-edge-assignments-build: - rustup target add $(WASM_TARGET) - cargo build --release --target $(WASM_TARGET) --package $(FASTLY_PACKAGE) + @$(MAKE) -C fastly-edge-assignments build # Test only the `fastly-edge-assignments` package .PHONY: fastly-edge-assignments-test fastly-edge-assignments-test: - cargo test --target $(WASM_TARGET) --package $(FASTLY_PACKAGE) + @$(MAKE) -C fastly-edge-assignments test diff --git a/fastly-edge-assignments/.cargo/config.toml b/fastly-edge-assignments/.cargo/config.toml index 6b77899c..946ef621 100644 --- a/fastly-edge-assignments/.cargo/config.toml +++ b/fastly-edge-assignments/.cargo/config.toml @@ -1,2 +1,5 @@ [build] target = "wasm32-wasi" + +[target.wasm32-wasi] +runner = "viceroy run -C fastly.toml -- " diff --git a/fastly-edge-assignments/Makefile b/fastly-edge-assignments/Makefile new file mode 100644 index 00000000..fba0b5e1 --- /dev/null +++ b/fastly-edge-assignments/Makefile @@ -0,0 +1,31 @@ +SHELL := bash +.ONESHELL: +.SHELLFLAGS := -eu -o pipefail -c +.DELETE_ON_ERROR: +MAKEFLAGS += --warn-undefined-variables +MAKEFLAGS += --no-builtin-rules + +WASM_TARGET=wasm32-wasi +BUILD_DIR=target/$(WASM_TARGET)/release +WASM_FILE=$(BUILD_DIR)/$(FASTLY_PACKAGE).wasm + +# Help target for easy documentation +.PHONY: help +help: + @echo "Available targets:" + @echo " build - Build the WASM target" + @echo " test - Run unit and integration tests" + @echo " clean - Clean all build artifacts" + +.PHONY: clean +clean: + rm -rf bin pkg + +.PHONY: build +build: + rustup target add $(WASM_TARGET) + fastly compute build + +.PHONY: test +test: + cargo nextest run diff --git a/fastly-edge-assignments/README.md b/fastly-edge-assignments/README.md index 6c6800f7..1846cef6 100644 --- a/fastly-edge-assignments/README.md +++ b/fastly-edge-assignments/README.md @@ -1,3 +1,33 @@ # Eppo Assignments on Fastly Compute@Edge TODO: Add a description + +## Development + +Install Rust toolchain: + +`curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh` + +Install Fastly CLI: + +`brew install fastly/tap/fastly` + +https://www.fastly.com/documentation/reference/tools/cli/ + +Install Viceroy: + +`cargo install viceroy` + +Build with Fastly: + +`make build` + +## Testing + +Install nextest: + +`cargo binstall cargo-nextest --secure` + +Run tests: + +`make test` diff --git a/fastly-edge-assignments/src/main.rs b/fastly-edge-assignments/src/main.rs index 665a755c..36b05fe9 100644 --- a/fastly-edge-assignments/src/main.rs +++ b/fastly-edge-assignments/src/main.rs @@ -3,8 +3,14 @@ mod handlers; use fastly::http::{Method, StatusCode}; use fastly::{Error, Request, Response}; -#[fastly::main] -fn main(req: Request) -> Result { +fn main() -> Result<(), Error> { + let ds_req = Request::from_client(); + let us_resp = handler(ds_req)?; + us_resp.send_to_client(); + Ok(()) +} + +fn handler(req: Request) -> Result { // Handle CORS preflight requests if req.get_method() == Method::OPTIONS { return Ok(Response::from_status(StatusCode::NO_CONTENT) @@ -26,3 +32,11 @@ fn main(req: Request) -> Result { .with_header("Access-Control-Allow-Origin", "*") .with_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")) } + +#[test] +fn test_health() { + let req = fastly::Request::get("https://precompute-edge-assignments.eppo.testcloud/health"); + let resp = handler(req).expect("request succeeds"); + assert_eq!(resp.get_status(), StatusCode::OK); + assert_eq!(resp.into_body_str(), "OK"); +} From 2ca741e8ea1a4b610e3a29884b38c3c5e61c59d0 Mon Sep 17 00:00:00 2001 From: Leo Romanovsky Date: Mon, 25 Nov 2024 10:56:57 -0800 Subject: [PATCH 2/6] refactor --- fastly-edge-assignments/src/main.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/fastly-edge-assignments/src/main.rs b/fastly-edge-assignments/src/main.rs index 36b05fe9..a174cc96 100644 --- a/fastly-edge-assignments/src/main.rs +++ b/fastly-edge-assignments/src/main.rs @@ -3,6 +3,9 @@ mod handlers; use fastly::http::{Method, StatusCode}; use fastly::{Error, Request, Response}; +#[cfg(test)] +const TEST_HOST: &str = "test-host"; + fn main() -> Result<(), Error> { let ds_req = Request::from_client(); let us_resp = handler(ds_req)?; @@ -35,7 +38,7 @@ fn handler(req: Request) -> Result { #[test] fn test_health() { - let req = fastly::Request::get("https://precompute-edge-assignments.eppo.testcloud/health"); + let req = fastly::Request::get(&format!("https://{}/health", TEST_HOST)); let resp = handler(req).expect("request succeeds"); assert_eq!(resp.get_status(), StatusCode::OK); assert_eq!(resp.into_body_str(), "OK"); From d65f6040df9da9c651b606d161d0b7ae62a2a3f3 Mon Sep 17 00:00:00 2001 From: Leo Romanovsky Date: Mon, 25 Nov 2024 10:58:36 -0800 Subject: [PATCH 3/6] add test for CORS headers --- fastly-edge-assignments/src/main.rs | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/fastly-edge-assignments/src/main.rs b/fastly-edge-assignments/src/main.rs index a174cc96..9381a170 100644 --- a/fastly-edge-assignments/src/main.rs +++ b/fastly-edge-assignments/src/main.rs @@ -43,3 +43,36 @@ fn test_health() { assert_eq!(resp.get_status(), StatusCode::OK); assert_eq!(resp.into_body_str(), "OK"); } + +#[test] +fn test_cors_headers() { + let req = Request::get(&format!("https://{}/health", TEST_HOST)); + let resp = handler(req).expect("request succeeds"); + + assert_eq!(resp.get_header("Access-Control-Allow-Origin").unwrap(), "*"); + assert_eq!( + resp.get_header("Access-Control-Allow-Methods").unwrap(), + "GET, POST, OPTIONS" + ); +} + +#[test] +fn test_options_request() { + let req = Request::new( + Method::OPTIONS, + &format!("https://{}/assignments", TEST_HOST), + ); + let resp = handler(req).expect("request succeeds"); + + assert_eq!(resp.get_status(), StatusCode::NO_CONTENT); + assert_eq!(resp.get_header("Access-Control-Allow-Origin").unwrap(), "*"); + assert_eq!( + resp.get_header("Access-Control-Allow-Methods").unwrap(), + "GET, POST, OPTIONS" + ); + assert_eq!( + resp.get_header("Access-Control-Allow-Headers").unwrap(), + "Content-Type" + ); + assert_eq!(resp.get_header("Access-Control-Max-Age").unwrap(), "86400"); +} From 91311307e0163657b09db73bc4a41dc2a4af0fad Mon Sep 17 00:00:00 2001 From: Leo Romanovsky Date: Mon, 9 Dec 2024 08:47:27 -0800 Subject: [PATCH 4/6] rm fastly-edge-assignments/Makefile --- fastly-edge-assignments/Makefile | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 fastly-edge-assignments/Makefile diff --git a/fastly-edge-assignments/Makefile b/fastly-edge-assignments/Makefile deleted file mode 100644 index fba0b5e1..00000000 --- a/fastly-edge-assignments/Makefile +++ /dev/null @@ -1,31 +0,0 @@ -SHELL := bash -.ONESHELL: -.SHELLFLAGS := -eu -o pipefail -c -.DELETE_ON_ERROR: -MAKEFLAGS += --warn-undefined-variables -MAKEFLAGS += --no-builtin-rules - -WASM_TARGET=wasm32-wasi -BUILD_DIR=target/$(WASM_TARGET)/release -WASM_FILE=$(BUILD_DIR)/$(FASTLY_PACKAGE).wasm - -# Help target for easy documentation -.PHONY: help -help: - @echo "Available targets:" - @echo " build - Build the WASM target" - @echo " test - Run unit and integration tests" - @echo " clean - Clean all build artifacts" - -.PHONY: clean -clean: - rm -rf bin pkg - -.PHONY: build -build: - rustup target add $(WASM_TARGET) - fastly compute build - -.PHONY: test -test: - cargo nextest run From d5b3d398c509946545d8ec4328b14b74720815f3 Mon Sep 17 00:00:00 2001 From: Leo Romanovsky Date: Mon, 9 Dec 2024 08:50:27 -0800 Subject: [PATCH 5/6] Update .github/workflows/fastly-edge-assignments.yml Co-authored-by: Oleksii Shmalko --- .github/workflows/fastly-edge-assignments.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/fastly-edge-assignments.yml b/.github/workflows/fastly-edge-assignments.yml index c9c4a0df..26a48264 100644 --- a/.github/workflows/fastly-edge-assignments.yml +++ b/.github/workflows/fastly-edge-assignments.yml @@ -4,8 +4,6 @@ on: push: branches: - main - release: - types: [published] pull_request: workflow_dispatch: From a30a882fa1b4fd9d16a2f8aceefa28caea6b29a2 Mon Sep 17 00:00:00 2001 From: Leo Romanovsky Date: Tue, 10 Dec 2024 19:01:50 -0800 Subject: [PATCH 6/6] Revert "rm fastly-edge-assignments/Makefile" This reverts commit 91311307e0163657b09db73bc4a41dc2a4af0fad. --- fastly-edge-assignments/Makefile | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 fastly-edge-assignments/Makefile diff --git a/fastly-edge-assignments/Makefile b/fastly-edge-assignments/Makefile new file mode 100644 index 00000000..fba0b5e1 --- /dev/null +++ b/fastly-edge-assignments/Makefile @@ -0,0 +1,31 @@ +SHELL := bash +.ONESHELL: +.SHELLFLAGS := -eu -o pipefail -c +.DELETE_ON_ERROR: +MAKEFLAGS += --warn-undefined-variables +MAKEFLAGS += --no-builtin-rules + +WASM_TARGET=wasm32-wasi +BUILD_DIR=target/$(WASM_TARGET)/release +WASM_FILE=$(BUILD_DIR)/$(FASTLY_PACKAGE).wasm + +# Help target for easy documentation +.PHONY: help +help: + @echo "Available targets:" + @echo " build - Build the WASM target" + @echo " test - Run unit and integration tests" + @echo " clean - Clean all build artifacts" + +.PHONY: clean +clean: + rm -rf bin pkg + +.PHONY: build +build: + rustup target add $(WASM_TARGET) + fastly compute build + +.PHONY: test +test: + cargo nextest run