Debian Codegraph @

src/lib.rs

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