363 lines
15 KiB
Rust
363 lines
15 KiB
Rust
#[cfg(test)]
|
|
mod cache_revision_tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn operational_saves_keep_cache_identity_but_cache_affecting_saves_rotate_it() {
|
|
let base = config::demo_config("127.0.0.1:9000".parse().unwrap());
|
|
let identity = cache_revision(&base);
|
|
|
|
let mut file_transport = base.clone();
|
|
file_transport
|
|
.web_services
|
|
.push(crate::web_server::Service {
|
|
id: "static-test".into(),
|
|
name: "Static Test".into(),
|
|
document_root: "/var/www/static-test".into(),
|
|
..Default::default()
|
|
});
|
|
let mut zero_copy = file_transport.clone();
|
|
zero_copy.web_services[0].sendfile = crate::web_server::SendfileMode::Auto;
|
|
assert_eq!(cache_revision(&file_transport), cache_revision(&zero_copy));
|
|
assert!(matches!(
|
|
cache_invalidation(&file_transport, &zero_copy),
|
|
CacheInvalidation::None
|
|
));
|
|
|
|
let mut logging = base.clone();
|
|
logging.logging.file.path = "/tmp/rustproxy-test-access.jsonl".into();
|
|
assert_eq!(cache_revision(&logging), identity);
|
|
assert!(matches!(
|
|
cache_invalidation(&base, &logging),
|
|
CacheInvalidation::None
|
|
));
|
|
|
|
let mut metrics = base.clone();
|
|
metrics.prometheus.include_system = !metrics.prometheus.include_system;
|
|
assert_eq!(cache_revision(&metrics), identity);
|
|
|
|
let mut response = base.clone();
|
|
response.server_header.value = "edge-test".into();
|
|
response.diagnostic_headers.request_id = !response.diagnostic_headers.request_id;
|
|
response.compression.gzip = !response.compression.gzip;
|
|
assert_eq!(cache_revision(&response), identity);
|
|
|
|
let mut docker = base.clone();
|
|
docker.docker_agents.agents.push(crate::docker::Agent {
|
|
id: "node1".into(),
|
|
name: "Node 1".into(),
|
|
..Default::default()
|
|
});
|
|
assert_eq!(cache_revision(&docker), identity);
|
|
|
|
let mut preset = base.clone();
|
|
preset.cache.memory_preset = Some(crate::cache::MemoryPreset::Recommended);
|
|
preset.cache.cold_preset = Some(crate::cache::ColdPreset::Recommended);
|
|
preset.transform.preset = Some(crate::transform::Preset::Uber);
|
|
preset.transform.memory_budget_bytes = 1024 * 1024 * 1024;
|
|
preset.transform.max_inflight = 128;
|
|
assert_eq!(cache_revision(&preset), identity);
|
|
assert!(matches!(
|
|
cache_invalidation(&base, &preset),
|
|
CacheInvalidation::None
|
|
));
|
|
|
|
let mut transform_policy = base.clone();
|
|
transform_policy.transform.json = !transform_policy.transform.json;
|
|
assert_ne!(cache_revision(&transform_policy), identity);
|
|
|
|
let mut transform_body_limit = base.clone();
|
|
transform_body_limit.transform.max_body_bytes += 1024;
|
|
assert_ne!(cache_revision(&transform_body_limit), identity);
|
|
|
|
let mut wire_only = base.clone();
|
|
wire_only.cache.bypass_query = !wire_only.cache.bypass_query;
|
|
wire_only.cache.bypass_prefixes.push("/private".into());
|
|
wire_only.cache.expose_status = !wire_only.cache.expose_status;
|
|
wire_only.cache.expose_hits = !wire_only.cache.expose_hits;
|
|
wire_only.cache.expose_cache_status = !wire_only.cache.expose_cache_status;
|
|
wire_only.cache.expose_resident_age = !wire_only.cache.expose_resident_age;
|
|
wire_only.cache.expose_only_hits = !wire_only.cache.expose_only_hits;
|
|
wire_only.cache.via_header = Some("RustProxy edge".into());
|
|
wire_only.cache.http_invalidation_allowed_cidrs = vec!["192.0.2.42".into()];
|
|
assert_eq!(cache_revision(&wire_only), identity);
|
|
assert!(matches!(
|
|
cache_invalidation(&base, &wire_only),
|
|
CacheInvalidation::None
|
|
));
|
|
|
|
for label in ["http2", "http3"] {
|
|
let mut protocol_only = base.clone();
|
|
if label == "http2" {
|
|
protocol_only.vhosts[0].http2_enabled = !base.vhosts[0].http2_enabled;
|
|
} else {
|
|
protocol_only.vhosts[0].http3_enabled = !base.vhosts[0].http3_enabled;
|
|
}
|
|
assert_ne!(
|
|
(
|
|
protocol_only.vhosts[0].http2_enabled,
|
|
protocol_only.vhosts[0].http3_enabled
|
|
),
|
|
(base.vhosts[0].http2_enabled, base.vhosts[0].http3_enabled),
|
|
"{label} toggle did not change a transport switch"
|
|
);
|
|
assert_eq!(
|
|
cache_revision(&protocol_only),
|
|
identity,
|
|
"{label} rotated cache"
|
|
);
|
|
assert!(matches!(
|
|
cache_invalidation(&base, &protocol_only),
|
|
CacheInvalidation::None
|
|
));
|
|
assert!(matches!(
|
|
cache_invalidation(&protocol_only, &base),
|
|
CacheInvalidation::None
|
|
));
|
|
}
|
|
let mut both_protocols = base.clone();
|
|
both_protocols.vhosts[0].http2_enabled = !base.vhosts[0].http2_enabled;
|
|
both_protocols.vhosts[0].http3_enabled = !base.vhosts[0].http3_enabled;
|
|
assert_eq!(cache_revision(&both_protocols), identity);
|
|
assert!(matches!(
|
|
cache_invalidation(&base, &both_protocols),
|
|
CacheInvalidation::None
|
|
));
|
|
|
|
let mut vhost_diagnostics = base.clone();
|
|
vhost_diagnostics.vhosts[0].cache.expose_only_hits = Some(true);
|
|
assert_eq!(cache_revision(&vhost_diagnostics), identity);
|
|
assert!(matches!(
|
|
cache_invalidation(&base, &vhost_diagnostics),
|
|
CacheInvalidation::None
|
|
));
|
|
|
|
let mut tuning = base.clone();
|
|
tuning.cache.max_entries += 1;
|
|
tuning.cache.max_bytes *= 2;
|
|
tuning.cache.max_object_bytes /= 2;
|
|
tuning.cache.transient_max_bytes *= 2;
|
|
tuning.cache.hot_eviction_max_objects += 1;
|
|
tuning.cache.hot_auto_trim_enabled = !base.cache.hot_auto_trim_enabled;
|
|
tuning.cache.hot_auto_trim_after_seconds += 10;
|
|
tuning.cache.hot_auto_trim_target_percent = 70;
|
|
tuning.cache.cold_max_bytes *= 2;
|
|
tuning.cache.cold_max_entries += 1;
|
|
tuning.cache.cold_gc_interval_seconds += 1;
|
|
tuning.cache.cold_gc_max_delete += 1;
|
|
tuning.cache.cold_after_seconds += 1;
|
|
tuning.cache.cold_on_idle = !base.cache.cold_on_idle;
|
|
tuning.cache.cold_max_hot_seconds += 1;
|
|
tuning.cache.cold_on_age = !base.cache.cold_on_age;
|
|
tuning.cache.cold_on_lru = !base.cache.cold_on_lru;
|
|
tuning.cache.cold_on_shutdown = !base.cache.cold_on_shutdown;
|
|
tuning.cache.collapse_enabled = !base.cache.collapse_enabled;
|
|
tuning.cache.collapse_wait_timeout_ms += 1;
|
|
tuning.cache.stale_if_error_timeout = !base.cache.stale_if_error_timeout;
|
|
tuning.cache.stale_if_error_statuses = vec![503];
|
|
tuning.cache.proactive_refresh_enabled = !base.cache.proactive_refresh_enabled;
|
|
tuning.cache.proactive_refresh_percent = 20;
|
|
tuning.cache.proactive_refresh_max_inflight = 8;
|
|
assert_eq!(cache_revision(&tuning), identity);
|
|
assert!(matches!(
|
|
cache_invalidation(&base, &tuning),
|
|
CacheInvalidation::None
|
|
));
|
|
|
|
for change in [
|
|
(|c: &mut config::Config| c.cache.default_ttl_seconds += 60) as fn(&mut config::Config),
|
|
|c| c.cache.max_ttl_seconds += 60,
|
|
|c| c.cache.varnish_grace_seconds += 1,
|
|
|c| c.cache.varnish_compatible = !c.cache.varnish_compatible,
|
|
|c| c.cache.static_ignore_cookies = !c.cache.static_ignore_cookies,
|
|
|c| c.cache.static_ignore_vary_cookie = !c.cache.static_ignore_vary_cookie,
|
|
|c| c.cache.static_extensions.push("json".into()),
|
|
|c| c.cache.cold_enabled = !c.cache.cold_enabled,
|
|
|c| c.cache.rules.push(serde_json::from_value(serde_json::json!(
|
|
{"id": "x", "conditions": [{"field": "path", "operator": "prefix", "values": ["/a"]}],
|
|
"actions": [{"type": "pass"}]})).unwrap()),
|
|
] {
|
|
let mut changed = base.clone();
|
|
change(&mut changed);
|
|
assert_ne!(cache_revision(&changed), identity);
|
|
assert!(matches!(cache_invalidation(&base, &changed), CacheInvalidation::All(_)));
|
|
}
|
|
|
|
let mut cache = base.clone();
|
|
cache.cache.default_ttl_seconds += 1;
|
|
let cache_identity = cache_revision(&cache);
|
|
assert_ne!(cache_identity, identity);
|
|
assert!(matches!(
|
|
cache_invalidation(&base, &cache),
|
|
CacheInvalidation::All(value) if value == cache_identity
|
|
));
|
|
|
|
let mut operational = base.clone();
|
|
operational.vhosts[0].load_balancing.keepalive.enabled = false;
|
|
operational.vhosts[0]
|
|
.load_balancing
|
|
.keepalive
|
|
.idle_timeout_seconds = 90;
|
|
operational.vhosts[0]
|
|
.load_balancing
|
|
.keepalive
|
|
.max_idle_per_host = 32;
|
|
operational.vhosts[0]
|
|
.load_balancing
|
|
.keepalive
|
|
.tcp_keepalive_seconds = 90;
|
|
operational.vhosts[0]
|
|
.load_balancing
|
|
.keepalive
|
|
.pool_recycle_requests = 1_000;
|
|
operational.vhosts[0]
|
|
.load_balancing
|
|
.keepalive
|
|
.pool_recycle_seconds = 1_800;
|
|
operational.vhosts[0]
|
|
.load_balancing
|
|
.keepalive
|
|
.http1_max_buffer_bytes = 65_536;
|
|
operational.vhosts[0].backends[0].weight += 1;
|
|
operational.vhosts[0].backends[0].max_concurrent_requests = 16;
|
|
operational.vhosts[0].backends[0].queue_timeout_ms = 100;
|
|
operational.vhosts[0].timing_headers = true;
|
|
operational.vhosts[0].acl_profile_id = Some("test-only-acl".into());
|
|
operational.vhosts[0].user_agent_blocking = Default::default();
|
|
operational.vhosts[0].proxy_timeouts.read_timeout_seconds = Some(180);
|
|
assert_eq!(cache_revision(&operational), identity);
|
|
assert_eq!(
|
|
vhost_cache_revision(&operational.vhosts[0]),
|
|
vhost_cache_revision(&base.vhosts[0])
|
|
);
|
|
assert!(matches!(
|
|
cache_invalidation(&base, &operational),
|
|
CacheInvalidation::None
|
|
));
|
|
assert!(matches!(
|
|
cache_invalidation(&operational, &base),
|
|
CacheInvalidation::None
|
|
));
|
|
|
|
let mut vhost = base.clone();
|
|
vhost.vhosts[0].backends[0].url = "http://127.0.0.1:9001".into();
|
|
assert_eq!(cache_revision(&vhost), identity);
|
|
let affected_id = base.vhosts[0].id.clone();
|
|
assert!(
|
|
matches!(cache_invalidation(&base, &vhost), CacheInvalidation::Scopes(ids) if ids == vec![affected_id])
|
|
);
|
|
|
|
let mut multiple = base.clone();
|
|
let mut another = multiple.vhosts[0].clone();
|
|
another.id = "other-scope".into();
|
|
another.frontend.hosts = vec!["other.example.test".into()];
|
|
multiple.vhosts.push(another);
|
|
let mut changed_one = multiple.clone();
|
|
changed_one.vhosts[0].backends[0].url = "http://127.0.0.1:9002".into();
|
|
assert!(
|
|
matches!(cache_invalidation(&multiple, &changed_one), CacheInvalidation::Scopes(ids)
|
|
if ids == vec![base.vhosts[0].id.clone()])
|
|
);
|
|
let mut changed_global = multiple.clone();
|
|
changed_global.transform.max_body_bytes += 1;
|
|
assert!(matches!(
|
|
cache_invalidation(&multiple, &changed_global),
|
|
CacheInvalidation::All(_)
|
|
));
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod headless_config_tests {
|
|
use super::*;
|
|
|
|
fn startup() -> crate::settings::Listeners {
|
|
crate::settings::Listeners::from_startup(
|
|
"127.0.0.1:8080".parse().unwrap(),
|
|
Some("127.0.0.1:8443".parse().unwrap()),
|
|
)
|
|
}
|
|
|
|
fn policy() -> ValidationPolicy {
|
|
ValidationPolicy {
|
|
tls_addr: Some("127.0.0.1:8443".parse().unwrap()),
|
|
cert_dir: None,
|
|
data_dir: None,
|
|
proxy_addr: "127.0.0.1:8080".parse().unwrap(),
|
|
admin_addr: "127.0.0.1:9090".parse().unwrap(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn minimal_document_gets_startup_listeners_and_all_listener_ids() {
|
|
let document = br#"{"vhosts":[{"id":"app","frontend":{"hosts":["*"]},
|
|
"backends":[{"id":"a","url":"http://127.0.0.1:9001","allow_private":true}]}]}"#;
|
|
let config = external_config::headless_config(document, &startup()).unwrap();
|
|
let ids: Vec<_> = config
|
|
.listeners
|
|
.endpoints
|
|
.iter()
|
|
.map(|endpoint| endpoint.id.as_str())
|
|
.collect();
|
|
assert_eq!(ids, ["http", "https"]);
|
|
assert_eq!(config.vhosts[0].frontend.listener_ids, ["http", "https"]);
|
|
assert!(!config.geoip_updates.enabled);
|
|
let http_only =
|
|
crate::settings::Listeners::from_startup("127.0.0.1:8080".parse().unwrap(), None);
|
|
let config = external_config::headless_config(document, &http_only).unwrap();
|
|
let mut policy = policy();
|
|
policy.tls_addr = None;
|
|
let snapshot = config::compile(config, 1, &policy).unwrap();
|
|
assert!(snapshot
|
|
.frontend("10.1.2.3", "127.0.0.1:8080".parse().unwrap(), false)
|
|
.is_some());
|
|
}
|
|
|
|
#[test]
|
|
fn declared_listeners_and_explicit_choices_are_kept() {
|
|
let document = br#"{"listeners":{"endpoints":[
|
|
{"id":"web","name":"Web","scope":"internal","enabled":true,"binding":{"address":"127.0.0.1:8080","tls":false,"http2":false}},
|
|
{"id":"off","name":"Off","scope":"internal","enabled":false,"binding":{"address":"127.0.0.1:8081","tls":false,"http2":false}}]},
|
|
"geoip_updates":{"enabled":true},
|
|
"vhosts":[
|
|
{"id":"a","frontend":{"hosts":["a.example"]},"backends":[{"id":"a","url":"http://127.0.0.1:9001","allow_private":true}]},
|
|
{"id":"b","frontend":{"hosts":["b.example"],"listener_ids":["web"]},"backends":[{"id":"b","url":"http://127.0.0.1:9002","allow_private":true}]}]}"#;
|
|
let config = external_config::headless_config(document, &startup()).unwrap();
|
|
assert_eq!(config.listeners.endpoints.len(), 2);
|
|
assert_eq!(config.vhosts[0].frontend.listener_ids, ["web"]);
|
|
assert_eq!(config.vhosts[1].frontend.listener_ids, ["web"]);
|
|
assert!(config.geoip_updates.enabled);
|
|
assert!(config::compile(config, 1, &policy()).is_ok());
|
|
}
|
|
|
|
#[test]
|
|
fn non_object_documents_are_rejected() {
|
|
assert!(external_config::headless_config(b"[]", &startup()).is_err());
|
|
assert!(external_config::headless_config(b"{\"vhosts\": [", &startup()).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn acme_auto_renewal_is_refused_in_headless_mode() {
|
|
let mut config = config::Config::default();
|
|
config.tls.certificates.push(crate::tls::Certificate {
|
|
id: "site".into(),
|
|
covered_hosts: Vec::new(),
|
|
hosts: vec!["example.org".into()],
|
|
cert_file: "site.pem".into(),
|
|
key_file: "site.key".into(),
|
|
issuer: Some(crate::tls::Issuer {
|
|
provider: "cloudflare".into(),
|
|
environment: "staging".into(),
|
|
auto_renew: true,
|
|
}),
|
|
});
|
|
assert!(ensure_headless_supported(&config).is_err());
|
|
config.tls.certificates[0]
|
|
.issuer
|
|
.as_mut()
|
|
.unwrap()
|
|
.auto_renew = false;
|
|
assert!(ensure_headless_supported(&config).is_ok());
|
|
}
|
|
}
|