Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add spanner rest port and upgrade google-cloud-sdk version #117

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 54 additions & 8 deletions src/google_cloud_sdk_emulators/mod.rs
DDtKey marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -8,12 +30,16 @@ 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;
#[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;

#[derive(Debug, Clone)]
pub struct CloudSdkArgs {
pub host: String,
pub port: u16,
pub rest_port: Option<u16>,
pub emulator: Emulator,
}

Expand Down Expand Up @@ -49,13 +75,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<u16>,
ready_condition: WaitFor,
}

Expand All @@ -75,21 +106,29 @@ impl Image for CloudSdk {
}

fn expose_ports(&self) -> Vec<u16> {
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<u16>,
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,
Expand All @@ -99,6 +138,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"),
)
Expand All @@ -107,6 +147,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"),
)
Expand All @@ -116,6 +157,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"),
)
Expand All @@ -124,14 +166,16 @@ impl CloudSdk {
pub fn pubsub() -> (Self, CloudSdkArgs) {
Self::new(
PUBSUB_PORT,
None,
Emulator::PubSub,
WaitFor::message_on_stderr("[pubsub] INFO: Server started, listening on"),
)
}

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"),
)
Expand Down Expand Up @@ -190,6 +234,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)));
}
}
Loading