Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Tighten CLI integration test harness

Manav Rathi1b48b7cea3f2
diff --git a/rust/apps/cli/tests/paste.rs b/rust/apps/cli/tests/paste.rs
index 1f611ca439..b4f425a0f8 100644
--- a/rust/apps/cli/tests/paste.rs
+++ b/rust/apps/cli/tests/paste.rs
@@ -1,34 +1,30 @@
mod support;
-use std::process::Output;
-
use reqwest::Url;
use support::{Cli, Fixture, TestResult};
use uuid::Uuid;
#[test]
fn paste() -> TestResult {
- let fixture = Fixture::start()?;
-
- full_link_roundtrip(&fixture)?;
- token_and_key_roundtrip(&fixture)?;
- stdin_roundtrip(&fixture)?;
-
- Ok(())
+ Fixture::run(|fixture| {
+ full_link_roundtrip(fixture)?;
+ token_and_key_roundtrip(fixture)?;
+ stdin_roundtrip(fixture)
+ })
}
fn full_link_roundtrip(fixture: &Fixture) -> TestResult {
- let cli = fixture.cli("full-link")?;
+ let cli = fixture.cli_session("full-link")?;
let text = unique_text("full-link");
let link = create_paste(&cli, fixture, &text)?;
- let consumed = cli_success(cli.run(&[
+ let consumed = cli.run_ok(&[
"paste",
"consume",
"--endpoint",
fixture.endpoint(),
&link.raw,
- ])?);
+ ])?;
assert_eq!(consumed, text);
let second_consume = cli.run(&[
@@ -53,11 +49,11 @@ fn full_link_roundtrip(fixture: &Fixture) -> TestResult {
}
fn token_and_key_roundtrip(fixture: &Fixture) -> TestResult {
- let cli = fixture.cli("token-key")?;
+ let cli = fixture.cli_session("token-key")?;
let text = unique_text("token-key");
let link = create_paste(&cli, fixture, &text)?;
- let consumed = cli_success(cli.run(&[
+ let consumed = cli.run_ok(&[
"paste",
"consume",
"--endpoint",
@@ -65,19 +61,19 @@ fn token_and_key_roundtrip(fixture: &Fixture) -> TestResult {
"--key",
&link.key,
&link.token,
- ])?);
+ ])?;
assert_eq!(consumed, text);
Ok(())
}
fn stdin_roundtrip(fixture: &Fixture) -> TestResult {
- let cli = fixture.cli("stdin")?;
+ let cli = fixture.cli_session("stdin")?;
let text = format!(
"paste stdin scenario {}\n\nsecond line stays intact\n",
Uuid::new_v4()
);
- let link = cli_success(cli.run_with_stdin(
+ let link = cli.run_ok_with_stdin(
&[
"paste",
"create",
@@ -87,23 +83,23 @@ fn stdin_roundtrip(fixture: &Fixture) -> TestResult {
fixture.paste_origin(),
],
&text,
- )?);
+ )?;
let link = PasteLink::parse(link.trim(), fixture.paste_origin());
- let consumed = cli_success(cli.run(&[
+ let consumed = cli.run_ok(&[
"paste",
"consume",
"--endpoint",
fixture.endpoint(),
&link.raw,
- ])?);
+ ])?;
assert_eq!(consumed, text);
Ok(())
}
fn create_paste(cli: &Cli, fixture: &Fixture, text: &str) -> TestResult<PasteLink> {
- let link = cli_success(cli.run(&[
+ let link = cli.run_ok(&[
"paste",
"create",
"--endpoint",
@@ -111,7 +107,7 @@ fn create_paste(cli: &Cli, fixture: &Fixture, text: &str) -> TestResult<PasteLin
"--paste-origin",
fixture.paste_origin(),
text,
- ])?);
+ ])?;
Ok(PasteLink::parse(link.trim(), fixture.paste_origin()))
}
@@ -162,14 +158,3 @@ impl PasteLink {
}
}
}
-
-fn cli_success(output: Output) -> String {
- assert!(
- output.status.success(),
- "ente-rs failed\nstatus: {}\nstdout:\n{}\nstderr:\n{}",
- output.status,
- String::from_utf8_lossy(&output.stdout),
- String::from_utf8_lossy(&output.stderr),
- );
- String::from_utf8(output.stdout).expect("ente-rs stdout should be UTF-8")
-}
diff --git a/rust/apps/cli/tests/support/cli.rs b/rust/apps/cli/tests/support/cli.rs
index 3c35865854..1047f4527f 100644
--- a/rust/apps/cli/tests/support/cli.rs
+++ b/rust/apps/cli/tests/support/cli.rs
@@ -23,6 +23,10 @@ impl Cli {
Ok(command(&self.bin, &self.config_dir, args).output()?)
}
+ pub fn run_ok(&self, args: &[&str]) -> TestResult<String> {
+ output_stdout(args, self.run(args)?)
+ }
+
pub fn run_with_stdin(&self, args: &[&str], stdin: &str) -> TestResult<Output> {
let mut child = command(&self.bin, &self.config_dir, args)
.stdin(Stdio::piped())
@@ -38,14 +42,29 @@ impl Cli {
Ok(child.wait_with_output()?)
}
+
+ pub fn run_ok_with_stdin(&self, args: &[&str], stdin: &str) -> TestResult<String> {
+ output_stdout(args, self.run_with_stdin(args, stdin)?)
+ }
}
fn command(bin: &Path, config_dir: &Path, args: &[&str]) -> Command {
let mut command = Command::new(bin);
+ command.args(args).env("ENTE_CLI_CONFIG_DIR", config_dir);
command
- .args(args)
- .env("ENTE_CLI_CONFIG_DIR", config_dir)
- .env("NO_PROXY", "127.0.0.1,localhost")
- .env("no_proxy", "127.0.0.1,localhost");
- command
+}
+
+fn output_stdout(args: &[&str], output: Output) -> TestResult<String> {
+ if output.status.success() {
+ Ok(String::from_utf8(output.stdout)?)
+ } else {
+ Err(format!(
+ "ente-rs {} failed\nstatus: {}\nstdout:\n{}\nstderr:\n{}",
+ args.join(" "),
+ output.status,
+ String::from_utf8_lossy(&output.stdout),
+ String::from_utf8_lossy(&output.stderr),
+ )
+ .into())
+ }
}
diff --git a/rust/apps/cli/tests/support/fixture.rs b/rust/apps/cli/tests/support/fixture.rs
index a4e9119ea6..b401c82015 100644
--- a/rust/apps/cli/tests/support/fixture.rs
+++ b/rust/apps/cli/tests/support/fixture.rs
@@ -17,27 +17,44 @@ pub struct Fixture {
}
impl Fixture {
+ pub fn run(test: impl FnOnce(&Self) -> TestResult) -> TestResult {
+ let mut fixture = Self::start()?;
+ let result = test(&fixture);
+ if result.is_err() {
+ fixture.retain_temp_dir();
+ }
+ result
+ }
+
pub fn start() -> TestResult<Self> {
let paths = Paths::discover()?;
- let temp_dir = TempDir::new("ente-cli-paste")?;
+ let mut temp_dir = TempDir::new("ente-cli-test")?;
let log_dir = temp_dir.path().join("logs");
let pglite_port = free_port()?;
let museum_port = free_port()?;
- let paste_port = free_port()?;
- let paste_origin = format!("http://{LOCAL_HOST}:{paste_port}");
+ let paste_origin = format!("http://{LOCAL_HOST}");
let endpoint = format!("http://{LOCAL_HOST}:{museum_port}");
- let credentials_file = temp_dir.path().join("credentials.yaml");
-
- museum::write_credentials(&credentials_file, pglite_port)?;
- let pglite = pglite::start(&paths.pglite_dir, &log_dir, pglite_port)?;
- let museum = museum::start(
- &paths.server_dir,
- &log_dir,
- &credentials_file,
- museum_port,
- pglite_port,
- &paste_origin,
- )?;
+ let museum_config_file = temp_dir.path().join("museum.yaml");
+
+ let result = (|| {
+ museum::write_config(&museum_config_file, museum_port, pglite_port, &paste_origin)?;
+ let pglite = pglite::start(&paths.pglite_dir, &log_dir, pglite_port)?;
+ let museum = museum::start(
+ &paths.server_dir,
+ &log_dir,
+ &museum_config_file,
+ museum_port,
+ )?;
+ Ok((pglite, museum))
+ })();
+
+ let (pglite, museum) = match result {
+ Ok(processes) => processes,
+ Err(error) => {
+ temp_dir.retain();
+ return Err(error);
+ }
+ };
Ok(Self {
temp_dir,
@@ -56,11 +73,15 @@ impl Fixture {
&self.paste_origin
}
- pub fn cli(&self, name: &str) -> TestResult<Cli> {
- let config_dir = self.temp_dir.path().join("cli").join(name);
+ pub fn cli_session(&self, scenario: &str) -> TestResult<Cli> {
+ let config_dir = self.temp_dir.path().join("cli").join(scenario);
fs::create_dir_all(&config_dir)?;
Ok(Cli::new(config_dir))
}
+
+ pub fn retain_temp_dir(&mut self) {
+ self.temp_dir.retain();
+ }
}
struct Paths {
@@ -88,23 +109,37 @@ impl Paths {
struct TempDir {
path: PathBuf,
+ retained: bool,
}
impl TempDir {
fn new(prefix: &str) -> TestResult<Self> {
let path = std::env::temp_dir().join(format!("{prefix}-{}", Uuid::new_v4()));
fs::create_dir_all(&path)?;
- Ok(Self { path })
+ Ok(Self {
+ path,
+ retained: false,
+ })
}
fn path(&self) -> &Path {
&self.path
}
+
+ fn retain(&mut self) {
+ eprintln!(
+ "retaining CLI integration test temp dir: {}",
+ self.path.display()
+ );
+ self.retained = true;
+ }
}
impl Drop for TempDir {
fn drop(&mut self) {
- let _ = fs::remove_dir_all(&self.path);
+ if !self.retained {
+ let _ = fs::remove_dir_all(&self.path);
+ }
}
}
diff --git a/rust/apps/cli/tests/support/museum.rs b/rust/apps/cli/tests/support/museum.rs
index 5f6a6e0e54..52ec34fb51 100644
--- a/rust/apps/cli/tests/support/museum.rs
+++ b/rust/apps/cli/tests/support/museum.rs
@@ -12,10 +12,8 @@ use super::{LOCAL_HOST, TestResult, process::ChildProcess};
pub fn start(
server_dir: &Path,
log_dir: &Path,
- credentials_file: &Path,
+ config_file: &Path,
museum_port: u16,
- pglite_port: u16,
- paste_origin: &str,
) -> TestResult<ChildProcess> {
require_go()?;
@@ -24,31 +22,29 @@ pub fn start(
.arg("run")
.arg("./cmd/museum")
.current_dir(server_dir)
- .env("ENTE_CREDENTIALS_FILE", credentials_file)
- .env("ENTE_HTTP_PORT", museum_port.to_string())
- .env("ENTE_APPS_PUBLIC_PASTE", paste_origin)
- .env("ENTE_DB_HOST", LOCAL_HOST)
- .env("ENTE_DB_PORT", pglite_port.to_string())
- .env("ENTE_DB_NAME", "postgres")
- .env("ENTE_DB_USER", "postgres")
- .env("ENTE_DB_PASSWORD", "")
- .env("ENTE_DB_SSLMODE", "disable")
- .env(
- "ENTE_INTERNAL_HARDCODED_OTT_LOCAL_DOMAIN_SUFFIX",
- "@ente-rust-test.org",
- )
- .env("ENTE_INTERNAL_HARDCODED_OTT_LOCAL_DOMAIN_VALUE", "123456");
+ .env("ENTE_CREDENTIALS_FILE", config_file);
let mut museum = ChildProcess::spawn("museum", &mut command, log_dir)?;
wait_for_museum(&mut museum, museum_port)?;
Ok(museum)
}
-pub fn write_credentials(path: &Path, pglite_port: u16) -> TestResult {
+pub fn write_config(
+ path: &Path,
+ museum_port: u16,
+ pglite_port: u16,
+ paste_origin: &str,
+) -> TestResult {
fs::write(
path,
format!(
- r#"db:
+ r#"http:
+ port: {museum_port}
+
+apps:
+ public-paste: "{paste_origin}"
+
+db:
host: {LOCAL_HOST}
port: {pglite_port}
name: postgres
@@ -64,19 +60,10 @@ s3:
endpoint: localhost:3200
region: eu-central-2
bucket: b2-eu-cen
- wasabi-eu-central-2-v3:
- key: changeme
- secret: changeme1234
- endpoint: localhost:3200
- region: eu-central-2
- bucket: wasabi-eu-central-2-v3
- compliance: false
- scw-eu-fr-v3:
- key: changeme
- secret: changeme1234
- endpoint: localhost:3200
- region: eu-central-2
- bucket: scw-eu-fr-v3
+
+jobs:
+ cron:
+ skip: true
"#
),
)?;
diff --git a/rust/apps/cli/tests/support/process.rs b/rust/apps/cli/tests/support/process.rs
index 26189a839f..71063b09e0 100644
--- a/rust/apps/cli/tests/support/process.rs
+++ b/rust/apps/cli/tests/support/process.rs
@@ -1,5 +1,5 @@
use std::{
- fs::{self, File, OpenOptions},
+ fs::{self, OpenOptions},
path::{Path, PathBuf},
process::{Child, Command, Stdio},
};
@@ -21,7 +21,7 @@ impl ChildProcess {
.append(true)
.open(&log_path)?;
let child = command
- .stdout(Stdio::from(clone_file(&log)?))
+ .stdout(Stdio::from(log.try_clone()?))
.stderr(Stdio::from(log))
.spawn()?;
Ok(Self {
@@ -61,7 +61,3 @@ impl Drop for ChildProcess {
let _ = self.child.wait();
}
}
-
-fn clone_file(file: &File) -> TestResult<File> {
- Ok(file.try_clone()?)
-}