generated from jmgilman/rust-template-lib
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcommon.rs
116 lines (105 loc) · 3.11 KB
/
common.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
use std::collections::HashMap;
use async_trait::async_trait;
use consulrs::{
api::{
check::common::AgentServiceCheckBuilder,
service::{common::AgentServiceAddressBuilder, requests::RegisterServiceRequest},
},
catalog,
client::{Client, ConsulClient, ConsulClientSettingsBuilder},
service,
};
pub use dockertest_server::servers::hashi::{
counting::{CountingServer, CountingServerConfig},
ConsulServer, ConsulServerConfig,
};
use dockertest_server::Test;
pub const CHECK_NAME: &str = "health";
pub const CONSUL_PORT: u32 = 9201;
pub const COUNTING_PORT: u32 = 9200;
pub const SERVICE_NAME: &str = "counting";
pub const VERSION: &str = "1.9.9";
#[async_trait]
pub trait ConsulServerHelper {
/// Returns a [ConsulClient] configured to connect to the [ConsulServer].
fn client(&self) -> ConsulClient;
/// Returns the node ID for the default node of the [ConsulServer].
async fn node(&self) -> String;
}
#[async_trait]
impl ConsulServerHelper for ConsulServer {
fn client(&self) -> ConsulClient {
ConsulClient::new(
ConsulClientSettingsBuilder::default()
.address(self.external_url())
.build()
.unwrap(),
)
.unwrap()
}
async fn node(&self) -> String {
let res = catalog::nodes(&self.client(), None).await;
res.unwrap().response.pop().unwrap().node
}
}
#[derive(Debug)]
pub struct TestService {
pub name: String,
pub check: String,
}
// Sets up a new test.
#[allow(dead_code)]
pub fn new_test() -> Test {
let mut test = Test::default();
let consul_config = ConsulServerConfig::builder()
.port(CONSUL_PORT)
.version(VERSION.into())
.build()
.unwrap();
let counting_config = CountingServerConfig::builder()
.port(COUNTING_PORT)
.build()
.unwrap();
test.register(consul_config);
test.register(counting_config);
test
}
#[allow(dead_code)]
pub async fn setup(client: &impl Client, counting: &CountingServer) -> TestService {
// Setup test service
let address = counting.internal_address();
let port = counting.internal_port;
let url = counting.internal_url();
let mut addresses = HashMap::new();
let test_address = AgentServiceAddressBuilder::default()
.address("192.168.1.2")
.port(1234 as u32)
.build()
.unwrap();
addresses.insert("lan_ipv4".to_string(), test_address);
service::register(
client,
SERVICE_NAME,
Some(
RegisterServiceRequest::builder()
.address(address)
.port(port)
.tagged_addresses(addresses)
.check(
AgentServiceCheckBuilder::default()
.name(CHECK_NAME)
.interval("1s")
.http(url)
.status("passing")
.build()
.unwrap(),
),
),
)
.await
.unwrap();
TestService {
name: SERVICE_NAME.into(),
check: CHECK_NAME.into(),
}
}