Skip to content

Commit

Permalink
Create fastly-edge-assignments integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
leoromanovsky committed Nov 25, 2024
1 parent 760d58d commit 2209629
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 7 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
66 changes: 66 additions & 0 deletions .github/workflows/fastly-edge-assignments.yml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions fastly-edge-assignments/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[build]
target = "wasm32-wasi"

[target.wasm32-wasi]
runner = "viceroy run -C fastly.toml -- "
31 changes: 31 additions & 0 deletions fastly-edge-assignments/Makefile
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions fastly-edge-assignments/README.md
Original file line number Diff line number Diff line change
@@ -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`
18 changes: 16 additions & 2 deletions fastly-edge-assignments/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ mod handlers;
use fastly::http::{Method, StatusCode};
use fastly::{Error, Request, Response};

#[fastly::main]
fn main(req: Request) -> Result<Response, Error> {
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<Response, Error> {
// Handle CORS preflight requests
if req.get_method() == Method::OPTIONS {
return Ok(Response::from_status(StatusCode::NO_CONTENT)
Expand All @@ -26,3 +32,11 @@ fn main(req: Request) -> Result<Response, Error> {
.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");
}

0 comments on commit 2209629

Please sign in to comment.