[edge] Reload and share links: in-process config swap with self-reverting trials, admin socket with health and certificate status, share-link cookies, visitor pages and throttle
This commit is contained in:
+89
-22
@@ -44,9 +44,14 @@ mod unix {
|
||||
}
|
||||
|
||||
async fn generation(edge: &Edge) -> String {
|
||||
let r = client::https(edge.https, "app.test", edge.roots(), &Req::get("app.test", "/"))
|
||||
.await
|
||||
.unwrap();
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"app.test",
|
||||
edge.roots(),
|
||||
&Req::get("app.test", "/"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 200, "{}", r.text());
|
||||
r.header("x-generation").unwrap_or("none").to_owned()
|
||||
}
|
||||
@@ -118,7 +123,11 @@ mod unix {
|
||||
let mut reloads = 0u64;
|
||||
let mut refused = 0u64;
|
||||
while started.elapsed() < Duration::from_secs(secs) {
|
||||
let text = if reloads % 2 == 0 { &config_b } else { &config_a };
|
||||
let text = if reloads.is_multiple_of(2) {
|
||||
&config_b
|
||||
} else {
|
||||
&config_a
|
||||
};
|
||||
let tmp = edge.config_path().with_extension("tmp");
|
||||
std::fs::write(&tmp, text).unwrap();
|
||||
std::fs::rename(&tmp, edge.config_path()).unwrap();
|
||||
@@ -237,7 +246,11 @@ mod unix {
|
||||
|
||||
// Not confirmed: back to "a" after the deadline, without anyone asking.
|
||||
let r = edge
|
||||
.admin("POST", "/v1/trial", Some(json!({ "confirm_within_secs": 2 })))
|
||||
.admin(
|
||||
"POST",
|
||||
"/v1/trial",
|
||||
Some(json!({ "confirm_within_secs": 2 })),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, 200, "{}", r.text);
|
||||
let trial_sha = r.json["config_sha256"].as_str().unwrap().to_owned();
|
||||
@@ -248,9 +261,13 @@ mod unix {
|
||||
// No reload and no second trial while one runs.
|
||||
assert_eq!(edge.admin("POST", "/v1/reload", None).await.status, 409);
|
||||
assert_eq!(
|
||||
edge.admin("POST", "/v1/trial", Some(json!({ "confirm_within_secs": 2 })))
|
||||
.await
|
||||
.status,
|
||||
edge.admin(
|
||||
"POST",
|
||||
"/v1/trial",
|
||||
Some(json!({ "confirm_within_secs": 2 }))
|
||||
)
|
||||
.await
|
||||
.status,
|
||||
409
|
||||
);
|
||||
tokio::time::sleep(Duration::from_millis(3500)).await;
|
||||
@@ -260,21 +277,37 @@ mod unix {
|
||||
assert_eq!(s.json["config_sha256"], sha256(&edge.config_text));
|
||||
// Confirming a trial that already reverted fails.
|
||||
let r = edge
|
||||
.admin("POST", "/v1/confirm", Some(json!({ "config_sha256": trial_sha })))
|
||||
.admin(
|
||||
"POST",
|
||||
"/v1/confirm",
|
||||
Some(json!({ "config_sha256": trial_sha })),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, 409, "{}", r.text);
|
||||
|
||||
// Confirmed: stays.
|
||||
let r = edge
|
||||
.admin("POST", "/v1/trial", Some(json!({ "confirm_within_secs": 2 })))
|
||||
.admin(
|
||||
"POST",
|
||||
"/v1/trial",
|
||||
Some(json!({ "confirm_within_secs": 2 })),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, 200, "{}", r.text);
|
||||
let wrong = edge
|
||||
.admin("POST", "/v1/confirm", Some(json!({ "config_sha256": "00" })))
|
||||
.admin(
|
||||
"POST",
|
||||
"/v1/confirm",
|
||||
Some(json!({ "config_sha256": "00" })),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(wrong.status, 409);
|
||||
let r = edge
|
||||
.admin("POST", "/v1/confirm", Some(json!({ "config_sha256": trial_sha })))
|
||||
.admin(
|
||||
"POST",
|
||||
"/v1/confirm",
|
||||
Some(json!({ "config_sha256": trial_sha })),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, 200, "{}", r.text);
|
||||
tokio::time::sleep(Duration::from_millis(2500)).await;
|
||||
@@ -283,7 +316,11 @@ mod unix {
|
||||
// Aborted: back at once. Abort is idempotent.
|
||||
std::fs::write(edge.trial_path(), &edge.config_text).unwrap();
|
||||
let r = edge
|
||||
.admin("POST", "/v1/trial", Some(json!({ "confirm_within_secs": 30 })))
|
||||
.admin(
|
||||
"POST",
|
||||
"/v1/trial",
|
||||
Some(json!({ "confirm_within_secs": 30 })),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, 200, "{}", r.text);
|
||||
assert_eq!(generation(&edge).await, "a");
|
||||
@@ -298,7 +335,11 @@ mod unix {
|
||||
// A broken trial is refused and nothing changes.
|
||||
std::fs::write(edge.trial_path(), "{}").unwrap();
|
||||
let r = edge
|
||||
.admin("POST", "/v1/trial", Some(json!({ "confirm_within_secs": 30 })))
|
||||
.admin(
|
||||
"POST",
|
||||
"/v1/trial",
|
||||
Some(json!({ "confirm_within_secs": 30 })),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, 422);
|
||||
assert_eq!(generation(&edge).await, "b");
|
||||
@@ -321,7 +362,10 @@ mod unix {
|
||||
sick.url()
|
||||
),
|
||||
),
|
||||
host_file("plain.test", &format!("upstreams = [\"{}\"]\n", plain.url())),
|
||||
host_file(
|
||||
"plain.test",
|
||||
&format!("upstreams = [\"{}\"]\n", plain.url()),
|
||||
),
|
||||
],
|
||||
..Default::default()
|
||||
});
|
||||
@@ -329,7 +373,10 @@ mod unix {
|
||||
let s = edge.admin("GET", "/v1/status", None).await;
|
||||
assert_eq!(s.status, 200, "{}", s.text);
|
||||
let hosts = s.json["hosts"].as_array().unwrap();
|
||||
let mixed = hosts.iter().find(|h| h["hostname"] == "mixed.test").unwrap();
|
||||
let mixed = hosts
|
||||
.iter()
|
||||
.find(|h| h["hostname"] == "mixed.test")
|
||||
.unwrap();
|
||||
assert_eq!(mixed["health_check"], true);
|
||||
let ups = mixed["upstreams"].as_array().unwrap();
|
||||
assert_eq!(ups.len(), 3);
|
||||
@@ -344,9 +391,17 @@ mod unix {
|
||||
assert_eq!(last(&ups[2])["kind"], "refused");
|
||||
assert!(ups[0]["checks"].as_array().unwrap().len() <= 5);
|
||||
assert!(mixed["certificate"]["state"] == "ok");
|
||||
let plain_h = hosts.iter().find(|h| h["hostname"] == "plain.test").unwrap();
|
||||
let plain_h = hosts
|
||||
.iter()
|
||||
.find(|h| h["hostname"] == "plain.test")
|
||||
.unwrap();
|
||||
assert_eq!(plain_h["health_check"], false);
|
||||
assert!(plain_h["upstreams"][0]["checks"].as_array().unwrap().is_empty());
|
||||
assert!(
|
||||
plain_h["upstreams"][0]["checks"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
);
|
||||
|
||||
// During a trial, watched hosts without a health check get `GET /` probes; any answer
|
||||
// below 500 counts as alive. Hosts with one are checked at least every `every_secs`.
|
||||
@@ -372,8 +427,12 @@ mod unix {
|
||||
assert!(!checks.is_empty(), "{}", s.text);
|
||||
assert_eq!(checks.last().unwrap()["ok"], true);
|
||||
assert_eq!(checks.last().unwrap()["source"], "watch");
|
||||
assert!(plain.requests().iter().any(|r| r.path == "/"
|
||||
&& r.header("host") == Some(b"plain.test".as_slice())));
|
||||
assert!(
|
||||
plain
|
||||
.requests()
|
||||
.iter()
|
||||
.any(|r| r.path == "/" && r.header("host") == Some(b"plain.test".as_slice()))
|
||||
);
|
||||
edge.admin("POST", "/v1/abort", None).await;
|
||||
}
|
||||
|
||||
@@ -391,11 +450,19 @@ mod unix {
|
||||
// Unknown paths and bad bodies get typed errors, not a crash.
|
||||
assert_eq!(edge.admin("GET", "/v1/nope", None).await.status, 404);
|
||||
let r = edge
|
||||
.admin("POST", "/v1/trial", Some(json!({ "confirm_within_secs": 2, "extra": 1 })))
|
||||
.admin(
|
||||
"POST",
|
||||
"/v1/trial",
|
||||
Some(json!({ "confirm_within_secs": 2, "extra": 1 })),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, 400, "{}", r.text);
|
||||
let r = edge
|
||||
.admin("POST", "/v1/trial", Some(json!({ "confirm_within_secs": 0 })))
|
||||
.admin(
|
||||
"POST",
|
||||
"/v1/trial",
|
||||
Some(json!({ "confirm_within_secs": 0 })),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, 400, "{}", r.text);
|
||||
assert_eq!(generation(&edge).await, "a");
|
||||
|
||||
Reference in New Issue
Block a user