test(pull): add test for pull

This commit is contained in:
grimhilt 2024-03-11 14:47:05 +01:00
parent d5891a1a93
commit 7180647d26
6 changed files with 96 additions and 33 deletions

26
tests/pull.rs Normal file
View File

@ -0,0 +1,26 @@
mod utils;
use utils::{utils::*, status_utils::*, server::ServerTest, client::ClientTest};
#[cfg(test)]
mod push_tests {
use super::*;
#[test]
fn simple_pull() {
let id = get_random_test_id();
let mut server = ServerTest::new(id.clone());
server.init();
let mut client = ClientTest::new(id).init();
let _ = server.add_file("file1", "foo");
client.run_cmd_ok("pull");
// tests
assert!(client.has_file("file1", "foo"));
client.clean();
server.clean();
}
}

View File

@ -10,7 +10,8 @@ mod push_tests {
#[test]
fn simple_push() {
let id = get_random_test_id();
let mut server = ServerTest::new(id.clone()).init();
let mut server = ServerTest::new(id.clone());
server.init();
let mut client = ClientTest::new(id).init();
let _ = client.add_file("file1", "foo");
@ -30,7 +31,8 @@ mod push_tests {
#[test]
fn push_update() {
let id = get_random_test_id();
let mut server = ServerTest::new(id.clone()).init();
let mut server = ServerTest::new(id.clone());
server.init();
let mut client = ClientTest::new(id).init();
// init content of file1
@ -64,7 +66,8 @@ mod push_tests {
#[test]
fn push_dir_explicit() {
let id = get_random_test_id();
let mut server = ServerTest::new(id.clone()).init();
let mut server = ServerTest::new(id.clone());
server.init();
let mut client = ClientTest::new(id).init();
let _ = client.add_dir("dir");
@ -89,7 +92,8 @@ mod push_tests {
#[test]
fn push_dir_implicit() {
let id = get_random_test_id();
let mut server = ServerTest::new(id.clone()).init();
let mut server = ServerTest::new(id.clone());
server.init();
let mut client = ClientTest::new(id).init();
let _ = client.add_dir("dir");
@ -114,7 +118,8 @@ mod push_tests {
#[test]
fn push_all() {
let id = get_random_test_id();
let mut server = ServerTest::new(id.clone()).init();
let mut server = ServerTest::new(id.clone());
server.init();
let mut client = ClientTest::new(id).init();
let _ = client.add_file("file1", "foo");

View File

@ -9,3 +9,6 @@ pub mod utils;
#[path = "utils/status_utils.rs"]
pub mod status_utils;
#[path = "utils/files_utils.rs"]
pub mod files_utils;

View File

@ -1,10 +1,12 @@
use std::str;
use std::process::{Command, Output};
use std::fs::{self, File};
use std::io::Write;
use std::io::{Write, BufReader, BufRead};
use std::env;
use std::path::PathBuf;
use super::files_utils::has_files;
pub struct ClientTest {
user: String, // the nextcloud user
volume: String, // temp dir for the test
@ -92,6 +94,12 @@ impl ClientTest {
Ok(())
}
pub fn has_file(&mut self, file: &str, content: &str) -> bool {
let full_path = PathBuf::from(self.volume.clone()).join(file);
has_files(full_path, file, content, self.test_id.clone())
}
/// get the files given by the status command in two vector (staged and not staged)
pub fn get_status(&mut self) -> (Vec<String>, Vec<String>) {
let out = self.run_cmd("status");

View File

@ -0,0 +1,25 @@
use std::io::{BufReader, BufRead, Write};
use std::fs::{File};
use std::path::PathBuf;
pub fn has_files(full_path: PathBuf, file: &str, content: &str, test_id: String) -> bool
{
if !full_path.exists() {
println!("id: {}", test_id.clone());
eprintln!("File '{}' doesn't exists on the server", file);
return false;
}
let f = File::open(full_path).unwrap();
for line in BufReader::new(f).lines(){
if let Ok(line) = line {
if line != content {
println!("id: {}", test_id);
eprintln!("File '{}' is not equal, {} != {}", file, line, content);
return false;
}
return line == content;
}
}
return true;
}

View File

@ -1,10 +1,12 @@
use std::process::Command;
use std::os::unix::fs::PermissionsExt;
use std::fs::{self, File, Permissions};
use std::io::{BufReader, BufRead};
use std::io::{BufReader, BufRead, Write};
use std::env;
use std::path::PathBuf;
use super::files_utils::has_files;
#[cfg(test)]
pub struct ServerTest {
user: String,
@ -25,15 +27,17 @@ impl ServerTest {
}
}
pub fn init(mut self) -> Self {
pub fn init(&mut self) -> &mut ServerTest{
self.add_dir(self.test_id.clone());
self.volume = self.volume.join(self.test_id.clone());
self.sync_test()
self.sync_test();
self
}
pub fn clean(mut self) -> Self {
pub fn clean(&mut self) -> &mut ServerTest{
self.remove_dir(self.test_id.clone());
self.sync_root()
self.sync_root();
self
}
pub fn add_dir(&mut self, path: String) -> &mut ServerTest {
@ -54,6 +58,16 @@ impl ServerTest {
self
}
pub fn add_file(&mut self, name: &str, content: &str) -> std::io::Result<()> {
let mut full_path = self.volume.clone();
full_path.push(name);
let mut file = File::create(full_path)?;
file.write_all(content.as_bytes())?;
self.sync_test();
Ok(())
}
pub fn remove_dir(&mut self, path: String) -> &mut ServerTest {
let mut full_path = self.volume.clone();
full_path.push(path);
@ -62,16 +76,16 @@ impl ServerTest {
self
}
fn sync_root(self) -> Self {
fn sync_root(&self) -> &Self {
self.sync("")
}
fn sync_test(self) -> Self {
fn sync_test(&self) -> &Self {
let test_id = self.test_id.clone();
self.sync(&test_id)
}
fn sync(self, path: &str) -> Self {
fn sync(&self, path: &str) -> &Self {
// perform the occ files:scan command inside the nextcloud docker container
let nextcloud_docker = "master-nextcloud-1";
@ -91,25 +105,7 @@ impl ServerTest {
pub fn has_file(&mut self, file: &str, content: &str) -> bool {
let full_path = self.volume.clone().join(file);
if !full_path.exists() {
println!("id: {}", self.test_id.clone());
eprintln!("File '{}' doesn't exists on the server", file);
return false;
}
let f = File::open(full_path).unwrap();
for line in BufReader::new(f).lines(){
if let Ok(line) = line {
if line != content {
println!("id: {}", self.test_id.clone());
eprintln!("File '{}' is not equal, {} != {}", file, line, content);
return false;
}
return line == content;
}
}
return true;
has_files(full_path, file, content, self.test_id.clone())
}
}