Skip to content

Commit 999f5ec

Browse files
committed
Add etcd authentication setup and user mapping in tests
1 parent 68195dc commit 999f5ec

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/lib.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ mod tests {
688688
use std::time::Duration;
689689

690690
use super::*;
691+
use etcd_client::Permission;
691692
use testcontainers::{
692693
core::{IntoContainerPort, WaitFor},
693694
runners::SyncRunner,
@@ -702,6 +703,32 @@ mod tests {
702703
"http://0.0.0.0:2379",
703704
];
704705

706+
const ETCD_USER: &str = "root";
707+
const ETCD_PASS: &str = "secret";
708+
709+
// Setup etcd root role/user and enable authentication
710+
async fn etcd_auth_setup(endpoint: String) {
711+
let mut client: Client = Client::connect([endpoint], None)
712+
.await
713+
.expect("connect etcd");
714+
715+
// add root user and role with full permissions
716+
client.role_add("root").await.expect("add role");
717+
client.role_grant_permission("root", Permission::read_write("/"))
718+
.await
719+
.expect("grant permission");
720+
721+
client.user_add(ETCD_USER, ETCD_PASS, None)
722+
.await
723+
.expect("add user");
724+
725+
client.user_grant_role(ETCD_USER, "root")
726+
.await
727+
.expect("grant role");
728+
729+
client.auth_enable().await.expect("enable auth");
730+
}
731+
705732
fn create_container() -> (Container<GenericImage>, String) {
706733
let container = GenericImage::new("quay.io/coreos/etcd", "v3.6.4")
707734
.with_exposed_port(2379.tcp())
@@ -723,6 +750,8 @@ mod tests {
723750
.expect("Exposed host port should be available");
724751

725752
let url = format!("{}:{}", host, port);
753+
let rt = tokio::runtime::Runtime::new().expect("Tokio runtime should be initialized");
754+
rt.block_on(etcd_auth_setup(url.clone()));
726755
(container, url)
727756
}
728757

@@ -739,6 +768,16 @@ mod tests {
739768
)
740769
.expect("Server should have been created");
741770

771+
// Create a user mapping
772+
Spi::run(
773+
format!(
774+
"CREATE USER MAPPING FOR CURRENT_USER SERVER etcd_test_server options (user '{}', password '{}')",
775+
ETCD_USER, ETCD_PASS
776+
)
777+
.as_str(),
778+
)
779+
.expect("User mapping should have been created");
780+
742781
// Create a foreign table
743782
Spi::run("CREATE FOREIGN TABLE test (key text, value text) server etcd_test_server options (rowid_column 'key')").expect("Test table should have been created");
744783
}

0 commit comments

Comments
 (0)