113 lines
3.1 KiB
Rust
113 lines
3.1 KiB
Rust
use std::process::Command;
|
|
use std::os::unix::fs::PermissionsExt;
|
|
use std::fs::{self, File, Permissions};
|
|
use std::io::Write;
|
|
use std::env;
|
|
use std::path::PathBuf;
|
|
|
|
use super::files_utils::has_files;
|
|
|
|
#[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) -> &mut ServerTest{
|
|
self.add_dir(&self.test_id.clone());
|
|
self.volume = self.volume.join(self.test_id.clone());
|
|
self.sync_root();
|
|
self
|
|
}
|
|
|
|
pub fn clean(&mut self) -> &mut ServerTest{
|
|
self.remove_dir(self.test_id.clone());
|
|
self.sync_root();
|
|
self
|
|
}
|
|
|
|
pub fn add_dir(&mut self, path: &str) -> &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),
|
|
}
|
|
|
|
// do not sync test directory when creating it
|
|
if !path.ends_with("_nextsync")
|
|
{
|
|
self.sync_test();
|
|
}
|
|
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);
|
|
|
|
let _ = fs::remove_dir_all(&full_path);
|
|
self.sync_test();
|
|
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 args = format!("exec -t --user www-data {} /var/www/html/occ files:scan --path=/{}/files/{}", nextcloud_docker, &self.user, path);
|
|
|
|
let _output = Command::new("docker")
|
|
.args(args.split(" "))
|
|
.output()
|
|
.expect("Could not execute docker exec command");
|
|
self
|
|
}
|
|
|
|
pub fn has_file(&mut self, file: &str, content: &str) -> bool {
|
|
let full_path = self.volume.clone().join(file);
|
|
has_files(full_path, file, content, self.test_id.clone())
|
|
}
|
|
}
|
|
|