1use std::env::var;
2
3use rustainers::{
4 ExposedPort,
5 ImageName,
6 RunnableContainer,
7 RunnableContainerBuilder,
8 ToRunnableContainer,
9 WaitStrategy,
10};
11use testresult::TestResult;
12use uuid::{NoContext, Uuid, timestamp::Timestamp};
13
14use crate::Url;
15
16const IMAGE_NAME: &str = "docker.io/nitrokey/nethsm";
18const DEFAULT_PORT: u16 = 8443;
19const DEFAULT_PATH: &str = "/api/v1";
20
21#[derive(Debug)]
23pub struct NetHsmImage {
24 pub image: ImageName,
26
27 pub port: ExposedPort,
29}
30
31impl NetHsmImage {
32 pub async fn url(&self) -> TestResult<Url> {
34 Ok(Url::new(&format!(
35 "https://localhost:{}{}",
36 self.port.host_port().await?,
37 DEFAULT_PATH
38 ))?)
39 }
40}
41
42impl Default for NetHsmImage {
43 fn default() -> Self {
44 let mut image = ImageName::new(IMAGE_NAME);
45 image.set_tag(var("NETHSM_IMAGE_TAG").unwrap_or("testing".into()));
46 Self {
47 image,
48 port: ExposedPort::new(DEFAULT_PORT),
49 }
50 }
51}
52
53impl ToRunnableContainer for NetHsmImage {
54 fn to_runnable(&self, builder: RunnableContainerBuilder) -> RunnableContainer {
55 builder
56 .with_image(self.image.clone())
57 .with_container_name(Some(format!(
58 "nethsm-test-{}",
59 Uuid::new_v7(Timestamp::now(NoContext))
60 )))
61 .with_wait_strategy(WaitStrategy::HttpSuccess {
62 https: true,
63 require_valid_certs: false,
64 path: "/".into(),
65 container_port: 8443.into(),
66 })
67 .with_port_mappings([self.port.clone()])
68 .build()
69 }
70}