Skip to content

Commit

Permalink
working on tests #2
Browse files Browse the repository at this point in the history
  • Loading branch information
cars10 committed Dec 30, 2024
1 parent e48c411 commit 445c386
Show file tree
Hide file tree
Showing 13 changed files with 456 additions and 145 deletions.
127 changes: 127 additions & 0 deletions Cargo.lock

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

9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ name = "autodok"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "autodok"
path = "src/lib.rs"

[[bin]]
name = "autodok"
path = "src/main.rs"

[dependencies]
axum = "0.7.7"
Expand All @@ -25,3 +31,4 @@ base64 = "0.22"
chrono = "0.4.38"
tar = "0.4.43"
bytes = "1.9.0"
reqwest = { version = "0.12", default-features = false, features = ["http2", "json"] }
14 changes: 11 additions & 3 deletions Dockerfile.test
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
FROM rust:slim AS builder

ENV PACKAGE_PATH=/usr/src/autodok
WORKDIR /usr/src

RUN USER=root cargo new autodok
RUN rustup target add x86_64-unknown-linux-musl
COPY Cargo.toml Cargo.lock /usr/src/autodok/
WORKDIR /usr/src/autodok
RUN cargo test
COPY Cargo.toml Cargo.lock $PACKAGE_PATH/
WORKDIR $PACKAGE_PATH
RUN touch $PACKAGE_PATH/src/lib.rs
RUN cargo test
RUN rm $PACKAGE_PATH/src/lib.rs

COPY . $PACKAGE_PATH

RUN touch $PACKAGE_PATH/src/lib.rs
RUN touch $PACKAGE_PATH/src/main.rs
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ prod: docker_build docker_push
### Tests

test: _start_test_docker_server _build_test
docker compose -f compose.test.yml run --rm test cargo test -- --nocapture
docker compose -f compose.test.yml run --rm test bash -c "touch src/main.rs && cargo test -- --nocapture"

test_down:
docker compose -f compose.test.yml down -v -t 1

test_bash: _start_test_docker_server
docker compose -f compose.test.yml run --rm -it test bash
Expand Down
3 changes: 1 addition & 2 deletions compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ services:
dockerfile: Dockerfile.test
environment:
DOCKER_HOST: https://docker:2376
DOCKER_TLS_CERTDIR: /certs
depends_on:
- server
volumes:
- docker_certs_client:/certs/client:ro
- ./src/:/usr/src/autodok/src
- ./tests/:/usr/src/autodok/tests
networks:
- docker_network

Expand Down
31 changes: 19 additions & 12 deletions src/docker.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::AutodokError;
use crate::error::{AutodokError, ImageParseError};
use bollard::{
container::{Config, CreateContainerOptions, NetworkingConfig, StartContainerOptions},
image::CreateImageOptions,
Expand All @@ -11,34 +11,39 @@ use futures_util::stream::StreamExt;
use log::{debug, info};
use std::collections::HashMap;

pub async fn update_image_and_container(
pub async fn pull_image_and_update_container(
docker: &Docker,
container: &str,
image: &str,
image: Option<String>,
wait: Option<bool>,
pull: Option<bool>,
) -> Result<(), AutodokError> {
docker.inspect_container(container, None).await?;
) -> Result<String, AutodokError> {
let inspect = docker.inspect_container(container, None).await?;

// use the provided image, or try to access the containers image from the docker api
let image = image
.or_else(|| inspect.config.and_then(|config| config.image))
.ok_or(AutodokError::Input(ImageParseError::EmptyImage))?;

if let Some(true) = wait {
do_the_deed(docker, container, image, pull).await?;
if wait.unwrap_or(false) {
do_the_deed(docker, container, image.clone(), pull).await?;
} else {
tokio::spawn({
let docker = docker.clone();
let container = container.to_string();
let image = image.to_string();
let image = image.clone();
async move {
do_the_deed(&docker, &container, &image, pull).await.unwrap();
do_the_deed(&docker, &container, image, pull).await.unwrap();
}
});
}
Ok(())
Ok(image)
}

async fn do_the_deed(
docker: &Docker,
container: &str,
image: &str,
image: String,
pull: Option<bool>,
) -> Result<(), AutodokError> {
if let Some(true) = pull {
Expand Down Expand Up @@ -77,7 +82,9 @@ async fn stop_start_container(
// stop and remove old container
info!(" Restarting container...");
docker.stop_container(&container, None).await?;
docker.remove_container(&container, None).await?;
if docker.inspect_container(&container, None).await.is_ok() {
docker.remove_container(&container, None).await?;
}

// build general options for new container
let create_options = Some(CreateContainerOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Error for ImageParseError {}
impl fmt::Display for ImageParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::EmptyImage => write!(f, "image missing"),
Self::EmptyImage => write!(f, "image not specified"),
Self::EmptyPart(image) => write!(f, "invalid image: {image}"),
}
}
Expand Down
Loading

0 comments on commit 445c386

Please sign in to comment.