110 lines
3.0 KiB
Rust
110 lines
3.0 KiB
Rust
use std::process::Command;
|
|
use std::os::unix::fs::PermissionsExt;
|
|
use std::fs::{self, File, Permissions};
|
|
use std::io::{BufReader, BufRead};
|
|
use std::env;
|
|
use std::path::PathBuf;
|
|
|
|
#[cfg(test)]
|
|
pub struct ServerTest {
|
|
user: String,
|
|
volume: PathBuf,
|
|
test_id: String
|
|
}
|
|
|
|
#[cfg(test)]
|
|
impl ServerTest {
|
|
pub fn new(id: String) -> Self {
|
|
let mut volume = env::current_dir().unwrap();
|
|
volume = volume.join("tests/data/admin/files");
|
|
|
|
ServerTest {
|
|
user: String::from("admin"),
|
|
volume,
|
|
test_id: id
|
|
}
|
|
}
|
|
|
|
pub fn init(mut self) -> Self {
|
|
self.add_dir(self.test_id.clone());
|
|
self.volume = self.volume.join(self.test_id.clone());
|
|
self.sync_test()
|
|
}
|
|
|
|
pub fn clean(mut self) -> Self {
|
|
self.remove_dir(self.test_id.clone());
|
|
self.sync_root()
|
|
}
|
|
|
|
pub fn add_dir(&mut self, path: String) -> &mut ServerTest {
|
|
let mut full_path = self.volume.clone();
|
|
full_path.push(path);
|
|
|
|
match fs::create_dir(&full_path) {
|
|
Ok(_) => {
|
|
// Set permissions to 777 to allow nextcloud to access it (workaround avoiding to
|
|
// set group and owner to www-data)
|
|
if let Err(e) = fs::set_permissions(&full_path, Permissions::from_mode(0o777)) {
|
|
eprintln!("Error setting permissions: {}", e);
|
|
}
|
|
},
|
|
Err(e) => eprintln!("Error creating directory: {}", e),
|
|
}
|
|
|
|
self
|
|
}
|
|
|
|
pub fn remove_dir(&mut self, path: String) -> &mut ServerTest {
|
|
let mut full_path = self.volume.clone();
|
|
full_path.push(path);
|
|
|
|
let _ = fs::remove_dir_all(&full_path);
|
|
self
|
|
}
|
|
|
|
fn sync_root(self) -> Self {
|
|
self.sync("")
|
|
}
|
|
|
|
fn sync_test(self) -> Self {
|
|
let test_id = self.test_id.clone();
|
|
self.sync(&test_id)
|
|
}
|
|
|
|
fn sync(self, path: &str) -> Self {
|
|
// perform the occ files:scan command inside the nextcloud docker container
|
|
|
|
let nextcloud_docker = "master-nextcloud-1";
|
|
let mut args = String::from("exec -ti --user www-data");
|
|
args.push_str(nextcloud_docker);
|
|
args.push_str("/var/www/html/occ files:scan --path=/");
|
|
args.push_str(&self.user);
|
|
args.push_str("files/");
|
|
args.push_str(path);
|
|
|
|
let _output = Command::new("docker")
|
|
.args(args.split(" "))
|
|
.output()
|
|
.expect("Could not execute nextsync command");
|
|
self
|
|
}
|
|
|
|
pub fn has_file(&mut self, file: &str, content: &str) -> bool {
|
|
let full_path = self.volume.clone().join(file);
|
|
|
|
if !full_path.exists() {
|
|
eprintln!("File '{}' does't exists", file);
|
|
return false;
|
|
}
|
|
|
|
let file = File::open(full_path).unwrap();
|
|
for line in BufReader::new(file).lines(){
|
|
if let Ok(line) = line {
|
|
return line == content;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|