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