From 76b7d7e7278911d07549ab91664202d352e69410 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Fri, 29 Sep 2023 18:17:13 +0530 Subject: [PATCH] feat: find and run test targets --- src/runner/target.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/runner/target.rs diff --git a/src/runner/target.rs b/src/runner/target.rs new file mode 100644 index 0000000..8bf24a6 --- /dev/null +++ b/src/runner/target.rs @@ -0,0 +1,70 @@ +// SPDX-FileCopyrightText: 2023 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later +use std::fs; +use std::path::Path; +use std::path::PathBuf; + +use crate::complaince::target::Target; +use crate::git::Git; +use crate::runner::results::*; +use crate::AppCtx; + +use crate::docker_compose::DockerCompose; + +pub const FTEST_TARGET_FILE: &str = "ftest.toml"; + +pub async fn run_target( + ctx: &AppCtx, + target: &PathBuf, +) -> ( + Vec, + Option>, +) { + let compose = DockerCompose::new(target.canonicalize().unwrap(), ctx.docker.clone()); + compose.up(); + let services = compose.services(); + + // 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(); + + let init_containers = crate::runner::init_scripts::launch_init_containers(&ctx, &target); + + let mut suite_results = Vec::with_capacity(target.suites.len()); + + for suite in target.suites.iter() { + let results = + crate::runner::suite::SuiteRunnerState::run(&target.container_host, suite, &ctx).await; + suite_results.push(results) + } + + // shut down target instance + let mut target_logs = Vec::with_capacity(services.len()); + for s in services.iter() { + target_logs.push(compose.logs(&s.service)); + } + + compose.down(true, true); + + (suite_results, init_containers) +} + +pub fn get_targets(control: &Path) -> Vec { + let mut res = Vec::default(); + let files_changed = Git::files_changed_from_previous_commit(&control); + + 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 +}