Skip to content
This repository was archived by the owner on Apr 15, 2024. It is now read-only.

optimize the image size of pod-quote and add unit test for pod-quote #259

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 3 additions & 5 deletions container/pod-quote/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ FROM rust:1.74.0 as pod-quote-builder

RUN apt-get update && apt-get install -y \
build-essential \
curl \
make \
libprotobuf-dev \
protobuf-compiler \
musl-dev \
wget \
libssl-dev \
pkg-config

Expand All @@ -19,7 +15,9 @@ COPY service/pod-quote /pod-quote

RUN cd /pod-quote && make build

FROM rust:1.74.0
FROM rust:1.74.0-slim

RUN apt-get update && apt-get install -y curl

WORKDIR /app
COPY --from=pod-quote-builder /pod-quote/target/release/pod_quote /app/pod_quote
Expand Down
56 changes: 56 additions & 0 deletions service/pod-quote/src/pod_quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,61 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
eprintln!("HTTP server error: {}", err);
}
});
// Keep the main thread running until a termination signal is received
tokio::signal::ctrl_c().await?;
println!("Received Ctrl+C signal, shutting down.");

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use hyper::Client;
use hyper::body::HttpBody as _;
use tokio::sync::oneshot;

// Helper function to start the server in a separate task
async fn start_server(addr: SocketAddr) -> (SocketAddr, oneshot::Receiver<()>) {
let (shutdown_tx, shutdown_rx) = oneshot::channel();
let server_task = tokio::spawn(async move {
let http_server = PerPodQuoteServer::new(addr, {
match tee::get_tee_type() {
tee::TeeType::PLAIN => panic!("Not found any TEE device!"),
t => t,
}
});
if let Err(err) = http_server.start().await {
eprintln!("HTTP server error: {}", err);
}
shutdown_tx.send(()).unwrap();
});
(addr, shutdown_rx)
}

#[tokio::test]
async fn test_quote_endpoint() {
// Define the address to bind the server
let addr = "127.0.0.1:3000".parse().unwrap();

// Start the server in a separate task
let (server_addr, _shutdown_rx) = start_server(addr).await;

// Make a request to the server
let client = Client::new();
let uri = format!("http://{}", server_addr).parse().unwrap();
let mut response = client.get(uri).await.unwrap();

// Read the response body asynchronously
let mut body = response.body_mut();
let mut full_body = Vec::new();
while let Some(chunk) = body.next().await {
let chunk = chunk.unwrap();
full_body.extend_from_slice(&chunk);
}

// Assert that the response is successful and contains some data
assert!(response.status().is_success());
assert!(!full_body.is_empty());
}
}