graph.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015, 2016, 2017 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. (standard-packages))))
  103. (run-with-store %store
  104. (export-graph (list p) 'port
  105. #:node-type %bag-emerged-node-type
  106. #:backend backend))
  107. ;; We should see exactly P and IMPLICIT, with one edge from P to each
  108. ;; element of IMPLICIT. O must not appear among NODES.
  109. (let-values (((nodes edges) (nodes+edges)))
  110. (and (equal? (match nodes
  111. (((labels names) ...)
  112. names))
  113. (map package-full-name (cons p implicit)))
  114. (equal? (match edges
  115. (((sources destinations) ...)
  116. (zip (map store-path-package-name sources)
  117. (map store-path-package-name destinations))))
  118. (map (lambda (destination)
  119. (list "p-0.drv"
  120. (string-append
  121. (package-full-name destination)
  122. ".drv")))
  123. implicit)))))))
  124. (test-assert "bag DAG" ;a big town in Iraq
  125. (let-values (((backend nodes+edges) (make-recording-backend)))
  126. (let ((p (dummy-package "p")))
  127. (run-with-store %store
  128. (export-graph (list p) 'port
  129. #:node-type %bag-node-type
  130. #:backend backend))
  131. ;; We should see P, its implicit inputs as well as the whole DAG, which
  132. ;; should include bootstrap binaries.
  133. (let-values (((nodes edges) (nodes+edges)))
  134. (every (lambda (name)
  135. (find (cut string=? name <>)
  136. (match nodes
  137. (((labels names) ...)
  138. names))))
  139. (match %bootstrap-inputs
  140. (((labels packages) ...)
  141. (map package-full-name packages))))))))
  142. (test-assert "bag DAG, including origins"
  143. (let-values (((backend nodes+edges) (make-recording-backend)))
  144. (let* ((m (lambda* (uri hash-type hash name #:key system)
  145. (text-file "foo-1.2.3.tar.gz" "This is a fake!")))
  146. (o (origin (method m) (uri "the-uri") (sha256 #vu8(0 1 2))))
  147. (p (dummy-package "p" (source o))))
  148. (run-with-store %store
  149. (export-graph (list p) 'port
  150. #:node-type %bag-with-origins-node-type
  151. #:backend backend))
  152. ;; We should see O among the nodes, with an edge coming from P.
  153. (let-values (((nodes edges) (nodes+edges)))
  154. (run-with-store %store
  155. (mlet %store-monad ((o* (lower-object o))
  156. (p* (lower-object p))
  157. (g (lower-object (default-guile))))
  158. (return
  159. (and (find (match-lambda
  160. ((file "the-uri") #t)
  161. (_ #f))
  162. nodes)
  163. (find (match-lambda
  164. ((source target)
  165. (and (string=? source (derivation-file-name p*))
  166. (string=? target o*))))
  167. edges)
  168. ;; There must also be an edge from O to G.
  169. (find (match-lambda
  170. ((source target)
  171. (and (string=? source o*)
  172. (string=? target (derivation-file-name g)))))
  173. edges)))))))))
  174. (test-assert "derivation DAG"
  175. (let-values (((backend nodes+edges) (make-recording-backend)))
  176. (run-with-store %store
  177. (mlet* %store-monad ((txt (text-file "text-file" "Hello!"))
  178. (guile (package->derivation %bootstrap-guile))
  179. (drv (gexp->derivation "output"
  180. #~(symlink #$txt #$output)
  181. #:guile-for-build
  182. guile)))
  183. ;; We should get at least these 3 nodes and corresponding edges.
  184. (mbegin %store-monad
  185. (export-graph (list drv) 'port
  186. #:node-type %derivation-node-type
  187. #:backend backend)
  188. (let-values (((nodes edges) (nodes+edges)))
  189. ;; XXX: For some reason we need to throw in some 'basename'.
  190. (return (and (match nodes
  191. (((ids labels) ...)
  192. (let ((ids (map basename ids)))
  193. (every (lambda (item)
  194. (member (basename item) ids))
  195. (list txt
  196. (derivation-file-name drv)
  197. (derivation-file-name guile))))))
  198. (every (cut member <>
  199. (map (lambda (edge)
  200. (map basename edge))
  201. edges))
  202. (list (map (compose basename derivation-file-name)
  203. (list drv guile))
  204. (list (basename (derivation-file-name drv))
  205. (basename txt))))))))))))
  206. (test-assert "reference DAG"
  207. (let-values (((backend nodes+edges) (make-recording-backend)))
  208. (run-with-store %store
  209. (mlet* %store-monad ((txt (text-file "text-file" "Hello!"))
  210. (guile (package->derivation %bootstrap-guile))
  211. (drv (gexp->derivation "output"
  212. #~(symlink #$txt #$output)
  213. #:guile-for-build
  214. guile))
  215. (out -> (derivation->output-path drv)))
  216. ;; We should see only OUT and TXT, with an edge from the former to the
  217. ;; latter.
  218. (mbegin %store-monad
  219. (built-derivations (list drv))
  220. (export-graph (list (derivation->output-path drv)) 'port
  221. #:node-type %reference-node-type
  222. #:backend backend)
  223. (let-values (((nodes edges) (nodes+edges)))
  224. (return
  225. (and (equal? (match nodes
  226. (((ids labels) ...)
  227. ids))
  228. (list out txt))
  229. (equal? edges `((,out ,txt)))))))))))
  230. (test-assert "referrer DAG"
  231. (let-values (((backend nodes+edges) (make-recording-backend)))
  232. (run-with-store %store
  233. (mlet* %store-monad ((txt (text-file "referrer-node" (random-text)))
  234. (drv (gexp->derivation "referrer"
  235. #~(symlink #$txt #$output)))
  236. (out -> (derivation->output-path drv)))
  237. ;; We should see only TXT and OUT, with an edge from the former to the
  238. ;; latter.
  239. (mbegin %store-monad
  240. (built-derivations (list drv))
  241. (export-graph (list txt) 'port
  242. #:node-type %referrer-node-type
  243. #:backend backend)
  244. (let-values (((nodes edges) (nodes+edges)))
  245. (return
  246. (and (equal? (match nodes
  247. (((ids labels) ...)
  248. ids))
  249. (list txt out))
  250. (equal? edges `((,txt ,out)))))))))))
  251. (test-assert "node-edges"
  252. (run-with-store %store
  253. (let ((packages (fold-packages cons '())))
  254. (mlet %store-monad ((edges (node-edges %package-node-type packages)))
  255. (return (and (null? (edges sed))
  256. (lset= eq?
  257. (edges guile-2.0)
  258. (match (package-direct-inputs guile-2.0)
  259. (((labels packages _ ...) ...)
  260. packages)))))))))
  261. (test-assert "node-transitive-edges + node-back-edges"
  262. (run-with-store %store
  263. (let ((packages (fold-packages cons '()))
  264. (bootstrap? (lambda (package)
  265. (string-contains
  266. (location-file (package-location package))
  267. "bootstrap.scm")))
  268. (trivial? (lambda (package)
  269. (eq? (package-build-system package)
  270. trivial-build-system))))
  271. (mlet %store-monad ((edges (node-back-edges %bag-node-type packages)))
  272. (let* ((glibc (canonical-package glibc))
  273. (dependents (node-transitive-edges (list glibc) edges))
  274. (diff (lset-difference eq? packages dependents)))
  275. ;; All the packages depend on libc, except bootstrap packages and
  276. ;; some that use TRIVIAL-BUILD-SYSTEM.
  277. (return (null? (remove (lambda (package)
  278. (or (trivial? package)
  279. (bootstrap? package)))
  280. diff))))))))
  281. (test-assert "node-transitive-edges, no duplicates"
  282. (run-with-store %store
  283. (let* ((p0 (dummy-package "p0"))
  284. (p1a (dummy-package "p1a" (inputs `(("p0" ,p0)))))
  285. (p1b (dummy-package "p1b" (inputs `(("p0" ,p0)))))
  286. (p2 (dummy-package "p2" (inputs `(("p1a" ,p1a) ("p1b" ,p1b))))))
  287. (mlet %store-monad ((edges (node-edges %package-node-type
  288. (list p2 p1a p1b p0))))
  289. (return (lset= eq? (node-transitive-edges (list p2) edges)
  290. (list p1a p1b p0)))))))
  291. (test-equal "node-reachable-count"
  292. '(3 3)
  293. (run-with-store %store
  294. (let* ((p0 (dummy-package "p0"))
  295. (p1a (dummy-package "p1a" (inputs `(("p0" ,p0)))))
  296. (p1b (dummy-package "p1b" (inputs `(("p0" ,p0)))))
  297. (p2 (dummy-package "p2" (inputs `(("p1a" ,p1a) ("p1b" ,p1b))))))
  298. (mlet* %store-monad ((all -> (list p2 p1a p1b p0))
  299. (edges (node-edges %package-node-type all))
  300. (back (node-back-edges %package-node-type all)))
  301. (return (list (node-reachable-count (list p2) edges)
  302. (node-reachable-count (list p0) back)))))))
  303. (test-end "graph")