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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use anyhow::{format_err, Context, Result};
use diem_types::chain_id::ChainId;
use reqwest::{blocking, StatusCode, Url};
use std::{
    path::Path,
    process::{Child, Command, Stdio},
};

pub struct Process {
    port: u16,
    process: Child,
}

impl Drop for Process {
    fn drop(&mut self) {
        // Kill process process if still running.
        match self.process.try_wait().unwrap() {
            Some(status) => {
                if !status.success() {
                    panic!(
                        "Process terminated with status: {}",
                        status.code().unwrap_or(-1)
                    );
                }
            }
            None => {
                self.process.kill().unwrap();
            }
        }
    }
}

impl Process {
    pub fn start(
        faucet_bin_path: &Path,
        port: u16,
        server_port: u16,
        diem_root_key_path: &Path,
    ) -> Self {
        Self {
            port,
            process: Command::new(faucet_bin_path)
                .arg("-s")
                .arg(format!("http://localhost:{}", server_port))
                .arg("-p")
                .arg(format!("{}", port))
                .arg("-m")
                .arg(
                    diem_root_key_path
                        .canonicalize()
                        .expect("Unable to get canonical path of diem root key file")
                        .to_str()
                        .unwrap(),
                )
                .arg("-c")
                .arg(ChainId::test().id().to_string())
                .stdin(Stdio::inherit())
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .spawn()
                .with_context(|| {
                    format!(
                        "Error launching faucet process with binary: {:?}",
                        faucet_bin_path
                    )
                })
                .expect("Failed to spawn faucet process"),
        }
    }

    pub fn mint_url(&self) -> String {
        format!("http://localhost:{}/mint", self.port)
    }

    pub fn health_check_url(&self) -> Url {
        Url::parse(format!("http://localhost:{}/-/healthy", self.port).as_str()).unwrap()
    }

    pub fn wait_for_connectivity(&self) -> Result<()> {
        let client = blocking::Client::new();
        let num_attempts = 60;

        for i in 0..num_attempts {
            println!("Wait for faucet connectivity attempt: {}", i);
            let resp = client.get(self.health_check_url()).send();

            if let Ok(ret) = resp {
                if let StatusCode::OK = ret.status() {
                    println!("{}", ret.text()?);
                    return Ok(());
                }
            }
            ::std::thread::sleep(::std::time::Duration::from_millis(500));
        }
        Err(format_err!("Faucet launch failed"))
    }
}