Skip to main content

Crate shiguredo_container

Crate shiguredo_container 

Source
Expand description

shiguredo_container — macOS / Linux 両対応のテスト用コンテナ管理ライブラリ。

macOS では Apple Container、Linux では Docker Engine API を利用してコンテナを管理する。 ユーザーは use shiguredo_container::* で両 OS 向けに同じ API を書ける。

§クイックスタート

alpine コンテナを起動してコマンドを実行し、終了後に削除する例。

use shiguredo_container::{AsyncRunner, ExecCommand, GenericImage, ImageExt};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // コンテナを起動し、`tail` で生存させ続ける
    let container = GenericImage::new("alpine", "latest")
        .with_cmd(["tail", "-f", "/dev/null"])
        .start()
        .await?;

    // コンテナ内でコマンドを実行する
    let mut result = container.exec(ExecCommand::new(["echo", "hello"])).await?;
    assert_eq!(result.exit_code().await?, Some(0));
    let stdout = result.stdout_to_vec().await?;
    assert_eq!(String::from_utf8_lossy(&stdout).trim(), "hello");

    // コンテナを削除する
    container.rm().await?;
    Ok(())
}

§Feature 一覧

  • blocking: 同期 API (Container / SyncRunner) を有効化する。
  • http_wait_plain: WaitFor::http (core::wait::HttpWaitStrategy) を有効化する。plain HTTP のみ対応 (TLS 非対応)。
  • watchdog: テストプロセスのクラッシュ (SIGKILL 含む) 時に孤立コンテナを掃除する。macOS のみ。

§プラットフォーム差

  • macOS: Apple Container の XPC 経由でコンテナを管理する。pause / unpause は未対応。
  • Linux: Docker Engine API 経由でコンテナを管理する。with_ssh は未対応。

対応範囲の詳細は docs/TESTCONTAINERS.md と README の WARNING を参照すること。

§環境変数

  • TESTCONTAINERS_COMMAND: keep を設定すると Drop 時にコンテナを削除しない (デバッグ用)。

Re-exports§

pub use crate::core::error::Error;
pub use crate::core::ContainerAsync;
pub use crate::core::ContainerRequest;
pub use crate::core::ExecCommand;
pub use crate::core::Healthcheck;
pub use crate::core::Image;
pub use crate::core::ImageExt;
pub use crate::core::WaitFor;
pub use crate::images::GenericImage;
pub use crate::runners::AsyncRunner;
pub use crate::core::Container;
pub use crate::runners::SyncRunner;

Modules§

core
コンテナ操作の中核モジュール。
images
GenericImage — 設定可能な汎用イメージ。 元の 0.27 の images::generic と同一シグネチャ。
runners
runners — コンテナ起動のトレイト。元の 0.27 の runners と同一シグネチャ。