From c1a6b7fa621416bd88987e84cbd075537b2b6f50 Mon Sep 17 00:00:00 2001 From: Artem Medvedev Date: Thu, 26 Sep 2024 02:40:37 +0200 Subject: [PATCH] feat: add test of runtime used --- Cargo.lock | 4 +- Makefile | 2 +- README.md | 51 +++++++++++++++++++ tests/cloud_first_test.rs | 47 ++++++++++++++++- tests/pretty_strings/tc-desktop-not-found.txt | 9 ++++ tests/pretty_strings/tcc-congratulations.tmpl | 12 +++++ 6 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 README.md create mode 100644 tests/pretty_strings/tc-desktop-not-found.txt create mode 100644 tests/pretty_strings/tcc-congratulations.tmpl diff --git a/Cargo.lock b/Cargo.lock index 47a0731..587c7af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1944,9 +1944,9 @@ dependencies = [ [[package]] name = "testcontainers" -version = "0.23.0" +version = "0.23.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b52850ed7c71aa299608b4d891473e1e2698e47287426b81c4938408f058aac" +checksum = "5f40cc2bd72e17f328faf8ca7687fe337e61bccd8acf9674fa78dd3792b045e1" dependencies = [ "async-trait", "bollard", diff --git a/Makefile b/Makefile index e273910..9f2146b 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,3 @@ .PHONY: test test: - cargo test + cargo test -- --nocapture diff --git a/README.md b/README.md new file mode 100644 index 0000000..6f3e9dc --- /dev/null +++ b/README.md @@ -0,0 +1,51 @@ +# testcontainers-cloud-rs-example + +This is an example repository with a simple test confirming proper connection from Testcontainers Desktop (or the CI agent) to your [Testcontainers Cloud](https://app.testcontainers.cloud) account. + +For details on how to bootstrap Testcontainers in an actual project, please refer to the [Testcontainers Rust Quickstart](https://rust.testcontainers.org/quickstart/testcontainers). + +## Clone the repository and run the first Testcontainers test suite + +```shell +git clone https://github.com/AtomicJar/testcontainers-cloud-rs-example.git +cd testcontainers-cloud-rs-example +make test +``` + +The `Make` command will run the test suite using `cargo test -- --nocapture`. + +Note that it's important to add the `--nocapture` flag, otherwise the output of the tests will be suppressed. + +## Confirm your environment is configured correctly + +The test output should show the Testcontainers logo and which container runtime was used: + +```shell +running 2 tests + ████████╗███████╗███████╗████████╗ ██████╗ ██████╗ ███╗ ██╗████████╗ █████╗ ██╗███╗ ██╗███████╗██████╗ ███████╗ + ╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝██╔════╝██╔═══██╗████╗ ██║╚══██╔══╝██╔══██╗██║████╗ ██║██╔════╝██╔══██╗██╔════╝ + ██║ █████╗ ███████╗ ██║ ██║ ██║ ██║██╔██╗ ██║ ██║ ███████║██║██╔██╗ ██║█████╗ ██████╔╝███████╗ + ██║ ██╔══╝ ╚════██║ ██║ ██║ ██║ ██║██║╚██╗██║ ██║ ██╔══██║██║██║╚██╗██║██╔══╝ ██╔══██╗╚════██║ + ██║ ███████╗███████║ ██║ ╚██████╗╚██████╔╝██║ ╚████║ ██║ ██║ ██║██║██║ ╚████║███████╗██║ ██║███████║ + ╚═╝ ╚══════╝╚══════╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝╚══════╝╚═╝ ╚═╝╚══════╝ + + Congratulations on running your first test! 🎉 + Runtime used: + Testcontainers Cloud via Testcontainers Desktop app + + You can now return to the website to complete your onboarding. + +test testcontainers_cloud_docker_engine ... ok +test create_postgres_client ... ok + +test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 1.04s +``` + +## (optional) Use Testcontainers Desktop to easily debug the database + +[Testcontainers Desktop](https://testcontainers.com/desktop/) helps developers with common tasks such as debugging your Testcontainers-powered dependencies. Let's practice! + +The tests in this project create a PostgreSQL database and populate it with sample data. You can [set a fixed port](https://newsletter.testcontainers.com/announcements/set-fixed-ports-to-easily-debug-development-services) for the `postgres` service, then [freeze containers shutdown](https://newsletter.testcontainers.com/announcements/freeze-containers-to-prevent-their-shutdown-while-you-debug) to easily connect to the database from your IDE after your tests run. + +See if you can inspect the database. Username: `postgres`. Password: `postgres`. + diff --git a/tests/cloud_first_test.rs b/tests/cloud_first_test.rs index e315ff9..645dff7 100644 --- a/tests/cloud_first_test.rs +++ b/tests/cloud_first_test.rs @@ -1,9 +1,43 @@ +use std::fmt::Display; + use sqlx::Connection; use testcontainers_modules::{ postgres::Postgres, - testcontainers::{runners::AsyncRunner, ImageExt}, + testcontainers::{core::client::docker_client_instance, runners::AsyncRunner, ImageExt}, }; +#[tokio::test] +async fn testcontainers_cloud_docker_engine() -> Result<(), Box> { + let client = docker_client_instance().await?; + let info = client.info().await?; + + let contains_cloud = + matches!(info.server_version.as_ref(), Some(v) if v.contains("testcontainerscloud")); + let contains_desktop = + matches!(info.server_version.as_ref(), Some(v) if v.contains("Testcontainers Desktop")); + + if !(contains_cloud || contains_desktop) { + Err(TestcontainersDesktopNotFound)? + } + + let runtime = Some("Testcontainers Cloud") + .filter(|_| contains_cloud) + .or(info.operating_system.as_deref()) + .unwrap_or("unknown"); + let runtime = if contains_desktop { + format!("{runtime} via Testcontainers Desktop app") + } else { + runtime.to_string() + }; + + println!( + include_str!("./pretty_strings/tcc-congratulations.tmpl"), + runtime = runtime + ); + + Ok(()) +} + #[tokio::test] // This test uses `AsyncRunner``, but the same is applicable for `SyncRunner` // Also, it uses `GenericImage` from `testcontainers` for a whole picture, but you can use community modules from [`https://github.com/testcontainers/testcontainers-rs-modules-community`] instead @@ -30,3 +64,14 @@ async fn create_postgres_client() -> Result<(), Box) -> std::fmt::Result { + write!(f, include_str!("./pretty_strings/tc-desktop-not-found.txt")) + } +} diff --git a/tests/pretty_strings/tc-desktop-not-found.txt b/tests/pretty_strings/tc-desktop-not-found.txt new file mode 100644 index 0000000..6db817b --- /dev/null +++ b/tests/pretty_strings/tc-desktop-not-found.txt @@ -0,0 +1,9 @@ + + ██████╗ ██╗ ██╗ ███╗ ██╗ ██████╗ ██╗ + ██╔═══██╗██║ ██║ ████╗ ██║██╔═══██╗ ██╗ ██╔╝ + ██║ ██║███████║ ██╔██╗ ██║██║ ██║ ╚═╝█████╗██║ + ██║ ██║██╔══██║ ██║╚██╗██║██║ ██║ ██╗╚════╝██║ + ╚██████╔╝██║ ██║ ██║ ╚████║╚██████╔╝ ╚═╝ ╚██╗ + ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ + + It seems you are not running Testcontainers Desktop nor the CI agent. Have you started it? diff --git a/tests/pretty_strings/tcc-congratulations.tmpl b/tests/pretty_strings/tcc-congratulations.tmpl new file mode 100644 index 0000000..f4dd813 --- /dev/null +++ b/tests/pretty_strings/tcc-congratulations.tmpl @@ -0,0 +1,12 @@ + ████████╗███████╗███████╗████████╗ ██████╗ ██████╗ ███╗ ██╗████████╗ █████╗ ██╗███╗ ██╗███████╗██████╗ ███████╗ + ╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝██╔════╝██╔═══██╗████╗ ██║╚══██╔══╝██╔══██╗██║████╗ ██║██╔════╝██╔══██╗██╔════╝ + ██║ █████╗ ███████╗ ██║ ██║ ██║ ██║██╔██╗ ██║ ██║ ███████║██║██╔██╗ ██║█████╗ ██████╔╝███████╗ + ██║ ██╔══╝ ╚════██║ ██║ ██║ ██║ ██║██║╚██╗██║ ██║ ██╔══██║██║██║╚██╗██║██╔══╝ ██╔══██╗╚════██║ + ██║ ███████╗███████║ ██║ ╚██████╗╚██████╔╝██║ ╚████║ ██║ ██║ ██║██║██║ ╚████║███████╗██║ ██║███████║ + ╚═╝ ╚══════╝╚══════╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝╚══════╝╚═╝ ╚═╝╚══════╝ + + Congratulations on running your first test! 🎉 + Runtime used: + {runtime} + + You can now return to the website to complete your onboarding.