From 8819043d9ca4686e873e48391c973b3d60798c9d Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 5 Oct 2023 09:57:44 -0500 Subject: [PATCH] Update to testcontainers-rs 0.15 --- Cargo.lock | 9 ++-- Cargo.toml | 1 + aggregator/Cargo.toml | 2 +- aggregator_core/Cargo.toml | 2 +- aggregator_core/src/datastore/test_util.rs | 7 ++- core/Cargo.toml | 2 +- core/src/test_util/testcontainers.rs | 51 +++++++++++++++++++++- integration_tests/Cargo.toml | 2 +- integration_tests/src/daphne.rs | 2 +- interop_binaries/Cargo.toml | 2 +- 10 files changed, 63 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1d3a110e1..72260d7c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -427,11 +427,10 @@ dependencies = [ [[package]] name = "bollard-stubs" -version = "1.41.0" +version = "1.42.0-rc.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed2f2e73fffe9455141e170fb9c1feb0ac521ec7e7dcd47a7cab72a658490fb8" +checksum = "ed59b5c00048f48d7af971b71f800fdf23e858844a6f9e4d32ca72e9399e7864" dependencies = [ - "chrono", "serde", "serde_with", ] @@ -4252,9 +4251,9 @@ dependencies = [ [[package]] name = "testcontainers" -version = "0.14.0" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e2b1567ca8a2b819ea7b28c92be35d9f76fb9edb214321dcc86eb96023d1f87" +checksum = "f83d2931d7f521af5bae989f716c3fa43a6af9af7ec7a5e21b59ae40878cec00" dependencies = [ "bollard-stubs", "futures", diff --git a/Cargo.toml b/Cargo.toml index e7e628fed..bb5cfa5e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,7 @@ serde_json = "1.0.107" serde_test = "1.0.175" serde_yaml = "0.9.25" rstest = "0.17.0" +testcontainers = "0.15.0" thiserror = "1.0" tokio = { version = "1.32", features = ["full", "tracing"] } trillium = "0.2.9" diff --git a/aggregator/Cargo.toml b/aggregator/Cargo.toml index 5b89cb2e0..3426056e0 100644 --- a/aggregator/Cargo.toml +++ b/aggregator/Cargo.toml @@ -80,7 +80,7 @@ serde_urlencoded = "0.7.1" serde_yaml.workspace = true signal-hook = "0.3.17" signal-hook-tokio = { version = "0.3.1", features = ["futures-v0_3"] } -testcontainers = { version = "0.14.0", optional = true } +testcontainers = { workspace = true, optional = true } thiserror.workspace = true tokio.workspace = true tokio-postgres = { version = "0.7.10", features = ["with-chrono-0_4", "with-serde_json-1", "with-uuid-1", "array-impls"] } diff --git a/aggregator_core/Cargo.toml b/aggregator_core/Cargo.toml index 7b708c80a..167966fee 100644 --- a/aggregator_core/Cargo.toml +++ b/aggregator_core/Cargo.toml @@ -44,7 +44,7 @@ serde.workspace = true serde_json.workspace = true serde_yaml.workspace = true sqlx = { version = "0.7.2", optional = true, features = ["runtime-tokio-rustls", "migrate", "postgres"] } -testcontainers = { version = "0.14.0", optional = true } +testcontainers = { workspace = true, optional = true } thiserror.workspace = true tokio.workspace = true tokio-postgres = { version = "0.7.10", features = ["with-chrono-0_4", "with-serde_json-1", "with-uuid-1", "array-impls"] } diff --git a/aggregator_core/src/datastore/test_util.rs b/aggregator_core/src/datastore/test_util.rs index ee6d2026f..5af2fcaff 100644 --- a/aggregator_core/src/datastore/test_util.rs +++ b/aggregator_core/src/datastore/test_util.rs @@ -3,7 +3,7 @@ use crate::{ test_util::noop_meter, }; use deadpool_postgres::{Manager, Pool}; -use janus_core::time::Clock; +use janus_core::{test_util::testcontainers::Postgres, time::Clock}; use lazy_static::lazy_static; use rand::{distributions::Standard, random, thread_rng, Rng}; use ring::aead::{LessSafeKey, UnboundKey, AES_128_GCM}; @@ -17,7 +17,7 @@ use std::{ sync::{Arc, Barrier, Weak}, thread::{self, JoinHandle}, }; -use testcontainers::{images::postgres::Postgres, RunnableImage}; +use testcontainers::RunnableImage; use tokio::sync::{oneshot, Mutex}; use tokio_postgres::{connect, Config, NoTls}; use tracing::trace; @@ -56,8 +56,7 @@ impl EphemeralDatabase { move || { // Start an instance of Postgres running in a container. let container_client = testcontainers::clients::Cli::default(); - let db_container = container_client - .run(RunnableImage::from(Postgres::default()).with_tag("14-alpine")); + let db_container = container_client.run(RunnableImage::from(Postgres::default())); const POSTGRES_DEFAULT_PORT: u16 = 5432; let port_number = db_container.get_host_port_ipv4(POSTGRES_DEFAULT_PORT); trace!("Postgres container is up with port {port_number}"); diff --git a/core/Cargo.toml b/core/Cargo.toml index de8e92388..fecee48dd 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -56,7 +56,7 @@ serde_json = { workspace = true, optional = true } serde_yaml.workspace = true stopper = { version = "0.2.0", optional = true } tempfile = { version = "3", optional = true } -testcontainers = { version = "0.14", optional = true } +testcontainers = { workspace = true, optional = true } thiserror.workspace = true tokio = { workspace = true, features = ["macros", "net", "rt"] } tokio-stream = { version = "0.1.14", features = ["net"], optional = true } diff --git a/core/src/test_util/testcontainers.rs b/core/src/test_util/testcontainers.rs index dfe3e2639..63a8ae670 100644 --- a/core/src/test_util/testcontainers.rs +++ b/core/src/test_util/testcontainers.rs @@ -1,7 +1,10 @@ //! Testing functionality that interacts with the testcontainers library. -use std::sync::{Arc, Mutex, Weak}; -use testcontainers::clients::Cli; +use std::{ + collections::HashMap, + sync::{Arc, Mutex, Weak}, +}; +use testcontainers::{clients::Cli, core::WaitFor, Image}; /// Returns a container client, possibly shared with other callers to this function. pub fn container_client() -> Arc { @@ -18,3 +21,47 @@ pub fn container_client() -> Arc { client }) } + +/// A [`testcontainers::Image`] that provides a Postgres server. +#[derive(Debug)] +pub struct Postgres { + env_vars: HashMap, +} + +impl Postgres { + const NAME: &str = "postgres"; + const TAG: &str = "14-alpine"; +} + +impl Default for Postgres { + fn default() -> Self { + Self { + env_vars: HashMap::from([ + ("POSTGRES_DB".to_owned(), "postgres".to_owned()), + ("POSTGRES_HOST_AUTH_METHOD".to_owned(), "trust".to_owned()), + ]), + } + } +} + +impl Image for Postgres { + type Args = (); + + fn name(&self) -> String { + Self::NAME.to_owned() + } + + fn tag(&self) -> String { + Self::TAG.to_owned() + } + + fn ready_conditions(&self) -> Vec { + Vec::from([WaitFor::message_on_stderr( + "database system is ready to accept connections", + )]) + } + + fn env_vars(&self) -> Box + '_> { + Box::new(self.env_vars.iter()) + } +} diff --git a/integration_tests/Cargo.toml b/integration_tests/Cargo.toml index c1145179a..3031edeea 100644 --- a/integration_tests/Cargo.toml +++ b/integration_tests/Cargo.toml @@ -32,7 +32,7 @@ rand = "0.8" reqwest = { version = "0.11", default-features = false, features = ["rustls-tls"] } serde.workspace = true serde_json = "1.0.107" -testcontainers = "0.14.0" +testcontainers.workspace = true tokio.workspace = true url = { version = "2.4.1", features = ["serde"] } diff --git a/integration_tests/src/daphne.rs b/integration_tests/src/daphne.rs index 6a4d894b7..5a804a020 100644 --- a/integration_tests/src/daphne.rs +++ b/integration_tests/src/daphne.rs @@ -10,7 +10,7 @@ use std::{ process::{Command, Stdio}, thread::panicking, }; -use testcontainers::{clients::Cli, images::generic::GenericImage, Container, RunnableImage}; +use testcontainers::{clients::Cli, Container, GenericImage, RunnableImage}; const DAPHNE_HELPER_IMAGE_NAME_AND_TAG: &str = "cloudflare/daphne-worker-helper:sha-f6b3ef1"; diff --git a/interop_binaries/Cargo.toml b/interop_binaries/Cargo.toml index 3cdf81dc6..0b6c2ecef 100644 --- a/interop_binaries/Cargo.toml +++ b/interop_binaries/Cargo.toml @@ -47,7 +47,7 @@ ring = "0.17.0" serde.workspace = true serde_json = "1.0.107" sqlx = { version = "0.7.2", features = ["runtime-tokio-rustls", "migrate", "postgres"] } -testcontainers = { version = "0.14" } +testcontainers.workspace = true tokio.workspace = true tracing = "0.1.37" tracing-log = "0.1.3"