#![cfg(feature = "etcd")]
mod common;
use std::time::Duration;
use confers::remote::{EtcdSourceBuilder, PolledSource};
#[tokio::test]
async fn test_etcd_source_connect() {
if !common::is_service_available("http://127.0.0.1:2379/health", Duration::from_secs(2)).await {
eprintln!("Skipping test: Etcd not available");
return;
}
let source = EtcdSourceBuilder::new()
.endpoint("127.0.0.1:2379")
.prefix("test-config")
.interval(Duration::from_secs(10))
.build()
.await
.expect("Failed to build Etcd source");
let result = source.poll().await;
match result {
Ok(config) => {
println!("Got config: {:?}", config);
}
Err(e) => {
println!("Expected error (no config): {:?}", e);
}
}
}
#[tokio::test]
async fn test_etcd_source_with_auth() {
if !common::is_service_available("http://127.0.0.1:2379/health", Duration::from_secs(2)).await {
eprintln!("Skipping test: Etcd not available");
return;
}
let source = EtcdSourceBuilder::new()
.endpoint("127.0.0.1:2379")
.prefix("test-config")
.build()
.await
.expect("Failed to build Etcd source");
let result = source.poll().await;
println!("Poll result: {:?}", result);
}
#[tokio::test]
async fn test_etcd_source_parse_config() {
if !common::is_service_available("http://127.0.0.1:2379/health", Duration::from_secs(2)).await {
eprintln!("Skipping test: Etcd not available");
return;
}
use reqwest::Client;
let client = Client::new();
let base_url = "http://127.0.0.1:2379/v3/kv/put";
let host_resp = client
.post(base_url)
.json(&serde_json::json!({
"key": "dGVzdC1jb25maWcvYXBwL2hvc3Q=", "value": "bG9jYWxob3N0" }))
.send()
.await;
if host_resp.is_err() {
eprintln!("Failed to write test config to Etcd");
return;
}
let port_resp = client
.post(base_url)
.json(&serde_json::json!({
"key": "dGVzdC1jb25maWcvYXBwL3BvcnQ=", "value": "ODA4MA==" }))
.send()
.await;
if port_resp.is_err() {
eprintln!("Failed to write port config to Etcd");
return;
}
let source = EtcdSourceBuilder::new()
.endpoint("127.0.0.1:2379")
.prefix("test-config")
.build()
.await
.expect("Failed to build Etcd source");
let result = source.poll().await;
match result {
Ok(config) => {
println!("Got config: {:?}", config);
assert!(!config.is_empty(), "Config should not be empty");
}
Err(e) => {
println!("Error: {:?}", e);
}
}
let _ = client
.post("http://127.0.0.1:2379/v3/kv/del")
.json(&serde_json::json!({
"key": "dGVzdC1jb25maWc="
}))
.send()
.await;
}
#[tokio::test]
async fn test_etcd_source_json_config() {
if !common::is_service_available("http://127.0.0.1:2379/health", Duration::from_secs(2)).await {
eprintln!("Skipping test: Etcd not available");
return;
}
use base64::Engine;
use reqwest::Client;
let client = Client::new();
let json_key = "dGVzdC1jb25maWctanNvbg=="; let json_value = base64::engine::general_purpose::STANDARD
.encode(r#"{"database": {"host": "db.example.com", "port": 5432}}"#);
let result = client
.post("http://127.0.0.1:2379/v3/kv/put")
.json(&serde_json::json!({
"key": json_key,
"value": json_value
}))
.send()
.await;
if result.is_err() {
eprintln!("Failed to write JSON config to Etcd");
return;
}
let source = EtcdSourceBuilder::new()
.endpoint("127.0.0.1:2379")
.prefix("test-config-json")
.build()
.await
.expect("Failed to build Etcd source");
let result = source.poll().await;
match result {
Ok(config) => {
println!("Got JSON config: {:?}", config);
assert!(!config.is_empty(), "Config should not be empty");
}
Err(e) => {
println!("Error: {:?}", e);
}
}
let _ = client
.post("http://127.0.0.1:2379/v3/kv/del")
.json(&serde_json::json!({
"key": json_key
}))
.send()
.await;
}