/
docker.rs
211 lines (193 loc) · 5.93 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#![allow(dead_code)]
use std::env;
use std::fs;
use std::path::Path;
use assert_cmd::Command;
use tar::{Builder, Header};
pub struct Context {
tar: Builder<Vec<u8>>,
}
pub fn sudoers() -> &'static str {
r###"
root ALL=(ALL:ALL) SETENV: ALL
user1 ALL=(ALL:ALL) NOPASSWD: ALL
"###
}
impl Context {
pub fn new() -> Context {
Context {
tar: Builder::new(Vec::with_capacity(1048576)),
}
}
pub fn add_file_mode(mut self, filename: impl AsRef<Path>,
data: impl AsRef<[u8]>, mode: u32)
-> anyhow::Result<Self>
{
let data = data.as_ref();
let filename = filename.as_ref();
let mut header = Header::new_gnu();
header.set_size(data.len() as u64);
header.set_path(filename)?;
header.set_mode(mode);
header.set_cksum();
self.tar.append(&header, data)?;
Ok(self)
}
pub fn add_file(self, filename: impl AsRef<Path>,
data: impl AsRef<[u8]>)
-> anyhow::Result<Self>
{
self.add_file_mode(filename, data, 0o644)
}
pub fn add_sudoers(self) -> anyhow::Result<Self> {
self.add_file("sudoers", sudoers())
}
pub fn add_bin(self) -> anyhow::Result<Self> {
self.add_file_mode("edgedb",
fs::read(env!("CARGO_BIN_EXE_edgedb"))?,
0o755)
}
pub fn build(mut self) -> anyhow::Result<Vec<u8>> {
self.tar.finish()?;
Ok(self.tar.into_inner()?)
}
}
pub fn build_image(context: Context, tagname: &str) -> anyhow::Result<()> {
Command::new("docker")
.arg("build").arg("-")
.arg("-t").arg(tagname)
.write_stdin(context.build()?)
.assert()
.success();
Ok(())
}
pub fn run_bg(container_name: &str, tagname: &str) -> std::process::Child {
std::process::Command::new("docker")
.arg("run")
.arg("--rm")
.arg("--name").arg(container_name)
.arg(tagname)
.spawn()
.expect("can run docker command")
}
pub fn stop(container_name: &str) {
Command::new("docker")
.arg("stop")
.arg(container_name)
.assert();
}
pub fn run(tagname: &str, script: &str) -> assert_cmd::assert::Assert {
Command::new("docker")
.arg("run")
.arg("--rm")
.arg("--mount=type=tmpfs,destination=/run/user/1000,tmpfs-mode=777")
.arg("-u").arg("1000")
.arg(tagname)
.args(&["sh", "-exc", script])
.assert()
}
pub fn run_docker(tagname: &str, script: &str)
-> assert_cmd::assert::Assert
{
let script = format!(r###"
docker ps -q -f 'name=edgedb_test' | xargs -r docker container kill
docker system prune --all --force
docker volume list -q -f 'name=edgedb_test' | xargs -r docker volume rm
{script}
"###, script=script);
let path = if let Ok(path) = env::var("DOCKER_VOLUME_PATH") {
path.to_string()
} else {
"/var/run/docker.sock".to_string()
};
Command::new("docker")
.arg("run")
.arg("--rm")
.arg("-u").arg("1000")
.arg(format!("--volume={0}:{0}", path))
.arg("--net=host")
.arg(tagname)
.args(&["bash", "-exc", &script])
.assert()
}
pub fn run_systemd(tagname: &str, script: &str)
-> assert_cmd::assert::Assert
{
let script = format!(r###"
export XDG_RUNTIME_DIR=/run/user/1000
/lib/systemd/systemd --user --log-level=debug &
{script}
"###, script=script);
let script = format!(r###"
cg_path=$(cat /proc/self/cgroup | grep -oP '(?<=name=).*' | sed s/://)
mkdir -p /run/user/1000 /sys/fs/cgroup/$cg_path
chown user1 /sys/fs/cgroup/$cg_path /run/user/1000
sudo -H -u user1 bash -exc {script}
"###, script=shell_words::quote(&script));
Command::new("docker")
.arg("run")
.arg("--rm")
.arg("--tmpfs=/run")
.arg("--tmpfs=/run/systemd/system")
.arg("--privileged")
.arg(tagname)
.args(&["sh", "-exc", &script])
.assert()
}
pub fn run_with(tagname: &str, script: &str, link: &str)
-> assert_cmd::assert::Assert
{
Command::new("docker")
.arg("run")
.arg("--rm")
.arg("-u").arg("1000")
.arg(format!("--link={0}:{0}", link))
.arg(tagname)
.args(&["sh", "-exc", script])
.assert()
}
pub fn sudo_test(dockerfile: &str, tagname: &str, nightly: bool)
-> Result<(), anyhow::Error>
{
let context = Context::new()
.add_file("Dockerfile", dockerfile)?
.add_sudoers()?
.add_bin()?;
build_image(context, tagname)?;
run(tagname, &format!(
r###"
RUST_LOG=info edgedb server install {arg}
echo --- DONE ---
/usr/bin/edgedb-server-* --version
"###, arg=if nightly { "--nightly" } else {""})
).success()
.stdout(predicates::str::contains("--- DONE ---"))
.stdout(predicates::function::function(|data: &str| {
let tail = &data[data.find("--- DONE ---").unwrap()..];
assert!(tail.contains("edgedb-server, version"));
true
}));
Ok(())
}
pub fn install_twice_test(dockerfile: &str, tagname: &str, nightly: bool)
-> Result<(), anyhow::Error>
{
let context = Context::new()
.add_file("Dockerfile", dockerfile)?
.add_sudoers()?
.add_bin()?;
build_image(context, tagname)?;
run(tagname, &format!(
r###"
RUST_LOG=info edgedb server install {arg}
echo --- DONE --- 1>&2
RUST_LOG=info edgedb server install {arg}
"###, arg=if nightly { "--nightly" } else {""})
).code(51)
.stderr(predicates::str::contains("--- DONE ---"))
.stderr(predicates::function::function(|data: &str| {
let tail = &data[data.find("--- DONE ---").unwrap()..];
tail.contains("already installed")
}));
Ok(())
}