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