Add a "check-config" command to validate config files & tests for it

This commit is contained in:
Andreas Fuchs 2024-10-04 10:49:08 -04:00
parent 70ee206031
commit 26ba489aa3
24 changed files with 492 additions and 0 deletions

View file

@ -0,0 +1,95 @@
use std::{
path::{Path, PathBuf},
process::Output,
};
use assert_cmd::Command;
type TestError = Box<dyn std::error::Error>;
type TestResult = Result<(), TestError>;
fn fixture_path<P>(name: P) -> PathBuf
where
P: AsRef<Path>,
{
PathBuf::from("tests/integrations/fixtures/check_config").join(name)
}
fn run<P>(file: P) -> Result<Output, TestError>
where
P: AsRef<Path>,
{
Command::cargo_bin("grapevine")?
.args(["check-config", "--log-format=json", "-c"])
.arg(fixture_path(file))
.output()
.map_err(Into::into)
}
macro_rules! make_snapshot_test {
($name:ident, $description:expr, $fixture_name:expr $(,)?) => {
#[test]
fn $name() -> TestResult {
let output = run($fixture_name)?;
let stdout = String::from_utf8(output.stdout)?;
let stderr = String::from_utf8(output.stderr)?;
let status_code = output.status.code();
insta::with_settings!({
description => $description,
omit_expression => true,
snapshot_suffix => "stdout",
}, {
insta::assert_snapshot!(stdout);
});
let stderr_parse = serde_json::Deserializer::from_str(&stderr)
.into_iter::<serde_json::Value>()
.collect::<Result<Vec<_>, _>>();
insta::with_settings!({
description => $description,
omit_expression => true,
snapshot_suffix => "stderr",
}, {
if let Ok(stderr_json) = stderr_parse {
insta::assert_json_snapshot!(stderr_json, {
".*.timestamp" => "[timestamp]"
});
} else {
insta::assert_snapshot!(stderr);
}
});
insta::with_settings!({
description => $description,
omit_expression => true,
snapshot_suffix => "status_code",
}, {
insta::assert_debug_snapshot!(status_code);
});
Ok(())
}
};
}
make_snapshot_test!(valid_config, "A normal config is valid", "valid.toml");
make_snapshot_test!(
minimal_valid_config,
"A configuration containing only the required keys is valid",
"minimal-valid.toml",
);
make_snapshot_test!(
invalid_keys,
"A config with invalid keys fails",
"invalid-keys.toml",
);
make_snapshot_test!(
invalid_values,
"A config with invalid values fails",
"invalid-values.toml",
);

View file

@ -0,0 +1,2 @@
some_name = "example.com"
prort = 6167

View file

@ -0,0 +1,2 @@
server_name = 6667
port = "ircd"

View file

@ -0,0 +1,8 @@
server_name = "example.com"
[server_discovery]
client.base_url = "https://matrix.example.com"
[database]
backend = "rocksdb"
path = "/var/lib/grapevine"

View file

@ -0,0 +1,21 @@
server_name = "example.com"
allow_registration = false
max_request_size = 20_000_000
[server_discovery]
server.authority = "matrix.example.com:443"
client.base_url = "https://matrix.example.com"
[database]
backend = "rocksdb"
path = "/var/lib/grapevine"
[federation]
enable = true
trusted_servers = ["matrix.org"]
[[listen]]
type="tcp"
address = "0.0.0.0"

4
tests/integrations/main.rs Executable file
View file

@ -0,0 +1,4 @@
// <https://github.com/rust-lang/rust-clippy/issues/11024>
#![allow(clippy::tests_outside_test_module)]
mod check_config;

View file

@ -0,0 +1,8 @@
---
source: tests/integrations/check_config.rs
description: A config with invalid keys fails
snapshot_kind: text
---
Some(
1,
)

View file

@ -0,0 +1,12 @@
---
source: tests/integrations/check_config.rs
description: A config with invalid keys fails
snapshot_kind: text
---
Error: failed to validate configuration
Caused by: failed to parse configuration file "tests/integrations/fixtures/check_config/invalid-keys.toml"
Caused by: TOML parse error at line 1, column 1
|
1 | some_name = "example.com"
| ^^^^^^^^^^^^^^^^^^^^^^^^^
missing field `server_name`

View file

@ -0,0 +1,6 @@
---
source: tests/integrations/check_config.rs
description: A config with invalid keys fails
snapshot_kind: text
---

View file

@ -0,0 +1,8 @@
---
source: tests/integrations/check_config.rs
description: A config with invalid values fails
snapshot_kind: text
---
Some(
1,
)

View file

@ -0,0 +1,12 @@
---
source: tests/integrations/check_config.rs
description: A config with invalid values fails
snapshot_kind: text
---
Error: failed to validate configuration
Caused by: failed to parse configuration file "tests/integrations/fixtures/check_config/invalid-values.toml"
Caused by: TOML parse error at line 1, column 15
|
1 | server_name = 6667
| ^^^^
invalid type: integer `6667`, expected a string

View file

@ -0,0 +1,6 @@
---
source: tests/integrations/check_config.rs
description: A config with invalid values fails
snapshot_kind: text
---

View file

@ -0,0 +1,8 @@
---
source: tests/integrations/check_config.rs
description: A configuration containing only the required keys is valid
snapshot_kind: text
---
Some(
0,
)

View file

@ -0,0 +1,15 @@
---
source: tests/integrations/check_config.rs
description: A configuration containing only the required keys is valid
snapshot_kind: text
---
[
{
"fields": {
"message": "Configuration looks good"
},
"level": "INFO",
"target": "grapevine::cli::check_config",
"timestamp": "[timestamp]"
}
]

View file

@ -0,0 +1,6 @@
---
source: tests/integrations/check_config.rs
description: A configuration containing only the required keys is valid
snapshot_kind: text
---

View file

@ -0,0 +1,8 @@
---
source: tests/integrations/check_config.rs
description: A normal config is valid
snapshot_kind: text
---
Some(
0,
)

View file

@ -0,0 +1,15 @@
---
source: tests/integrations/check_config.rs
description: A normal config is valid
snapshot_kind: text
---
[
{
"fields": {
"message": "Configuration looks good"
},
"level": "INFO",
"target": "grapevine::cli::check_config",
"timestamp": "[timestamp]"
}
]

View file

@ -0,0 +1,6 @@
---
source: tests/integrations/check_config.rs
description: A normal config is valid
snapshot_kind: text
---