graph: Add '%reverse-package-node-type'.
[jackhill/guix/guix.git] / tests / graph.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
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)
21 #:use-module (guix graph)
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)
27 #:use-module (guix grafts)
28 #:use-module (guix build-system gnu)
29 #:use-module (guix build-system trivial)
30 #:use-module (guix gexp)
31 #:use-module (guix utils)
32 #:use-module (gnu packages)
33 #:use-module (gnu packages base)
34 #:use-module (gnu packages guile)
35 #:use-module (gnu packages libunistring)
36 #:use-module (gnu packages bootstrap)
37 #:use-module (ice-9 match)
38 #:use-module (srfi srfi-1)
39 #:use-module (srfi srfi-11)
40 #:use-module (srfi srfi-26)
41 #:use-module (srfi srfi-64))
42
43 (define %store
44 (open-connection-for-tests))
45
46 ;; Globally disable grafts because they can trigger early builds.
47 (%graft? #f)
48
49 (define (make-recording-backend)
50 "Return a <graph-backend> and a thunk that returns the recorded nodes and
51 edges."
52 (let ((nodes '())
53 (edges '()))
54 (define (record-node id label port)
55 (set! nodes (cons (list id label) nodes)))
56 (define (record-edge source target port)
57 (set! edges (cons (list source target) edges)))
58 (define (return)
59 (values (reverse nodes) (reverse edges)))
60
61 (values (graph-backend "test" "This is the test backend."
62 (const #t) (const #t)
63 record-node record-edge)
64 return)))
65
66 (define (package->tuple package)
67 "Return a tuple representing PACKAGE as produced by %PACKAGE-NODE-TYPE."
68 (list (object-address package)
69 (package-full-name package)))
70
71 (define (edge->tuple source target)
72 "Likewise for an edge from SOURCE to TARGET."
73 (list (object-address source)
74 (object-address target)))
75
76 \f
77 (test-begin "graph")
78
79 (test-assert "package DAG"
80 (let-values (((backend nodes+edges) (make-recording-backend)))
81 (let* ((p1 (dummy-package "p1"))
82 (p2 (dummy-package "p2" (inputs `(("p1" ,p1)))))
83 (p3 (dummy-package "p3" (inputs `(("p2" ,p2) ("p1", p1))))))
84 (run-with-store %store
85 (export-graph (list p3) 'port
86 #:node-type %package-node-type
87 #:backend backend))
88 ;; We should see nothing more than these 3 packages.
89 (let-values (((nodes edges) (nodes+edges)))
90 (and (equal? nodes (map package->tuple (list p3 p2 p1)))
91 (equal? edges
92 (map edge->tuple
93 (list p3 p3 p2)
94 (list p2 p1 p1))))))))
95
96 (test-assert "reverse package DAG"
97 (let-values (((backend nodes+edges) (make-recording-backend)))
98 (run-with-store %store
99 (export-graph (list libunistring) 'port
100 #:node-type %reverse-package-node-type
101 #:backend backend))
102 ;; We should see nothing more than these 3 packages.
103 (let-values (((nodes edges) (nodes+edges)))
104 (and (member (package->tuple guile-2.0) nodes)
105 (->bool (member (edge->tuple libunistring guile-2.0) edges))))))
106
107 (test-assert "bag-emerged DAG"
108 (let-values (((backend nodes+edges) (make-recording-backend)))
109 (let* ((o (dummy-origin (method (lambda _
110 (text-file "foo" "bar")))))
111 (p (dummy-package "p" (source o)))
112 (implicit (map (match-lambda
113 ((label package) package))
114 (standard-packages))))
115 (run-with-store %store
116 (export-graph (list p) 'port
117 #:node-type %bag-emerged-node-type
118 #:backend backend))
119 ;; We should see exactly P and IMPLICIT, with one edge from P to each
120 ;; element of IMPLICIT. O must not appear among NODES.
121 (let-values (((nodes edges) (nodes+edges)))
122 (and (equal? (match nodes
123 (((labels names) ...)
124 names))
125 (map package-full-name (cons p implicit)))
126 (equal? (match edges
127 (((sources destinations) ...)
128 (zip (map store-path-package-name sources)
129 (map store-path-package-name destinations))))
130 (map (lambda (destination)
131 (list "p-0.drv"
132 (string-append
133 (package-full-name destination)
134 ".drv")))
135 implicit)))))))
136
137 (test-assert "bag DAG" ;a big town in Iraq
138 (let-values (((backend nodes+edges) (make-recording-backend)))
139 (let ((p (dummy-package "p")))
140 (run-with-store %store
141 (export-graph (list p) 'port
142 #:node-type %bag-node-type
143 #:backend backend))
144 ;; We should see P, its implicit inputs as well as the whole DAG, which
145 ;; should include bootstrap binaries.
146 (let-values (((nodes edges) (nodes+edges)))
147 (every (lambda (name)
148 (find (cut string=? name <>)
149 (match nodes
150 (((labels names) ...)
151 names))))
152 (match %bootstrap-inputs
153 (((labels packages) ...)
154 (map package-full-name packages))))))))
155
156 (test-assert "bag DAG, including origins"
157 (let-values (((backend nodes+edges) (make-recording-backend)))
158 (let* ((m (lambda* (uri hash-type hash name #:key system)
159 (text-file "foo-1.2.3.tar.gz" "This is a fake!")))
160 (o (origin (method m) (uri "the-uri") (sha256 #vu8(0 1 2))))
161 (p (dummy-package "p" (source o))))
162 (run-with-store %store
163 (export-graph (list p) 'port
164 #:node-type %bag-with-origins-node-type
165 #:backend backend))
166 ;; We should see O among the nodes, with an edge coming from P.
167 (let-values (((nodes edges) (nodes+edges)))
168 (run-with-store %store
169 (mlet %store-monad ((o* (lower-object o))
170 (p* (lower-object p))
171 (g (lower-object (default-guile))))
172 (return
173 (and (find (match-lambda
174 ((file "the-uri") #t)
175 (_ #f))
176 nodes)
177 (find (match-lambda
178 ((source target)
179 (and (string=? source (derivation-file-name p*))
180 (string=? target o*))))
181 edges)
182
183 ;; There must also be an edge from O to G.
184 (find (match-lambda
185 ((source target)
186 (and (string=? source o*)
187 (string=? target (derivation-file-name g)))))
188 edges)))))))))
189
190 (test-assert "derivation DAG"
191 (let-values (((backend nodes+edges) (make-recording-backend)))
192 (run-with-store %store
193 (mlet* %store-monad ((txt (text-file "text-file" "Hello!"))
194 (guile (package->derivation %bootstrap-guile))
195 (drv (gexp->derivation "output"
196 #~(symlink #$txt #$output)
197 #:guile-for-build
198 guile)))
199 ;; We should get at least these 3 nodes and corresponding edges.
200 (mbegin %store-monad
201 (export-graph (list drv) 'port
202 #:node-type %derivation-node-type
203 #:backend backend)
204 (let-values (((nodes edges) (nodes+edges)))
205 ;; XXX: For some reason we need to throw in some 'basename'.
206 (return (and (match nodes
207 (((ids labels) ...)
208 (let ((ids (map basename ids)))
209 (every (lambda (item)
210 (member (basename item) ids))
211 (list txt
212 (derivation-file-name drv)
213 (derivation-file-name guile))))))
214 (every (cut member <>
215 (map (lambda (edge)
216 (map basename edge))
217 edges))
218 (list (map (compose basename derivation-file-name)
219 (list drv guile))
220 (list (basename (derivation-file-name drv))
221 (basename txt))))))))))))
222
223 (test-assert "reference DAG"
224 (let-values (((backend nodes+edges) (make-recording-backend)))
225 (run-with-store %store
226 (mlet* %store-monad ((txt (text-file "text-file" "Hello!"))
227 (guile (package->derivation %bootstrap-guile))
228 (drv (gexp->derivation "output"
229 #~(symlink #$txt #$output)
230 #:guile-for-build
231 guile))
232 (out -> (derivation->output-path drv)))
233 ;; We should see only OUT and TXT, with an edge from the former to the
234 ;; latter.
235 (mbegin %store-monad
236 (built-derivations (list drv))
237 (export-graph (list (derivation->output-path drv)) 'port
238 #:node-type %reference-node-type
239 #:backend backend)
240 (let-values (((nodes edges) (nodes+edges)))
241 (return
242 (and (equal? (match nodes
243 (((ids labels) ...)
244 ids))
245 (list out txt))
246 (equal? edges `((,out ,txt)))))))))))
247
248 (test-assert "referrer DAG"
249 (let-values (((backend nodes+edges) (make-recording-backend)))
250 (run-with-store %store
251 (mlet* %store-monad ((txt (text-file "referrer-node" (random-text)))
252 (drv (gexp->derivation "referrer"
253 #~(symlink #$txt #$output)))
254 (out -> (derivation->output-path drv)))
255 ;; We should see only TXT and OUT, with an edge from the former to the
256 ;; latter.
257 (mbegin %store-monad
258 (built-derivations (list drv))
259 (export-graph (list txt) 'port
260 #:node-type %referrer-node-type
261 #:backend backend)
262 (let-values (((nodes edges) (nodes+edges)))
263 (return
264 (and (equal? (match nodes
265 (((ids labels) ...)
266 ids))
267 (list txt out))
268 (equal? edges `((,txt ,out)))))))))))
269
270 (test-assert "node-edges"
271 (run-with-store %store
272 (let ((packages (fold-packages cons '())))
273 (mlet %store-monad ((edges (node-edges %package-node-type packages)))
274 (return (and (null? (edges sed))
275 (lset= eq?
276 (edges guile-2.0)
277 (match (package-direct-inputs guile-2.0)
278 (((labels packages _ ...) ...)
279 packages)))))))))
280
281 (test-assert "node-transitive-edges + node-back-edges"
282 (run-with-store %store
283 (let ((packages (fold-packages cons '()))
284 (bootstrap? (lambda (package)
285 (string-contains
286 (location-file (package-location package))
287 "bootstrap.scm")))
288 (trivial? (lambda (package)
289 (eq? (package-build-system package)
290 trivial-build-system))))
291 (mlet %store-monad ((edges (node-back-edges %bag-node-type packages)))
292 (let* ((glibc (canonical-package glibc))
293 (dependents (node-transitive-edges (list glibc) edges))
294 (diff (lset-difference eq? packages dependents)))
295 ;; All the packages depend on libc, except bootstrap packages and
296 ;; some that use TRIVIAL-BUILD-SYSTEM.
297 (return (null? (remove (lambda (package)
298 (or (trivial? package)
299 (bootstrap? package)))
300 diff))))))))
301
302 (test-assert "node-transitive-edges, no duplicates"
303 (run-with-store %store
304 (let* ((p0 (dummy-package "p0"))
305 (p1a (dummy-package "p1a" (inputs `(("p0" ,p0)))))
306 (p1b (dummy-package "p1b" (inputs `(("p0" ,p0)))))
307 (p2 (dummy-package "p2" (inputs `(("p1a" ,p1a) ("p1b" ,p1b))))))
308 (mlet %store-monad ((edges (node-edges %package-node-type
309 (list p2 p1a p1b p0))))
310 (return (lset= eq? (node-transitive-edges (list p2) edges)
311 (list p1a p1b p0)))))))
312
313 (test-equal "node-reachable-count"
314 '(3 3)
315 (run-with-store %store
316 (let* ((p0 (dummy-package "p0"))
317 (p1a (dummy-package "p1a" (inputs `(("p0" ,p0)))))
318 (p1b (dummy-package "p1b" (inputs `(("p0" ,p0)))))
319 (p2 (dummy-package "p2" (inputs `(("p1a" ,p1a) ("p1b" ,p1b))))))
320 (mlet* %store-monad ((all -> (list p2 p1a p1b p0))
321 (edges (node-edges %package-node-type all))
322 (back (node-back-edges %package-node-type all)))
323 (return (list (node-reachable-count (list p2) edges)
324 (node-reachable-count (list p0) back)))))))
325
326 (test-end "graph")