From e56d005ba844dadcde44a2162b33f3fd089774e4 Mon Sep 17 00:00:00 2001 From: Solomon Jacobs Date: Mon, 11 Dec 2023 11:30:12 +0100 Subject: [PATCH] test termination 9: move `Environment` In order to write an integration test, the relevant functionality needs to be part of the libary crate `robotmk`. The goal is to move `run_attempts_until_succesful`. This change moves `Environment`. CMK-15433 --- v2/robotmk/src/bin/scheduler/build.rs | 5 +- .../src/bin/scheduler/internal_config.rs | 186 +----------------- v2/robotmk/src/bin/scheduler/main.rs | 1 - v2/robotmk/src/bin/scheduler/results.rs | 1 - v2/robotmk/src/bin/scheduler/rf/rebot.rs | 2 +- v2/robotmk/src/bin/scheduler/setup/rcc.rs | 3 +- v2/robotmk/src/environment.rs | 180 +++++++++++++++++ 7 files changed, 190 insertions(+), 188 deletions(-) delete mode 100644 v2/robotmk/src/bin/scheduler/results.rs diff --git a/v2/robotmk/src/bin/scheduler/build.rs b/v2/robotmk/src/bin/scheduler/build.rs index aa663558..9931657c 100644 --- a/v2/robotmk/src/bin/scheduler/build.rs +++ b/v2/robotmk/src/bin/scheduler/build.rs @@ -1,9 +1,8 @@ -use super::internal_config::{ - apply_current_settings, Environment, GlobalConfig, RCCEnvironment, Suite, -}; +use super::internal_config::{GlobalConfig, Suite}; use super::logging::log_and_return_error; use robotmk::child_process_supervisor::{ChildProcessOutcome, ChildProcessSupervisor, StdioPaths}; use robotmk::command_spec::CommandSpec; +use robotmk::environment::{apply_current_settings, Environment, RCCEnvironment}; use robotmk::results::{BuildOutcome, BuildStates, EnvironmentBuildStage}; use robotmk::lock::Locker; diff --git a/v2/robotmk/src/bin/scheduler/internal_config.rs b/v2/robotmk/src/bin/scheduler/internal_config.rs index ac8549c6..9afc9843 100644 --- a/v2/robotmk/src/bin/scheduler/internal_config.rs +++ b/v2/robotmk/src/bin/scheduler/internal_config.rs @@ -1,14 +1,12 @@ use crate::rf::robot::Robot; use crate::sessions::session::Session; -use robotmk::command_spec::CommandSpec; -use robotmk::config::EnvironmentConfig; use robotmk::config::{Config, RCCConfig, WorkingDirectoryCleanupConfig}; -use robotmk::environment::ResultCode; +use robotmk::environment::Environment; use robotmk::lock::Locker; use robotmk::results::suite_results_directory; use robotmk::section::Host; -use camino::{Utf8Path, Utf8PathBuf}; +use camino::Utf8PathBuf; use tokio_util::sync::CancellationToken; pub struct GlobalConfig { @@ -88,189 +86,15 @@ pub fn sort_suites_by_id(suites: &mut [Suite]) { suites.sort_by_key(|suite| suite.id.to_string()); } -#[derive(Clone)] -#[cfg_attr(test, derive(Debug, PartialEq))] -pub enum Environment { - System(SystemEnvironment), - Rcc(RCCEnvironment), -} - -#[derive(Clone)] -#[cfg_attr(test, derive(Debug, PartialEq))] -pub struct SystemEnvironment {} - -#[derive(Clone)] -#[cfg_attr(test, derive(Debug, PartialEq))] -pub struct RCCEnvironment { - pub binary_path: Utf8PathBuf, - pub robot_yaml_path: Utf8PathBuf, - pub controller: String, - pub space: String, - pub build_timeout: u64, - pub env_json_path: Option, -} - -impl Environment { - pub fn new( - suite_id: &str, - rcc_binary_path: &Utf8Path, - environment_config: &EnvironmentConfig, - ) -> Self { - match environment_config { - EnvironmentConfig::System => Self::System(SystemEnvironment {}), - EnvironmentConfig::Rcc(rcc_environment_config) => Self::Rcc(RCCEnvironment { - binary_path: rcc_binary_path.to_path_buf(), - robot_yaml_path: rcc_environment_config.robot_yaml_path.clone(), - controller: String::from("robotmk"), - space: suite_id.to_string(), - build_timeout: rcc_environment_config.build_timeout, - env_json_path: rcc_environment_config.env_json_path.clone(), - }), - } - } - - pub fn wrap(&self, command_spec: CommandSpec) -> CommandSpec { - match self { - Self::System(system_environment) => system_environment.wrap(command_spec), - Self::Rcc(rcc_environment) => rcc_environment.wrap(command_spec), - } - } - - pub fn create_result_code(&self, exit_code: i32) -> ResultCode { - match self { - Self::System(_) => SystemEnvironment::create_result_code(exit_code), - Self::Rcc(_) => RCCEnvironment::create_result_code(exit_code), - } - } -} - -impl SystemEnvironment { - fn wrap(&self, command_spec: CommandSpec) -> CommandSpec { - command_spec - } - - fn create_result_code(exit_code: i32) -> ResultCode { - if exit_code == 0 { - return ResultCode::AllTestsPassed; - } - ResultCode::RobotCommandFailed - } -} - -impl RCCEnvironment { - fn wrap(&self, command_spec: CommandSpec) -> CommandSpec { - let mut wrapped_spec = CommandSpec::new(&self.binary_path); - wrapped_spec - .add_argument("task") - .add_argument("script") - .add_argument("--no-build"); - apply_current_settings( - &self.robot_yaml_path, - &self.controller, - &self.space, - self.env_json_path.as_deref(), - &mut wrapped_spec, - ); - wrapped_spec - .add_argument("--") - .add_argument(command_spec.executable) - .add_arguments(command_spec.arguments); - wrapped_spec - } - - fn create_result_code(exit_code: i32) -> ResultCode { - match exit_code { - 0 => ResultCode::AllTestsPassed, - 10 => ResultCode::RobotCommandFailed, - _ => ResultCode::EnvironmentFailed, - } - } -} - -pub fn apply_current_settings( - robot_yaml_path: &Utf8Path, - controller: &str, - space: &str, - env_json_path: Option<&Utf8Path>, - command_spec: &mut CommandSpec, -) { - command_spec - .add_argument("--robot") - .add_argument(robot_yaml_path) - .add_argument("--controller") - .add_argument(controller) - .add_argument("--space") - .add_argument(space); - if let Some(env_json_path) = &env_json_path { - command_spec - .add_argument("--environment") - .add_argument(env_json_path); - } -} - #[cfg(test)] mod tests { use super::*; - use robotmk::config::RCCEnvironmentConfig; - - fn command_spec_for_wrap() -> CommandSpec { - let mut command_spec = CommandSpec::new("C:\\x\\y\\z.exe"); - command_spec - .add_argument("arg1") - .add_argument("--flag") - .add_argument("--option") - .add_argument("option_value"); - command_spec - } - - #[test] - fn test_system_wrap() { - assert_eq!( - SystemEnvironment {}.wrap(command_spec_for_wrap()), - command_spec_for_wrap() - ); - } - - #[test] - fn test_rcc_wrap() { - let mut expected = CommandSpec::new("C:\\bin\\z.exe"); - expected - .add_argument("task") - .add_argument("script") - .add_argument("--no-build") - .add_argument("--robot") - .add_argument("C:\\my_suite\\robot.yaml") - .add_argument("--controller") - .add_argument("robotmk") - .add_argument("--space") - .add_argument("my_suite") - .add_argument("--environment") - .add_argument("C:\\my_suite\\env.json") - .add_argument("--") - .add_argument("C:\\x\\y\\z.exe") - .add_argument("arg1") - .add_argument("--flag") - .add_argument("--option") - .add_argument("option_value"); - assert_eq!( - RCCEnvironment { - binary_path: Utf8PathBuf::from("C:\\bin\\z.exe"), - robot_yaml_path: Utf8PathBuf::from("C:\\my_suite\\robot.yaml"), - controller: String::from("robotmk"), - space: String::from("my_suite"), - build_timeout: 600, - env_json_path: Some("C:\\my_suite\\env.json".into()) - } - .wrap(command_spec_for_wrap()), - expected - ); - } - use crate::sessions::session::{CurrentSession, UserSession}; use robotmk::config::{ - EnvironmentConfig, ExecutionConfig, RCCProfileConfig, RetryStrategy, RobotFrameworkConfig, - SessionConfig, SuiteConfig, UserSessionConfig, + EnvironmentConfig, ExecutionConfig, RCCEnvironmentConfig, RCCProfileConfig, RetryStrategy, + RobotFrameworkConfig, SessionConfig, SuiteConfig, UserSessionConfig, }; + use robotmk::environment::{Environment, RCCEnvironment, SystemEnvironment}; use std::collections::HashMap; diff --git a/v2/robotmk/src/bin/scheduler/main.rs b/v2/robotmk/src/bin/scheduler/main.rs index 2dfced3c..d515bd7d 100644 --- a/v2/robotmk/src/bin/scheduler/main.rs +++ b/v2/robotmk/src/bin/scheduler/main.rs @@ -2,7 +2,6 @@ mod build; mod cli; mod internal_config; mod logging; -mod results; mod rf; mod scheduling; mod sessions; diff --git a/v2/robotmk/src/bin/scheduler/results.rs b/v2/robotmk/src/bin/scheduler/results.rs deleted file mode 100644 index 8b137891..00000000 --- a/v2/robotmk/src/bin/scheduler/results.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/v2/robotmk/src/bin/scheduler/rf/rebot.rs b/v2/robotmk/src/bin/scheduler/rf/rebot.rs index 1a83562f..5edde63e 100644 --- a/v2/robotmk/src/bin/scheduler/rf/rebot.rs +++ b/v2/robotmk/src/bin/scheduler/rf/rebot.rs @@ -1,6 +1,6 @@ use super::robot::PYTHON_EXECUTABLE; -use crate::internal_config::Environment; use robotmk::command_spec::CommandSpec; +use robotmk::environment::Environment; use robotmk::environment::ResultCode; use robotmk::results::{RebotOutcome, RebotResult}; diff --git a/v2/robotmk/src/bin/scheduler/setup/rcc.rs b/v2/robotmk/src/bin/scheduler/setup/rcc.rs index 91c535a1..57d67fcb 100644 --- a/v2/robotmk/src/bin/scheduler/setup/rcc.rs +++ b/v2/robotmk/src/bin/scheduler/setup/rcc.rs @@ -1,8 +1,9 @@ use super::icacls::run_icacls_command; -use crate::internal_config::{sort_suites_by_id, Environment, GlobalConfig, Suite}; +use crate::internal_config::{sort_suites_by_id, GlobalConfig, Suite}; use crate::logging::log_and_return_error; use crate::sessions::session::{CurrentSession, RunOutcome, RunSpec, Session}; use robotmk::command_spec::CommandSpec; +use robotmk::environment::Environment; use robotmk::results::RCCSetupFailures; use anyhow::{bail, Context, Result}; diff --git a/v2/robotmk/src/environment.rs b/v2/robotmk/src/environment.rs index c3f73bc6..e2eee613 100644 --- a/v2/robotmk/src/environment.rs +++ b/v2/robotmk/src/environment.rs @@ -1,5 +1,185 @@ +use crate::command_spec::CommandSpec; +use crate::config::EnvironmentConfig; + +use camino::{Utf8Path, Utf8PathBuf}; + pub enum ResultCode { AllTestsPassed, RobotCommandFailed, EnvironmentFailed, } + +#[derive(Clone, Debug, PartialEq)] +pub enum Environment { + System(SystemEnvironment), + Rcc(RCCEnvironment), +} + +#[derive(Clone, Debug, PartialEq)] +pub struct SystemEnvironment {} + +#[derive(Clone, Debug, PartialEq)] +pub struct RCCEnvironment { + pub binary_path: Utf8PathBuf, + pub robot_yaml_path: Utf8PathBuf, + pub controller: String, + pub space: String, + pub build_timeout: u64, + pub env_json_path: Option, +} + +impl Environment { + pub fn new( + suite_id: &str, + rcc_binary_path: &Utf8Path, + environment_config: &EnvironmentConfig, + ) -> Self { + match environment_config { + EnvironmentConfig::System => Self::System(SystemEnvironment {}), + EnvironmentConfig::Rcc(rcc_environment_config) => Self::Rcc(RCCEnvironment { + binary_path: rcc_binary_path.to_path_buf(), + robot_yaml_path: rcc_environment_config.robot_yaml_path.clone(), + controller: String::from("robotmk"), + space: suite_id.to_string(), + build_timeout: rcc_environment_config.build_timeout, + env_json_path: rcc_environment_config.env_json_path.clone(), + }), + } + } + + pub fn wrap(&self, command_spec: CommandSpec) -> CommandSpec { + match self { + Self::System(system_environment) => system_environment.wrap(command_spec), + Self::Rcc(rcc_environment) => rcc_environment.wrap(command_spec), + } + } + + pub fn create_result_code(&self, exit_code: i32) -> ResultCode { + match self { + Self::System(_) => SystemEnvironment::create_result_code(exit_code), + Self::Rcc(_) => RCCEnvironment::create_result_code(exit_code), + } + } +} + +impl SystemEnvironment { + fn wrap(&self, command_spec: CommandSpec) -> CommandSpec { + command_spec + } + + fn create_result_code(exit_code: i32) -> ResultCode { + if exit_code == 0 { + return ResultCode::AllTestsPassed; + } + ResultCode::RobotCommandFailed + } +} + +impl RCCEnvironment { + fn wrap(&self, command_spec: CommandSpec) -> CommandSpec { + let mut wrapped_spec = CommandSpec::new(&self.binary_path); + wrapped_spec + .add_argument("task") + .add_argument("script") + .add_argument("--no-build"); + apply_current_settings( + &self.robot_yaml_path, + &self.controller, + &self.space, + self.env_json_path.as_deref(), + &mut wrapped_spec, + ); + wrapped_spec + .add_argument("--") + .add_argument(command_spec.executable) + .add_arguments(command_spec.arguments); + wrapped_spec + } + + fn create_result_code(exit_code: i32) -> ResultCode { + match exit_code { + 0 => ResultCode::AllTestsPassed, + 10 => ResultCode::RobotCommandFailed, + _ => ResultCode::EnvironmentFailed, + } + } +} + +pub fn apply_current_settings( + robot_yaml_path: &Utf8Path, + controller: &str, + space: &str, + env_json_path: Option<&Utf8Path>, + command_spec: &mut CommandSpec, +) { + command_spec + .add_argument("--robot") + .add_argument(robot_yaml_path) + .add_argument("--controller") + .add_argument(controller) + .add_argument("--space") + .add_argument(space); + if let Some(env_json_path) = &env_json_path { + command_spec + .add_argument("--environment") + .add_argument(env_json_path); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn command_spec_for_wrap() -> CommandSpec { + let mut command_spec = CommandSpec::new("C:\\x\\y\\z.exe"); + command_spec + .add_argument("arg1") + .add_argument("--flag") + .add_argument("--option") + .add_argument("option_value"); + command_spec + } + + #[test] + fn test_system_wrap() { + assert_eq!( + SystemEnvironment {}.wrap(command_spec_for_wrap()), + command_spec_for_wrap() + ); + } + + #[test] + fn test_rcc_wrap() { + let mut expected = CommandSpec::new("C:\\bin\\z.exe"); + expected + .add_argument("task") + .add_argument("script") + .add_argument("--no-build") + .add_argument("--robot") + .add_argument("C:\\my_suite\\robot.yaml") + .add_argument("--controller") + .add_argument("robotmk") + .add_argument("--space") + .add_argument("my_suite") + .add_argument("--environment") + .add_argument("C:\\my_suite\\env.json") + .add_argument("--") + .add_argument("C:\\x\\y\\z.exe") + .add_argument("arg1") + .add_argument("--flag") + .add_argument("--option") + .add_argument("option_value"); + assert_eq!( + RCCEnvironment { + binary_path: Utf8PathBuf::from("C:\\bin\\z.exe"), + robot_yaml_path: Utf8PathBuf::from("C:\\my_suite\\robot.yaml"), + controller: String::from("robotmk"), + space: String::from("my_suite"), + build_timeout: 600, + env_json_path: Some("C:\\my_suite\\env.json".into()) + } + .wrap(command_spec_for_wrap()), + expected + ); + } +}