Skip to content

Commit

Permalink
test termination 9: move Environment
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SoloJacobs committed Dec 11, 2023
1 parent 7206f7d commit e56d005
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 188 deletions.
5 changes: 2 additions & 3 deletions v2/robotmk/src/bin/scheduler/build.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
186 changes: 5 additions & 181 deletions v2/robotmk/src/bin/scheduler/internal_config.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<Utf8PathBuf>,
}

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;

Expand Down
1 change: 0 additions & 1 deletion v2/robotmk/src/bin/scheduler/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod build;
mod cli;
mod internal_config;
mod logging;
mod results;
mod rf;
mod scheduling;
mod sessions;
Expand Down
1 change: 0 additions & 1 deletion v2/robotmk/src/bin/scheduler/results.rs

This file was deleted.

2 changes: 1 addition & 1 deletion v2/robotmk/src/bin/scheduler/rf/rebot.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
3 changes: 2 additions & 1 deletion v2/robotmk/src/bin/scheduler/setup/rcc.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
Loading

0 comments on commit e56d005

Please sign in to comment.