graph.scm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 (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 "reverse package DAG"
  105. (let-values (((backend nodes+edges) (make-recording-backend)))
  106. (run-with-store %store
  107. (export-graph (list libunistring) 'port
  108. #:node-type %reverse-package-node-type
  109. #:backend backend))
  110. ;; We should see nothing more than these 3 packages.
  111. (let-values (((nodes edges) (nodes+edges)))
  112. (and (member (package->tuple guile-2.0) nodes)
  113. (->bool (member (edge->tuple libunistring guile-2.0) edges))))))
  114. (test-assert "bag-emerged DAG"
  115. (let-values (((backend nodes+edges) (make-recording-backend)))
  116. (let* ((o (dummy-origin (method (lambda _
  117. (text-file "foo" "bar")))))
  118. (p (dummy-package "p" (source o)))
  119. (implicit (map (match-lambda
  120. ((label package) package)
  121. ((label package output) package))
  122. (standard-packages))))
  123. (run-with-store %store
  124. (export-graph (list p) 'port
  125. #:node-type %bag-emerged-node-type
  126. #:backend backend))
  127. ;; We should see exactly P and IMPLICIT, with one edge from P to each
  128. ;; element of IMPLICIT. O must not appear among NODES. Note: IMPLICIT
  129. ;; contains "glibc" twice, once for "out" and a second time for
  130. ;; "static", hence the 'delete-duplicates' call below.
  131. (let-values (((nodes edges) (nodes+edges)))
  132. (and (equal? (match nodes
  133. (((labels names) ...)
  134. names))
  135. (map package-full-name
  136. (cons p (delete-duplicates implicit))))
  137. (equal? (match edges
  138. (((sources destinations) ...)
  139. (zip (map store-path-package-name sources)
  140. (map store-path-package-name destinations))))
  141. (map (lambda (destination)
  142. (list "p-0.drv"
  143. (string-append
  144. (package-full-name destination "-")
  145. ".drv")))
  146. implicit)))))))
  147. (test-assert "bag DAG" ;a big town in Iraq
  148. (let-values (((backend nodes+edges) (make-recording-backend)))
  149. (let ((p (dummy-package "p")))
  150. (run-with-store %store
  151. (export-graph (list p) 'port
  152. #:node-type %bag-node-type
  153. #:backend backend))
  154. ;; We should see P, its implicit inputs as well as the whole DAG, which
  155. ;; should include bootstrap binaries.
  156. (let-values (((nodes edges) (nodes+edges)))
  157. (every (lambda (name)
  158. (find (cut string=? name <>)
  159. (match nodes
  160. (((labels names) ...)
  161. names))))
  162. (match (%bootstrap-inputs)
  163. (((labels packages) ...)
  164. (map package-full-name (filter package? packages)))))))))
  165. (test-assert "bag DAG, including origins"
  166. (let-values (((backend nodes+edges) (make-recording-backend)))
  167. (let* ((m (lambda* (uri hash-type hash name #:key system)
  168. (text-file "foo-1.2.3.tar.gz" "This is a fake!")))
  169. (o (origin
  170. (method m) (uri "the-uri")
  171. (sha256
  172. (base32
  173. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))))
  174. (p (dummy-package "p" (source o))))
  175. (run-with-store %store
  176. (export-graph (list p) 'port
  177. #:node-type %bag-with-origins-node-type
  178. #:backend backend))
  179. ;; We should see O among the nodes, with an edge coming from P.
  180. (let-values (((nodes edges) (nodes+edges)))
  181. (run-with-store %store
  182. (mlet %store-monad ((o* (lower-object o))
  183. (p* (lower-object p))
  184. (g (lower-object (default-guile))))
  185. (return
  186. (and (find (match-lambda
  187. ((file "the-uri") #t)
  188. (_ #f))
  189. nodes)
  190. (find (match-lambda
  191. ((source target)
  192. (and (string=? source (derivation-file-name p*))
  193. (string=? target o*))))
  194. edges)
  195. ;; There must also be an edge from O to G.
  196. (find (match-lambda
  197. ((source target)
  198. (and (string=? source o*)
  199. (string=? target (derivation-file-name g)))))
  200. edges)))))))))
  201. (test-assert "reverse bag DAG"
  202. (let-values (((dune bap ocaml-base)
  203. (values (specification->package "ocaml4.07-dune")
  204. (specification->package "bap")
  205. (specification->package "ocaml4.07-base")))
  206. ((backend nodes+edges) (make-recording-backend)))
  207. (run-with-store %store
  208. (export-graph (list dune) 'port
  209. #:node-type %reverse-bag-node-type
  210. #:backend backend))
  211. (run-with-store %store
  212. (mlet %store-monad ((dune-drv (package->derivation dune))
  213. (bap-drv (package->derivation bap))
  214. (ocaml-base-drv (package->derivation ocaml-base)))
  215. ;; OCAML-BASE uses 'dune-build-system' so DUNE is a direct dependency.
  216. ;; BAP is much higher in the stack but it should be there.
  217. (let-values (((nodes edges) (nodes+edges)))
  218. (return
  219. (and (member `(,(derivation-file-name bap-drv)
  220. ,(package-full-name bap))
  221. nodes)
  222. (->bool (member (map derivation-file-name
  223. (list dune-drv ocaml-base-drv))
  224. edges)))))))))
  225. (test-assert "derivation DAG"
  226. (let-values (((backend nodes+edges) (make-recording-backend)))
  227. (run-with-store %store
  228. (mlet* %store-monad ((txt (text-file "text-file" "Hello!"))
  229. (guile (package->derivation %bootstrap-guile))
  230. (drv (gexp->derivation "output"
  231. #~(symlink #$txt #$output)
  232. #:guile-for-build
  233. guile)))
  234. ;; We should get at least these 3 nodes and corresponding edges.
  235. (mbegin %store-monad
  236. (export-graph (list drv) 'port
  237. #:node-type %derivation-node-type
  238. #:backend backend)
  239. (let-values (((nodes edges) (nodes+edges)))
  240. ;; XXX: For some reason we need to throw in some 'basename'.
  241. (return (and (match nodes
  242. (((ids labels) ...)
  243. (let ((ids (map basename ids)))
  244. (every (lambda (item)
  245. (member (basename item) ids))
  246. (list txt
  247. (derivation-file-name drv)
  248. (derivation-file-name guile))))))
  249. (every (cut member <>
  250. (map (lambda (edge)
  251. (map basename edge))
  252. edges))
  253. (list (map (compose basename derivation-file-name)
  254. (list drv guile))
  255. (list (basename (derivation-file-name drv))
  256. (basename txt))))))))))))
  257. (test-assert "reference DAG"
  258. (let-values (((backend nodes+edges) (make-recording-backend)))
  259. (run-with-store %store
  260. (mlet* %store-monad ((txt (text-file "text-file" "Hello!"))
  261. (guile (package->derivation %bootstrap-guile))
  262. (drv (gexp->derivation "output"
  263. #~(symlink #$txt #$output)
  264. #:guile-for-build
  265. guile))
  266. (out -> (derivation->output-path drv)))
  267. ;; We should see only OUT and TXT, with an edge from the former to the
  268. ;; latter.
  269. (mbegin %store-monad
  270. (built-derivations (list drv))
  271. (export-graph (list (derivation->output-path drv)) 'port
  272. #:node-type %reference-node-type
  273. #:backend backend)
  274. (let-values (((nodes edges) (nodes+edges)))
  275. (return
  276. (and (equal? (match nodes
  277. (((ids labels) ...)
  278. ids))
  279. (list out txt))
  280. (equal? edges `((,out ,txt)))))))))))
  281. (test-assert "referrer DAG"
  282. (let-values (((backend nodes+edges) (make-recording-backend)))
  283. (run-with-store %store
  284. (mlet* %store-monad ((txt (text-file "referrer-node" (random-text)))
  285. (drv (gexp->derivation "referrer"
  286. #~(symlink #$txt #$output)))
  287. (out -> (derivation->output-path drv)))
  288. ;; We should see only TXT and OUT, with an edge from the former to the
  289. ;; latter.
  290. (mbegin %store-monad
  291. (built-derivations (list drv))
  292. (export-graph (list txt) 'port
  293. #:node-type %referrer-node-type
  294. #:backend backend)
  295. (let-values (((nodes edges) (nodes+edges)))
  296. (return
  297. (and (equal? (match nodes
  298. (((ids labels) ...)
  299. ids))
  300. (list txt out))
  301. (equal? edges `((,txt ,out)))))))))))
  302. (test-assert "module graph"
  303. (let-values (((backend nodes+edges) (make-recording-backend)))
  304. (run-with-store %store
  305. (export-graph '((gnu packages guile)) 'port
  306. #:node-type %module-node-type
  307. #:backend backend))
  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. (test-assert "node-edges"
  319. (run-with-store %store
  320. (let ((packages (fold-packages cons '())))
  321. (mlet %store-monad ((edges (node-edges %package-node-type packages)))
  322. (return (and (null? (edges hello))
  323. (lset= eq?
  324. (edges guile-2.0)
  325. (match (package-direct-inputs guile-2.0)
  326. (((labels packages _ ...) ...)
  327. packages)))))))))
  328. (test-assert "node-transitive-edges + node-back-edges"
  329. (run-with-store %store
  330. (let ((packages (fold-packages cons '()))
  331. (bootstrap? (lambda (package)
  332. (string-contains
  333. (location-file (package-location package))
  334. "bootstrap.scm")))
  335. (trivial? (lambda (package)
  336. (eq? (package-build-system package)
  337. trivial-build-system))))
  338. (mlet %store-monad ((edges (node-back-edges %bag-node-type packages)))
  339. (let* ((glibc (canonical-package glibc))
  340. (dependents (node-transitive-edges (list glibc) edges))
  341. (diff (lset-difference eq? packages dependents)))
  342. ;; All the packages depend on libc, except bootstrap packages and
  343. ;; some that use TRIVIAL-BUILD-SYSTEM.
  344. (return (null? (remove (lambda (package)
  345. (or (trivial? package)
  346. (bootstrap? package)))
  347. diff))))))))
  348. (test-assert "node-transitive-edges, no duplicates"
  349. (run-with-store %store
  350. (let* ((p0 (dummy-package "p0"))
  351. (p1a (dummy-package "p1a" (inputs `(("p0" ,p0)))))
  352. (p1b (dummy-package "p1b" (inputs `(("p0" ,p0)))))
  353. (p2 (dummy-package "p2" (inputs `(("p1a" ,p1a) ("p1b" ,p1b))))))
  354. (mlet %store-monad ((edges (node-edges %package-node-type
  355. (list p2 p1a p1b p0))))
  356. (return (lset= eq? (node-transitive-edges (list p2) edges)
  357. (list p1a p1b p0)))))))
  358. (test-assert "node-transitive-edges, references"
  359. (run-with-store %store
  360. (mlet* %store-monad ((d0 (package->derivation %bootstrap-guile))
  361. (d1 (gexp->derivation "d1"
  362. #~(begin
  363. (mkdir #$output)
  364. (symlink #$%bootstrap-guile
  365. (string-append
  366. #$output "/l")))))
  367. (d2 (gexp->derivation "d2"
  368. #~(begin
  369. (mkdir #$output)
  370. (symlink #$d1
  371. (string-append
  372. #$output "/l")))))
  373. (_ (built-derivations (list d2)))
  374. (->node -> (node-type-convert %reference-node-type))
  375. (o2 (->node (derivation->output-path d2)))
  376. (o1 (->node (derivation->output-path d1)))
  377. (o0 (->node (derivation->output-path d0)))
  378. (edges (node-edges %reference-node-type
  379. (append o0 o1 o2)))
  380. (reqs ((store-lift requisites) o2)))
  381. (return (lset= string=?
  382. (append o2 (node-transitive-edges o2 edges)) reqs)))))
  383. (test-equal "node-reachable-count"
  384. '(3 3)
  385. (run-with-store %store
  386. (let* ((p0 (dummy-package "p0"))
  387. (p1a (dummy-package "p1a" (inputs `(("p0" ,p0)))))
  388. (p1b (dummy-package "p1b" (inputs `(("p0" ,p0)))))
  389. (p2 (dummy-package "p2" (inputs `(("p1a" ,p1a) ("p1b" ,p1b))))))
  390. (mlet* %store-monad ((all -> (list p2 p1a p1b p0))
  391. (edges (node-edges %package-node-type all))
  392. (back (node-back-edges %package-node-type all)))
  393. (return (list (node-reachable-count (list p2) edges)
  394. (node-reachable-count (list p0) back)))))))
  395. (test-equal "shortest-path, packages + derivations"
  396. '(("p5" "p4" "p1" "p0")
  397. ("p3" "p2" "p1" "p0")
  398. #f
  399. ("p5-0.drv" "p4-0.drv" "p1-0.drv" "p0-0.drv"))
  400. (run-with-store %store
  401. (let* ((p0 (dummy-package "p0"))
  402. (p1 (dummy-package "p1" (inputs `(("p0" ,p0)))))
  403. (p2 (dummy-package "p2" (inputs `(("p1" ,p1)))))
  404. (p3 (dummy-package "p3" (inputs `(("p2" ,p2)))))
  405. (p4 (dummy-package "p4" (inputs `(("p1" ,p1)))))
  406. (p5 (dummy-package "p5" (inputs `(("p4" ,p4) ("p3" ,p3))))))
  407. (mlet* %store-monad ((path1 (shortest-path p5 p0 %package-node-type))
  408. (path2 (shortest-path p3 p0 %package-node-type))
  409. (nope (shortest-path p3 p4 %package-node-type))
  410. (drv5 (package->derivation p5))
  411. (drv0 (package->derivation p0))
  412. (path3 (shortest-path drv5 drv0
  413. %derivation-node-type)))
  414. (return (append (map (lambda (path)
  415. (and path (map package-name path)))
  416. (list path1 path2 nope))
  417. (list (map (node-type-label %derivation-node-type)
  418. path3))))))))
  419. (test-equal "shortest-path, reverse packages"
  420. '("libffi" "guile" "guile-json")
  421. (run-with-store %store
  422. (mlet %store-monad ((path (shortest-path (specification->package "libffi")
  423. guile-json
  424. %reverse-package-node-type)))
  425. (return (map package-name path)))))
  426. (test-equal "shortest-path, references"
  427. `(("d2" "d1" ,(package-full-name %bootstrap-guile "-"))
  428. (,(package-full-name %bootstrap-guile "-") "d1" "d2"))
  429. (run-with-store %store
  430. (mlet* %store-monad ((d0 (package->derivation %bootstrap-guile))
  431. (d1 (gexp->derivation "d1"
  432. #~(begin
  433. (mkdir #$output)
  434. (symlink #$%bootstrap-guile
  435. (string-append
  436. #$output "/l")))))
  437. (d2 (gexp->derivation "d2"
  438. #~(begin
  439. (mkdir #$output)
  440. (symlink #$d1
  441. (string-append
  442. #$output "/l")))))
  443. (_ (built-derivations (list d2)))
  444. (->node -> (node-type-convert %reference-node-type))
  445. (o2 (->node (derivation->output-path d2)))
  446. (o0 (->node (derivation->output-path d0)))
  447. (path (shortest-path (first o2) (first o0)
  448. %reference-node-type))
  449. (rpath (shortest-path (first o0) (first o2)
  450. %referrer-node-type)))
  451. (return (list (map (node-type-label %reference-node-type) path)
  452. (map (node-type-label %referrer-node-type) rpath))))))
  453. (test-end "graph")