Skip to content

Commit

Permalink
Update Fastly crate from 0.10.4 to 0.11.1
Browse files Browse the repository at this point in the history
This updates the test fixtures to use the newly released `0.11.1` Fastly crate.

When `fastly::acl` hostcalls were added in #438, the corresponding SDK support
was not yet released. So #438 added tests using the still-unreleased-SDK, but
put that code behind a compile-time gate ([here](#438 (comment))).

This change removes that compile-time gate in
`test-fixtures/src/bin/acl.rs`, effectively changing these tests from a
no-op to running the desired test cases.

A few other text-fixtures stopped compiling with the updated SDK, which
I've tried to address:

- `test-fixtures/src/bin/inspect.rs`
   cc/ @ulyssa

- `test-fixtures/src/bin/kv_store.rs`
   cc/ @joeshaw
  • Loading branch information
awilliams-fastly committed Dec 5, 2024
1 parent a2b9fe3 commit 4af02d5
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 84 deletions.
64 changes: 55 additions & 9 deletions test-fixtures/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions test-fixtures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ edition = "2021"
license = "Apache-2.0 WITH LLVM-exception"
publish = false

[features]
# Temporary feature used until the fastly SDK is updated
# to a version which contains the fastly_acl hostcalls.
acl_hostcalls = []

[dependencies]
anyhow = "1.0.86"
base64 = "0.21.2"
fastly = "0.10.4"
fastly-shared = "0.10.4"
fastly-sys = "0.10.4"
fastly = "0.11.1"
fastly-shared = "0.11.1"
fastly-sys = "0.11.1"
hex-literal = "0.4.1"
bytes = "1.0.0"
http = "1.1.0"
Expand Down
94 changes: 44 additions & 50 deletions test-fixtures/src/bin/acl.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,56 @@
//! A guest program to test that acls works properly.
use fastly::acl::Acl;
use fastly::Error;
use std::net::{Ipv4Addr, Ipv6Addr};

fn main() -> Result<(), Error> {
// Temporary until fastly SDK is released which
// includes the fastly::acl module.
#[cfg(feature = "acl_hostcalls")]
{
use fastly::acl::Acl;
use std::net::{Ipv4Addr, Ipv6Addr};

match Acl::open("DOES-NOT-EXIST") {
Err(fastly::acl::OpenError::AclNotFound) => { /* OK */ }
Err(other) => panic!("expected error opening non-existant acl, got: {:?}", other),
_ => panic!("expected error opening non-existant acl, got Ok"),
}
match Acl::open("DOES-NOT-EXIST") {
Err(fastly::acl::OpenError::AclNotFound) => { /* OK */ }
Err(other) => panic!("expected error opening non-existant acl, got: {:?}", other),
_ => panic!("expected error opening non-existant acl, got Ok"),
}

let acl1 = Acl::open("my-acl-1")?;
let acl1 = Acl::open("my-acl-1")?;

match acl1.try_lookup(Ipv4Addr::new(192, 168, 1, 1).into())? {
Some(lookup_match) => {
assert_eq!(lookup_match.prefix(), "192.168.0.0/16");
assert!(lookup_match.is_block());
}
None => panic!("expected match"),
};
match acl1.try_lookup(Ipv4Addr::new(23, 23, 23, 23).into())? {
Some(lookup_match) => {
assert_eq!(lookup_match.prefix(), "23.23.23.23/32");
assert!(lookup_match.is_allow());
}
None => panic!("expected match"),
};
if let Some(lookup_match) = acl1.try_lookup(Ipv4Addr::new(100, 100, 100, 100).into())? {
panic!("expected no match, got: {:?}", lookup_match);
match acl1.try_lookup(Ipv4Addr::new(192, 168, 1, 1).into())? {
Some(lookup_match) => {
assert_eq!(lookup_match.prefix(), "192.168.0.0/16");
assert!(lookup_match.is_block());
}
None => panic!("expected match"),
};
match acl1.try_lookup(Ipv4Addr::new(23, 23, 23, 23).into())? {
Some(lookup_match) => {
assert_eq!(lookup_match.prefix(), "23.23.23.23/32");
assert!(lookup_match.is_allow());
}
None => panic!("expected match"),
};
if let Some(lookup_match) = acl1.try_lookup(Ipv4Addr::new(100, 100, 100, 100).into())? {
panic!("expected no match, got: {:?}", lookup_match);
}

let acl2 = Acl::open("my-acl-2")?;
let acl2 = Acl::open("my-acl-2")?;

match acl2.try_lookup(Ipv6Addr::new(0x2000, 0, 0, 0, 0, 1, 2, 3).into())? {
Some(lookup_match) => {
assert_eq!(lookup_match.prefix(), "2000::/24");
assert!(lookup_match.is_block());
}
None => panic!("expected match"),
};
match acl2.try_lookup(Ipv6Addr::new(0xFACE, 0, 2, 3, 4, 5, 6, 7).into())? {
Some(lookup_match) => {
assert_eq!(lookup_match.prefix(), "face::/16");
assert!(lookup_match.is_allow());
}
None => panic!("expected match"),
};
if let Some(lookup_match) =
acl2.try_lookup(Ipv6Addr::new(0xFADE, 1, 2, 3, 4, 5, 6, 7).into())?
{
panic!("expected no match, got: {:?}", lookup_match);
};
}
match acl2.try_lookup(Ipv6Addr::new(0x2000, 0, 0, 0, 0, 1, 2, 3).into())? {
Some(lookup_match) => {
assert_eq!(lookup_match.prefix(), "2000::/24");
assert!(lookup_match.is_block());
}
None => panic!("expected match"),
};
match acl2.try_lookup(Ipv6Addr::new(0xFACE, 0, 2, 3, 4, 5, 6, 7).into())? {
Some(lookup_match) => {
assert_eq!(lookup_match.prefix(), "face::/16");
assert!(lookup_match.is_allow());
}
None => panic!("expected match"),
};
if let Some(lookup_match) =
acl2.try_lookup(Ipv6Addr::new(0xFADE, 1, 2, 3, 4, 5, 6, 7).into())?
{
panic!("expected no match, got: {:?}", lookup_match);
};

Ok(())
}
18 changes: 8 additions & 10 deletions test-fixtures/src/bin/inspect.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use fastly::experimental::{inspect, InspectConfig, InspectError, InspectVerdict};
use fastly::handle::{BodyHandle, RequestHandle};
use fastly::http::{HeaderName, HeaderValue, Method, StatusCode};
use fastly::handle::BodyHandle;
use fastly::http::StatusCode;
use fastly::security::{inspect, InspectConfig};
use fastly::{Error, Request, Response};

#[fastly::main]
fn main(mut req: Request) -> Result<Response, Error> {
fn main(req: Request) -> Result<Response, Error> {
let (req, body) = req.into_handles();
let body = body.unwrap_or_else(BodyHandle::new);

Expand All @@ -16,21 +16,19 @@ fn main(mut req: Request) -> Result<Response, Error> {
Ok(x) => {
let body = format!(
"inspect result: waf_response={}, tags={:?}, decision_ms={}ms, verdict={:?}",
x.waf_response(),
x.status(),
x.tags(),
x.decision_ms().as_millis(),
x.verdict()
);

Response::from_status(StatusCode::OK)
.with_body_text_plain(&body)
Response::from_status(StatusCode::OK).with_body_text_plain(&body)
}
Err(e) => {
let body = format!("Error: {e:?}");

Response::from_status(StatusCode::BAD_REQUEST)
.with_body_text_plain(&body)
},
Response::from_status(StatusCode::BAD_REQUEST).with_body_text_plain(&body)
}
};

Ok(resp)
Expand Down
27 changes: 20 additions & 7 deletions test-fixtures/src/bin/kv_store.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
//! A guest program to test that KV store works properly.
use fastly::kv_store::{KVStore, KVStoreError::KVStoreNotFound};
use fastly::kv_store::{
KVStore,
KVStoreError::{ItemNotFound, StoreNotFound},
};

fn main() {
// Check we can't get a store that does not exist
match KVStore::open("non_existant") {
Err(KVStoreNotFound(_)) => {}
Err(StoreNotFound(_)) => {}
_ => panic!(),
}

let store_one = KVStore::open("store_one").unwrap().unwrap();
// Test that we can get data using the `data` config key
assert_eq!(
store_one.lookup_str("first").unwrap().unwrap(),
store_one.lookup("first").unwrap().take_body().into_string(),
"This is some data"
);
// Test that we can get data from a file using the `path` config key
assert_eq!(
store_one.lookup_str("second").unwrap().unwrap(),
store_one
.lookup("second")
.unwrap()
.take_body()
.into_string(),
"More data"
);

let mut empty_store = KVStore::open("empty_store").unwrap().unwrap();
let empty_store = KVStore::open("empty_store").unwrap().unwrap();
// Check that the value "bar" is not in the store
assert_eq!(empty_store.lookup_str("bar"), Ok(None));
match empty_store.lookup("bar") {
Err(ItemNotFound) => {}
_ => panic!(),
}
empty_store.insert("bar", "foo").unwrap();
// Check that the value "bar" is now in the store
assert_eq!(empty_store.lookup_str("bar").unwrap().unwrap(), "foo");
assert_eq!(
empty_store.lookup("bar").unwrap().take_body().into_string(),
"foo"
);
}

0 comments on commit 4af02d5

Please sign in to comment.