graph.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015, 2016, 2017, 2018 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 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 "derivation DAG"
  179. (let-values (((backend nodes+edges) (make-recording-backend)))
  180. (run-with-store %store
  181. (mlet* %store-monad ((txt (text-file "text-file" "Hello!"))
  182. (guile (package->derivation %bootstrap-guile))
  183. (drv (gexp->derivation "output"
  184. #~(symlink #$txt #$output)
  185. #:guile-for-build
  186. guile)))
  187. ;; We should get at least these 3 nodes and corresponding edges.
  188. (mbegin %store-monad
  189. (export-graph (list drv) 'port
  190. #:node-type %derivation-node-type
  191. #:backend backend)
  192. (let-values (((nodes edges) (nodes+edges)))
  193. ;; XXX: For some reason we need to throw in some 'basename'.
  194. (return (and (match nodes
  195. (((ids labels) ...)
  196. (let ((ids (map basename ids)))
  197. (every (lambda (item)
  198. (member (basename item) ids))
  199. (list txt
  200. (derivation-file-name drv)
  201. (derivation-file-name guile))))))
  202. (every (cut member <>
  203. (map (lambda (edge)
  204. (map basename edge))
  205. edges))
  206. (list (map (compose basename derivation-file-name)
  207. (list drv guile))
  208. (list (basename (derivation-file-name drv))
  209. (basename txt))))))))))))
  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. (test-assert "referrer DAG"
  235. (let-values (((backend nodes+edges) (make-recording-backend)))
  236. (run-with-store %store
  237. (mlet* %store-monad ((txt (text-file "referrer-node" (random-text)))
  238. (drv (gexp->derivation "referrer"
  239. #~(symlink #$txt #$output)))
  240. (out -> (derivation->output-path drv)))
  241. ;; We should see only TXT and OUT, with an edge from the former to the
  242. ;; latter.
  243. (mbegin %store-monad
  244. (built-derivations (list drv))
  245. (export-graph (list txt) 'port
  246. #:node-type %referrer-node-type
  247. #:backend backend)
  248. (let-values (((nodes edges) (nodes+edges)))
  249. (return
  250. (and (equal? (match nodes
  251. (((ids labels) ...)
  252. ids))
  253. (list txt out))
  254. (equal? edges `((,txt ,out)))))))))))
  255. (test-assert "module graph"
  256. (let-values (((backend nodes+edges) (make-recording-backend)))
  257. (run-with-store %store
  258. (export-graph '((gnu packages guile)) 'port
  259. #:node-type %module-node-type
  260. #:backend backend))
  261. (let-values (((nodes edges) (nodes+edges)))
  262. (and (member '(gnu packages guile)
  263. (match nodes
  264. (((ids labels) ...) ids)))
  265. (->bool (and (member (list '(gnu packages guile)
  266. '(gnu packages libunistring))
  267. edges)
  268. (member (list '(gnu packages guile)
  269. '(gnu packages bdw-gc))
  270. edges)))))))
  271. (test-assert "node-edges"
  272. (run-with-store %store
  273. (let ((packages (fold-packages cons '())))
  274. (mlet %store-monad ((edges (node-edges %package-node-type packages)))
  275. (return (and (null? (edges hello))
  276. (lset= eq?
  277. (edges guile-2.0)
  278. (match (package-direct-inputs guile-2.0)
  279. (((labels packages _ ...) ...)
  280. packages)))))))))
  281. (test-assert "node-transitive-edges + node-back-edges"
  282. (run-with-store %store
  283. (let ((packages (fold-packages cons '()))
  284. (bootstrap? (lambda (package)
  285. (string-contains
  286. (location-file (package-location package))
  287. "bootstrap.scm")))
  288. (trivial? (lambda (package)
  289. (eq? (package-build-system package)
  290. trivial-build-system))))
  291. (mlet %store-monad ((edges (node-back-edges %bag-node-type packages)))
  292. (let* ((glibc (canonical-package glibc))
  293. (dependents (node-transitive-edges (list glibc) edges))
  294. (diff (lset-difference eq? packages dependents)))
  295. ;; All the packages depend on libc, except bootstrap packages and
  296. ;; some that use TRIVIAL-BUILD-SYSTEM.
  297. (return (null? (remove (lambda (package)
  298. (or (trivial? package)
  299. (bootstrap? package)))
  300. diff))))))))
  301. (test-assert "node-transitive-edges, no duplicates"
  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 ((edges (node-edges %package-node-type
  308. (list p2 p1a p1b p0))))
  309. (return (lset= eq? (node-transitive-edges (list p2) edges)
  310. (list p1a p1b p0)))))))
  311. (test-equal "node-reachable-count"
  312. '(3 3)
  313. (run-with-store %store
  314. (let* ((p0 (dummy-package "p0"))
  315. (p1a (dummy-package "p1a" (inputs `(("p0" ,p0)))))
  316. (p1b (dummy-package "p1b" (inputs `(("p0" ,p0)))))
  317. (p2 (dummy-package "p2" (inputs `(("p1a" ,p1a) ("p1b" ,p1b))))))
  318. (mlet* %store-monad ((all -> (list p2 p1a p1b p0))
  319. (edges (node-edges %package-node-type all))
  320. (back (node-back-edges %package-node-type all)))
  321. (return (list (node-reachable-count (list p2) edges)
  322. (node-reachable-count (list p0) back)))))))
  323. (test-end "graph")