1#![feature(async_await)]
2
3use harbourmaster::{Container, Error, Protocol};
4use std::net::TcpListener;
5
6pub struct CouchDbContainer {
7 container: Container,
8 host_port: u16,
9}
10
11impl CouchDbContainer {
12 pub async fn new() -> Result<Self, Error> {
13 let host_port = get_unused_port()?;
14 let container = Container::builder("couchdb")
15 .pull_on_build(true)
16 .name("couchdb")
17 .slug_length(6)
18 .expose(5984, host_port, Protocol::Tcp)
19 .build()
20 .await?;
21
22 Ok(Self {
23 container,
24 host_port,
25 })
26 }
27
28 pub fn port(&self) -> u16 {
29 self.host_port
30 }
31
32 pub async fn delete(self) -> Result<(), Error> {
33 self.container.delete().await?;
34 Ok(())
35 }
36}
37
38fn get_unused_port() -> Result<u16, std::io::Error> {
39 let listener = TcpListener::bind("localhost:0")?;
40 let port = listener.local_addr()?.port();
41 Ok(port)
42}