feat: initialize target container

This commit is contained in:
Aravinth Manivannan 2023-09-27 17:18:15 +05:30
parent d7404aa074
commit 17efcac145
Signed by: realaravinth
GPG key ID: F8F50389936984FF
2 changed files with 99 additions and 0 deletions

View file

@ -0,0 +1,97 @@
use std::collections::HashMap;
use crate::complaince::target::Target;
use crate::docker::Docker;
use crate::utils::get_random;
use super::results::{ArchivableContainer, ArchivableInitResult};
pub fn launch_init_containers(target: &Target) -> Option<Vec<ArchivableInitResult>> {
if let Some(init_scripts) = target.init_scripts.as_ref() {
let mut init_results = Vec::with_capacity(init_scripts.len());
for init in init_scripts.iter() {
let mut env = HashMap::new();
let auth = get_random(32);
env.insert("FTEST_AUTH".into(), auth.clone());
if let Some(custom_vars) = init.env_vars.clone() {
env.extend(custom_vars);
}
let name = format!("{}--{}", init.name, &auth[0..5]);
Docker::run_container(
&name,
&init.container,
false,
&env,
Some("ftest".to_string()),
true,
);
let logs = Docker::get_logs(&name);
let exit_code = Docker::get_exit_status(&name);
Docker::rm_container(&name, true);
let c = ArchivableInitResult {
success: exit_code == 0,
exit_code,
container: ArchivableContainer {
name: name.clone(),
logs,
},
};
init_results.push(c);
}
Some(init_results)
} else {
None
}
}
#[cfg(test)]
mod tests {
use url::Url;
use super::*;
use crate::docker_compose::DockerCompose;
use crate::{AppCtx, Ctx, Settings};
use crate::complaince::suite::Test;
#[actix_rt::test]
async fn launch_init_containers_works() {
// let settings = Settings::new().unwrap();
// let ctx = AppCtx::new(Ctx::new(settings.clone()).await);
// let base_dir = Path::new(&ctx.settings.repository.base_dir);
// let control = base_dir.join("control");
let compose = DockerCompose::new("../ftest-control/targets/forgejo".into());
compose.up();
let mut env_vars: HashMap<String, String> = HashMap::new();
env_vars.insert("FORGEJO_URL".into(), "http://forgejo:7000".into());
env_vars.insert("FORGEJO_SSH_URL".into(), "http://forgejo:2222".into());
env_vars.insert("CI".into(), "true".into());
let target = Target {
version: "1".into(),
name: "launch_init_containers_works".into(),
homepage: Url::parse("https://launch_init_containers_works.example.org").unwrap(),
repository: Url::parse("https://launch_init_containers_works.example.org").unwrap(),
init_scripts: Some(vec![Test {
name: "forgejo-configure".into(),
url: Url::parse("https://git.batsense.net/ForgeFlux/forgejo-init-script").unwrap(),
version: semver::Version::parse("1.0.1").unwrap(),
container: "forgeflux/forgejo-init-script".into(),
env_vars: Some(env_vars),
}]),
suites: Vec::default(),
};
let init = launch_init_containers(&target);
assert!(init.is_some());
let init = init.unwrap();
assert_eq!(init.len(), 1);
let init = init.get(0).unwrap();
assert!(init.success);
assert_eq!(init.exit_code, 0);
assert!(init.container.logs.contains("All Good"));
compose.down(true, true);
}
}

2
src/runner/mod.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod init_scripts;
pub mod results;