#[allow(dead_code)]
mod support;
use support::server::{destination_or_skip, on_server};
use support::Sandbox;
const STORED: &str = "declarado";
fn store_profile(sandbox: &Sandbox, dsn: &str) {
let parsed = url_parts(dsn);
let written = sandbox.run_with_input(
&[
"conn",
"add",
STORED,
"--host",
&parsed.host,
"--port",
&parsed.port,
"--user",
&parsed.user,
"--database",
&parsed.database,
"--password-stdin",
"--ca",
&parsed.authority_file,
],
&parsed.secret,
);
assert_eq!(
written.code, 0,
"the profile must be stored: {}",
written.err
);
}
const ELSEWHERE: &str = "desviado";
fn store_unreachable_profile(sandbox: &Sandbox) {
let written = sandbox.run_with_input(
&[
"conn",
"add",
ELSEWHERE,
"--host",
"127.0.0.1",
"--port",
"1",
"--user",
"ninguem",
"--database",
"nenhum",
"--password-stdin",
],
"nada",
);
assert_eq!(
written.code, 0,
"the second profile must be stored: {}",
written.err
);
}
fn make_default(sandbox: &Sandbox, profile: &str) {
let chosen = sandbox.run(&["conn", "default", profile]);
assert_eq!(
chosen.code, 0,
"the default must be settable: {}",
chosen.err
);
}
struct Parts {
host: String,
port: String,
user: String,
secret: String,
database: String,
authority_file: String,
}
fn url_parts(dsn: &str) -> Parts {
let body = dsn.trim_start_matches("mariadb://");
let (credential, rest) = body
.split_once('@')
.expect("a destination carries a credential");
let (user, secret) = credential
.split_once(':')
.expect("a credential carries a secret");
let (authority, tail) = rest
.split_once('/')
.expect("a destination names a database");
let (host, port) = authority.split_once(':').unwrap_or((authority, "3306"));
let database = tail.split('?').next().unwrap_or(tail);
let authority_file = tail
.split_once("ca=")
.map(|(_, path)| path.split('&').next().unwrap_or(path))
.expect("the declared destination names a trust anchor");
Parts {
host: host.to_owned(),
port: port.to_owned(),
user: user.to_owned(),
secret: secret.to_owned(),
database: database.to_owned(),
authority_file: authority_file.to_owned(),
}
}
#[test]
fn the_same_destination_is_named_whether_it_was_written_or_inherited() {
let Some(dsn) = destination_or_skip("the declared destination") else {
return;
};
let sandbox = Sandbox::new();
store_profile(&sandbox, &dsn);
let written = on_server(&sandbox, &dsn, &["query", "run", "SELECT 1 AS a"]);
assert_eq!(written.code, 0, "the read must succeed: {}", written.err);
let by_text = written.answer();
let inherited = sandbox.run(&["query", "run", "SELECT 1 AS a"]);
assert_eq!(
inherited.code, 0,
"the read must succeed: {}",
inherited.err
);
let by_default = inherited.answer();
assert_eq!(
by_text["target_resolved"]["host"], by_default["target_resolved"]["host"],
"both calls reached the same server, so both must name it"
);
assert_eq!(by_text["target_source"], "dsn");
assert_eq!(
by_default["target_source"], "config",
"a destination nobody wrote on the call is declared as inherited"
);
assert_eq!(
by_default["target_resolved"]["profile"], STORED,
"the inherited destination names the profile that served it"
);
}
#[test]
fn a_profile_written_on_the_call_is_declared_as_written_there() {
let Some(dsn) = destination_or_skip("the declared destination") else {
return;
};
let sandbox = Sandbox::new();
store_profile(&sandbox, &dsn);
let named = sandbox.run(&["--profile", STORED, "query", "run", "SELECT 1 AS a"]);
assert_eq!(named.code, 0, "the read must succeed: {}", named.err);
let answer = named.answer();
assert_eq!(answer["target_source"], "argv");
assert_eq!(answer["target_resolved"]["profile"], STORED);
}
#[test]
fn a_change_names_its_destination_or_is_refused_before_the_server_is_reached() {
let Some(dsn) = destination_or_skip("the declared destination") else {
return;
};
let sandbox = Sandbox::new();
store_profile(&sandbox, &dsn);
let inherited = sandbox.run(&[
"query",
"run",
"UPDATE orders SET total = total WHERE id = 1",
"--yes",
]);
assert_eq!(
inherited.code, 3,
"a change with an unwritten destination must be refused: {}",
inherited.err
);
assert_eq!(inherited.code(), "validation.target_not_written");
let written = on_server(
&sandbox,
&dsn,
&[
"query",
"run",
"UPDATE orders SET total = total WHERE id = 1",
"--yes",
],
);
assert_eq!(
written.code, 0,
"the same change must pass once the call names where it goes: {}",
written.err
);
}
#[test]
fn a_read_is_still_served_from_the_stored_default() {
let Some(dsn) = destination_or_skip("the declared destination") else {
return;
};
let sandbox = Sandbox::new();
store_profile(&sandbox, &dsn);
let inherited = sandbox.run(&["schema", "tables"]);
assert_eq!(
inherited.code, 0,
"a listing changes nothing and may inherit its destination: {}",
inherited.err
);
}
#[test]
fn a_destination_written_on_the_call_outranks_a_different_one_in_the_settings() {
let Some(dsn) = destination_or_skip("the declared destination") else {
return;
};
let sandbox = Sandbox::new();
store_profile(&sandbox, &dsn);
store_unreachable_profile(&sandbox);
make_default(&sandbox, ELSEWHERE);
let written = sandbox.run(&["--profile", STORED, "query", "run", "SELECT 1 AS a"]);
assert_eq!(
written.code, 0,
"the destination written on the call must be the one used: {}",
written.err
);
let answer = written.answer();
assert_eq!(answer["target_source"], "argv");
assert_eq!(answer["target_resolved"]["profile"], STORED);
let inherited = sandbox.run(&["query", "run", "SELECT 1 AS a"]);
assert_eq!(
inherited.code, 5,
"the default really does point elsewhere, so the same read without a \
written destination must fail to reach anything: {}",
inherited.err
);
}
#[test]
fn a_destination_written_on_the_call_is_never_traded_for_the_one_in_force() {
let Some(dsn) = destination_or_skip("the declared destination") else {
return;
};
let sandbox = Sandbox::new();
store_profile(&sandbox, &dsn);
store_unreachable_profile(&sandbox);
make_default(&sandbox, STORED);
let served = sandbox.run(&["query", "run", "SELECT 1 AS a"]);
assert_eq!(
served.code, 0,
"the default must be the usable one, or this proof shows nothing: {}",
served.err
);
let displaced = sandbox.run(&["--profile", ELSEWHERE, "query", "run", "SELECT 1 AS a"]);
assert_eq!(
displaced.code, 5,
"a call naming a destination that answers nothing must fail there \
rather than be served from the one in force: {}",
displaced.err
);
assert_eq!(displaced.code(), "network.unreachable");
}