ftest/src/runner/target.rs

77 lines
2.2 KiB
Rust
Raw Normal View History

2023-09-29 18:17:13 +05:30
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
2023-09-29 19:35:01 +05:30
2023-09-29 18:17:13 +05:30
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use crate::complaince::target::Target;
use crate::ctx::MinAppContext;
2023-09-29 18:17:13 +05:30
use crate::git::Git;
use crate::runner::results::*;
use crate::docker_compose::DockerCompose;
2023-09-29 19:18:20 +05:30
const FTEST_TARGET_FILE: &str = "ftest.toml";
2023-09-29 18:17:13 +05:30
pub async fn run_target(
ctx: &dyn MinAppContext,
2023-09-29 19:18:20 +05:30
target: PathBuf,
2023-09-29 18:17:13 +05:30
) -> (
Vec<ArchivableSuiteResult>,
Option<Vec<ArchivableInitResult>>,
2023-10-05 00:57:36 +05:30
Vec<ArchivableContainer>,
2023-09-29 18:17:13 +05:30
) {
let compose = DockerCompose::new(target.canonicalize().unwrap(), ctx.docker_().clone());
2023-09-29 18:17:13 +05:30
compose.up();
2023-10-05 00:57:36 +05:30
let mut services = compose.services();
2023-09-29 18:17:13 +05:30
// Read test suite
let ftest_path = target.join(FTEST_TARGET_FILE);
let ftest_def = fs::read_to_string(&ftest_path).unwrap();
let target: Target = toml::from_str(&ftest_def).unwrap();
2023-09-29 19:18:20 +05:30
let init_containers = crate::runner::init_scripts::launch_init_containers(ctx, &target);
2023-09-29 18:17:13 +05:30
let mut suite_results = Vec::with_capacity(target.suites.len());
for suite in target.suites.iter() {
let results =
2023-09-29 19:18:20 +05:30
crate::runner::suite::SuiteRunnerState::run(&target.container_host, suite, ctx).await;
2023-09-29 18:17:13 +05:30
suite_results.push(results)
}
// shut down target instance
let mut target_logs = Vec::with_capacity(services.len());
2023-10-05 00:57:36 +05:30
for s in services.drain(0..) {
let logs = compose.logs(&s.service);
target_logs.push(ArchivableContainer {
logs,
name: s.service,
});
2023-09-29 18:17:13 +05:30
}
compose.down(true, true);
2023-10-05 00:57:36 +05:30
(suite_results, init_containers, target_logs)
2023-09-29 18:17:13 +05:30
}
pub fn get_targets(control: &Path) -> Vec<PathBuf> {
let mut res = Vec::default();
2023-09-29 19:18:20 +05:30
let files_changed = Git::files_changed_from_previous_commit(control);
2023-09-29 18:17:13 +05:30
let targets = control.clone().join("targets");
for entry in fs::read_dir(targets).unwrap() {
let entry = entry.unwrap();
if entry.path().is_dir() {
for file in files_changed.iter() {
if file.contains(entry.file_name().to_str().unwrap()) {
res.push(entry.path())
}
}
}
}
res
}