crazy_train/
runner.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//! This module defines the [`Runner`] struct, which is responsible for managing and executing a sequence of steps in a defined order.
//! Each step can be customized with initial commands, checks, and tests, providing a flexible and extensible execution flow.
//!
//! The `Runner` can also generate and display an execution plan for the steps to be taken.
//! The steps can be randomized using the [`Randomizer`], enhancing the unpredictability of the execution.
//!
use crate::{
    executer,
    randomizer::Randomizer,
    step::{self, StepTrait},
    Error, Result,
};
use colored::Colorize;
use std::time::Instant;

/// A struct that orchestrates the execution of a series of steps.
pub struct Runner {
    steps: Vec<Box<dyn StepTrait>>,
    init: Option<Box<dyn StepTrait>>,
    randomizer: Randomizer,
}

/// Creates a new [`Runner`] instance with the given steps.
#[must_use]
pub fn new(steps: Vec<Box<dyn StepTrait>>) -> Runner {
    Runner {
        steps,
        init: None,
        randomizer: Randomizer::default(),
    }
}

impl Runner {
    /// Sets an initial step for the runner.
    #[must_use]
    pub fn init_step(mut self, step: Box<dyn StepTrait>) -> Self {
        self.init = Some(step);
        self
    }

    /// Sets a custom randomizer for the runner.
    #[must_use]
    pub fn randomizer(mut self, randomizer: Randomizer) -> Self {
        self.randomizer = randomizer;
        self
    }

    // Dumps the execution plan for the steps to be executed.
    ///
    /// # Errors
    ///
    /// when could not present the plan
    pub fn dump_plan(&self) -> Result<String> {
        let mut output: Vec<String> = Vec::new();

        output.push("====================================".to_string());
        output.push("          Execution Plan Dump        ".green().to_string());
        output.push("====================================".to_string());
        output.push(format!("{}: {}", "Step Count".bold(), &self.steps.len()));
        output.push(format!("{}: {}", "Seed".bold(), &self.randomizer.seed));
        output.push("------------------------------------".to_string());

        for (i, step) in self.steps.iter().enumerate() {
            let execution_plan = step.plan(&self.randomizer)?;
            output.push(
                format!("Step {}: {}", i + 1, execution_plan.id)
                    .green()
                    .to_string(),
            );
            output.push("------------------------------------".to_string());
            output.push("Command:".bold().to_string());
            output.push(execution_plan.command.clone());
            output.push("State:".bold().to_string());
            output.push("---".to_string());

            let state = serde_yaml::to_string(&step.to_yaml()).unwrap_or_default();
            output.push(state);

            output.push("------------------------------------".to_string());
        }

        Ok(output.join("\n"))
    }

    /// Executes the steps in the runner.
    ///
    /// # Errors
    /// On the first step that fails
    pub fn run(&self) -> Result<()> {
        println!("{}", self.dump_plan()?);
        for step in &self.steps {
            let step_plan = step.plan(&self.randomizer)?;

            println!();
            println!("{}", format!("Run step: {}", step_plan.id).yellow());
            println!();

            step.setup()?;
            let start = Instant::now();
            println!("{}", "Execute plan...".yellow());
            let result = step.plan(&self.randomizer)?.execute()?;
            println!(
                "{}",
                format!("Execute plan finished in {:?}", start.elapsed()).yellow()
            );
            let is_success = step.is_success(&result).map_err(|err| Error::StepError {
                kind: step::Kind::Plan,
                description: err.to_string(),
                command_output: result,
            })?;

            if !is_success {
                continue;
            }

            if let Some(check_command) = step.run_check() {
                let start = Instant::now();
                println!("{}", "Execute check...".yellow());
                let output = executer::run_sh(&check_command)?;
                println!(
                    "{}",
                    format!("Execute check finished in {:?}", start.elapsed()).yellow()
                );
                if output.status_code != Some(0) {
                    return Err(Error::StepError {
                        kind: step::Kind::Check,
                        description: "check not finish with status code 0".to_string(),
                        command_output: output,
                    });
                }
            }

            if let Some(test_command) = step.run_test() {
                let start = Instant::now();
                println!("{}", "Execute test...".yellow());
                let output = executer::run_sh(&test_command)?;
                println!(
                    "{}",
                    format!("Execute tests finished in {:?}", start.elapsed()).yellow()
                );
                if output.status_code != Some(0) {
                    return Err(Error::StepError {
                        kind: step::Kind::Test,
                        description: "test command not finish with status code 0".to_string(),
                        command_output: output,
                    });
                }
            }
        }

        println!("{}", "Execution plan is pass successfully".green());
        Ok(())
    }
}

#[cfg(test)]
mod tests {

    use std::path::PathBuf;

    use serde::{Deserialize, Serialize};

    use super::*;
    use crate::{executer::Output, generator::StringDef, step::Plan};

    #[derive(Serialize, Deserialize)]
    struct TestStepOne {
        location: PathBuf,
    }

    #[derive(Serialize, Deserialize)]
    struct TestStepTwo {
        location: PathBuf,
    }

    impl StepTrait for TestStepOne {
        fn setup(&self) -> crate::errors::Result<()> {
            Ok(std::fs::create_dir_all(&self.location)?)
        }

        fn plan(&self, randomizer: &Randomizer) -> Result<Plan> {
            let eco_string = randomizer.string(StringDef::default()).to_string();
            Ok(Plan {
                id: std::any::type_name::<Self>().to_string(),
                command: format!(
                    "echo {eco_string} >> {}",
                    self.location.join("test.txt").display()
                ),
            })
        }

        fn is_success(&self, execution_result: &Output) -> Result<bool, &'static str> {
            if execution_result.status_code == Some(0) {
                Ok(true)
            } else {
                Err("status code should be 0")
            }
        }

        fn run_check(&self) -> Option<String> {
            Some(format!(
                "test -f {}",
                self.location.join("test.txt").display()
            ))
        }

        fn run_test(&self) -> Option<String> {
            Some(format!(
                "test -f {}",
                self.location.join("test.txt").display()
            ))
        }

        fn to_yaml(&self) -> serde_yaml::Value {
            serde_yaml::to_value(self).expect("serialize")
        }
    }

    impl StepTrait for TestStepTwo {
        fn setup(&self) -> crate::errors::Result<()> {
            Ok(std::fs::create_dir_all(&self.location)?)
        }

        fn plan(&self, randomizer: &Randomizer) -> Result<Plan> {
            let eco_string = randomizer.string(StringDef::default()).to_string();
            Ok(Plan {
                id: std::any::type_name::<Self>().to_string(),
                command: format!(
                    "cat {eco_string} >> {}",
                    self.location.join("test.txt").display()
                ),
            })
        }

        fn is_success(&self, execution_result: &Output) -> Result<bool, &'static str> {
            if execution_result.status_code == Some(1) {
                Ok(true)
            } else {
                Err("status code should be 1")
            }
        }

        fn to_yaml(&self) -> serde_yaml::Value {
            serde_yaml::to_value(self).expect("serialize")
        }
    }

    #[test]
    fn can_run() {
        let base_location = std::env::temp_dir().join("crazy-train");
        let location_step_1 = base_location.join("step-1");
        let location_step_2 = base_location.join("step-2");

        let step_one = TestStepOne {
            location: location_step_1,
        };
        let step_two = TestStepTwo {
            location: location_step_2,
        };
        let randomaizer = Randomizer::with_seed(42);
        let runner = new(vec![Box::new(step_one), Box::new(step_two)]).randomizer(randomaizer);

        assert!(runner.run().is_ok());
    }
}