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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#![allow(dead_code)]

mod commands;
pub use commands::{get_command, CommandBuilder};

mod params;
mod testing_directory;

use crate::{
    jormungandr::{legacy::LegacyConfigError, JormungandrError, JormungandrProcess, RestError},
    testing::{configuration::get_jormungandr_app, SpeedBenchmarkDef, SpeedBenchmarkRun},
};
use assert_cmd::assert::OutputAssertExt;
use assert_fs::{fixture::FixtureError, TempDir};
use jormungandr_lib::crypto::hash::Hash;
use jortestkit::process::{self as process_utils, ProcessError};
pub use params::{
    CommunicationParams, ConfigurableNodeConfig, JormungandrBootstrapper, JormungandrParams,
};
use serde::Deserialize;
use std::{
    fmt::Debug,
    path::{Path, PathBuf},
    process::{Child, Command, ExitStatus, Stdio},
    time::{Duration, Instant},
};
pub use testing_directory::TestingDirectory;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum StartupError {
    #[error("could not start jormungandr due to process issue")]
    JormungandrNotLaunched(#[from] ProcessError),
    #[error("error setting up temporary filesystem fixture")]
    FsFixture(#[from] FixtureError),
    #[error("failed to convert jormungandr configuration to a legacy version")]
    LegacyConfig(#[from] LegacyConfigError),
    #[error("node wasn't properly bootstrap after {timeout} s. Log file: {log_content}")]
    Timeout { timeout: u64, log_content: String },
    #[error("error(s) while starting")]
    JormungandrError(#[from] JormungandrError),
    #[error("expected message not found: {entry} in logs: {log_content}")]
    EntryNotFoundInLogs { entry: String, log_content: String },
    #[error("too many failures while attempting to start jormungandr")]
    TooManyAttempts,
    #[error("Block0 hash is not valid")]
    InvalidBlock0Hash(#[from] chain_crypto::hash::Error),
    #[error("Process exited with status {0}")]
    ProcessExited(ExitStatus),
    #[error("Cannot get rest status")]
    CannotGetRestStatus(#[from] RestError),
    #[error("start params not defined")]
    StartParamsNotDefined,
    #[error(transparent)]
    Params(#[from] crate::jormungandr::starter::params::Error),
}

pub enum StartupVerificationMode {
    Log,
    Rest,
}

#[derive(Clone, Debug, Deserialize)]
pub struct FaketimeConfig {
    /// Clock drift (1 = no drift, 2 = double speed)
    pub drift: f32,
    /// Offset from the real clock in seconds
    pub offset: i32,
}

#[derive(Debug, Copy, Clone, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum LeadershipMode {
    Leader,
    Passive,
}

#[derive(Debug, Copy, Clone, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum PersistenceMode {
    Persistent,
    InMemory,
}

#[derive(Debug, Clone)]
pub enum NodeBlock0 {
    Hash(Hash),
    File(PathBuf),
}

#[derive(Clone, Debug, Copy)]
pub enum FromGenesis {
    Hash,
    File,
}

#[derive(Clone, Debug, Copy)]
pub enum OnFail {
    RetryOnce,
    Panic,
    RetryUnlimitedOnPortOccupied,
}

pub struct Starter {
    jormungandr_app_path: Option<PathBuf>,
    configured_starter: ConfiguredStarter,
    alias: String,
    temp_dir: Option<TestingDirectory>,
    config: Option<JormungandrParams>,
    benchmark: Option<SpeedBenchmarkDef>,
}

impl Default for Starter {
    fn default() -> Self {
        let alias = "".to_owned();

        Self {
            configured_starter: ConfiguredStarter::new(&alias),
            alias,
            temp_dir: None,
            config: None,
            benchmark: None,
            jormungandr_app_path: None,
        }
    }
}

impl Starter {
    pub fn alias(mut self, alias: String) -> Self {
        self.alias = alias;
        self
    }

    pub fn jormungandr_app(self, path: PathBuf) -> Self {
        self.jormungandr_app_option(&Some(path))
    }

    pub fn jormungandr_app_option(mut self, path: &Option<PathBuf>) -> Self {
        self.jormungandr_app_path = path.clone();
        self
    }

    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.configured_starter.timeout = timeout;
        self
    }

    pub fn benchmark(mut self, name: &str) -> Self {
        self.benchmark = Some(SpeedBenchmarkDef::new(name.to_owned()));
        self
    }

    pub fn verify_by(mut self, verification_mode: StartupVerificationMode) -> Self {
        self.configured_starter.verification_mode = verification_mode;
        self
    }

    pub fn on_fail(mut self, on_fail: OnFail) -> Self {
        self.configured_starter.on_fail = on_fail;
        self
    }

    pub fn config(mut self, config: JormungandrParams) -> Self {
        self.config = Some(config);
        self
    }

    pub fn temp_dir(mut self, temp_dir: TempDir) -> Self {
        self.temp_dir = Some(TestingDirectory::from_temp(temp_dir).into_persistent());
        self
    }

    pub fn working_dir(mut self, path: &Path) -> Self {
        self.temp_dir = Some(path.to_path_buf().into());
        self
    }

    pub fn testing_dir(mut self, testing_directory: TestingDirectory) -> Self {
        self.temp_dir = Some(testing_directory);
        self
    }

    pub fn verbose(mut self, verbose: bool) -> Self {
        self.configured_starter.verbose = verbose;
        self
    }

    fn get_jormungandr_app_path(&self) -> PathBuf {
        self.jormungandr_app_path
            .clone()
            .unwrap_or_else(get_jormungandr_app)
    }

    pub fn start_benchmark_run(&self) -> Option<SpeedBenchmarkRun> {
        self.benchmark.as_ref().map(|benchmark_def| {
            benchmark_def
                .clone()
                .target(self.configured_starter.timeout)
                .start()
        })
    }

    pub fn start_with_fail_in_logs(
        mut self,
        expected_msg_in_logs: &str,
    ) -> Result<(), StartupError> {
        let app = self.get_jormungandr_app_path();
        let temp_dir = self.temp_dir.take();
        let params = self.config.ok_or(StartupError::StartParamsNotDefined)?;
        let comm = params.comm();
        self.configured_starter.start_with_fail_in_logs(
            comm,
            temp_dir,
            expected_msg_in_logs,
            get_command(&params, app, params.leadership()),
        )
    }

    pub fn start_async(mut self) -> Result<JormungandrProcess, StartupError> {
        let app = self.get_jormungandr_app_path();
        let temp_dir = self.temp_dir.take();
        let params = self.config.ok_or(StartupError::StartParamsNotDefined)?;
        let comm = params.comm();
        self.configured_starter.start_async(
            comm,
            temp_dir,
            get_command(&params, app, params.leadership()),
        )
    }

    pub fn start(mut self) -> Result<JormungandrProcess, StartupError> {
        let app = self.get_jormungandr_app_path();
        let temp_dir = self.temp_dir.take();
        let benchmark = self.start_benchmark_run();
        let params = self.config.ok_or(StartupError::StartParamsNotDefined)?;
        let command = get_command(&params, app, params.leadership());
        let process = self.configured_starter.start(params, temp_dir, command)?;
        finish_benchmark(benchmark);
        Ok(process)
    }

    pub fn start_should_fail_with_message(self, expected_msg: &str) -> Result<(), StartupError> {
        let app = self.get_jormungandr_app_path();
        let params = self.config.ok_or(StartupError::StartParamsNotDefined)?;
        let command = get_command(&params, app, params.leadership());
        ConfiguredStarter::new(&self.alias).start_with_fail_in_stderr(command, expected_msg);
        Ok(())
    }
}

fn finish_benchmark(benchmark_run: Option<SpeedBenchmarkRun>) {
    if let Some(benchmark_run) = benchmark_run {
        benchmark_run.stop().print();
    }
}

pub struct ConfiguredStarter {
    alias: String,
    timeout: Duration,
    sleep: u64,
    verification_mode: StartupVerificationMode,
    on_fail: OnFail,
    verbose: bool,
}

impl ConfiguredStarter {
    pub fn new(alias: impl Into<String>) -> Self {
        Self {
            alias: alias.into(),
            timeout: Duration::from_secs(30),
            sleep: 2,
            verification_mode: StartupVerificationMode::Rest,
            on_fail: OnFail::RetryUnlimitedOnPortOccupied,
            verbose: false,
        }
    }

    pub fn start_with_fail_in_logs(
        self,
        comms: CommunicationParams,
        temp_dir: Option<TestingDirectory>,
        expected_msg_in_logs: &str,
        command: Command,
    ) -> Result<(), StartupError> {
        let sleep_duration = self.sleep;
        let timeout = self.timeout;

        let start = Instant::now();
        let process = self.start_async(comms, temp_dir, command)?;

        loop {
            if start.elapsed() > timeout {
                return Err(StartupError::EntryNotFoundInLogs {
                    entry: expected_msg_in_logs.to_string(),
                    log_content: process.logger.get_log_content(),
                });
            }
            process_utils::sleep(sleep_duration);
            if process.logger.contains_any_of(&[expected_msg_in_logs]) {
                return Ok(());
            }
        }
    }

    pub fn start_with_fail_in_stderr(self, mut command: Command, expected_msg: &str) {
        let verbose = self.verbose;
        crate::cond_println!(verbose, "Running start command: {:?}", command);
        crate::cond_println!(
            verbose,
            "Expecting node to fail with message '{expected_msg}'..."
        );
        command
            .stderr(Stdio::piped())
            .stdout(Stdio::piped())
            .assert()
            .failure()
            .stderr(predicates::str::contains(expected_msg));
    }

    pub fn start_process(&self, command: &mut Command) -> Child {
        let verbose = self.verbose;
        crate::cond_println!(verbose, "Running start command: {:?}", command);
        crate::cond_println!(verbose, "Bootstrapping...");
        command
            .spawn()
            .expect("failed to execute 'start jormungandr node'")
    }

    pub fn start_async(
        self,
        comms: CommunicationParams,
        temp_dir: Option<TestingDirectory>,
        mut command: Command,
    ) -> Result<JormungandrProcess, StartupError> {
        JormungandrProcess::new(
            self.start_process(&mut command),
            comms,
            temp_dir,
            self.alias.clone(),
        )
    }

    pub fn start(
        self,
        mut params: JormungandrParams,
        mut temp_dir: Option<TestingDirectory>,
        mut command: Command,
    ) -> Result<JormungandrProcess, StartupError> {
        let mut retry_counter = 1;
        loop {
            let process = self.start_process(&mut command);

            let mut jormungandr = JormungandrProcess::new(
                process,
                params.comm(),
                temp_dir.take(),
                self.alias.clone(),
            )?;

            match (
                jormungandr.wait_for_bootstrap(&self.verification_mode, self.timeout),
                self.on_fail,
            ) {
                (Ok(()), _) => {
                    crate::cond_println!(self.verbose, "jormungandr is up");
                    return Ok(jormungandr);
                }

                (
                    Err(StartupError::JormungandrError(JormungandrError::PortAlreadyInUse)),
                    OnFail::RetryUnlimitedOnPortOccupied,
                ) => {
                    crate::cond_println!(
                        self.verbose,
                        "Port already in use error detected. Retrying with different port... "
                    );
                    params.refresh_instance_params();
                }
                (Err(err), OnFail::Panic) => {
                    panic!("Jormungandr node cannot start due to error: {}", err);
                }
                (Err(err), _) => {
                    crate::cond_println!(
                        self.verbose,
                        "Jormungandr failed to start due to error {:?}. Retrying... ",
                        err
                    );
                    retry_counter -= 1;
                }
            }

            if retry_counter < 0 {
                return Err(StartupError::TooManyAttempts);
            }

            temp_dir = jormungandr.steal_temp_dir();
        }
    }
}