Debian Codegraph @

src/lib.rs

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #![cfg_attr(feature="docinclude", feature(external_doc))]
2 #![cfg_attr(feature="docinclude", doc(include="../README.md"))]
3
4 mod builder;
5 mod instructions;
6
7 pub use crate::{
8 builder::{
9 Dockerfile,
10 DockerfileBuilder,
11 },
12 instructions::{
13 Add,
14 Arg,
15 Cmd,
16 Copy,
17 Directive,
18 Entrypoint,
19 Env,
20 Expose,
21 From,
22 Healthcheck,
23 Label,
24 Onbuild,
25 Run,
26 Shell,
27 Stopsignal,
28 User,
29 Volume,
30 Workdir,
31 Instruction,
32 },
33 };
34
35 //////////////////////////////////////////////////////////////////////////////////////////////////
36 // Unit Tests ////////////////////////////////////////////////////////////////////////////////////
37
38 #[cfg(test)]
39 mod test {
40 use super::*;
41
42 #[test]
43 fn dockerfile_generation_simple() {
44 assert_eq!(Dockerfile::base("rust:1.30-slim")
45 .finish().to_string(),
46 r##"FROM rust:1.30-slim
47 "##)
48 }
49
50 #[test]
51 fn dockerfile_generation_with_directives_and_args() {
52 assert_eq!(Dockerfile::base("rust:1.30-slim")
53 .push_initial_directive(Directive::new("escape=`"))
54 .push_initial_arg(Arg::new("TEST"))
55 .push_initial_arg(Arg::new("OTHER=1"))
56 .push(Copy::new("/static ./static".to_string()))
57 .finish().to_string(),
58 r##"# escape=`
59 ARG TEST
60 ARG OTHER=1
61 FROM rust:1.30-slim
62 COPY /static ./static
63 "##)
64 }
65
66 #[test]
67 fn dockerfile_readme_example() {
68 // Build up a new Dockerfile.
69 let dockerfile = Dockerfile::base("rust:${RUST_VERSION}-slim")
70 .push_initial_arg(Arg::new("RUST_VERSION=1.31"))
71 .push(Copy::new("/static ./static"))
72 .push(Cmd::new("echo 'Hello. Goodbye.'"))
73 .finish();
74
75 // Write it out as a string.
76 let output = dockerfile.to_string();
77 assert_eq!(output,
78 r##"ARG RUST_VERSION=1.31
79 FROM rust:${RUST_VERSION}-slim
80 COPY /static ./static
81 CMD echo 'Hello. Goodbye.'
82 "##)
83 }
84
85 //////////////////////////////////////////////////////////////////////////////////////////////
86 // Exhaustive Tests //////////////////////////////////////////////////////////////////////////
87 //
88 // The tests here are intended to test each of the different instruction types in relative
89 // isolation.
90
91 #[test]
92 fn dockerfile_with_add() {
93 assert_eq!(Dockerfile::base("rust:1.31-slim")
94 .push(Add::new("/file ./file"))
95 .push(Cmd::new("echo 'Hello, world.'"))
96 .finish().to_string(),
97 r##"FROM rust:1.31-slim
98 ADD /file ./file
99 CMD echo 'Hello, world.'
100 "##)
101 }
102
103 #[test]
104 fn dockerfile_with_arg() {
105 assert_eq!(Dockerfile::base("rust:1.31-slim")
106 .push(Arg::new("VAL=testing"))
107 .push(Cmd::new("echo 'Hello, world.'"))
108 .finish().to_string(),
109 r##"FROM rust:1.31-slim
110 ARG VAL=testing
111 CMD echo 'Hello, world.'
112 "##)
113 }
114
115 #[test]
116 fn dockerfile_with_cmd() {
117 assert_eq!(Dockerfile::base("rust:1.31-slim")
118 .push(Cmd::new("echo 'Hello, world.'"))
119 .finish().to_string(),
120 r##"FROM rust:1.31-slim
121 CMD echo 'Hello, world.'
122 "##)
123 }
124
125 #[test]
126 fn dockerfile_with_copy() {
127 assert_eq!(Dockerfile::base("rust:1.31-slim")
128 .push(Copy::new("/static ./static"))
129 .push(Cmd::new("echo 'Hello, world.'"))
130 .finish().to_string(),
131 r##"FROM rust:1.31-slim
132 COPY /static ./static
133 CMD echo 'Hello, world.'
134 "##)
135 }
136
137 #[test]
138 fn dockerfile_with_directive() {
139 assert_eq!(Dockerfile::base("rust:1.31-slim")
140 .push(Directive::new("escape=`"))
141 .push(Cmd::new("echo 'Hello, world.'"))
142 .finish().to_string(),
143 r##"FROM rust:1.31-slim
144 # escape=`
145 CMD echo 'Hello, world.'
146 "##)
147 }
148
149 #[test]
150 fn dockerfile_with_entrypoint() {
151 assert_eq!(Dockerfile::base("rust:1.31-slim")
152 .push(Entrypoint::new("echo"))
153 .push(Cmd::new("'Hello, world.'"))
154 .finish().to_string(),
155 r##"FROM rust:1.31-slim
156 ENTRYPOINT echo
157 CMD 'Hello, world.'
158 "##)
159 }
160
161 #[test]
162 fn dockerfile_with_env() {
163 assert_eq!(Dockerfile::base("rust:1.31-slim")
164 .push(Env::new("VAL=test"))
165 .push(Cmd::new("echo 'Hello, world.'"))
166 .finish().to_string(),
167 r##"FROM rust:1.31-slim
168 ENV VAL=test
169 CMD echo 'Hello, world.'
170 "##)
171 }
172
173 #[test]
174 fn dockerfile_with_expose() {
175 assert_eq!(Dockerfile::base("rust:1.31-slim")
176 .push(Expose::new("80"))
177 .push(Cmd::new("echo 'Hello, world.'"))
178 .finish().to_string(),
179 r##"FROM rust:1.31-slim
180 EXPOSE 80
181 CMD echo 'Hello, world.'
182 "##)
183 }
184
185 #[test]
186 fn dockerfile_with_from() {
187 assert_eq!(Dockerfile::base("rust:1.31-slim")
188 .push(From::new("rust:1.31-slim as other"))
189 .push(Cmd::new("echo 'Hello, world.'"))
190 .finish().to_string(),
191 r##"FROM rust:1.31-slim
192 FROM rust:1.31-slim as other
193 CMD echo 'Hello, world.'
194 "##)
195 }
196
197 #[test]
198 fn dockerfile_with_healthcheck() {
199 assert_eq!(Dockerfile::base("rust:1.31-slim")
200 .push(Healthcheck::new("CMD pgrep 1"))
201 .push(Cmd::new("echo 'Hello, world.'"))
202 .finish().to_string(),
203 r##"FROM rust:1.31-slim
204 HEALTHCHECK CMD pgrep 1
205 CMD echo 'Hello, world.'
206 "##)
207 }
208
209 #[test]
210 fn dockerfile_with_label() {
211 assert_eq!(Dockerfile::base("rust:1.31-slim")
212 .push(Label::new("maintainer='Anthony J Dodd'"))
213 .push(Cmd::new("echo 'Hello, world.'"))
214 .finish().to_string(),
215 r##"FROM rust:1.31-slim
216 LABEL maintainer='Anthony J Dodd'
217 CMD echo 'Hello, world.'
218 "##)
219 }
220
221 #[test]
222 fn dockerfile_with_onbuild() {
223 assert_eq!(Dockerfile::base("rust:1.31-slim")
224 .push(Onbuild::new("RUN echo 'ehlo'"))
225 .push(Cmd::new("echo 'Hello, world.'"))
226 .finish().to_string(),
227 r##"FROM rust:1.31-slim
228 ONBUILD RUN echo 'ehlo'
229 CMD echo 'Hello, world.'
230 "##)
231 }
232
233 #[test]
234 fn dockerfile_with_run() {
235 assert_eq!(Dockerfile::base("rust:1.31-slim")
236 .push(Run::new("apt-get update -yy"))
237 .push(Cmd::new("echo 'Hello, world.'"))
238 .finish().to_string(),
239 r##"FROM rust:1.31-slim
240 RUN apt-get update -yy
241 CMD echo 'Hello, world.'
242 "##)
243 }
244
245 #[test]
246 fn dockerfile_with_shell() {
247 assert_eq!(Dockerfile::base("rust:1.31-slim")
248 .push(Shell::new(r#"["/bin/sh", "-c"]"#))
249 .push(Cmd::new("echo 'Hello, world.'"))
250 .finish().to_string(),
251 r##"FROM rust:1.31-slim
252 SHELL ["/bin/sh", "-c"]
253 CMD echo 'Hello, world.'
254 "##)
255 }
256
257 #[test]
258 fn dockerfile_with_stopsignal() {
259 assert_eq!(Dockerfile::base("rust:1.31-slim")
260 .push(Stopsignal::new("SIGKILL"))
261 .push(Cmd::new("echo 'Hello, world.'"))
262 .finish().to_string(),
263 r##"FROM rust:1.31-slim
264 STOPSIGNAL SIGKILL
265 CMD echo 'Hello, world.'
266 "##)
267 }
268
269 #[test]
270 fn dockerfile_with_user() {
271 assert_eq!(Dockerfile::base("rust:1.31-slim")
272 .push(User::new("root"))
273 .push(Cmd::new("echo 'Hello, world.'"))
274 .finish().to_string(),
275 r##"FROM rust:1.31-slim
276 USER root
277 CMD echo 'Hello, world.'
278 "##)
279 }
280
281 #[test]
282 fn dockerfile_with_volume() {
283 assert_eq!(Dockerfile::base("rust:1.31-slim")
284 .push(Volume::new(r#"["/data"]"#))
285 .push(Cmd::new("echo 'Hello, world.'"))
286 .finish().to_string(),
287 r##"FROM rust:1.31-slim
288 VOLUME ["/data"]
289 CMD echo 'Hello, world.'
290 "##)
291 }
292
293 #[test]
294 fn dockerfile_with_workdir() {
295 assert_eq!(Dockerfile::base("rust:1.31-slim")
296 .push(Workdir::new("/app"))
297 .push(Cmd::new("echo 'Hello, world.'"))
298 .finish().to_string(),
299 r##"FROM rust:1.31-slim
300 WORKDIR /app
301 CMD echo 'Hello, world.'
302 "##)
303 }
304 }