From 17ed5dd14d36d61008c441196e9c0418a5aa7929 Mon Sep 17 00:00:00 2001 From: Philippe Laflamme Date: Tue, 9 Apr 2024 10:59:50 -0400 Subject: [PATCH 1/5] feat: add spanner REST API port --- src/google_cloud_sdk_emulators/mod.rs | 38 +++++++++++++++++++++------ 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/google_cloud_sdk_emulators/mod.rs b/src/google_cloud_sdk_emulators/mod.rs index e0d6d1a..281069f 100644 --- a/src/google_cloud_sdk_emulators/mod.rs +++ b/src/google_cloud_sdk_emulators/mod.rs @@ -8,12 +8,14 @@ pub const BIGTABLE_PORT: u16 = 8086; pub const DATASTORE_PORT: u16 = 8081; pub const FIRESTORE_PORT: u16 = 8080; pub const PUBSUB_PORT: u16 = 8085; -pub const SPANNER_PORT: u16 = 9010; +pub const SPANNER_GRPC_PORT: u16 = 9010; +pub const SPANNER_REST_PORT: u16 = 9020; #[derive(Debug, Clone)] pub struct CloudSdkArgs { pub host: String, pub port: u16, + pub rest_port: Option, pub emulator: Emulator, } @@ -49,13 +51,18 @@ impl ImageArgs for CloudSdkArgs { args.push("--host-port".to_owned()); args.push(format!("{}:{}", self.host, self.port)); + if let Some(rest_port) = self.rest_port { + args.push("--rest-port".to_owned()); + args.push(rest_port.to_string()); + } + Box::new(args.into_iter()) } } #[derive(Debug)] pub struct CloudSdk { - exposed_port: u16, + exposed_ports: Vec, ready_condition: WaitFor, } @@ -75,21 +82,29 @@ impl Image for CloudSdk { } fn expose_ports(&self) -> Vec { - vec![self.exposed_port] + self.exposed_ports.clone() } } impl CloudSdk { - fn new(port: u16, emulator: Emulator, ready_condition: WaitFor) -> (Self, CloudSdkArgs) { + fn new( + port: u16, + rest_port: Option, + emulator: Emulator, + ready_condition: WaitFor, + ) -> (Self, CloudSdkArgs) { let arguments = CloudSdkArgs { host: HOST.to_owned(), port, + rest_port, emulator, }; - let exposed_port = port; + let mut exposed_ports = vec![port]; + exposed_ports.extend(rest_port); + ( Self { - exposed_port, + exposed_ports, ready_condition, }, arguments, @@ -99,6 +114,7 @@ impl CloudSdk { pub fn bigtable() -> (Self, CloudSdkArgs) { Self::new( BIGTABLE_PORT, + None, Emulator::Bigtable, WaitFor::message_on_stderr("[bigtable] Cloud Bigtable emulator running on"), ) @@ -107,6 +123,7 @@ impl CloudSdk { pub fn firestore() -> (Self, CloudSdkArgs) { Self::new( FIRESTORE_PORT, + None, Emulator::Firestore, WaitFor::message_on_stderr("[firestore] Dev App Server is now running"), ) @@ -116,6 +133,7 @@ impl CloudSdk { let project = project.into(); Self::new( DATASTORE_PORT, + None, Emulator::Datastore { project }, WaitFor::message_on_stderr("[datastore] Dev App Server is now running"), ) @@ -124,6 +142,7 @@ impl CloudSdk { pub fn pubsub() -> (Self, CloudSdkArgs) { Self::new( PUBSUB_PORT, + None, Emulator::PubSub, WaitFor::message_on_stderr("[pubsub] INFO: Server started, listening on"), ) @@ -131,7 +150,8 @@ impl CloudSdk { pub fn spanner() -> (Self, CloudSdkArgs) { Self::new( - SPANNER_PORT, // gRPC port + SPANNER_GRPC_PORT, + Some(SPANNER_REST_PORT), Emulator::Spanner, WaitFor::message_on_stderr("Cloud Spanner emulator running"), ) @@ -190,6 +210,8 @@ mod tests { let docker = clients::Cli::default(); let node = docker.run(google_cloud_sdk_emulators::CloudSdk::spanner()); assert!(RANDOM_PORTS - .contains(&node.get_host_port_ipv4(google_cloud_sdk_emulators::SPANNER_PORT))); + .contains(&node.get_host_port_ipv4(google_cloud_sdk_emulators::SPANNER_GRPC_PORT))); + assert!(RANDOM_PORTS + .contains(&node.get_host_port_ipv4(google_cloud_sdk_emulators::SPANNER_REST_PORT))); } } From 53c937cd0a5850eb652c8fd83c33d5a890498fc4 Mon Sep 17 00:00:00 2001 From: Philippe Laflamme Date: Tue, 9 Apr 2024 11:05:26 -0400 Subject: [PATCH 2/5] feat: bump google-cloud-sdk --- src/google_cloud_sdk_emulators/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/google_cloud_sdk_emulators/mod.rs b/src/google_cloud_sdk_emulators/mod.rs index 281069f..bf7298f 100644 --- a/src/google_cloud_sdk_emulators/mod.rs +++ b/src/google_cloud_sdk_emulators/mod.rs @@ -1,7 +1,7 @@ use testcontainers::{core::WaitFor, Image, ImageArgs}; -const NAME: &str = "google/cloud-sdk"; -const TAG: &str = "362.0.0-emulators"; +const NAME: &str = "gcr.io/google.com/cloudsdktool/google-cloud-cli"; +const TAG: &str = "471.0.0-emulators"; const HOST: &str = "0.0.0.0"; pub const BIGTABLE_PORT: u16 = 8086; From 67590db5c0beb7216bebe0f202a73e61f297be00 Mon Sep 17 00:00:00 2001 From: Philippe Laflamme Date: Tue, 9 Apr 2024 12:16:30 -0400 Subject: [PATCH 3/5] Revert "feat: bump google-cloud-sdk" This reverts commit 53c937cd0a5850eb652c8fd83c33d5a890498fc4. --- src/google_cloud_sdk_emulators/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/google_cloud_sdk_emulators/mod.rs b/src/google_cloud_sdk_emulators/mod.rs index bf7298f..281069f 100644 --- a/src/google_cloud_sdk_emulators/mod.rs +++ b/src/google_cloud_sdk_emulators/mod.rs @@ -1,7 +1,7 @@ use testcontainers::{core::WaitFor, Image, ImageArgs}; -const NAME: &str = "gcr.io/google.com/cloudsdktool/google-cloud-cli"; -const TAG: &str = "471.0.0-emulators"; +const NAME: &str = "google/cloud-sdk"; +const TAG: &str = "362.0.0-emulators"; const HOST: &str = "0.0.0.0"; pub const BIGTABLE_PORT: u16 = 8086; From 0f635ab83f0de44d6e8bd72b819488cb9eb589d1 Mon Sep 17 00:00:00 2001 From: Philippe Laflamme Date: Tue, 9 Apr 2024 12:19:30 -0400 Subject: [PATCH 4/5] fix: mark SPANNER_PORT as deprecated instead of removing --- src/google_cloud_sdk_emulators/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/google_cloud_sdk_emulators/mod.rs b/src/google_cloud_sdk_emulators/mod.rs index 281069f..4ac2961 100644 --- a/src/google_cloud_sdk_emulators/mod.rs +++ b/src/google_cloud_sdk_emulators/mod.rs @@ -8,6 +8,8 @@ pub const BIGTABLE_PORT: u16 = 8086; pub const DATASTORE_PORT: u16 = 8081; pub const FIRESTORE_PORT: u16 = 8080; pub const PUBSUB_PORT: u16 = 8085; +#[deprecated(since = "0.3.8", note = "please use `SPANNER_GRPC_PORT` instead")] +pub const SPANNER_PORT: u16 = SPANNER_GRPC_PORT; pub const SPANNER_GRPC_PORT: u16 = 9010; pub const SPANNER_REST_PORT: u16 = 9020; From 73ab5dbd8bbb33e709f0b4cb5cffa5534a5e8026 Mon Sep 17 00:00:00 2001 From: Philippe Laflamme Date: Tue, 9 Apr 2024 12:43:56 -0400 Subject: [PATCH 5/5] docs: add some documentation to google sdk module --- src/google_cloud_sdk_emulators/mod.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/google_cloud_sdk_emulators/mod.rs b/src/google_cloud_sdk_emulators/mod.rs index 4ac2961..85253ff 100644 --- a/src/google_cloud_sdk_emulators/mod.rs +++ b/src/google_cloud_sdk_emulators/mod.rs @@ -1,3 +1,25 @@ +/// Module to work with [`Google Cloud Emulators`] inside of tests. +/// +/// The same image can be used to run multiple emulators, using the `emulator` argument allows +/// selecting the one to run. +/// +/// This module is based on the official [`GCloud SDK image`]. +/// +/// # Example +/// ``` +/// use testcontainers::clients; +/// use testcontainers_modules::google_cloud_sdk_emulators; +/// +/// let docker = clients::Cli::default(); +/// let container = docker.run(google_cloud_sdk_emulators::CloudSdk::spanner()); +/// +/// let spanner_host = format!("localhost:{}", container.get_host_port_ipv4(google_cloud_sdk_emulators::SPANNER_REST_PORT)); +/// +/// // do something with the started spanner instance. +/// ``` +/// +/// [`Google Cloud Emulators`]: https://cloud.google.com/sdk/gcloud/reference/beta/emulators +/// [`GCloud SDK image`]: https://cloud.google.com/sdk/docs/downloads-docker use testcontainers::{core::WaitFor, Image, ImageArgs}; const NAME: &str = "google/cloud-sdk";