graph.scm 17 KB

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