Commit | Line | Data |
---|---|---|
88856916 | 1 | ;;; GNU Guix --- Functional package management for GNU |
312df1d4 | 2 | ;;; Copyright © 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org> |
88856916 LC |
3 | ;;; |
4 | ;;; This file is part of GNU Guix. | |
5 | ;;; | |
6 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
7 | ;;; under the terms of the GNU General Public License as published by | |
8 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
9 | ;;; your option) any later version. | |
10 | ;;; | |
11 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
12 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ;;; GNU General Public License for more details. | |
15 | ;;; | |
16 | ;;; You should have received a copy of the GNU General Public License | |
17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
18 | ||
19 | (define-module (test-graph) | |
20 | #:use-module (guix tests) | |
8fb58371 | 21 | #:use-module (guix graph) |
88856916 LC |
22 | #:use-module (guix scripts graph) |
23 | #:use-module (guix packages) | |
24 | #:use-module (guix derivations) | |
25 | #:use-module (guix store) | |
26 | #:use-module (guix monads) | |
ef8de985 | 27 | #:use-module (guix grafts) |
88856916 | 28 | #:use-module (guix build-system gnu) |
923d846c | 29 | #:use-module (guix build-system trivial) |
88856916 | 30 | #:use-module (guix gexp) |
923d846c | 31 | #:use-module (guix utils) |
88856916 | 32 | #:use-module (gnu packages) |
923d846c | 33 | #:use-module (gnu packages base) |
72402021 | 34 | #:use-module (gnu packages bootstrap) |
923d846c | 35 | #:use-module (gnu packages guile) |
b96a0640 | 36 | #:use-module (gnu packages libunistring) |
88856916 LC |
37 | #:use-module (gnu packages bootstrap) |
38 | #:use-module (ice-9 match) | |
39 | #:use-module (srfi srfi-1) | |
40 | #:use-module (srfi srfi-11) | |
41 | #:use-module (srfi srfi-26) | |
42 | #:use-module (srfi srfi-64)) | |
43 | ||
44 | (define %store | |
45 | (open-connection-for-tests)) | |
46 | ||
ef8de985 LC |
47 | ;; Globally disable grafts because they can trigger early builds. |
48 | (%graft? #f) | |
49 | ||
88856916 LC |
50 | (define (make-recording-backend) |
51 | "Return a <graph-backend> and a thunk that returns the recorded nodes and | |
52 | edges." | |
53 | (let ((nodes '()) | |
54 | (edges '())) | |
55 | (define (record-node id label port) | |
56 | (set! nodes (cons (list id label) nodes))) | |
57 | (define (record-edge source target port) | |
58 | (set! edges (cons (list source target) edges))) | |
59 | (define (return) | |
60 | (values (reverse nodes) (reverse edges))) | |
61 | ||
51377437 RW |
62 | (values (graph-backend "test" "This is the test backend." |
63 | (const #t) (const #t) | |
88856916 LC |
64 | record-node record-edge) |
65 | return))) | |
66 | ||
67 | (define (package->tuple package) | |
68 | "Return a tuple representing PACKAGE as produced by %PACKAGE-NODE-TYPE." | |
69 | (list (object-address package) | |
70 | (package-full-name package))) | |
71 | ||
72 | (define (edge->tuple source target) | |
73 | "Likewise for an edge from SOURCE to TARGET." | |
74 | (list (object-address source) | |
75 | (object-address target))) | |
76 | ||
77 | \f | |
78 | (test-begin "graph") | |
79 | ||
80 | (test-assert "package DAG" | |
81 | (let-values (((backend nodes+edges) (make-recording-backend))) | |
82 | (let* ((p1 (dummy-package "p1")) | |
83 | (p2 (dummy-package "p2" (inputs `(("p1" ,p1))))) | |
84 | (p3 (dummy-package "p3" (inputs `(("p2" ,p2) ("p1", p1)))))) | |
85 | (run-with-store %store | |
86 | (export-graph (list p3) 'port | |
87 | #:node-type %package-node-type | |
88 | #:backend backend)) | |
89 | ;; We should see nothing more than these 3 packages. | |
90 | (let-values (((nodes edges) (nodes+edges))) | |
91 | (and (equal? nodes (map package->tuple (list p3 p2 p1))) | |
92 | (equal? edges | |
93 | (map edge->tuple | |
94 | (list p3 p3 p2) | |
95 | (list p2 p1 p1)))))))) | |
96 | ||
b96a0640 LC |
97 | (test-assert "reverse package DAG" |
98 | (let-values (((backend nodes+edges) (make-recording-backend))) | |
99 | (run-with-store %store | |
100 | (export-graph (list libunistring) 'port | |
101 | #:node-type %reverse-package-node-type | |
102 | #:backend backend)) | |
103 | ;; We should see nothing more than these 3 packages. | |
104 | (let-values (((nodes edges) (nodes+edges))) | |
105 | (and (member (package->tuple guile-2.0) nodes) | |
106 | (->bool (member (edge->tuple libunistring guile-2.0) edges)))))) | |
107 | ||
88856916 LC |
108 | (test-assert "bag-emerged DAG" |
109 | (let-values (((backend nodes+edges) (make-recording-backend))) | |
f88282af LC |
110 | (let* ((o (dummy-origin (method (lambda _ |
111 | (text-file "foo" "bar"))))) | |
112 | (p (dummy-package "p" (source o))) | |
113 | (implicit (map (match-lambda | |
cafc97e2 LC |
114 | ((label package) package) |
115 | ((label package output) package)) | |
f88282af | 116 | (standard-packages)))) |
88856916 LC |
117 | (run-with-store %store |
118 | (export-graph (list p) 'port | |
119 | #:node-type %bag-emerged-node-type | |
120 | #:backend backend)) | |
121 | ;; We should see exactly P and IMPLICIT, with one edge from P to each | |
cafc97e2 LC |
122 | ;; element of IMPLICIT. O must not appear among NODES. Note: IMPLICIT |
123 | ;; contains "glibc" twice, once for "out" and a second time for | |
124 | ;; "static", hence the 'delete-duplicates' call below. | |
88856916 LC |
125 | (let-values (((nodes edges) (nodes+edges))) |
126 | (and (equal? (match nodes | |
127 | (((labels names) ...) | |
128 | names)) | |
cafc97e2 LC |
129 | (map package-full-name |
130 | (cons p (delete-duplicates implicit)))) | |
88856916 LC |
131 | (equal? (match edges |
132 | (((sources destinations) ...) | |
133 | (zip (map store-path-package-name sources) | |
134 | (map store-path-package-name destinations)))) | |
135 | (map (lambda (destination) | |
136 | (list "p-0.drv" | |
137 | (string-append | |
ede121de | 138 | (package-full-name destination "-") |
88856916 LC |
139 | ".drv"))) |
140 | implicit))))))) | |
141 | ||
923d846c | 142 | (test-assert "bag DAG" ;a big town in Iraq |
88856916 LC |
143 | (let-values (((backend nodes+edges) (make-recording-backend))) |
144 | (let ((p (dummy-package "p"))) | |
145 | (run-with-store %store | |
146 | (export-graph (list p) 'port | |
147 | #:node-type %bag-node-type | |
148 | #:backend backend)) | |
149 | ;; We should see P, its implicit inputs as well as the whole DAG, which | |
150 | ;; should include bootstrap binaries. | |
151 | (let-values (((nodes edges) (nodes+edges))) | |
152 | (every (lambda (name) | |
153 | (find (cut string=? name <>) | |
154 | (match nodes | |
155 | (((labels names) ...) | |
156 | names)))) | |
a2b2070b | 157 | (match (%bootstrap-inputs) |
88856916 | 158 | (((labels packages) ...) |
a2b2070b | 159 | (map package-full-name (filter package? packages))))))))) |
88856916 | 160 | |
38b92daa LC |
161 | (test-assert "bag DAG, including origins" |
162 | (let-values (((backend nodes+edges) (make-recording-backend))) | |
163 | (let* ((m (lambda* (uri hash-type hash name #:key system) | |
164 | (text-file "foo-1.2.3.tar.gz" "This is a fake!"))) | |
165 | (o (origin (method m) (uri "the-uri") (sha256 #vu8(0 1 2)))) | |
166 | (p (dummy-package "p" (source o)))) | |
167 | (run-with-store %store | |
168 | (export-graph (list p) 'port | |
169 | #:node-type %bag-with-origins-node-type | |
170 | #:backend backend)) | |
171 | ;; We should see O among the nodes, with an edge coming from P. | |
172 | (let-values (((nodes edges) (nodes+edges))) | |
173 | (run-with-store %store | |
174 | (mlet %store-monad ((o* (lower-object o)) | |
51385362 LC |
175 | (p* (lower-object p)) |
176 | (g (lower-object (default-guile)))) | |
38b92daa LC |
177 | (return |
178 | (and (find (match-lambda | |
179 | ((file "the-uri") #t) | |
180 | (_ #f)) | |
181 | nodes) | |
182 | (find (match-lambda | |
183 | ((source target) | |
184 | (and (string=? source (derivation-file-name p*)) | |
185 | (string=? target o*)))) | |
51385362 LC |
186 | edges) |
187 | ||
188 | ;; There must also be an edge from O to G. | |
189 | (find (match-lambda | |
190 | ((source target) | |
191 | (and (string=? source o*) | |
192 | (string=? target (derivation-file-name g))))) | |
38b92daa LC |
193 | edges))))))))) |
194 | ||
2b81eac0 LC |
195 | (test-assert "reverse bag DAG" |
196 | (let-values (((dune bap ocaml-base) | |
197 | (values (specification->package "dune") | |
198 | (specification->package "bap") | |
312df1d4 | 199 | (specification->package "ocaml4.07-base"))) |
2b81eac0 LC |
200 | ((backend nodes+edges) (make-recording-backend))) |
201 | (run-with-store %store | |
202 | (export-graph (list dune) 'port | |
203 | #:node-type %reverse-bag-node-type | |
204 | #:backend backend)) | |
205 | ||
206 | (run-with-store %store | |
207 | (mlet %store-monad ((dune-drv (package->derivation dune)) | |
208 | (bap-drv (package->derivation bap)) | |
209 | (ocaml-base-drv (package->derivation ocaml-base))) | |
210 | ;; OCAML-BASE uses 'dune-build-system' so DUNE is a direct dependency. | |
211 | ;; BAP is much higher in the stack but it should be there. | |
212 | (let-values (((nodes edges) (nodes+edges))) | |
213 | (return | |
214 | (and (member `(,(derivation-file-name bap-drv) | |
215 | ,(package-full-name bap)) | |
216 | nodes) | |
217 | (->bool (member (map derivation-file-name | |
218 | (list dune-drv ocaml-base-drv)) | |
219 | edges))))))))) | |
220 | ||
88856916 LC |
221 | (test-assert "derivation DAG" |
222 | (let-values (((backend nodes+edges) (make-recording-backend))) | |
223 | (run-with-store %store | |
224 | (mlet* %store-monad ((txt (text-file "text-file" "Hello!")) | |
225 | (guile (package->derivation %bootstrap-guile)) | |
226 | (drv (gexp->derivation "output" | |
227 | #~(symlink #$txt #$output) | |
228 | #:guile-for-build | |
229 | guile))) | |
230 | ;; We should get at least these 3 nodes and corresponding edges. | |
231 | (mbegin %store-monad | |
232 | (export-graph (list drv) 'port | |
233 | #:node-type %derivation-node-type | |
234 | #:backend backend) | |
235 | (let-values (((nodes edges) (nodes+edges))) | |
236 | ;; XXX: For some reason we need to throw in some 'basename'. | |
237 | (return (and (match nodes | |
238 | (((ids labels) ...) | |
239 | (let ((ids (map basename ids))) | |
240 | (every (lambda (item) | |
241 | (member (basename item) ids)) | |
242 | (list txt | |
243 | (derivation-file-name drv) | |
244 | (derivation-file-name guile)))))) | |
245 | (every (cut member <> | |
246 | (map (lambda (edge) | |
247 | (map basename edge)) | |
248 | edges)) | |
249 | (list (map (compose basename derivation-file-name) | |
250 | (list drv guile)) | |
251 | (list (basename (derivation-file-name drv)) | |
252 | (basename txt)))))))))))) | |
253 | ||
254 | (test-assert "reference DAG" | |
255 | (let-values (((backend nodes+edges) (make-recording-backend))) | |
256 | (run-with-store %store | |
257 | (mlet* %store-monad ((txt (text-file "text-file" "Hello!")) | |
258 | (guile (package->derivation %bootstrap-guile)) | |
259 | (drv (gexp->derivation "output" | |
260 | #~(symlink #$txt #$output) | |
261 | #:guile-for-build | |
262 | guile)) | |
263 | (out -> (derivation->output-path drv))) | |
264 | ;; We should see only OUT and TXT, with an edge from the former to the | |
265 | ;; latter. | |
266 | (mbegin %store-monad | |
267 | (built-derivations (list drv)) | |
268 | (export-graph (list (derivation->output-path drv)) 'port | |
269 | #:node-type %reference-node-type | |
270 | #:backend backend) | |
271 | (let-values (((nodes edges) (nodes+edges))) | |
272 | (return | |
273 | (and (equal? (match nodes | |
274 | (((ids labels) ...) | |
275 | ids)) | |
276 | (list out txt)) | |
277 | (equal? edges `((,out ,txt))))))))))) | |
278 | ||
7f8fec0f LC |
279 | (test-assert "referrer DAG" |
280 | (let-values (((backend nodes+edges) (make-recording-backend))) | |
281 | (run-with-store %store | |
282 | (mlet* %store-monad ((txt (text-file "referrer-node" (random-text))) | |
283 | (drv (gexp->derivation "referrer" | |
284 | #~(symlink #$txt #$output))) | |
285 | (out -> (derivation->output-path drv))) | |
286 | ;; We should see only TXT and OUT, with an edge from the former to the | |
287 | ;; latter. | |
288 | (mbegin %store-monad | |
289 | (built-derivations (list drv)) | |
290 | (export-graph (list txt) 'port | |
291 | #:node-type %referrer-node-type | |
292 | #:backend backend) | |
293 | (let-values (((nodes edges) (nodes+edges))) | |
294 | (return | |
295 | (and (equal? (match nodes | |
296 | (((ids labels) ...) | |
297 | ids)) | |
298 | (list txt out)) | |
299 | (equal? edges `((,txt ,out))))))))))) | |
300 | ||
b06a70e0 LC |
301 | (test-assert "module graph" |
302 | (let-values (((backend nodes+edges) (make-recording-backend))) | |
303 | (run-with-store %store | |
304 | (export-graph '((gnu packages guile)) 'port | |
305 | #:node-type %module-node-type | |
306 | #:backend backend)) | |
307 | ||
308 | (let-values (((nodes edges) (nodes+edges))) | |
309 | (and (member '(gnu packages guile) | |
310 | (match nodes | |
311 | (((ids labels) ...) ids))) | |
312 | (->bool (and (member (list '(gnu packages guile) | |
313 | '(gnu packages libunistring)) | |
314 | edges) | |
315 | (member (list '(gnu packages guile) | |
316 | '(gnu packages bdw-gc)) | |
317 | edges))))))) | |
318 | ||
923d846c LC |
319 | (test-assert "node-edges" |
320 | (run-with-store %store | |
321 | (let ((packages (fold-packages cons '()))) | |
322 | (mlet %store-monad ((edges (node-edges %package-node-type packages))) | |
a99b0ad7 | 323 | (return (and (null? (edges hello)) |
923d846c LC |
324 | (lset= eq? |
325 | (edges guile-2.0) | |
326 | (match (package-direct-inputs guile-2.0) | |
327 | (((labels packages _ ...) ...) | |
328 | packages))))))))) | |
329 | ||
330 | (test-assert "node-transitive-edges + node-back-edges" | |
331 | (run-with-store %store | |
332 | (let ((packages (fold-packages cons '())) | |
333 | (bootstrap? (lambda (package) | |
334 | (string-contains | |
335 | (location-file (package-location package)) | |
336 | "bootstrap.scm"))) | |
337 | (trivial? (lambda (package) | |
338 | (eq? (package-build-system package) | |
339 | trivial-build-system)))) | |
340 | (mlet %store-monad ((edges (node-back-edges %bag-node-type packages))) | |
341 | (let* ((glibc (canonical-package glibc)) | |
342 | (dependents (node-transitive-edges (list glibc) edges)) | |
343 | (diff (lset-difference eq? packages dependents))) | |
344 | ;; All the packages depend on libc, except bootstrap packages and | |
345 | ;; some that use TRIVIAL-BUILD-SYSTEM. | |
346 | (return (null? (remove (lambda (package) | |
347 | (or (trivial? package) | |
348 | (bootstrap? package))) | |
349 | diff)))))))) | |
350 | ||
88d5858f LC |
351 | (test-assert "node-transitive-edges, no duplicates" |
352 | (run-with-store %store | |
353 | (let* ((p0 (dummy-package "p0")) | |
354 | (p1a (dummy-package "p1a" (inputs `(("p0" ,p0))))) | |
355 | (p1b (dummy-package "p1b" (inputs `(("p0" ,p0))))) | |
356 | (p2 (dummy-package "p2" (inputs `(("p1a" ,p1a) ("p1b" ,p1b)))))) | |
357 | (mlet %store-monad ((edges (node-edges %package-node-type | |
358 | (list p2 p1a p1b p0)))) | |
359 | (return (lset= eq? (node-transitive-edges (list p2) edges) | |
360 | (list p1a p1b p0))))))) | |
361 | ||
72402021 LC |
362 | (test-assert "node-transitive-edges, references" |
363 | (run-with-store %store | |
364 | (mlet* %store-monad ((d0 (package->derivation %bootstrap-guile)) | |
365 | (d1 (gexp->derivation "d1" | |
366 | #~(begin | |
367 | (mkdir #$output) | |
368 | (symlink #$%bootstrap-guile | |
369 | (string-append | |
370 | #$output "/l"))))) | |
371 | (d2 (gexp->derivation "d2" | |
372 | #~(begin | |
373 | (mkdir #$output) | |
374 | (symlink #$d1 | |
375 | (string-append | |
376 | #$output "/l"))))) | |
377 | (_ (built-derivations (list d2))) | |
378 | (->node -> (node-type-convert %reference-node-type)) | |
379 | (o2 (->node (derivation->output-path d2))) | |
380 | (o1 (->node (derivation->output-path d1))) | |
381 | (o0 (->node (derivation->output-path d0))) | |
382 | (edges (node-edges %reference-node-type | |
383 | (append o0 o1 o2))) | |
384 | (reqs ((store-lift requisites) o2))) | |
385 | (return (lset= string=? | |
386 | (append o2 (node-transitive-edges o2 edges)) reqs))))) | |
387 | ||
e144e342 LC |
388 | (test-equal "node-reachable-count" |
389 | '(3 3) | |
390 | (run-with-store %store | |
391 | (let* ((p0 (dummy-package "p0")) | |
392 | (p1a (dummy-package "p1a" (inputs `(("p0" ,p0))))) | |
393 | (p1b (dummy-package "p1b" (inputs `(("p0" ,p0))))) | |
394 | (p2 (dummy-package "p2" (inputs `(("p1a" ,p1a) ("p1b" ,p1b)))))) | |
395 | (mlet* %store-monad ((all -> (list p2 p1a p1b p0)) | |
396 | (edges (node-edges %package-node-type all)) | |
397 | (back (node-back-edges %package-node-type all))) | |
398 | (return (list (node-reachable-count (list p2) edges) | |
399 | (node-reachable-count (list p0) back))))))) | |
400 | ||
36c21924 LC |
401 | (test-equal "shortest-path, packages + derivations" |
402 | '(("p5" "p4" "p1" "p0") | |
403 | ("p3" "p2" "p1" "p0") | |
404 | #f | |
405 | ("p5-0.drv" "p4-0.drv" "p1-0.drv" "p0-0.drv")) | |
406 | (run-with-store %store | |
407 | (let* ((p0 (dummy-package "p0")) | |
408 | (p1 (dummy-package "p1" (inputs `(("p0" ,p0))))) | |
409 | (p2 (dummy-package "p2" (inputs `(("p1" ,p1))))) | |
410 | (p3 (dummy-package "p3" (inputs `(("p2" ,p2))))) | |
411 | (p4 (dummy-package "p4" (inputs `(("p1" ,p1))))) | |
412 | (p5 (dummy-package "p5" (inputs `(("p4" ,p4) ("p3" ,p3)))))) | |
413 | (mlet* %store-monad ((path1 (shortest-path p5 p0 %package-node-type)) | |
414 | (path2 (shortest-path p3 p0 %package-node-type)) | |
415 | (nope (shortest-path p3 p4 %package-node-type)) | |
416 | (drv5 (package->derivation p5)) | |
417 | (drv0 (package->derivation p0)) | |
418 | (path3 (shortest-path drv5 drv0 | |
419 | %derivation-node-type))) | |
420 | (return (append (map (lambda (path) | |
421 | (and path (map package-name path))) | |
422 | (list path1 path2 nope)) | |
423 | (list (map (node-type-label %derivation-node-type) | |
424 | path3)))))))) | |
425 | ||
426 | (test-equal "shortest-path, reverse packages" | |
427 | '("libffi" "guile" "guile-json") | |
428 | (run-with-store %store | |
429 | (mlet %store-monad ((path (shortest-path (specification->package "libffi") | |
430 | guile-json | |
431 | %reverse-package-node-type))) | |
432 | (return (map package-name path))))) | |
433 | ||
434 | (test-equal "shortest-path, references" | |
435 | `(("d2" "d1" ,(package-full-name %bootstrap-guile "-")) | |
436 | (,(package-full-name %bootstrap-guile "-") "d1" "d2")) | |
437 | (run-with-store %store | |
438 | (mlet* %store-monad ((d0 (package->derivation %bootstrap-guile)) | |
439 | (d1 (gexp->derivation "d1" | |
440 | #~(begin | |
441 | (mkdir #$output) | |
442 | (symlink #$%bootstrap-guile | |
443 | (string-append | |
444 | #$output "/l"))))) | |
445 | (d2 (gexp->derivation "d2" | |
446 | #~(begin | |
447 | (mkdir #$output) | |
448 | (symlink #$d1 | |
449 | (string-append | |
450 | #$output "/l"))))) | |
451 | (_ (built-derivations (list d2))) | |
452 | (->node -> (node-type-convert %reference-node-type)) | |
453 | (o2 (->node (derivation->output-path d2))) | |
454 | (o0 (->node (derivation->output-path d0))) | |
455 | (path (shortest-path (first o2) (first o0) | |
456 | %reference-node-type)) | |
457 | (rpath (shortest-path (first o0) (first o2) | |
458 | %referrer-node-type))) | |
459 | (return (list (map (node-type-label %reference-node-type) path) | |
460 | (map (node-type-label %referrer-node-type) rpath)))))) | |
461 | ||
88856916 | 462 | (test-end "graph") |