1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::{env::var, thread::sleep, time::Duration};
use tc_core::{Image, Container, Docker, WaitForMessage};


const DEFAULT_WAIT: u64 = 1000;


#[derive(Debug, Default, Clone)]
pub struct DynamoDbArgs;

impl IntoIterator for DynamoDbArgs {
    type Item = String;
    type IntoIter = ::std::vec::IntoIter<String>;

    fn into_iter(self) -> <Self as IntoIterator>::IntoIter {
        vec![].into_iter()
    }
}

#[derive(Debug)]
pub struct DynamoDb {
    arguments: DynamoDbArgs
}

impl Default for DynamoDb {
    fn default() -> Self {
        DynamoDb {
            arguments: DynamoDbArgs {}
        }
    }
}

impl Image for DynamoDb {
    type Args = DynamoDbArgs;

    fn descriptor(&self) -> String {
        "amazon/dynamodb-local:1.11.119".to_string()
    }

    fn wait_until_ready<D: Docker>(&self, container: &Container<D, Self>) {
        container
            .logs()
            .stdout
            .wait_for_message("Initializing DynamoDB Local with the following configuration")
            .unwrap();

        let additional_sleep_period = var("DYNAMODB_ADDITIONAL_SLEEP_PERIOD")
            .map(|value| value.parse().unwrap_or(DEFAULT_WAIT))
            .unwrap_or(DEFAULT_WAIT);

        let sleep_period = Duration::from_millis(additional_sleep_period);

        trace!(
            "Waiting for an additional {:?} for container {}.",
            sleep_period,
            container.id()
        );

        sleep(sleep_period)
    }

    fn args(&self) -> <Self as Image>::Args {
        self.arguments.clone()
    }

    fn with_args(self, arguments: <Self as Image>::Args) -> Self {
        DynamoDb {
            arguments
        }
    }
}