v0.25.1
This commit is contained in:
1 parent
87e571c170
commit
fa7abc9e53
308 files changed
+49746
-50237
No files matched your search
@@ -0,0 +1,362 @@
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn observation(service: Service, addresses: Vec<HostAddress>) -> BTreeMap<String, Observation> {
|
||||
let inventory = Inventory {
|
||||
agent_version: "0.22.3".into(),
|
||||
hostname: "node1".into(),
|
||||
public_ip: "10.2.3.4".into(),
|
||||
public_ip_source: "route".into(),
|
||||
certificate_sha256: String::new(),
|
||||
addresses,
|
||||
observed_at: "now".into(),
|
||||
services: vec![service],
|
||||
};
|
||||
let mut observed = BTreeMap::new();
|
||||
observed.insert(
|
||||
"node1".into(),
|
||||
Observation {
|
||||
agent_id: "node1".into(),
|
||||
ok: true,
|
||||
checked_at_unix_ms: 1,
|
||||
error: None,
|
||||
inventory: Some(inventory),
|
||||
},
|
||||
);
|
||||
observed
|
||||
}
|
||||
|
||||
fn service() -> Service {
|
||||
Service {
|
||||
key: "container:app:8080/tcp".into(),
|
||||
technical_name: "app-8080".into(),
|
||||
container_id: "abc".into(),
|
||||
container_name: "app".into(),
|
||||
image: "demo".into(),
|
||||
private_port: 8080,
|
||||
protocol: "tcp".into(),
|
||||
bindings: vec![Binding {
|
||||
bind_ip: "0.0.0.0".into(),
|
||||
public_port: 18080,
|
||||
}],
|
||||
public_port: 0,
|
||||
bind_ip: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generated_vhost_supports_base_domain_custom_domain_aliases_and_selected_ip() {
|
||||
let mut desired = crate::config::demo_config("127.0.0.1:9000".parse().unwrap());
|
||||
desired.vhosts.clear();
|
||||
desired.docker_agents.agents.push(Agent {
|
||||
id: "node1".into(),
|
||||
name: "Node 1".into(),
|
||||
endpoint: "http://10.0.0.2:9876".into(),
|
||||
token: "012345678901234567890123".into(),
|
||||
tls_fingerprint_sha256: None,
|
||||
base_domain: "docker1.lab.local".into(),
|
||||
poll_interval_seconds: 30,
|
||||
certificate_id: Some("docker-cert".into()),
|
||||
acl_profile_id: None,
|
||||
default_target_ip: None,
|
||||
listener_ids: vec!["http".into()],
|
||||
assignments: vec![Assignment {
|
||||
service_key: "container:app:8080/tcp".into(),
|
||||
hostname: "app1".into(),
|
||||
aliases: vec!["app-alt".into(), "myapp.example.com".into()],
|
||||
target_ip: Some("10.2.3.5".parse().unwrap()),
|
||||
force_ssl: Some(true),
|
||||
websockets: false,
|
||||
cache_enabled: true,
|
||||
..Default::default()
|
||||
}],
|
||||
enabled: true,
|
||||
});
|
||||
desired.docker_agents.agents[0].acl_profile_id = Some("office".into());
|
||||
let observed = observation(
|
||||
service(),
|
||||
vec![
|
||||
HostAddress {
|
||||
ip: "10.2.3.4".into(),
|
||||
source: "interface".into(),
|
||||
interface: "eno1".into(),
|
||||
scope: "private".into(),
|
||||
},
|
||||
HostAddress {
|
||||
ip: "10.2.3.5".into(),
|
||||
source: "interface".into(),
|
||||
interface: "eno2".into(),
|
||||
scope: "private".into(),
|
||||
},
|
||||
],
|
||||
);
|
||||
let effective = effective_config(desired, &observed).unwrap();
|
||||
assert_eq!(effective.vhosts.len(), 1);
|
||||
assert_eq!(
|
||||
effective.vhosts[0].frontend.hosts,
|
||||
vec![
|
||||
"app1.docker1.lab.local",
|
||||
"app-alt.docker1.lab.local",
|
||||
"myapp.example.com"
|
||||
]
|
||||
);
|
||||
assert_eq!(effective.vhosts[0].backends[0].url, "http://10.2.3.5:18080");
|
||||
assert_eq!(
|
||||
effective.vhosts[0].certificate_id.as_deref(),
|
||||
Some("docker-cert")
|
||||
);
|
||||
assert!(effective.vhosts[0].force_ssl);
|
||||
assert!(!effective.vhosts[0].websockets);
|
||||
assert!(effective.vhosts[0].cache.enabled.unwrap_or(false));
|
||||
assert_eq!(
|
||||
effective.vhosts[0].acl_profile_id.as_deref(),
|
||||
Some("office")
|
||||
);
|
||||
assert_eq!(
|
||||
effective.vhosts[0].load_balancing.health_check.mode,
|
||||
crate::balance::HealthMode::Tcp
|
||||
);
|
||||
assert!(!effective.vhosts[0].load_balancing.passive_health.enabled);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn docker_alias_count_is_not_artificially_capped() {
|
||||
let assignment = Assignment {
|
||||
hostname: "app".into(),
|
||||
aliases: (0..40).map(|index| format!("alias-{index}")).collect(),
|
||||
..Default::default()
|
||||
};
|
||||
let hosts = assignment_hosts(&assignment, "docker.example.test").unwrap();
|
||||
assert_eq!(hosts.len(), 41);
|
||||
assert_eq!(hosts[0], "app.docker.example.test");
|
||||
assert_eq!(hosts[40], "alias-39.docker.example.test");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generated_vhost_inherits_global_server_header_policy() {
|
||||
let mut desired = crate::config::demo_config("127.0.0.1:9000".parse().unwrap());
|
||||
desired.vhosts.clear();
|
||||
desired.server_header = crate::settings::ServerHeader {
|
||||
mode: crate::settings::ServerMode::Custom,
|
||||
value: "RustProxy".into(),
|
||||
};
|
||||
desired.docker_agents.agents.push(Agent {
|
||||
id: "node1".into(),
|
||||
name: "Node 1".into(),
|
||||
endpoint: "http://10.0.0.2:9876".into(),
|
||||
token: "012345678901234567890123".into(),
|
||||
base_domain: "docker1.lab.local".into(),
|
||||
listener_ids: vec!["http".into()],
|
||||
assignments: vec![Assignment {
|
||||
service_key: "container:app:8080/tcp".into(),
|
||||
hostname: "app".into(),
|
||||
..Default::default()
|
||||
}],
|
||||
..Default::default()
|
||||
});
|
||||
let observed = observation(
|
||||
service(),
|
||||
vec![HostAddress {
|
||||
ip: "10.2.3.4".into(),
|
||||
source: "interface".into(),
|
||||
interface: "eno1".into(),
|
||||
scope: "private".into(),
|
||||
}],
|
||||
);
|
||||
let effective = effective_config(desired, &observed).unwrap();
|
||||
assert!(effective.vhosts[0].server_header.is_none());
|
||||
|
||||
let policy = crate::config::ValidationPolicy {
|
||||
proxy_addr: "127.0.0.1:8080".parse().unwrap(),
|
||||
admin_addr: "127.0.0.1:9090".parse().unwrap(),
|
||||
tls_addr: None,
|
||||
cert_dir: None,
|
||||
data_dir: None,
|
||||
};
|
||||
let snapshot = crate::config::compile(effective, 0, &policy).unwrap();
|
||||
let frontend = snapshot
|
||||
.frontend(
|
||||
"app.docker1.lab.local",
|
||||
"127.0.0.1:8080".parse().unwrap(),
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
assert!(matches!(
|
||||
frontend.server_header.mode,
|
||||
crate::settings::ServerMode::Custom
|
||||
));
|
||||
assert_eq!(frontend.server_header.value, "RustProxy");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_assignment_defaults_keep_redirect_off_websockets_on_and_cache_off() {
|
||||
let assignment: Assignment = serde_json::from_value(serde_json::json!({
|
||||
"service_key": "container:app:8080/tcp",
|
||||
"hostname": "app"
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(assignment.force_ssl, None);
|
||||
assert!(assignment.websockets);
|
||||
assert!(!assignment.cache_enabled);
|
||||
assert!(!assignment.passive_health.enabled);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn docker_assignment_propagates_explicit_passive_health_policy() {
|
||||
let mut desired = crate::config::demo_config("127.0.0.1:9000".parse().unwrap());
|
||||
desired.vhosts.clear();
|
||||
desired.docker_agents.agents.push(Agent {
|
||||
id: "node1".into(),
|
||||
name: "Node 1".into(),
|
||||
endpoint: "http://10.0.0.2:9876".into(),
|
||||
token: "012345678901234567890123".into(),
|
||||
base_domain: "docker1.lab.local".into(),
|
||||
listener_ids: vec!["http".into()],
|
||||
assignments: vec![Assignment {
|
||||
service_key: "container:app:8080/tcp".into(),
|
||||
hostname: "app".into(),
|
||||
passive_health: crate::balance::PassiveHealth {
|
||||
enabled: true,
|
||||
count_http_5xx: false,
|
||||
count_slow_responses: true,
|
||||
fall: Some(4),
|
||||
rise: Some(3),
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
}],
|
||||
..Default::default()
|
||||
});
|
||||
let observed = observation(
|
||||
service(),
|
||||
vec![HostAddress {
|
||||
ip: "10.2.3.4".into(),
|
||||
source: "interface".into(),
|
||||
interface: "eno1".into(),
|
||||
scope: "private".into(),
|
||||
}],
|
||||
);
|
||||
let effective = effective_config(desired, &observed).unwrap();
|
||||
let passive = &effective.vhosts[0].load_balancing.passive_health;
|
||||
assert!(passive.enabled);
|
||||
assert!(!passive.count_http_5xx);
|
||||
assert!(passive.count_slow_responses);
|
||||
assert_eq!(passive.fall, Some(4));
|
||||
assert_eq!(passive.rise, Some(3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn docker_assignment_passive_health_survives_serialization_round_trip() {
|
||||
let original = Assignment {
|
||||
service_key: "container:app:8080/tcp".into(),
|
||||
hostname: "app".into(),
|
||||
passive_health: crate::balance::PassiveHealth {
|
||||
enabled: true,
|
||||
count_connect_errors: false,
|
||||
count_timeouts: true,
|
||||
count_io_errors: false,
|
||||
count_http_5xx: true,
|
||||
count_slow_responses: true,
|
||||
fall: Some(5),
|
||||
rise: Some(4),
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
let encoded = serde_json::to_value(&original).unwrap();
|
||||
let restored: Assignment = serde_json::from_value(encoded).unwrap();
|
||||
let passive = restored.passive_health;
|
||||
assert!(passive.enabled);
|
||||
assert!(!passive.count_connect_errors);
|
||||
assert!(passive.count_timeouts);
|
||||
assert!(!passive.count_io_errors);
|
||||
assert!(passive.count_http_5xx);
|
||||
assert!(passive.count_slow_responses);
|
||||
assert_eq!(passive.fall, Some(5));
|
||||
assert_eq!(passive.rise, Some(4));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn force_ssl_assignment_requires_plain_and_tls_listener_pair() {
|
||||
let listeners = crate::settings::Listeners::from_startup(
|
||||
"127.0.0.1:8080".parse().unwrap(),
|
||||
Some("127.0.0.1:8443".parse().unwrap()),
|
||||
);
|
||||
let agent = Agent {
|
||||
id: "node1".into(),
|
||||
name: "Node 1".into(),
|
||||
endpoint: "http://10.0.0.2:9876".into(),
|
||||
token: "012345678901234567890123".into(),
|
||||
base_domain: "docker1.lab.local".into(),
|
||||
listener_ids: vec!["http".into(), "https".into()],
|
||||
assignments: vec![Assignment {
|
||||
service_key: "container:app:8080/tcp".into(),
|
||||
hostname: "app".into(),
|
||||
force_ssl: Some(true),
|
||||
..Default::default()
|
||||
}],
|
||||
..Default::default()
|
||||
};
|
||||
let mut valid = Settings {
|
||||
agents: vec![agent.clone()],
|
||||
};
|
||||
valid.validate(&listeners).unwrap();
|
||||
let mut invalid = Settings {
|
||||
agents: vec![Agent {
|
||||
listener_ids: vec!["http".into()],
|
||||
..agent
|
||||
}],
|
||||
};
|
||||
assert!(invalid.validate(&listeners).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn concrete_docker_binding_keeps_its_own_port() {
|
||||
let service = Service {
|
||||
bindings: vec![
|
||||
Binding {
|
||||
bind_ip: "10.2.3.4".into(),
|
||||
public_port: 18080,
|
||||
},
|
||||
Binding {
|
||||
bind_ip: "10.2.3.5".into(),
|
||||
public_port: 28080,
|
||||
},
|
||||
],
|
||||
..service()
|
||||
};
|
||||
assert_eq!(
|
||||
port_for_ip(&service, "10.2.3.4".parse().unwrap()),
|
||||
Some(18080)
|
||||
);
|
||||
assert_eq!(
|
||||
port_for_ip(&service, "10.2.3.5".parse().unwrap()),
|
||||
Some(28080)
|
||||
);
|
||||
assert_eq!(port_for_ip(&service, "10.2.3.6".parse().unwrap()), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tls_fingerprint_normalizes_common_copy_paste_format() {
|
||||
let raw = "AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99";
|
||||
let normalized = normalize_fingerprint(raw).unwrap();
|
||||
assert_eq!(normalized.len(), 64);
|
||||
assert_eq!(normalized, normalized.to_ascii_lowercase());
|
||||
assert_eq!(fingerprint_bytes(&normalized).unwrap()[0], 0xaa);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generated_vhost_id_stays_within_config_limit() {
|
||||
let id = generated_id(
|
||||
"agent-name-that-is-deliberately-far-longer-than-the-config-id-limit",
|
||||
"compose:very-long-project:very-long-service:12345/tcp",
|
||||
);
|
||||
assert!(
|
||||
id.len() <= 64,
|
||||
"generated Docker vhost id is too long: {id}"
|
||||
);
|
||||
assert!(
|
||||
crate::config::valid_id(&id),
|
||||
"generated Docker vhost id is invalid: {id}"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user