This repository was archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdocker.rs
181 lines (146 loc) · 4.23 KB
/
docker.rs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#[cfg(test)]
use std::io::Command;
#[cfg(test)]
use std::io::timer;
#[cfg(test)]
use std::time::Duration;
use super::common::containers::Containers as Containers;
use super::common::images::Images as Images;
use super::common::version::Version as Version;
use super::common::sys_info::SysInfo as SysInfo;
use super::methods::container as container;
use super::methods::image as image;
use super::methods::info as info;
pub struct Docker {
pub socket_path: String
}
impl Docker {
///
/// GET /containers/json
///
pub fn get_containers(&self) -> Containers {
container::get_containers(self.socket_path.as_slice())
}
///
/// POST /containers/(id)/stop
///
pub fn stop_container(&self, id: &str) {
container::stop_container_impl(self.socket_path.as_slice(), id, None);
}
pub fn stop_container_with_timeout(&self, id: &str, wait_time: uint) {
container::stop_container_impl(self.socket_path.as_slice(), id, Some(wait_time));
}
///
/// POST /containers/(id)/restart
///
pub fn restart_container(&self, id: &str) {
container::restart_container_impl(self.socket_path.as_slice(), id, None);
}
pub fn restart_container_with_timeout(&self, id: &str, wait_time: uint) {
container::restart_container_impl(self.socket_path.as_slice(), id, Some(wait_time));
}
///
/// DELETE /containers/(id)/
///
pub fn remove_container(&self, id: &str) {
container::remove_container_impl(self.socket_path.as_slice(), id, false);
}
pub fn remove_container_with_force(&self, id:&str) {
container::remove_container_impl(self.socket_path.as_slice(), id, true);
}
///
/// GET /images/json
///
pub fn get_images(&self) -> Images {
image::get_images(self.socket_path.as_slice())
}
///
/// GET /info
///
pub fn get_sys_info(&self) -> SysInfo {
info::get_sys_info(self.socket_path.as_slice())
}
///
/// GET /version
///
pub fn get_version(&self) -> Version {
info::get_version(self.socket_path.as_slice())
}
}
///
/// Test(s)
///
#[cfg(test)]
fn make_client() -> Docker {
Docker { socket_path: "/var/run/docker.sock".to_string() }
}
#[cfg(test)]
fn start_busybox_container() -> Option<String> {
match Command::new("docker").arg("run").arg("-t").arg("-d")
.arg("busybox:latest").output() {
Ok(process_output) => {
let output = String::from_utf8(process_output.output).unwrap();
timer::sleep(Duration::milliseconds(1000));
let clean_output = output.as_slice().replace("\r\n", "");
let container_id = clean_output.as_slice().trim();
Some(String::from_str(container_id))
}
Err(_) => None
}
}
#[allow(type_limits)]
#[test]
fn test_get_containers() {
let client = make_client();
let containers = client.get_containers();
let count: uint = containers.len();
assert!(count >= 0);
}
#[test]
fn test_stop_and_remove_container() {
// Start a test container
let container_id = match start_busybox_container() {
Some(id) => id,
None => fail!("Failed to start test container")
};
// Stop test container
let client = Docker { socket_path: "/var/run/docker.sock".to_string() };
client.stop_container(container_id.as_slice());
// Remove test container
client.remove_container(container_id.as_slice());
}
#[test]
fn test_restart_container() {
let container_id = match start_busybox_container() {
Some(id) => id,
None => fail!("Failed to start test container")
};
let client = make_client();
client.restart_container(container_id.as_slice());
timer::sleep(Duration::milliseconds(3000));
client.stop_container(container_id.as_slice());
client.remove_container(container_id.as_slice());
}
#[allow(type_limits)]
#[test]
fn test_get_images() {
let client = make_client();
let images = client.get_images();
let count: uint = images.len();
assert!(count >= 0);
}
#[test]
fn test_get_sys_info() {
let client = make_client();
let sys_info: SysInfo = client.get_sys_info();
assert!(sys_info.Debug == 0);
assert!(sys_info.DriverStatus.len() > 0);
}
#[test]
fn test_get_version() {
let client = make_client();
let version = client.get_version();
assert!(version.Version != "".to_string());
assert!(version.GitCommit != "".to_string());
assert!(version.GoVersion != "".to_string());
}