switchgear_testing/
services.rs1use std::env;
2
3pub const SKIP_INTEGRATION_TESTS_ENV: &str = "SWGR_SKIP_INTEGRATION_TESTS";
4
5#[derive(Debug)]
6pub struct IntegrationTestServices {
7 credentials: Option<String>,
8 postgres: Option<String>,
9 mysql: Option<String>,
10 lightning: Option<LightningIntegrationTestServices>,
11}
12
13#[derive(Debug, Clone)]
14pub struct LightningIntegrationTestServices {
15 pub cln: String,
16 pub lnd: String,
17}
18
19impl IntegrationTestServices {
20 pub fn create() -> anyhow::Result<Self> {
21 let _ = dotenvy::dotenv();
22
23 let credentials = match Self::env_or_panic("CREDENTIALS_SERVER_PORT") {
24 None => None,
25 Some(port) => {
26 let port = port.parse::<u16>()?;
27 Self::env_or_panic("CREDENTIALS_SERVER_HOSTNAME")
28 .map(|s| format!("http://{s}:{port}/credentials.tar.gz"))
29 }
30 };
31
32 if credentials.is_none() {
33 return Ok(Self {
34 credentials,
35 postgres: None,
36 mysql: None,
37 lightning: None,
38 });
39 }
40
41 let postgres = match (&credentials, Self::env_or_panic("POSTGRES_PORT")) {
42 (Some(_), Some(port)) => {
43 let port = port.parse::<u16>()?;
44 Self::env_or_panic("POSTGRES_HOSTNAME").map(|s| format!("{s}:{port}"))
45 }
46 _ => None,
47 };
48
49 let mysql = match (&credentials, Self::env_or_panic("MYSQL_PORT")) {
50 (Some(_), Some(port)) => {
51 let port = port.parse::<u16>()?;
52 Self::env_or_panic("MYSQL_HOSTNAME").map(|s| format!("{s}:{port}"))
53 }
54 _ => None,
55 };
56
57 let cln = match Self::env_or_panic("CLN_PORT") {
58 None => None,
59 Some(port) => {
60 let port = port.parse::<u16>()?;
61 Self::env_or_panic("CLN_HOSTNAME").map(|s| format!("{s}:{port}"))
62 }
63 };
64
65 let lnd = match Self::env_or_panic("LND_PORT") {
66 None => None,
67 Some(port) => {
68 let port = port.parse::<u16>()?;
69 Self::env_or_panic("LND_HOSTNAME").map(|s| format!("{s}:{port}"))
70 }
71 };
72
73 let lightning = match (&credentials, cln, lnd) {
74 (Some(_), Some(cln), Some(lnd)) => Some(LightningIntegrationTestServices { cln, lnd }),
75 _ => None,
76 };
77
78 Ok(Self {
79 credentials,
80 postgres,
81 mysql,
82 lightning,
83 })
84 }
85
86 fn env_or_panic(config_env: &str) -> Option<String> {
87 if env::var(SKIP_INTEGRATION_TESTS_ENV).is_ok_and(|s| s.to_lowercase() == "true") {
88 eprintln!("⚠️ WARNING: {SKIP_INTEGRATION_TESTS_ENV} is true, skipping integration tests for {config_env}");
89 return None;
90 }
91
92 match env::var(config_env) {
93 Ok(r) => Some(r),
94 Err(_) => {
95 panic!(
96 "
97
98❌❌❌ ERROR ❌❌❌
99
100Do one of:
101
102CONFIGURE INTEGRATION TEST ENVIRONMENT
103
104* configure integration tests - see testing/README.md
105* set env {config_env} to configure the service
106
107- or -
108
109SKIP INTEGRATION TESTS
110
111* set env {SKIP_INTEGRATION_TESTS_ENV}=true
112
113❌❌❌ ERROR ❌❌❌
114
115"
116 );
117 }
118 }
119 }
120
121 pub fn credentials(&self) -> Option<&String> {
122 self.credentials.as_ref()
123 }
124
125 pub fn postgres(&self) -> Option<&String> {
126 self.postgres.as_ref()
127 }
128
129 pub fn mysql(&self) -> Option<&String> {
130 self.mysql.as_ref()
131 }
132
133 pub fn lightning(&self) -> Option<&LightningIntegrationTestServices> {
134 self.lightning.as_ref()
135 }
136}