Skip to content

Commit

Permalink
feat: add test of runtime used
Browse files Browse the repository at this point in the history
  • Loading branch information
DDtKey committed Sep 26, 2024
1 parent 3838e32 commit c1a6b7f
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.PHONY: test
test:
cargo test
cargo test -- --nocapture
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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`.

47 changes: 46 additions & 1 deletion tests/cloud_first_test.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error + 'static>> {
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
Expand All @@ -30,3 +64,14 @@ async fn create_postgres_client() -> Result<(), Box<dyn std::error::Error + 'sta
assert_eq!(count, 7, "unexpected number of guides");
Ok(())
}

#[derive(Debug)]
struct TestcontainersDesktopNotFound;

impl std::error::Error for TestcontainersDesktopNotFound {}

impl Display for TestcontainersDesktopNotFound {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, include_str!("./pretty_strings/tc-desktop-not-found.txt"))
}
}
9 changes: 9 additions & 0 deletions tests/pretty_strings/tc-desktop-not-found.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

██████╗ ██╗ ██╗ ███╗ ██╗ ██████╗ ██╗
██╔═══██╗██║ ██║ ████╗ ██║██╔═══██╗ ██╗ ██╔╝
██║ ██║███████║ ██╔██╗ ██║██║ ██║ ╚═╝█████╗██║
██║ ██║██╔══██║ ██║╚██╗██║██║ ██║ ██╗╚════╝██║
╚██████╔╝██║ ██║ ██║ ╚████║╚██████╔╝ ╚═╝ ╚██╗
╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝

It seems you are not running Testcontainers Desktop nor the CI agent. Have you started it?
12 changes: 12 additions & 0 deletions tests/pretty_strings/tcc-congratulations.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
████████╗███████╗███████╗████████╗ ██████╗ ██████╗ ███╗ ██╗████████╗ █████╗ ██╗███╗ ██╗███████╗██████╗ ███████╗
╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝██╔════╝██╔═══██╗████╗ ██║╚══██╔══╝██╔══██╗██║████╗ ██║██╔════╝██╔══██╗██╔════╝
██║ █████╗ ███████╗ ██║ ██║ ██║ ██║██╔██╗ ██║ ██║ ███████║██║██╔██╗ ██║█████╗ ██████╔╝███████╗
██║ ██╔══╝ ╚════██║ ██║ ██║ ██║ ██║██║╚██╗██║ ██║ ██╔══██║██║██║╚██╗██║██╔══╝ ██╔══██╗╚════██║
██║ ███████╗███████║ ██║ ╚██████╗╚██████╔╝██║ ╚████║ ██║ ██║ ██║██║██║ ╚████║███████╗██║ ██║███████║
╚═╝ ╚══════╝╚══════╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝╚══════╝╚═╝ ╚═╝╚══════╝

Congratulations on running your first test! 🎉
Runtime used:
{runtime}

You can now return to the website to complete your onboarding.

0 comments on commit c1a6b7f

Please sign in to comment.