switchgear_testing/
services.rs

1use std::env;
2
3const TESTING_ENV_FILE_PATH: &str = "./testing.env";
4
5#[derive(Debug)]
6pub struct IntegrationTestServices {
7    credentials: String,
8    postgres: String,
9    mysql: String,
10    lightning: LightningIntegrationTestServices,
11}
12
13#[derive(Debug, Clone)]
14pub struct LightningIntegrationTestServices {
15    pub cln: String,
16    pub lnd: String,
17}
18
19impl Default for IntegrationTestServices {
20    fn default() -> Self {
21        Self::new()
22    }
23}
24
25impl IntegrationTestServices {
26    pub fn new() -> Self {
27        let _ = dotenvy::from_filename(TESTING_ENV_FILE_PATH);
28
29        let credentials = format!(
30            "http://{}:{}/credentials.tar.gz",
31            Self::env_or_panic("CREDENTIALS_SERVER_HOSTNAME"),
32            Self::env_or_panic("CREDENTIALS_SERVER_PORT")
33        );
34
35        let postgres = format!(
36            "{}:{}",
37            Self::env_or_panic("POSTGRES_HOSTNAME"),
38            Self::env_or_panic("POSTGRES_PORT")
39        );
40
41        let mysql = format!(
42            "{}:{}",
43            Self::env_or_panic("MYSQL_HOSTNAME"),
44            Self::env_or_panic("MYSQL_PORT")
45        );
46
47        let cln = format!(
48            "{}:{}",
49            Self::env_or_panic("CLN_HOSTNAME"),
50            Self::env_or_panic("CLN_PORT")
51        );
52
53        let lnd = format!(
54            "{}:{}",
55            Self::env_or_panic("LND_HOSTNAME"),
56            Self::env_or_panic("LND_PORT")
57        );
58
59        Self {
60            credentials,
61            postgres,
62            mysql,
63            lightning: LightningIntegrationTestServices { cln, lnd },
64        }
65    }
66
67    fn env_or_panic(config_env: &str) -> String {
68        env::var(config_env).unwrap_or_else(|_| {
69            panic!(
70                "
71
72❌ INVALID INTEGRATION TEST ENVIRONMENT ❌
73
74Env var '{config_env}' is not set.
75
76See testing/README.md to configure integration tests and services.
77
78",
79            )
80        })
81    }
82
83    pub fn credentials(&self) -> &str {
84        &self.credentials
85    }
86
87    pub fn postgres(&self) -> &str {
88        &self.postgres
89    }
90
91    pub fn mysql(&self) -> &str {
92        &self.mysql
93    }
94
95    pub fn lightning(&self) -> &LightningIntegrationTestServices {
96        &self.lightning
97    }
98}