#![cfg(feature = "consul")]
mod common;
use std::time::Duration;
use confers::remote::{ConsulSourceBuilder, PolledSource};
#[tokio::test]
async fn test_consul_source_connect() {
if !common::is_service_available(
"http://127.0.0.1:8500/v1/status/leader",
Duration::from_secs(2),
)
.await
{
eprintln!("Skipping test: Consul not available");
return;
}
let source = ConsulSourceBuilder::new()
.address("127.0.0.1:8500")
.prefix("test-config")
.interval(Duration::from_secs(10))
.build()
.expect("Failed to build Consul 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_consul_source_with_token() {
if !common::is_service_available(
"http://127.0.0.1:8500/v1/status/leader",
Duration::from_secs(2),
)
.await
{
eprintln!("Skipping test: Consul not available");
return;
}
let source = ConsulSourceBuilder::new()
.address("127.0.0.1:8500")
.prefix("test-config")
.build()
.expect("Failed to build Consul source");
let result = source.poll().await;
println!("Poll result: {:?}", result);
}
#[tokio::test]
async fn test_consul_source_parse_config() {
if !common::is_service_available(
"http://127.0.0.1:8500/v1/status/leader",
Duration::from_secs(2),
)
.await
{
eprintln!("Skipping test: Consul not available");
return;
}
use reqwest::Client as HttpClient;
let http_client = HttpClient::new();
let put_result: Result<reqwest::Response, _> = http_client
.put("http://127.0.0.1:8500/v1/kv/test-config/app")
.body(r#"{"host": "localhost", "port": 8080}"#)
.send()
.await;
if put_result.is_err() {
eprintln!("Failed to write test config to Consul");
return;
}
let source = ConsulSourceBuilder::new()
.address("127.0.0.1:8500")
.prefix("test-config")
.build()
.expect("Failed to build Consul 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 _ = http_client
.delete("http://127.0.0.1:8500/v1/kv/test-config")
.send()
.await;
}