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

Control Podman via unit test code #50

Merged
merged 1 commit into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
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
15 changes: 2 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,10 @@ jobs:
run: nix develop --command cargo-deny check

- name: Clippy
run: nix develop --command cargo clippy --all-targets

- name: Start MinIO
run: |
podman run \
-d \
-p 9000:9000 \
-e "MINIO_ACCESS_KEY=minioadmin" \
-e "MINIO_SECRET_KEY=minioadmin" \
minio/minio \
server \
/data
run: nix develop --command cargo clippy --all-targets -- -D warnings

- name: Test
run: nix build -L --no-sandbox .#test
run: nix develop --command cargo test

build:
name: Build
Expand Down
20 changes: 20 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ members = [
"common",
"storage",

"testing-utils",

"agent",
"archiver",
"ctl",
Expand Down
17 changes: 1 addition & 16 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,7 @@
};

packages =
{
test = naersk'.buildPackage {
mode = "test";
src = ./.;

nativeBuildInputs = nativeBuildInputs;
buildInputs = buildInputs;

# Ensure detailed test output appears in nix build log
cargoTestOptions = x: x ++ ["1>&2"];

AWS_ACCESS_KEY_ID = "minioadmin";
AWS_SECRET_ACCESS_KEY = "minioadmin";
};
}
// import ./agent {inherit pkgs naersk' version gitRevision nativeBuildInputs buildInputs;}
import ./agent {inherit pkgs naersk' version gitRevision nativeBuildInputs buildInputs;}
// import ./archiver {inherit pkgs naersk' version gitRevision nativeBuildInputs buildInputs;}
// import ./ctl {inherit pkgs naersk' version gitRevision nativeBuildInputs buildInputs;}
// import ./event-processor {inherit pkgs naersk' version gitRevision nativeBuildInputs buildInputs;};
Expand Down
12 changes: 0 additions & 12 deletions run_minio.sh

This file was deleted.

3 changes: 3 additions & 0 deletions storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ toml.workspace = true
tracing = "0.1"

[dev-dependencies]
ctor = "0.2.5"
hex = "0.4.3"
lazy_static = "1.4.0"
rand = "0.8.5"
satori-testing-utils = { path = "../testing-utils" }
tempfile = "3.4.0"
66 changes: 44 additions & 22 deletions storage/src/providers/s3_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,36 @@ mod test {
use super::*;
use rand::Rng;
use s3::BucketConfiguration;
use satori_testing_utils::MinioDriver;
use std::sync::{Arc, Mutex};

lazy_static::lazy_static! {
static ref MINIO: Arc<Mutex<Option<MinioDriver>>> = Arc::new(Mutex::new(None));
}

#[ctor::ctor]
fn init_minio() {
MINIO.lock().unwrap().replace(MinioDriver::default());

std::env::set_var("AWS_ACCESS_KEY_ID", "minioadmin");
std::env::set_var("AWS_SECRET_ACCESS_KEY", "minioadmin");
}

#[ctor::dtor]
fn shutdown_minio() {
MINIO.lock().unwrap().as_ref().unwrap().stop();
}

fn generate_random_bucket_name() -> String {
let id = rand::thread_rng()
.sample_iter(&rand::distributions::Alphanumeric)
.take(8)
.map(char::from)
.collect::<String>()
.to_lowercase();

format!("satori-storage-test-{id}")
}

mod no_encryption {
use super::*;
Expand All @@ -258,19 +288,15 @@ mod test {
( $test:ident ) => {
#[tokio::test]
async fn $test() {
let id = rand::thread_rng()
.sample_iter(&rand::distributions::Alphanumeric)
.take(8)
.map(char::from)
.collect::<String>()
.to_lowercase();
let bucket_name = format!("satori-storage-test-{id}");
let endpoint = MINIO.lock().unwrap().as_ref().unwrap().endpoint();

let bucket = super::generate_random_bucket_name();

Bucket::create_with_path_style(
&bucket_name,
&bucket,
Region::Custom {
region: "".into(),
endpoint: "http://localhost:9000".into(),
endpoint: endpoint.clone(),
},
Credentials::default().unwrap(),
BucketConfiguration::default(),
Expand All @@ -279,9 +305,9 @@ mod test {
.unwrap();

let provider = crate::StorageConfig::S3(S3Config {
bucket: bucket_name,
bucket,
region: "".into(),
endpoint: "http://localhost:9000".into(),
endpoint,
encryption: EncryptionConfig::default(),
})
.create_provider();
Expand All @@ -301,19 +327,15 @@ mod test {
( $test:ident ) => {
#[tokio::test]
async fn $test() {
let id = rand::thread_rng()
.sample_iter(&rand::distributions::Alphanumeric)
.take(8)
.map(char::from)
.collect::<String>()
.to_lowercase();
let bucket_name = format!("satori-storage-test-{id}");
let endpoint = MINIO.lock().unwrap().as_ref().unwrap().endpoint();

let bucket = super::generate_random_bucket_name();

Bucket::create_with_path_style(
&bucket_name,
&bucket,
Region::Custom {
region: "".into(),
endpoint: "http://localhost:9000".into(),
endpoint: endpoint.clone(),
},
Credentials::default().unwrap(),
BucketConfiguration::default(),
Expand All @@ -322,9 +344,9 @@ mod test {
.unwrap();

let provider = crate::StorageConfig::S3(S3Config {
bucket: bucket_name,
bucket,
region: "".into(),
endpoint: "http://localhost:9000".into(),
endpoint,
encryption: toml::from_str(
"
[event]
Expand Down
8 changes: 8 additions & 0 deletions testing-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "satori-testing-utils"
license.workspace = true
version.workspace = true
edition.workspace = true

[dependencies]
rand = "0.8.5"
4 changes: 4 additions & 0 deletions testing-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod minio;
mod podman;

pub use self::{minio::MinioDriver, podman::PodmanDriver};
33 changes: 33 additions & 0 deletions testing-utils/src/minio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::PodmanDriver;

pub struct MinioDriver {
pub podman: PodmanDriver,
pub endpoint: String,
}

impl Default for MinioDriver {
fn default() -> Self {
let port = rand::random::<u16>() % 1000 + 9000;

let podman = PodmanDriver::new(
"docker.io/minio/minio",
&[&format!("{port}:9000")],
&["MINIO_ACCESS_KEY=minioadmin", "MINIO_SECRET_KEY=minioadmin"],
&["server", "/data"],
);

let endpoint = format!("http://localhost:{}", port);

Self { podman, endpoint }
}
}

impl MinioDriver {
pub fn stop(&self) {
self.podman.stop();
}

pub fn endpoint(&self) -> String {
self.endpoint.clone()
}
}
60 changes: 60 additions & 0 deletions testing-utils/src/podman.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::process::Command;

pub struct PodmanDriver {
container_id: String,
}

impl PodmanDriver {
pub fn new(image: &str, ports: &[&str], env_vars: &[&str], args: &[&str]) -> Self {
// Build Podman args
let mut podman_args = vec!["run".to_string(), "--detach".to_string()];

for port in ports.iter() {
podman_args.push("-p".to_string());
podman_args.push(port.to_string());
}

for var in env_vars.iter() {
podman_args.push("-e".to_string());
podman_args.push(var.to_string());
}

podman_args.push(image.to_string());

for arg in args.iter() {
podman_args.push(arg.to_string());
}

// Start the container
let container_start = Command::new("podman").args(podman_args).output().unwrap();
println!("Container start: {:?}", container_start);

if container_start.status.success() && container_start.status.code().unwrap() == 0 {
let container_id = String::from_utf8(container_start.stdout)
.unwrap()
.trim()
.to_string();
println!("Container ID: {container_id}");

Self { container_id }
} else {
panic!("Failed to start container");
}
}

pub fn stop(&self) {
// Stop the container
let container_stop = Command::new("podman")
.args(vec!["stop", &self.container_id])
.output()
.unwrap();
println!("Container stop: {:?}", container_stop);

// Remove the container
let container_remove = Command::new("podman")
.args(vec!["rm", &self.container_id])
.output()
.unwrap();
println!("Container remove: {:?}", container_remove);
}
}