Skip to content

Commit

Permalink
Merge pull request #165 from bbutkovic/bbutkovic/geo-impl
Browse files Browse the repository at this point in the history
Implement Geolocation mocking
  • Loading branch information
mgattozzi committed Nov 9, 2022
2 parents 1c2850e + cfc2f72 commit 1905d9a
Show file tree
Hide file tree
Showing 15 changed files with 955 additions and 8 deletions.
2 changes: 2 additions & 0 deletions cli/src/main.rs
Expand Up @@ -42,12 +42,14 @@ pub async fn serve(opts: Opts) -> Result<(), Error> {
if let Some(config_path) = opts.config_path() {
let config = FastlyConfig::from_file(config_path)?;
let backends = config.backends();
let geolocation = config.geolocation();
let dictionaries = config.dictionaries();
let object_store = config.object_store();
let backend_names = itertools::join(backends.keys(), ", ");

ctx = ctx
.with_backends(backends.clone())
.with_geolocation(geolocation.clone())
.with_dictionaries(dictionaries.clone())
.with_object_store(object_store.clone())
.with_config_path(config_path.into());
Expand Down
7 changes: 6 additions & 1 deletion cli/tests/integration/common.rs
Expand Up @@ -8,7 +8,7 @@ use tokio::sync::Mutex;
use tracing_subscriber::filter::EnvFilter;
use viceroy_lib::{
body::Body,
config::{Backend, Backends, Dictionaries, FastlyConfig, ObjectStore},
config::{Backend, Backends, Dictionaries, FastlyConfig, Geolocation, ObjectStore},
ExecuteCtx, ProfilingStrategy, ViceroyService,
};

Expand Down Expand Up @@ -47,6 +47,7 @@ pub struct Test {
module_path: PathBuf,
backends: Backends,
dictionaries: Dictionaries,
geolocation: Geolocation,
object_store: ObjectStore,
hosts: Vec<HostSpec>,
log_stdout: bool,
Expand All @@ -64,6 +65,7 @@ impl Test {
module_path,
backends: Backends::new(),
dictionaries: Dictionaries::new(),
geolocation: Geolocation::new(),
object_store: ObjectStore::new(),
hosts: Vec::new(),
log_stdout: false,
Expand All @@ -81,6 +83,7 @@ impl Test {
module_path,
backends: Backends::new(),
dictionaries: Dictionaries::new(),
geolocation: Geolocation::new(),
object_store: ObjectStore::new(),
hosts: Vec::new(),
log_stdout: false,
Expand All @@ -95,6 +98,7 @@ impl Test {
Ok(Self {
backends: config.backends().to_owned(),
dictionaries: config.dictionaries().to_owned(),
geolocation: config.geolocation().to_owned(),
object_store: config.object_store().to_owned(),
..self
})
Expand Down Expand Up @@ -187,6 +191,7 @@ impl Test {
.expect("failed to set up execution context")
.with_backends(self.backends.clone())
.with_dictionaries(self.dictionaries.clone())
.with_geolocation(self.geolocation.clone())
.with_object_store(self.object_store.clone())
.with_log_stderr(self.log_stderr)
.with_log_stdout(self.log_stdout);
Expand Down
122 changes: 122 additions & 0 deletions cli/tests/integration/geolocation_lookup.rs
@@ -0,0 +1,122 @@
use crate::common::{Test, TestResult};
use hyper::{body::to_bytes, StatusCode};

#[tokio::test(flavor = "multi_thread")]
async fn json_geolocation_lookup_works() -> TestResult {
const FASTLY_TOML: &str = r#"
name = "json-geolocation-lookup"
description = "json geolocation lookup test"
authors = ["Test User <test_user@fastly.com>"]
language = "rust"
[local_server]
[local_server.geolocation]
use_default_loopback = false
file = "../test-fixtures/data/geolocation-mapping.json"
format = "json"
"#;

let resp = Test::using_fixture("geolocation-lookup.wasm")
.using_fastly_toml(FASTLY_TOML)?
.against_empty()
.await;

assert_eq!(resp.status(), StatusCode::OK);
assert!(to_bytes(resp.into_body())
.await
.expect("can read body")
.to_vec()
.is_empty());

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn inline_toml_geolocation_lookup_works() -> TestResult {
const FASTLY_TOML: &str = r#"
name = "inline-toml-geolocation-lookup"
description = "inline toml geolocation lookup test"
authors = ["Test User <test_user@fastly.com>"]
language = "rust"
[local_server]
[local_server.geolocation]
use_default_loopback = false
format = "inline-toml"
[local_server.geolocation.addresses]
[local_server.geolocation.addresses."127.0.0.1"]
as_name = "Fastly Test"
as_number = 12345
area_code = 123
city = "Test City"
conn_speed = "broadband"
conn_type = "wired"
continent = "NA"
country_code = "CA"
country_code3 = "CAN"
country_name = "Canada"
latitude = 12.345
longitude = 54.321
metro_code = 0
postal_code = "12345"
proxy_description = "?"
proxy_type = "?"
region = "BC"
utc_offset = -700
[local_server.geolocation.addresses."0000:0000:0000:0000:0000:0000:0000:0001"]
as_name = "Fastly Test IPv6"
as_number = 12345
area_code = 123
city = "Test City IPv6"
conn_speed = "broadband"
conn_type = "wired"
continent = "NA"
country_code = "CA"
country_code3 = "CAN"
country_name = "Canada"
latitude = 12.345
longitude = 54.321
metro_code = 0
postal_code = "12345"
proxy_description = "?"
proxy_type = "?"
region = "BC"
utc_offset = -700
"#;

let resp = Test::using_fixture("geolocation-lookup.wasm")
.using_fastly_toml(FASTLY_TOML)?
.against_empty()
.await;

assert_eq!(resp.status(), StatusCode::OK);
assert!(to_bytes(resp.into_body())
.await
.expect("can read body")
.to_vec()
.is_empty());

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn default_configuration_geolocation_lookup_works() -> TestResult {
const FASTLY_TOML: &str = r#"
name = "default-config-geolocation-lookup"
description = "default config geolocation lookup test"
authors = ["Test User <test_user@fastly.com>"]
language = "rust"
"#;

let resp = Test::using_fixture("geolocation-lookup-default.wasm")
.using_fastly_toml(FASTLY_TOML)?
.against_empty()
.await;

assert_eq!(resp.status(), StatusCode::OK);
assert!(to_bytes(resp.into_body())
.await
.expect("can read body")
.to_vec()
.is_empty());

Ok(())
}
1 change: 1 addition & 0 deletions cli/tests/integration/main.rs
Expand Up @@ -3,6 +3,7 @@ mod common;
mod dictionary_lookup;
mod downstream_req;
mod env_vars;
mod geolocation_lookup;
mod http_semantics;
mod logging;
mod memory;
Expand Down
17 changes: 17 additions & 0 deletions lib/src/config.rs
Expand Up @@ -28,6 +28,10 @@ mod backends;
pub use self::backends::Backend;
pub type Backends = HashMap<String, Arc<Backend>>;

/// Types and deserializers for geolocation configuration settings.
mod geolocation;
pub use self::geolocation::Geolocation;

/// Types and deserializers for object store configuration settings.
mod object_store;
pub use crate::object_store::ObjectStore;
Expand Down Expand Up @@ -70,6 +74,10 @@ impl FastlyConfig {
&self.local_server.backends.0
}

pub fn geolocation(&self) -> &Geolocation {
&self.local_server.geolocation
}

/// Get the dictionaries configuration.
pub fn dictionaries(&self) -> &Dictionaries {
&self.local_server.dictionaries.0
Expand Down Expand Up @@ -158,6 +166,7 @@ impl TryInto<FastlyConfig> for TomlFastlyConfig {
#[derive(Clone, Debug, Default)]
pub struct LocalServerConfig {
backends: BackendsConfig,
geolocation: Geolocation,
dictionaries: DictionariesConfig,
object_store: ObjectStoreConfig,
}
Expand All @@ -169,6 +178,7 @@ pub struct LocalServerConfig {
#[derive(Deserialize)]
struct RawLocalServerConfig {
backends: Option<Table>,
geolocation: Option<Table>,
dictionaries: Option<Table>,
object_store: Option<Table>,
}
Expand All @@ -178,6 +188,7 @@ impl TryInto<LocalServerConfig> for RawLocalServerConfig {
fn try_into(self) -> Result<LocalServerConfig, Self::Error> {
let Self {
backends,
geolocation,
dictionaries,
object_store,
} = self;
Expand All @@ -186,6 +197,11 @@ impl TryInto<LocalServerConfig> for RawLocalServerConfig {
} else {
BackendsConfig::default()
};
let geolocation = if let Some(geolocation) = geolocation {
geolocation.try_into()?
} else {
Geolocation::default()
};
let dictionaries = if let Some(dictionaries) = dictionaries {
dictionaries.try_into()?
} else {
Expand All @@ -199,6 +215,7 @@ impl TryInto<LocalServerConfig> for RawLocalServerConfig {

Ok(LocalServerConfig {
backends,
geolocation,
dictionaries,
object_store,
})
Expand Down

0 comments on commit 1905d9a

Please sign in to comment.