Skip to content

Commit

Permalink
adding assets tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Sep 25, 2024
1 parent 7533263 commit 545a8ee
Show file tree
Hide file tree
Showing 17 changed files with 306 additions and 482 deletions.
65 changes: 31 additions & 34 deletions cli/src/commands/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ async fn remove(filename: String, path: Option<String>, slug: Option<String>) ->
}

async fn list() -> DaikokuResult<()> {
logger::loading(format!("<yellow>Retrieving</> assets"));
logger::loading(format!(
"<yellow>Retrieving</> assets, limited to those identified by slugs."
));

let environment = get_default_environment()?;

Expand Down Expand Up @@ -236,6 +238,12 @@ async fn list() -> DaikokuResult<()> {
.iter()
.for_each(|asset| logger::println(asset.slug.clone()));

if assets.is_empty() {
logger::println("no assets found".to_string());
}

logger::success("".to_string());

Ok(())
} else {
Err(DaikokuCliError::DaikokuStrError(format!(
Expand Down Expand Up @@ -281,17 +289,6 @@ async fn sync() -> DaikokuResult<()> {
}
}

let environment = get_default_environment()?;

let host = environment
.server
.replace("http://", "")
.replace("https://", "");

let apikey = read_apikey_from_secrets(true)?;

let url: String = format!("{}/tenant-assets/bulk", environment.server);

let files = pages
.iter()
.map(|page| File::open(page.path.clone().unwrap()).unwrap())
Expand All @@ -300,6 +297,12 @@ async fn sync() -> DaikokuResult<()> {
let contents = pages
.iter()
.enumerate()
.filter(|(_idx, page)| {
page.filename
.clone()
.map(|filename| !filename.starts_with("."))
.unwrap_or(false)
})
.map(|(idx, page)| {
let mut content = Vec::new();
let _ = files.get(idx).unwrap().read_to_end(&mut content).unwrap();
Expand All @@ -312,27 +315,21 @@ async fn sync() -> DaikokuResult<()> {
})
.collect::<Vec<Asset>>();

let req = reqwest::Client::new()
.post(url)
.header(header::HOST, host)
.header(header::AUTHORIZATION, format!("Basic {}", apikey))
.body(Bytes::from(serde_json::to_string(&contents).map_err(
|_err| {
DaikokuCliError::ParsingError("failed to convert assets to json array".to_string())
},
)?))
.send()
.await
.map_err(|err| DaikokuCliError::DaikokuStrError(err.to_string()))?;

let status = req.status().as_u16();
if status == 200 {
logger::success("synchronization done".to_string());
list().await
} else {
Err(DaikokuCliError::DaikokuStrError(format!(
"failed to reach the Daikoku server {}",
status
)))
if contents.is_empty() {
logger::println("already up to date".to_string());
logger::done();
return Ok(());
}

let _resp: Vec<u8> = daikoku_cms_api_post(
"/tenant-assets/bulk",
Bytes::from(serde_json::to_string(&contents).map_err(|_err| {
DaikokuCliError::ParsingError("failed to convert assets to json array".to_string())
})?),
false,
)
.await?;

logger::success("synchronization done".to_string());
list().await
}
108 changes: 100 additions & 8 deletions cli/tests/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,102 @@ mod cli;

use std::{fs, path::PathBuf};

use cli::commands::{assets, cli::run_test_with_s3, cms, environment};
use cli::commands::{
assets,
cli::{run_test_with_s3, CustomRun},
cms, environment,
};

use serial_test::serial;

// #[tokio::test]
// #[serial]
// async fn push() -> Result<(), Box<dyn std::error::Error + 'static>> {
// run_test_with_s3(|_| {
// cms::clear(true);

// let project_path = cms::get_temporary_path();
// cms::init("cms", project_path.clone());

// environment::add("prod", "localhost");

// let _ = fs::copy(
// "tests/resources/daikoku.svg",
// PathBuf::from(project_path)
// .join("cms")
// .join("assets")
// .join("daikoku.svg"),
// );

// assets::push(
// "daikoku.svg",
// "daikoku-logo",
// "daikoku logo svg",
// "daikoku-logo",
// );
// })
// .await
// }

// #[tokio::test]
// #[serial]
// async fn remove() -> Result<(), Box<dyn std::error::Error + 'static>> {
// run_test_with_s3(|_| {
// cms::clear(true);

// let project_path = cms::get_temporary_path();
// cms::init("cms", project_path.clone());

// environment::add("prod", "localhost");

// let _ = fs::copy(
// "tests/resources/daikoku.svg",
// PathBuf::from(project_path)
// .join("cms")
// .join("assets")
// .join("daikoku.svg"),
// );

// let slug = "daikoku-logo";

// assets::push("daikoku.svg", "daikoku-logo", "daikoku logo svg", slug);

// assets::remove("daikoku.svg", slug);
// })
// .await
// }

// #[tokio::test]
// #[serial]
// async fn list() -> Result<(), Box<dyn std::error::Error + 'static>> {
// run_test_with_s3(|_| {
// cms::clear(true);

// let project_path = cms::get_temporary_path();
// cms::init("cms", project_path.clone());

// environment::add("prod", "localhost");

// let _ = fs::copy(
// "tests/resources/daikoku.svg",
// PathBuf::from(project_path)
// .join("cms")
// .join("assets")
// .join("daikoku.svg"),
// );

// let slug = "daikoku-logo";

// assets::push("daikoku.svg", "daikoku-logo", "daikoku logo svg", slug);

// assets::list().run_and_expect("daikoku-logo");
// })
// .await
// }

#[tokio::test]
#[serial]
async fn push() -> Result<(), Box<dyn std::error::Error + 'static>> {
async fn sync() -> Result<(), Box<dyn std::error::Error + 'static>> {
run_test_with_s3(|_| {
cms::clear(true);

Expand All @@ -25,12 +114,15 @@ async fn push() -> Result<(), Box<dyn std::error::Error + 'static>> {
.join("daikoku.svg"),
);

assets::push(
"daikoku.svg",
"daikoku-logo",
"daikoku logo svg",
"daikoku-logo",
);
let slug = "daikoku-logo";

assets::push("daikoku.svg", "daikoku-logo", "daikoku logo svg", slug);

assets::sync();

assets::list().run_and_expect("daikoku-logo");

assets::remove("daikoku.svg", slug);
})
.await
}
28 changes: 8 additions & 20 deletions cli/tests/cli/commands/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,19 @@ pub(crate) fn push(
])
}

pub(crate) fn remove(filename: Option<&str>, path: Option<&str>, slug: Option<&str>) -> Assert {
match (filename, path, slug) {
(Some(filename), _, _) => CLI::run([
"assets",
"remove",
format!("--filename={}", filename.to_string()).as_str(),
]),
(_, Some(path), _) => CLI::run([
"assets",
"remove",
format!("--path={}", path.to_string()).as_str(),
]),
(_, _, Some(slug)) => CLI::run([
"assets",
"remove",
format!("--slug={}", slug.to_string()).as_str(),
]),
_ => panic!("unknown command"),
}
pub(crate) fn remove(filename: &str, slug: &str) -> Assert {
CLI::run([
"assets",
"remove",
format!("--slug={}", slug.to_string()).as_str(),
format!("--filename={}", filename.to_string()).as_str(),
])
}

pub(crate) fn list() -> Assert {
CLI::run(["assets", "list"])
}

pub(crate) fn sync(apikey: &str, cookie: &str) -> Assert {
pub(crate) fn sync() -> Assert {
CLI::run(["assets", "sync"])
}
68 changes: 48 additions & 20 deletions cli/tests/cli/commands/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ where
pub(crate) struct CLI {
pub postgres_container: ContainerAsync<GenericImage>,
pub daikoku_container: ContainerAsync<GenericImage>,
pub s3_container: Option<ContainerAsync<GenericImage>>,
}

impl CLI {
Expand All @@ -64,30 +63,61 @@ impl CLI {
Ok(CLI {
postgres_container: postgres_container,
daikoku_container: daikoku_container,
s3_container: None,
})
}

pub(crate) async fn start_with_s3() -> Result<CLI, Box<dyn std::error::Error + 'static>> {
let mut state_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
state_path.push("tests/resources/s3");

let s3 = GenericImage::new("scireum/s3-ninja", "latest")
.with_wait_for(WaitFor::message_on_stdout("System is UP and RUNNING"))
.with_mapped_port(9002, 9000.tcp())
.with_mount(Mount::bind_mount(
state_path.into_os_string().into_string().unwrap(),
"/home/sirius/data",
))
.with_network("daikoku");

let s3_container = s3.start().await?;
let root_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

let content =
fs::read_to_string(root_path.clone().join("tests/resources/.s3_env")).unwrap();

let lines = content.split("\n").collect::<Vec<&str>>();

let s3_bucket = lines
.iter()
.find(|line| line.contains("S3_BUCKET"))
.unwrap()
.replace("S3_BUCKET=", "");

let s3_endpoint = lines
.iter()
.find(|line| line.contains("S3_ENDPOINT"))
.unwrap()
.replace("S3_ENDPOINT=", "");

let s3_key = lines
.iter()
.find(|line| line.contains("S3_KEY"))
.unwrap()
.replace("S3_KEY=", "");

let s3_secret = lines
.iter()
.find(|line| line.contains("S3_SECRET"))
.unwrap()
.replace("S3_SECRET=", "");

let _ = fs::write(
root_path
.clone()
.join("tests/resources/daikoku-state.ndjson"),
fs::read_to_string(
root_path
.clone()
.join("tests/resources/daikoku-state-template.ndjson"),
)
.unwrap()
.replace("@@S3_ENDPOINT@@", s3_endpoint.trim())
.replace("@@S3_KEY@@", s3_key.trim())
.replace("@@S3_SECRET@@", s3_secret.trim())
.replace("@@S3_BUCKET@@", s3_bucket.trim()),
);

let (postgres_container, daikoku_container) = Self::start_containers().await?;
let cli = CLI {
postgres_container: postgres_container,
daikoku_container: daikoku_container,
s3_container: Some(s3_container),
};
Ok(cli)
}
Expand All @@ -103,8 +133,7 @@ impl CLI {
.with_mapped_port(5432, 5432.tcp())
.with_env_var("POSTGRES_USER", "postgres")
.with_env_var("POSTGRES_PASSWORD", "postgres")
.with_env_var("POSTGRES_DB", "daikoku")
.with_network("daikoku");
.with_env_var("POSTGRES_DB", "daikoku");

let postgres_container = postgres.start().await?;
let host = postgres_container
Expand All @@ -127,8 +156,7 @@ impl CLI {
.with_mount(Mount::bind_mount(
state_path.into_os_string().into_string().unwrap(),
"/tmp",
))
.with_network("daikoku");
));

let daikoku_container = daikoku.start().await?;

Expand Down
1 change: 1 addition & 0 deletions cli/tests/resources/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.s3_env
Loading

0 comments on commit 545a8ee

Please sign in to comment.