graph.scm 24 KB

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