graph.scm 24 KB

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