use super::super::super::schema::{MIGRATIONS, SCHEMA_VERSION, version_refusal};
const THIS: usize = 14;
fn text() -> &'static str {
assert!(
MIGRATIONS.len() > THIS,
"there are {} migrations",
MIGRATIONS.len()
);
MIGRATIONS[THIS]
}
fn one_line() -> String {
text()
.lines()
.filter(|line| !line.trim_start().starts_with("--"))
.collect::<Vec<_>>()
.join(" ")
.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
}
fn comment() -> String {
text()
.lines()
.filter_map(|line| line.trim_start().strip_prefix("--"))
.flat_map(str::split_whitespace)
.collect::<Vec<_>>()
.join(" ")
}
#[test]
fn version_15_makes_the_pending_tenant_table_once() {
assert_eq!(
text().matches("CREATE TABLE vcon.pending_tenant (").count(),
1,
"the fifteenth makes pending_tenant once"
);
}
#[test]
fn the_binary_s_version_is_at_least_15() {
assert!(
usize::try_from(SCHEMA_VERSION).expect("positive") > THIS,
"this binary's version: {SCHEMA_VERSION}"
);
}
#[test]
fn a_pending_tenant_row_is_an_id_and_the_time_its_create_began() {
assert!(
one_line().contains(
"CREATE TABLE vcon.pending_tenant ( id bytea PRIMARY KEY, started_at bigint NOT NULL );"
),
"the columns and the key: {}",
one_line()
);
}
#[test]
fn the_pending_tenant_table_has_the_policy_of_the_registry_s_rows() {
let line = one_line();
for statement in [
"ALTER TABLE vcon.pending_tenant ENABLE ROW LEVEL SECURITY;",
"ALTER TABLE vcon.pending_tenant FORCE ROW LEVEL SECURITY;",
"CREATE POLICY registry_rows ON vcon.pending_tenant USING (vcon.in_registry() = (id IS \
NOT NULL)) WITH CHECK (vcon.in_registry());",
"GRANT SELECT, INSERT, DELETE ON vcon.pending_tenant TO vcon_app;",
] {
assert!(line.contains(statement), "{statement}: {line}");
}
}
#[test]
fn the_migration_says_why_the_pending_tenant_table_starts_empty() {
assert!(
comment().contains(
"The table starts empty: migrate holds the database's lock, which a serving server \
holds too, so no create is under way while it runs."
),
"the reason, in the migration's comment: {}",
comment()
);
}
#[test]
fn a_version_14_database_is_refused_with_the_command_that_upgrades_it() {
let refusal = version_refusal(Some(14)).expect("refused");
assert!(refusal.contains("vcon-server migrate"), "{refusal}");
}
#[test]
fn the_application_role_is_granted_no_update_on_pending_tenant() {
assert!(
!one_line().contains("UPDATE ON vcon.pending_tenant") && !one_line().contains("TRUNCATE"),
"a create begins once: {}",
one_line()
);
}
#[test]
fn version_15_makes_the_pending_file_table_once() {
assert_eq!(
text().matches("CREATE TABLE vcon.pending_file (").count(),
1,
"the fifteenth makes pending_file once"
);
}
#[test]
fn a_pending_file_row_is_a_tenant_s_file_and_the_time_its_writing_began() {
assert!(
one_line().contains(
"CREATE TABLE vcon.pending_file ( tenant bytea NOT NULL, file_id bytea NOT NULL, \
started_at bigint NOT NULL, PRIMARY KEY (tenant, file_id) );"
),
"{}",
one_line()
);
}
#[test]
fn the_pending_file_table_has_the_policy_of_a_tenant_s_rows() {
for statement in [
"ALTER TABLE vcon.pending_file ENABLE ROW LEVEL SECURITY;",
"ALTER TABLE vcon.pending_file FORCE ROW LEVEL SECURITY;",
"CREATE POLICY tenant_rows ON vcon.pending_file USING (tenant = vcon.tenant()) \
WITH CHECK (tenant = vcon.tenant());",
"GRANT SELECT, INSERT, DELETE ON vcon.pending_file TO vcon_app;",
] {
assert!(one_line().contains(statement), "{statement}");
}
}
#[test]
fn the_migration_says_why_pack_files_under_way_are_recorded() {
let comment = comment();
assert!(
comment.contains("vcon.pending_file") && comment.contains("before it puts the file"),
"the reason, in the migration's comment: {comment}"
);
}
#[test]
fn version_15_makes_the_lease_table_once() {
assert_eq!(
text().matches("CREATE TABLE vcon.lease (").count(),
1,
"the fifteenth makes lease once"
);
}
#[test]
fn a_lease_row_is_a_job_its_holder_and_two_times_of_the_database() {
assert!(
one_line().contains(
"CREATE TABLE vcon.lease ( name text PRIMARY KEY, holder text NOT NULL, taken_at \
timestamptz NOT NULL, expires_at timestamptz NOT NULL );"
),
"{}",
one_line()
);
}
#[test]
fn the_lease_table_has_the_policy_of_the_registry_s_rows() {
for statement in [
"ALTER TABLE vcon.lease ENABLE ROW LEVEL SECURITY;",
"ALTER TABLE vcon.lease FORCE ROW LEVEL SECURITY;",
"CREATE POLICY registry_rows ON vcon.lease USING (vcon.in_registry() = (name IS NOT \
NULL)) WITH CHECK (vcon.in_registry());",
"GRANT SELECT, INSERT, UPDATE, DELETE ON vcon.lease TO vcon_app;",
] {
assert!(one_line().contains(statement), "{statement}");
}
}
#[test]
fn the_migration_says_a_lease_s_times_are_the_database_s() {
let comment = comment();
assert!(
comment.contains("vcon.lease")
&& comment.contains("by the database's clock")
&& comment.contains("no server's own clock"),
"the reason, in the migration's comment: {comment}"
);
}
#[test]
fn the_application_role_is_granted_no_update_on_pending_file() {
assert!(
!one_line().contains("UPDATE ON vcon.pending_file"),
"a file's writing begins once: {}",
one_line()
);
}
#[cfg(feature = "backend-tests")]
mod on_a_server {
use super::super::super::super::schema::{MIGRATIONS, Migrated};
use super::super::live::{Db, TENANT_A, TENANT_B, in_scope, no_scope};
use super::super::v8::on_a_server::{one, owner, refused};
use super::THIS;
const ROWS: &str = "SELECT coalesce(string_agg(encode(id, 'hex') || ':' || started_at, ',' \
ORDER BY id), '') FROM vcon.pending_tenant";
fn pending(id: &str, at: i64) -> String {
format!("INSERT INTO vcon.pending_tenant (id, started_at) VALUES ('\\x{id}', {at})")
}
async fn run(client: &tokio_postgres::Client, scope: &str, sql: &str) {
in_scope(client, scope, sql)
.await
.unwrap_or_else(|e| panic!("{sql}: {e}"));
}
#[tokio::test]
async fn a_version_14_database_is_brought_to_version_15() {
let db = Db::fresh().await;
db.migrate_with(&MIGRATIONS[..THIS])
.await
.expect("version 14");
assert_eq!(
db.migrate_with(&MIGRATIONS[..=THIS])
.await
.expect("version 15"),
Migrated { from: 14, to: 15 },
"the fifteenth migration alone"
);
assert_eq!(one(&db.app().await, "registry", ROWS).await, "", "empty");
}
#[test]
fn a_server_of_this_binary_refuses_a_database_at_version_14() {
let db = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("a runtime")
.block_on(async {
let db = Db::fresh().await;
db.migrate_with(&MIGRATIONS[..THIS])
.await
.expect("version 14");
db
});
let (_dir, settings) = db.settings("vcon_app");
let crate::config::StorageError(why) =
super::super::super::super::catalog::Database::connect(&settings).expect_err("refused");
assert!(
why.contains(&format!(
"version 14, older than this binary's {}",
super::super::super::super::schema::SCHEMA_VERSION
)) && why.contains("vcon-server migrate"),
"the versions and the command: {why}"
);
}
#[tokio::test]
async fn the_application_role_adds_reads_and_deletes_creating_rows() {
let db = Db::migrated().await;
let app = db.app().await;
run(&app, "registry", &pending("01", 7)).await;
run(&app, "registry", &pending("02", -7)).await;
assert_eq!(one(&app, "registry", ROWS).await, "01:7,02:-7", "both rows");
run(
&app,
"registry",
"DELETE FROM vcon.pending_tenant WHERE id = '\\x01'",
)
.await;
assert_eq!(one(&app, "registry", ROWS).await, "02:-7", "one is left");
}
#[tokio::test]
async fn the_application_role_cannot_change_a_creating_row() {
let db = Db::migrated().await;
let app = db.app().await;
run(&app, "registry", &pending("01", 7)).await;
for sql in [
"UPDATE vcon.pending_tenant SET started_at = 8",
"TRUNCATE vcon.pending_tenant",
] {
assert_eq!(
refused(&app, "registry", sql).await,
"permission denied for table pending_tenant",
"{sql}"
);
}
}
#[tokio::test]
async fn a_delete_with_no_scope_is_refused_and_keeps_the_rows() {
let db = Db::migrated().await;
let app = db.app().await;
run(&app, "registry", &pending("01", 7)).await;
assert_eq!(
no_scope(&app, "DELETE FROM vcon.pending_tenant").await,
Err("no tenant set for this transaction".to_owned()),
"a statement outside the registry's transactions"
);
assert_eq!(one(&app, "registry", ROWS).await, "01:7", "the row is kept");
}
#[tokio::test]
async fn a_tenant_s_transaction_reads_and_adds_no_creating_row() {
let db = Db::migrated().await;
run(&owner(&db).await, "registry", &pending("01", 7)).await;
let app = db.app().await;
for sql in [
"SELECT count(*) FROM vcon.pending_tenant".to_owned(),
pending("02", 1),
"DELETE FROM vcon.pending_tenant".to_owned(),
] {
assert_eq!(
refused(&app, TENANT_A, &sql).await,
"this transaction is a tenant's, not the registry's",
"{sql}"
);
}
assert_eq!(
one(&app, "registry", ROWS).await,
"01:7",
"the registry's row is kept"
);
}
const FILES: &str = "SELECT coalesce(string_agg(encode(file_id, 'hex') || ':' || started_at, \
',' ORDER BY file_id), '') FROM vcon.pending_file";
fn pending_file(tenant: &str, file: &str, at: i64) -> String {
format!(
"INSERT INTO vcon.pending_file (tenant, file_id, started_at) \
VALUES ('\\x{tenant}', '\\x{file}', {at})"
)
}
#[tokio::test]
async fn the_application_role_adds_reads_and_deletes_pending_file_rows() {
let db = Db::migrated().await;
let app = db.app().await;
run(&app, TENANT_A, &pending_file(TENANT_A, "01", 7)).await;
run(&app, TENANT_A, &pending_file(TENANT_A, "02", -7)).await;
assert_eq!(one(&app, TENANT_A, FILES).await, "01:7,02:-7", "both rows");
run(
&app,
TENANT_A,
"DELETE FROM vcon.pending_file WHERE file_id = '\\x01'",
)
.await;
assert_eq!(one(&app, TENANT_A, FILES).await, "02:-7", "one is left");
}
#[tokio::test]
async fn the_application_role_cannot_change_a_pending_file_row() {
let db = Db::migrated().await;
let app = db.app().await;
run(&app, TENANT_A, &pending_file(TENANT_A, "01", 7)).await;
for sql in [
"UPDATE vcon.pending_file SET started_at = 8",
"TRUNCATE vcon.pending_file",
] {
assert_eq!(
refused(&app, TENANT_A, sql).await,
"permission denied for table pending_file",
"{sql}"
);
}
}
#[tokio::test]
async fn a_tenant_neither_reads_nor_deletes_another_tenant_s_pending_files() {
let db = Db::migrated().await;
let app = db.app().await;
run(&app, TENANT_A, &pending_file(TENANT_A, "01", 7)).await;
assert_eq!(one(&app, TENANT_B, FILES).await, "", "B reads none of A's");
run(&app, TENANT_B, "DELETE FROM vcon.pending_file").await;
assert_eq!(one(&app, TENANT_A, FILES).await, "01:7", "A's row is kept");
let why = refused(&app, TENANT_B, &pending_file(TENANT_A, "02", 7)).await;
assert!(
why.contains("row-level security"),
"B cannot add a row of A's: {why}"
);
}
#[tokio::test]
async fn a_pending_file_statement_with_no_tenant_is_refused() {
let db = Db::migrated().await;
let app = db.app().await;
for sql in [
"SELECT * FROM vcon.pending_file".to_owned(),
"DELETE FROM vcon.pending_file".to_owned(),
pending_file(TENANT_A, "01", 7),
] {
let why = no_scope(&app, &sql).await.expect_err("refused");
assert!(
why.contains("no tenant set for this transaction"),
"{sql}: {why}"
);
}
}
const LEASES: &str = "SELECT coalesce(string_agg(name || ':' || holder, ',' ORDER BY name), \
'') FROM vcon.lease";
fn lease(name: &str, holder: &str) -> String {
format!(
"INSERT INTO vcon.lease (name, holder, taken_at, expires_at) \
VALUES ('{name}', '{holder}', now(), now() + interval '1 minute')"
)
}
#[tokio::test]
async fn the_application_role_takes_renews_and_gives_up_lease_rows() {
let db = Db::migrated().await;
let app = db.app().await;
run(&app, "registry", &lease("sweep", "a")).await;
run(&app, "registry", &lease("pack", "b")).await;
assert_eq!(
one(&app, "registry", LEASES).await,
"pack:b,sweep:a",
"both"
);
run(
&app,
"registry",
"UPDATE vcon.lease SET holder = 'c' WHERE name = 'sweep'",
)
.await;
run(
&app,
"registry",
"DELETE FROM vcon.lease WHERE name = 'pack'",
)
.await;
assert_eq!(
one(&app, "registry", LEASES).await,
"sweep:c",
"one, renamed"
);
}
#[tokio::test]
async fn the_application_role_cannot_empty_the_lease_table_at_once() {
let db = Db::migrated().await;
let app = db.app().await;
run(&app, "registry", &lease("sweep", "a")).await;
assert_eq!(
refused(&app, "registry", "TRUNCATE vcon.lease").await,
"permission denied for table lease",
"no TRUNCATE"
);
assert_eq!(one(&app, "registry", LEASES).await, "sweep:a", "kept");
}
#[tokio::test]
async fn a_tenant_s_transaction_reads_and_writes_no_lease_row() {
let db = Db::migrated().await;
run(&owner(&db).await, "registry", &lease("sweep", "a")).await;
let app = db.app().await;
for sql in [
LEASES.to_owned(),
lease("pack", "b"),
"UPDATE vcon.lease SET holder = 'c'".to_owned(),
"DELETE FROM vcon.lease".to_owned(),
] {
assert_eq!(
refused(&app, TENANT_A, &sql).await,
"this transaction is a tenant's, not the registry's",
"{sql}"
);
}
assert_eq!(one(&app, "registry", LEASES).await, "sweep:a", "kept");
}
#[tokio::test]
async fn a_lease_statement_with_no_scope_is_refused() {
let db = Db::migrated().await;
let app = db.app().await;
run(&app, "registry", &lease("sweep", "a")).await;
for sql in [
"UPDATE vcon.lease SET holder = 'c'".to_owned(),
"DELETE FROM vcon.lease".to_owned(),
lease("pack", "b"),
] {
let why = no_scope(&app, &sql).await.expect_err("refused");
assert!(
why.contains("no tenant set for this transaction"),
"{sql}: {why}"
);
}
assert_eq!(one(&app, "registry", LEASES).await, "sweep:a", "kept");
}
#[tokio::test]
async fn the_registry_s_transaction_reads_and_adds_no_pending_file_row() {
let db = Db::migrated().await;
run(
&owner(&db).await,
TENANT_A,
&pending_file(TENANT_A, "01", 7),
)
.await;
let app = db.app().await;
for sql in [
FILES.to_owned(),
pending_file(TENANT_A, "02", 1),
"DELETE FROM vcon.pending_file".to_owned(),
] {
assert_eq!(
refused(&app, "registry", &sql).await,
"this transaction is the registry's, not a tenant's",
"{sql}"
);
}
assert_eq!(
one(&app, TENANT_A, FILES).await,
"01:7",
"the tenant's row is kept"
);
}
}