store.scm 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 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-store)
  19. #:use-module (guix tests)
  20. #:use-module (guix store)
  21. #:use-module (guix utils)
  22. #:use-module (guix monads)
  23. #:use-module (gcrypt hash)
  24. #:use-module (guix base32)
  25. #:use-module (guix packages)
  26. #:use-module (guix derivations)
  27. #:use-module (guix serialization)
  28. #:use-module (guix build utils)
  29. #:use-module (guix gexp)
  30. #:use-module (gnu packages)
  31. #:use-module (gnu packages bootstrap)
  32. #:use-module (ice-9 match)
  33. #:use-module (ice-9 regex)
  34. #:use-module (rnrs bytevectors)
  35. #:use-module (rnrs io ports)
  36. #:use-module (web uri)
  37. #:use-module (srfi srfi-1)
  38. #:use-module (srfi srfi-11)
  39. #:use-module (srfi srfi-26)
  40. #:use-module (srfi srfi-34)
  41. #:use-module (srfi srfi-64))
  42. ;; Test the (guix store) module.
  43. (define %store
  44. (open-connection-for-tests))
  45. (define %shell
  46. (or (getenv "SHELL") (getenv "CONFIG_SHELL")))
  47. (test-begin "store")
  48. (test-assert "open-connection with file:// URI"
  49. (let ((store (open-connection (string-append "file://"
  50. (%daemon-socket-uri)))))
  51. (and (add-text-to-store store "foo" "bar")
  52. (begin
  53. (close-connection store)
  54. #t))))
  55. (test-equal "connection handshake error"
  56. EPROTO
  57. (let ((port (%make-void-port "rw")))
  58. (guard (c ((store-connection-error? c)
  59. (and (eq? port (store-connection-error-file c))
  60. (store-connection-error-code c))))
  61. (open-connection #f #:port port)
  62. 'broken)))
  63. (test-equal "store-path-hash-part"
  64. "283gqy39v3g9dxjy26rynl0zls82fmcg"
  65. (store-path-hash-part
  66. (string-append (%store-prefix)
  67. "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
  68. (test-equal "store-path-hash-part #f"
  69. #f
  70. (store-path-hash-part
  71. (string-append (%store-prefix)
  72. "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
  73. (test-equal "store-path-package-name"
  74. "guile-2.0.7"
  75. (store-path-package-name
  76. (string-append (%store-prefix)
  77. "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
  78. (test-equal "store-path-package-name #f"
  79. #f
  80. (store-path-package-name
  81. "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
  82. (test-assert "direct-store-path?"
  83. (and (direct-store-path?
  84. (string-append (%store-prefix)
  85. "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
  86. (not (direct-store-path?
  87. (string-append
  88. (%store-prefix)
  89. "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))
  90. (not (direct-store-path? (%store-prefix)))))
  91. (test-skip (if %store 0 13))
  92. (test-equal "add-data-to-store"
  93. #vu8(1 2 3 4 5)
  94. (call-with-input-file (add-data-to-store %store "data" #vu8(1 2 3 4 5))
  95. get-bytevector-all))
  96. (test-assert "valid-path? live"
  97. (let ((p (add-text-to-store %store "hello" "hello, world")))
  98. (valid-path? %store p)))
  99. (test-assert "valid-path? false"
  100. (not (valid-path? %store
  101. (string-append (%store-prefix) "/"
  102. (make-string 32 #\e) "-foobar"))))
  103. (test-assert "valid-path? error"
  104. (with-store s
  105. (guard (c ((store-protocol-error? c) #t))
  106. (valid-path? s "foo")
  107. #f)))
  108. (test-assert "valid-path? recovery"
  109. ;; Prior to Nix commit 51800e0 (18 Mar. 2014), the daemon would immediately
  110. ;; close the connection after receiving a 'valid-path?' RPC with a non-store
  111. ;; file name. See
  112. ;; <http://article.gmane.org/gmane.linux.distributions.nixos/12411> for
  113. ;; details.
  114. (with-store s
  115. (let-syntax ((true-if-error (syntax-rules ()
  116. ((_ exp)
  117. (guard (c ((store-protocol-error? c) #t))
  118. exp #f)))))
  119. (and (true-if-error (valid-path? s "foo"))
  120. (true-if-error (valid-path? s "bar"))
  121. (true-if-error (valid-path? s "baz"))
  122. (true-if-error (valid-path? s "chbouib"))
  123. (valid-path? s (add-text-to-store s "valid" "yeah"))))))
  124. (test-assert "hash-part->path"
  125. (let ((p (add-text-to-store %store "hello" "hello, world")))
  126. (equal? (hash-part->path %store (store-path-hash-part p))
  127. p)))
  128. (test-assert "dead-paths"
  129. (let ((p (add-text-to-store %store "random-text" (random-text))))
  130. (->bool (member p (dead-paths %store)))))
  131. ;; FIXME: Find a test for `live-paths'.
  132. ;;
  133. ;; (test-assert "temporary root is in live-paths"
  134. ;; (let* ((p1 (add-text-to-store %store "random-text"
  135. ;; (random-text) '()))
  136. ;; (b (add-text-to-store %store "link-builder"
  137. ;; (format #f "echo ~a > $out" p1)
  138. ;; '()))
  139. ;; (d1 (derivation %store "link"
  140. ;; "/bin/sh" `("-e" ,b)
  141. ;; #:inputs `((,b) (,p1))))
  142. ;; (p2 (derivation->output-path d1)))
  143. ;; (and (add-temp-root %store p2)
  144. ;; (build-derivations %store (list d1))
  145. ;; (valid-path? %store p1)
  146. ;; (member (pk p2) (live-paths %store)))))
  147. (test-assert "permanent root"
  148. (let* ((p (with-store store
  149. (let ((p (add-text-to-store store "random-text"
  150. (random-text))))
  151. (add-permanent-root p)
  152. (add-permanent-root p) ; should not throw
  153. p))))
  154. (and (member p (live-paths %store))
  155. (begin
  156. (remove-permanent-root p)
  157. (->bool (member p (dead-paths %store)))))))
  158. (test-assert "dead path can be explicitly collected"
  159. (let ((p (add-text-to-store %store "random-text"
  160. (random-text) '())))
  161. (let-values (((paths freed) (delete-paths %store (list p))))
  162. (and (equal? paths (list p))
  163. ;; XXX: On some file systems (notably Btrfs), freed
  164. ;; may return 0. See <https://bugs.gnu.org/29363>.
  165. ;;(> freed 0)
  166. (not (file-exists? p))))))
  167. (test-assert "add-text-to-store vs. delete-paths"
  168. ;; Before, 'add-text-to-store' would return PATH2 without noticing that it
  169. ;; is no longer valid.
  170. (with-store store
  171. (let* ((text (random-text))
  172. (path (add-text-to-store store "delete-me" text))
  173. (deleted (delete-paths store (list path)))
  174. (path2 (add-text-to-store store "delete-me" text)))
  175. (and (string=? path path2)
  176. (equal? deleted (list path))
  177. (valid-path? store path)
  178. (file-exists? path)))))
  179. (test-assert "add-to-store vs. delete-paths"
  180. ;; Same as above.
  181. (with-store store
  182. (let* ((file (search-path %load-path "guix.scm"))
  183. (path (add-to-store store "delete-me" #t "sha256" file))
  184. (deleted (delete-paths store (list path)))
  185. (path2 (add-to-store store "delete-me" #t "sha256" file)))
  186. (and (string=? path path2)
  187. (equal? deleted (list path))
  188. (valid-path? store path)
  189. (file-exists? path)))))
  190. (test-equal "add-file-tree-to-store"
  191. `(42
  192. ("." directory #t)
  193. ("./bar" directory #t)
  194. ("./foo" directory #t)
  195. ("./foo/a" regular "file a")
  196. ("./foo/b" symlink "a")
  197. ("./foo/c" directory #t)
  198. ("./foo/c/p" regular "file p")
  199. ("./foo/c/q" directory #t)
  200. ("./foo/c/q/x" regular
  201. ,(string-append "#!" %shell "\nexit 42"))
  202. ("./foo/c/q/y" symlink "..")
  203. ("./foo/c/q/z" directory #t))
  204. (let* ((tree `("file-tree" directory
  205. ("foo" directory
  206. ("a" regular (data "file a"))
  207. ("b" symlink "a")
  208. ("c" directory
  209. ("p" regular (data ,(string->utf8 "file p")))
  210. ("q" directory
  211. ("x" executable
  212. (data ,(string-append "#!" %shell "\nexit 42")))
  213. ("y" symlink "..")
  214. ("z" directory))))
  215. ("bar" directory)))
  216. (result (add-file-tree-to-store %store tree)))
  217. (cons (status:exit-val (system* (string-append result "/foo/c/q/x")))
  218. (with-directory-excursion result
  219. (map (lambda (file)
  220. (let ((type (stat:type (lstat file))))
  221. `(,file ,type
  222. ,(match type
  223. ((or 'regular 'executable)
  224. (call-with-input-file file
  225. get-string-all))
  226. ('symlink (readlink file))
  227. ('directory #t)))))
  228. (find-files "." #:directories? #t))))))
  229. (test-equal "add-file-tree-to-store, flat"
  230. "Hello, world!"
  231. (let* ((tree `("flat-file" regular (data "Hello, world!")))
  232. (result (add-file-tree-to-store %store tree)))
  233. (and (file-exists? result)
  234. (call-with-input-file result get-string-all))))
  235. (test-assert "references"
  236. (let* ((t1 (add-text-to-store %store "random1"
  237. (random-text)))
  238. (t2 (add-text-to-store %store "random2"
  239. (random-text) (list t1))))
  240. (and (equal? (list t1) (references %store t2))
  241. (equal? (list t2) (referrers %store t1))
  242. (null? (references %store t1))
  243. (null? (referrers %store t2)))))
  244. (test-assert "references/substitutes missing reference info"
  245. (with-store s
  246. (set-build-options s #:use-substitutes? #f)
  247. (guard (c ((store-protocol-error? c) #t))
  248. (let* ((b (add-to-store s "bash" #t "sha256"
  249. (search-bootstrap-binary "bash"
  250. (%current-system))))
  251. (d (derivation s "the-thing" b '("--help")
  252. #:inputs `((,b)))))
  253. (references/substitutes s (list (derivation->output-path d) b))
  254. #f))))
  255. (test-assert "references/substitutes with substitute info"
  256. (with-store s
  257. (set-build-options s #:use-substitutes? #t)
  258. (let* ((t1 (add-text-to-store s "random1" (random-text)))
  259. (t2 (add-text-to-store s "random2" (random-text)
  260. (list t1)))
  261. (t3 (add-text-to-store s "build" "echo -n $t2 > $out"))
  262. (b (add-to-store s "bash" #t "sha256"
  263. (search-bootstrap-binary "bash"
  264. (%current-system))))
  265. (d (derivation s "the-thing" b `("-e" ,t3)
  266. #:inputs `((,b) (,t3) (,t2))
  267. #:env-vars `(("t2" . ,t2))))
  268. (o (derivation->output-path d)))
  269. (with-derivation-narinfo d
  270. (sha256 => (sha256 (string->utf8 t2)))
  271. (references => (list t2))
  272. (equal? (references/substitutes s (list o t3 t2 t1))
  273. `((,t2) ;refs of O
  274. () ;refs of T3
  275. (,t1) ;refs of T2
  276. ())))))) ;refs of T1
  277. (test-equal "substitutable-path-info when substitutes are turned off"
  278. '()
  279. (with-store s
  280. (set-build-options s #:use-substitutes? #f)
  281. (let* ((b (add-to-store s "bash" #t "sha256"
  282. (search-bootstrap-binary "bash"
  283. (%current-system))))
  284. (d (derivation s "the-thing" b '("--version")
  285. #:inputs `((,b))))
  286. (o (derivation->output-path d)))
  287. (with-derivation-narinfo d
  288. (substitutable-path-info s (list o))))))
  289. (test-equal "substitutable-paths when substitutes are turned off"
  290. '()
  291. (with-store s
  292. (set-build-options s #:use-substitutes? #f)
  293. (let* ((b (add-to-store s "bash" #t "sha256"
  294. (search-bootstrap-binary "bash"
  295. (%current-system))))
  296. (d (derivation s "the-thing" b '("--version")
  297. #:inputs `((,b))))
  298. (o (derivation->output-path d)))
  299. (with-derivation-narinfo d
  300. (substitutable-paths s (list o))))))
  301. (test-assert "requisites"
  302. (let* ((t1 (add-text-to-store %store "random1"
  303. (random-text) '()))
  304. (t2 (add-text-to-store %store "random2"
  305. (random-text) (list t1)))
  306. (t3 (add-text-to-store %store "random3"
  307. (random-text) (list t2)))
  308. (t4 (add-text-to-store %store "random4"
  309. (random-text) (list t1 t3))))
  310. (define (same? x y)
  311. (and (= (length x) (length y))
  312. (lset= equal? x y)))
  313. (and (same? (requisites %store (list t1)) (list t1))
  314. (same? (requisites %store (list t2)) (list t1 t2))
  315. (same? (requisites %store (list t3)) (list t1 t2 t3))
  316. (same? (requisites %store (list t4)) (list t1 t2 t3 t4))
  317. (same? (requisites %store (list t1 t2 t3 t4))
  318. (list t1 t2 t3 t4)))))
  319. (test-assert "derivers"
  320. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  321. (s (add-to-store %store "bash" #t "sha256"
  322. (search-bootstrap-binary "bash"
  323. (%current-system))))
  324. (d (derivation %store "the-thing"
  325. s `("-e" ,b)
  326. #:env-vars `(("foo" . ,(random-text)))
  327. #:inputs `((,b) (,s))))
  328. (o (derivation->output-path d)))
  329. (and (build-derivations %store (list d))
  330. (equal? (query-derivation-outputs %store (derivation-file-name d))
  331. (list o))
  332. (equal? (valid-derivers %store o)
  333. (list (derivation-file-name d))))))
  334. (test-assert "topologically-sorted, one item"
  335. (let* ((a (add-text-to-store %store "a" "a"))
  336. (b (add-text-to-store %store "b" "b" (list a)))
  337. (c (add-text-to-store %store "c" "c" (list b)))
  338. (d (add-text-to-store %store "d" "d" (list c)))
  339. (s (topologically-sorted %store (list d))))
  340. (equal? s (list a b c d))))
  341. (test-assert "topologically-sorted, several items"
  342. (let* ((a (add-text-to-store %store "a" "a"))
  343. (b (add-text-to-store %store "b" "b" (list a)))
  344. (c (add-text-to-store %store "c" "c" (list b)))
  345. (d (add-text-to-store %store "d" "d" (list c)))
  346. (s1 (topologically-sorted %store (list d a c b)))
  347. (s2 (topologically-sorted %store (list b d c a b d))))
  348. (equal? s1 s2 (list a b c d))))
  349. (test-assert "topologically-sorted, more difficult"
  350. (let* ((a (add-text-to-store %store "a" "a"))
  351. (b (add-text-to-store %store "b" "b" (list a)))
  352. (c (add-text-to-store %store "c" "c" (list b)))
  353. (d (add-text-to-store %store "d" "d" (list c)))
  354. (w (add-text-to-store %store "w" "w"))
  355. (x (add-text-to-store %store "x" "x" (list w)))
  356. (y (add-text-to-store %store "y" "y" (list x d)))
  357. (s1 (topologically-sorted %store (list y)))
  358. (s2 (topologically-sorted %store (list c y)))
  359. (s3 (topologically-sorted %store (cons y (references %store y)))))
  360. ;; The order in which 'references' returns the references of Y is
  361. ;; unspecified, so accommodate.
  362. (let* ((x-then-d? (equal? (references %store y) (list x d))))
  363. (and (equal? s1
  364. (if x-then-d?
  365. (list w x a b c d y)
  366. (list a b c d w x y)))
  367. (equal? s2
  368. (if x-then-d?
  369. (list a b c w x d y)
  370. (list a b c d w x y)))
  371. (lset= string=? s1 s3)))))
  372. (test-assert "current-build-output-port, UTF-8"
  373. ;; Are UTF-8 strings in the build log properly interpreted?
  374. (string-contains
  375. (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
  376. (call-with-output-string
  377. (lambda (port)
  378. (parameterize ((current-build-output-port port))
  379. (let* ((s "Here’s a Greek letter: λ.")
  380. (d (build-expression->derivation
  381. %store "foo" `(display ,s)
  382. #:guile-for-build
  383. (package-derivation s %bootstrap-guile (%current-system)))))
  384. (guard (c ((store-protocol-error? c) #t))
  385. (build-derivations %store (list d))))))))
  386. "Here’s a Greek letter: λ."))
  387. (test-assert "current-build-output-port, UTF-8 + garbage"
  388. ;; What about a mixture of UTF-8 + garbage?
  389. (string-contains
  390. (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
  391. (call-with-output-string
  392. (lambda (port)
  393. (parameterize ((current-build-output-port port))
  394. (let ((d (build-expression->derivation
  395. %store "foo"
  396. `(begin
  397. (use-modules (rnrs io ports))
  398. (display "garbage: ")
  399. (put-bytevector (current-output-port) #vu8(128))
  400. (display "lambda: λ\n"))
  401. #:guile-for-build
  402. (package-derivation %store %bootstrap-guile))))
  403. (guard (c ((store-protocol-error? c) #t))
  404. (build-derivations %store (list d))))))))
  405. "garbage: �lambda: λ"))
  406. (test-assert "log-file, derivation"
  407. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  408. (s (add-to-store %store "bash" #t "sha256"
  409. (search-bootstrap-binary "bash"
  410. (%current-system))))
  411. (d (derivation %store "the-thing"
  412. s `("-e" ,b)
  413. #:env-vars `(("foo" . ,(random-text)))
  414. #:inputs `((,b) (,s)))))
  415. (and (build-derivations %store (list d))
  416. (file-exists? (pk (log-file %store (derivation-file-name d)))))))
  417. (test-assert "log-file, output file name"
  418. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  419. (s (add-to-store %store "bash" #t "sha256"
  420. (search-bootstrap-binary "bash"
  421. (%current-system))))
  422. (d (derivation %store "the-thing"
  423. s `("-e" ,b)
  424. #:env-vars `(("foo" . ,(random-text)))
  425. #:inputs `((,b) (,s))))
  426. (o (derivation->output-path d)))
  427. (and (build-derivations %store (list d))
  428. (file-exists? (pk (log-file %store o)))
  429. (string=? (log-file %store (derivation-file-name d))
  430. (log-file %store o)))))
  431. (test-assert "no substitutes"
  432. (with-store s
  433. (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
  434. (d2 (package-derivation s %bootstrap-glibc (%current-system)))
  435. (o (map derivation->output-path (list d1 d2))))
  436. (set-build-options s #:use-substitutes? #f)
  437. (and (not (has-substitutes? s (derivation-file-name d1)))
  438. (not (has-substitutes? s (derivation-file-name d2)))
  439. (null? (substitutable-paths s o))
  440. (null? (substitutable-path-info s o))))))
  441. (test-assert "build-things with output path"
  442. (with-store s
  443. (let* ((c (random-text)) ;contents of the output
  444. (d (build-expression->derivation
  445. s "substitute-me"
  446. `(call-with-output-file %output
  447. (lambda (p)
  448. (display ,c p)))
  449. #:guile-for-build
  450. (package-derivation s %bootstrap-guile (%current-system))))
  451. (o (derivation->output-path d)))
  452. (set-build-options s #:use-substitutes? #f)
  453. ;; Pass 'build-things' the output file name, O. However, since there
  454. ;; are no substitutes for O, it will just do nothing.
  455. (build-things s (list o))
  456. (not (valid-path? s o)))))
  457. (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
  458. (test-assert "substitute query"
  459. (with-store s
  460. (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
  461. (o (derivation->output-path d)))
  462. ;; Create fake substituter data, to be read by 'guix substitute'.
  463. (with-derivation-narinfo d
  464. ;; Remove entry from the local cache.
  465. (false-if-exception
  466. (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
  467. "/guix/substitute")))
  468. ;; Make sure 'guix substitute' correctly communicates the above
  469. ;; data.
  470. (set-build-options s #:use-substitutes? #t
  471. #:substitute-urls (%test-substitute-urls))
  472. (and (has-substitutes? s o)
  473. (equal? (list o) (substitutable-paths s (list o)))
  474. (match (pk 'spi (substitutable-path-info s (list o)))
  475. (((? substitutable? s))
  476. (and (string=? (substitutable-deriver s)
  477. (derivation-file-name d))
  478. (null? (substitutable-references s))
  479. (equal? (substitutable-nar-size s) 1234)))))))))
  480. (test-assert "substitute query, alternating URLs"
  481. (let* ((d (with-store s
  482. (package-derivation s %bootstrap-guile (%current-system))))
  483. (o (derivation->output-path d)))
  484. (with-derivation-narinfo d
  485. ;; Remove entry from the local cache.
  486. (false-if-exception
  487. (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
  488. "/guix/substitute")))
  489. ;; Note: We reconnect to the daemon to force a new instance of 'guix
  490. ;; substitute' to be used; otherwise the #:substitute-urls of
  491. ;; 'set-build-options' would have no effect.
  492. (and (with-store s ;the right substitute URL
  493. (set-build-options s #:use-substitutes? #t
  494. #:substitute-urls (%test-substitute-urls))
  495. (has-substitutes? s o))
  496. (with-store s ;the wrong one
  497. (set-build-options s #:use-substitutes? #t
  498. #:substitute-urls (list
  499. "http://does-not-exist"))
  500. (not (has-substitutes? s o)))
  501. (with-store s ;the right one again
  502. (set-build-options s #:use-substitutes? #t
  503. #:substitute-urls (%test-substitute-urls))
  504. (has-substitutes? s o))
  505. (with-store s ;empty list of URLs
  506. (set-build-options s #:use-substitutes? #t
  507. #:substitute-urls '())
  508. (not (has-substitutes? s o)))))))
  509. (test-assert "substitute"
  510. (with-store s
  511. (let* ((c (random-text)) ; contents of the output
  512. (d (build-expression->derivation
  513. s "substitute-me"
  514. `(call-with-output-file %output
  515. (lambda (p)
  516. (exit 1) ; would actually fail
  517. (display ,c p)))
  518. #:guile-for-build
  519. (package-derivation s %bootstrap-guile (%current-system))))
  520. (o (derivation->output-path d)))
  521. (with-derivation-substitute d c
  522. (set-build-options s #:use-substitutes? #t
  523. #:substitute-urls (%test-substitute-urls))
  524. (and (has-substitutes? s o)
  525. (build-derivations s (list d))
  526. (equal? c (call-with-input-file o get-string-all)))))))
  527. (test-assert "substitute + build-things with output path"
  528. (with-store s
  529. (let* ((c (random-text)) ;contents of the output
  530. (d (build-expression->derivation
  531. s "substitute-me"
  532. `(call-with-output-file %output
  533. (lambda (p)
  534. (exit 1) ;would actually fail
  535. (display ,c p)))
  536. #:guile-for-build
  537. (package-derivation s %bootstrap-guile (%current-system))))
  538. (o (derivation->output-path d)))
  539. (with-derivation-substitute d c
  540. (set-build-options s #:use-substitutes? #t
  541. #:substitute-urls (%test-substitute-urls))
  542. (and (has-substitutes? s o)
  543. (build-things s (list o)) ;give the output path
  544. (valid-path? s o)
  545. (equal? c (call-with-input-file o get-string-all)))))))
  546. (test-assert "substitute, corrupt output hash"
  547. ;; Tweak the substituter into installing a substitute whose hash doesn't
  548. ;; match the one announced in the narinfo. The daemon must notice this and
  549. ;; raise an error.
  550. (with-store s
  551. (let* ((c "hello, world") ; contents of the output
  552. (d (build-expression->derivation
  553. s "corrupt-substitute"
  554. `(mkdir %output)
  555. #:guile-for-build
  556. (package-derivation s %bootstrap-guile (%current-system))))
  557. (o (derivation->output-path d)))
  558. (with-derivation-substitute d c
  559. (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
  560. ;; Make sure we use 'guix substitute'.
  561. (set-build-options s
  562. #:use-substitutes? #t
  563. #:fallback? #f
  564. #:substitute-urls (%test-substitute-urls))
  565. (and (has-substitutes? s o)
  566. (guard (c ((store-protocol-error? c)
  567. ;; XXX: the daemon writes "hash mismatch in downloaded
  568. ;; path", but the actual error returned to the client
  569. ;; doesn't mention that.
  570. (pk 'corrupt c)
  571. (not (zero? (store-protocol-error-status c)))))
  572. (build-derivations s (list d))
  573. #f))))))
  574. (test-assert "substitute --fallback"
  575. (with-store s
  576. (let* ((t (random-text)) ; contents of the output
  577. (d (build-expression->derivation
  578. s "substitute-me-not"
  579. `(call-with-output-file %output
  580. (lambda (p)
  581. (display ,t p)))
  582. #:guile-for-build
  583. (package-derivation s %bootstrap-guile (%current-system))))
  584. (o (derivation->output-path d)))
  585. ;; Create fake substituter data, to be read by 'guix substitute'.
  586. (with-derivation-narinfo d
  587. ;; Make sure we use 'guix substitute'.
  588. (set-build-options s #:use-substitutes? #t
  589. #:substitute-urls (%test-substitute-urls))
  590. (and (has-substitutes? s o)
  591. (guard (c ((store-protocol-error? c)
  592. ;; The substituter failed as expected. Now make
  593. ;; sure that #:fallback? #t works correctly.
  594. (set-build-options s
  595. #:use-substitutes? #t
  596. #:substitute-urls
  597. (%test-substitute-urls)
  598. #:fallback? #t)
  599. (and (build-derivations s (list d))
  600. (equal? t (call-with-input-file o
  601. get-string-all)))))
  602. ;; Should fail.
  603. (build-derivations s (list d))
  604. #f))))))
  605. (test-assert "export/import several paths"
  606. (let* ((texts (unfold (cut >= <> 10)
  607. (lambda _ (random-text))
  608. 1+
  609. 0))
  610. (files (map (cut add-text-to-store %store "text" <>) texts))
  611. (dump (call-with-bytevector-output-port
  612. (cut export-paths %store files <>))))
  613. (delete-paths %store files)
  614. (and (every (negate file-exists?) files)
  615. (let* ((source (open-bytevector-input-port dump))
  616. (imported (import-paths %store source)))
  617. (and (equal? imported files)
  618. (every file-exists? files)
  619. (equal? texts
  620. (map (lambda (file)
  621. (call-with-input-file file
  622. get-string-all))
  623. files)))))))
  624. (test-assert "export/import paths, ensure topological order"
  625. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  626. (file1 (add-text-to-store %store "foo" (random-text)
  627. (list file0)))
  628. (file2 (add-text-to-store %store "bar" (random-text)
  629. (list file1)))
  630. (files (list file1 file2))
  631. (dump1 (call-with-bytevector-output-port
  632. (cute export-paths %store (list file1 file2) <>)))
  633. (dump2 (call-with-bytevector-output-port
  634. (cute export-paths %store (list file2 file1) <>))))
  635. (delete-paths %store files)
  636. (and (every (negate file-exists?) files)
  637. (bytevector=? dump1 dump2)
  638. (let* ((source (open-bytevector-input-port dump1))
  639. (imported (import-paths %store source)))
  640. ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
  641. (and (equal? imported (list file1 file2))
  642. (every file-exists? files)
  643. (equal? (list file0) (references %store file1))
  644. (equal? (list file1) (references %store file2)))))))
  645. (test-assert "export/import incomplete"
  646. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  647. (file1 (add-text-to-store %store "foo" (random-text)
  648. (list file0)))
  649. (file2 (add-text-to-store %store "bar" (random-text)
  650. (list file1)))
  651. (dump (call-with-bytevector-output-port
  652. (cute export-paths %store (list file2) <>))))
  653. (delete-paths %store (list file0 file1 file2))
  654. (guard (c ((store-protocol-error? c)
  655. (and (not (zero? (store-protocol-error-status c)))
  656. (string-contains (store-protocol-error-message c)
  657. "not valid"))))
  658. ;; Here we get an exception because DUMP does not include FILE0 and
  659. ;; FILE1, which are dependencies of FILE2.
  660. (import-paths %store (open-bytevector-input-port dump)))))
  661. (test-assert "export/import recursive"
  662. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  663. (file1 (add-text-to-store %store "foo" (random-text)
  664. (list file0)))
  665. (file2 (add-text-to-store %store "bar" (random-text)
  666. (list file1)))
  667. (dump (call-with-bytevector-output-port
  668. (cute export-paths %store (list file2) <>
  669. #:recursive? #t))))
  670. (delete-paths %store (list file0 file1 file2))
  671. (let ((imported (import-paths %store (open-bytevector-input-port dump))))
  672. (and (equal? imported (list file0 file1 file2))
  673. (every file-exists? (list file0 file1 file2))
  674. (equal? (list file0) (references %store file1))
  675. (equal? (list file1) (references %store file2))))))
  676. (test-assert "write-file & export-path yield the same result"
  677. ;; Here we compare 'write-file' and the daemon's own implementation.
  678. ;; 'write-file' is the reference because we know it sorts file
  679. ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
  680. ;; currently happen to be sorted as a side-effect of some unrelated
  681. ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
  682. ;; changes there.
  683. (run-with-store %store
  684. (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
  685. (out1 -> (derivation->output-path drv1))
  686. (data -> (unfold (cut >= <> 26)
  687. (lambda (i)
  688. (random-bytevector 128))
  689. 1+ 0))
  690. (build
  691. -> #~(begin
  692. (use-modules (rnrs io ports) (srfi srfi-1))
  693. (let ()
  694. (define letters
  695. (map (lambda (i)
  696. (string
  697. (integer->char
  698. (+ i (char->integer #\a)))))
  699. (iota 26)))
  700. (define (touch file data)
  701. (call-with-output-file file
  702. (lambda (port)
  703. (put-bytevector port data))))
  704. (mkdir #$output)
  705. (chdir #$output)
  706. ;; The files must be different so they have
  707. ;; different inode numbers, and the inode
  708. ;; order must differ from the lexicographic
  709. ;; order.
  710. (for-each touch
  711. (append (drop letters 10)
  712. (take letters 10))
  713. (list #$@data))
  714. #t)))
  715. (drv2 (gexp->derivation "bunch" build))
  716. (out2 -> (derivation->output-path drv2))
  717. (item-info -> (store-lift query-path-info)))
  718. (mbegin %store-monad
  719. (built-derivations (list drv1 drv2))
  720. (foldm %store-monad
  721. (lambda (item result)
  722. (define ref-hash
  723. (let-values (((port get) (open-sha256-port)))
  724. (write-file item port)
  725. (close-port port)
  726. (get)))
  727. ;; 'query-path-info' returns a hash produced by using the
  728. ;; daemon's C++ 'dump' function, which is the implementation
  729. ;; under test.
  730. (>>= (item-info item)
  731. (lambda (info)
  732. (return
  733. (and result
  734. (bytevector=? (path-info-hash info) ref-hash))))))
  735. #t
  736. (list out1 out2))))
  737. #:guile-for-build (%guile-for-build)))
  738. (test-assert "import corrupt path"
  739. (let* ((text (random-text))
  740. (file (add-text-to-store %store "text" text))
  741. (dump (call-with-bytevector-output-port
  742. (cut export-paths %store (list file) <>))))
  743. (delete-paths %store (list file))
  744. ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
  745. ;; the file contents in DUMP, regardless of the store prefix.
  746. (let* ((index #x70)
  747. (byte (bytevector-u8-ref dump index)))
  748. (bytevector-u8-set! dump index (logxor #xff byte)))
  749. (and (not (file-exists? file))
  750. (guard (c ((store-protocol-error? c)
  751. (pk 'c c)
  752. (and (not (zero? (store-protocol-error-status c)))
  753. (string-contains (store-protocol-error-message c)
  754. "corrupt"))))
  755. (let* ((source (open-bytevector-input-port dump))
  756. (imported (import-paths %store source)))
  757. (pk 'corrupt-imported imported)
  758. #f)))))
  759. (test-assert "verify-store"
  760. (let* ((text (random-text))
  761. (file1 (add-text-to-store %store "foo" text))
  762. (file2 (add-text-to-store %store "bar" (random-text)
  763. (list file1))))
  764. (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
  765. (begin
  766. (delete-file file1)
  767. (not (pk 'verify2 (verify-store %store)))) ;bad! ;
  768. (begin
  769. ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
  770. ;; without actually creating the file. ;
  771. (call-with-output-file file1
  772. (lambda (port)
  773. (display text port)))
  774. (pk 'verify3 (verify-store %store)))))) ;OK again
  775. (test-assert "verify-store + check-contents"
  776. ;; XXX: This test is I/O intensive.
  777. (with-store s
  778. (let* ((text (random-text))
  779. (drv (build-expression->derivation
  780. s "corrupt"
  781. `(let ((out (assoc-ref %outputs "out")))
  782. (call-with-output-file out
  783. (lambda (port)
  784. (display ,text port)))
  785. #t)
  786. #:guile-for-build
  787. (package-derivation s %bootstrap-guile (%current-system))))
  788. (file (derivation->output-path drv)))
  789. (with-derivation-substitute drv text
  790. (and (build-derivations s (list drv))
  791. (verify-store s #:check-contents? #t) ;should be OK
  792. (begin
  793. (chmod file #o644)
  794. (call-with-output-file file
  795. (lambda (port)
  796. (display "corrupt!" port)))
  797. #t)
  798. ;; Make sure the corruption is detected. We don't test repairing
  799. ;; because only "trusted" users are allowed to do it, but we
  800. ;; don't expose that notion of trusted users that nix-daemon
  801. ;; supports because it seems dubious and redundant with what the
  802. ;; OS provides (in Nix "trusted" users have additional
  803. ;; privileges, such as overriding the set of substitute URLs, but
  804. ;; we instead want to allow anyone to modify them, provided
  805. ;; substitutes are signed by a root-approved key.)
  806. (not (verify-store s #:check-contents? #t))
  807. ;; Delete the corrupt item to leave the store in a clean state.
  808. (delete-paths s (list file)))))))
  809. (test-assert "build-things, check mode"
  810. (with-store store
  811. (call-with-temporary-output-file
  812. (lambda (entropy entropy-port)
  813. (write (random-text) entropy-port)
  814. (force-output entropy-port)
  815. (let* ((drv (build-expression->derivation
  816. store "non-deterministic"
  817. `(begin
  818. (use-modules (rnrs io ports))
  819. (let ((out (assoc-ref %outputs "out")))
  820. (call-with-output-file out
  821. (lambda (port)
  822. ;; Rely on the fact that tests do not use the
  823. ;; chroot, and thus ENTROPY is readable.
  824. (display (call-with-input-file ,entropy
  825. get-string-all)
  826. port)))
  827. #t))
  828. #:guile-for-build
  829. (package-derivation store %bootstrap-guile (%current-system))))
  830. (file (derivation->output-path drv)))
  831. (and (build-things store (list (derivation-file-name drv)))
  832. (begin
  833. (write (random-text) entropy-port)
  834. (force-output entropy-port)
  835. (guard (c ((store-protocol-error? c)
  836. (pk 'determinism-exception c)
  837. (and (not (zero? (store-protocol-error-status c)))
  838. (string-contains (store-protocol-error-message c)
  839. "deterministic"))))
  840. ;; This one will produce a different result. Since we're in
  841. ;; 'check' mode, this must fail.
  842. (build-things store (list (derivation-file-name drv))
  843. (build-mode check))
  844. #f))))))))
  845. (test-assert "build-succeeded trace in check mode"
  846. (string-contains
  847. (call-with-output-string
  848. (lambda (port)
  849. (let ((d (build-expression->derivation
  850. %store "foo" '(mkdir (assoc-ref %outputs "out"))
  851. #:guile-for-build
  852. (package-derivation %store %bootstrap-guile))))
  853. (build-derivations %store (list d))
  854. (parameterize ((current-build-output-port port))
  855. (build-derivations %store (list d) (build-mode check))))))
  856. "@ build-succeeded"))
  857. (test-assert "build multiple times"
  858. (with-store store
  859. ;; Ask to build twice.
  860. (set-build-options store #:rounds 2 #:use-substitutes? #f)
  861. (call-with-temporary-output-file
  862. (lambda (entropy entropy-port)
  863. (write (random-text) entropy-port)
  864. (force-output entropy-port)
  865. (let* ((drv (build-expression->derivation
  866. store "non-deterministic"
  867. `(begin
  868. (use-modules (rnrs io ports))
  869. (let ((out (assoc-ref %outputs "out")))
  870. (call-with-output-file out
  871. (lambda (port)
  872. ;; Rely on the fact that tests do not use the
  873. ;; chroot, and thus ENTROPY is accessible.
  874. (display (call-with-input-file ,entropy
  875. get-string-all)
  876. port)
  877. (call-with-output-file ,entropy
  878. (lambda (port)
  879. (write 'foobar port)))))
  880. #t))
  881. #:guile-for-build
  882. (package-derivation store %bootstrap-guile (%current-system))))
  883. (file (derivation->output-path drv)))
  884. (guard (c ((store-protocol-error? c)
  885. (pk 'multiple-build c)
  886. (and (not (zero? (store-protocol-error-status c)))
  887. (string-contains (store-protocol-error-message c)
  888. "deterministic"))))
  889. ;; This one will produce a different result on the second run.
  890. (current-build-output-port (current-error-port))
  891. (build-things store (list (derivation-file-name drv)))
  892. #f))))))
  893. (test-equal "store-lower"
  894. "Lowered."
  895. (let* ((add (store-lower text-file))
  896. (file (add %store "foo" "Lowered.")))
  897. (call-with-input-file file get-string-all)))
  898. (test-equal "current-system"
  899. "bar"
  900. (parameterize ((%current-system "frob"))
  901. (run-with-store %store
  902. (mbegin %store-monad
  903. (set-current-system "bar")
  904. (current-system))
  905. #:system "foo")))
  906. (test-assert "query-path-info"
  907. (let* ((ref (add-text-to-store %store "ref" "foo"))
  908. (item (add-text-to-store %store "item" "bar" (list ref)))
  909. (info (query-path-info %store item)))
  910. (and (equal? (path-info-references info) (list ref))
  911. (equal? (path-info-hash info)
  912. (sha256
  913. (string->utf8
  914. (call-with-output-string (cut write-file item <>))))))))
  915. (test-assert "path-info-deriver"
  916. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  917. (s (add-to-store %store "bash" #t "sha256"
  918. (search-bootstrap-binary "bash"
  919. (%current-system))))
  920. (d (derivation %store "the-thing"
  921. s `("-e" ,b)
  922. #:env-vars `(("foo" . ,(random-text)))
  923. #:inputs `((,b) (,s))))
  924. (o (derivation->output-path d)))
  925. (and (build-derivations %store (list d))
  926. (not (path-info-deriver (query-path-info %store b)))
  927. (string=? (derivation-file-name d)
  928. (path-info-deriver (query-path-info %store o))))))
  929. (test-equal "build-cores"
  930. (list 0 42)
  931. (with-store store
  932. (let* ((build (add-text-to-store store "build.sh"
  933. "echo $NIX_BUILD_CORES > $out"))
  934. (bash (add-to-store store "bash" #t "sha256"
  935. (search-bootstrap-binary "bash"
  936. (%current-system))))
  937. (drv1 (derivation store "the-thing" bash
  938. `("-e" ,build)
  939. #:inputs `((,bash) (,build))
  940. #:env-vars `(("x" . ,(random-text)))))
  941. (drv2 (derivation store "the-thing" bash
  942. `("-e" ,build)
  943. #:inputs `((,bash) (,build))
  944. #:env-vars `(("x" . ,(random-text))))))
  945. (and (build-derivations store (list drv1))
  946. (begin
  947. (set-build-options store #:build-cores 42)
  948. (build-derivations store (list drv2)))
  949. (list (call-with-input-file (derivation->output-path drv1)
  950. read)
  951. (call-with-input-file (derivation->output-path drv2)
  952. read))))))
  953. (test-equal "multiplexed-build-output"
  954. '("Hello from first." "Hello from second.")
  955. (with-store store
  956. (let* ((build (add-text-to-store store "build.sh"
  957. "echo Hello from $NAME.; echo > $out"))
  958. (bash (add-to-store store "bash" #t "sha256"
  959. (search-bootstrap-binary "bash"
  960. (%current-system))))
  961. (drv1 (derivation store "one" bash
  962. `("-e" ,build)
  963. #:inputs `((,bash) (,build))
  964. #:env-vars `(("NAME" . "first")
  965. ("x" . ,(random-text)))))
  966. (drv2 (derivation store "two" bash
  967. `("-e" ,build)
  968. #:inputs `((,bash) (,build))
  969. #:env-vars `(("NAME" . "second")
  970. ("x" . ,(random-text))))))
  971. (set-build-options store
  972. #:print-build-trace #t
  973. #:multiplexed-build-output? #t
  974. #:max-build-jobs 10)
  975. (let ((port (open-output-string)))
  976. ;; Send the build log to PORT.
  977. (parameterize ((current-build-output-port port))
  978. (build-derivations store (list drv1 drv2)))
  979. ;; Retrieve the build log; make sure it contains valid "@ build-log"
  980. ;; traces that allow us to retrieve each builder's output (we assume
  981. ;; there's exactly one "build-output" trace for each builder, which is
  982. ;; reasonable.)
  983. (let* ((log (get-output-string port))
  984. (started (fold-matches
  985. (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
  986. log '() cons))
  987. (done (fold-matches
  988. (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
  989. log '() cons))
  990. (output (fold-matches
  991. (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
  992. log '() cons))
  993. (drv-pid (lambda (name)
  994. (lambda (m)
  995. (let ((drv (match:substring m 1))
  996. (pid (string->number
  997. (match:substring m 4))))
  998. (and (string-suffix? name drv) pid)))))
  999. (pid-log (lambda (pid)
  1000. (lambda (m)
  1001. (let ((n (string->number
  1002. (match:substring m 1)))
  1003. (len (string->number
  1004. (match:substring m 2)))
  1005. (str (match:substring m 3)))
  1006. (and (= pid n)
  1007. (= (string-length str) (- len 1))
  1008. str)))))
  1009. (pid1 (any (drv-pid "one.drv") started))
  1010. (pid2 (any (drv-pid "two.drv") started)))
  1011. (list (any (pid-log pid1) output)
  1012. (any (pid-log pid2) output)))))))
  1013. (test-end "store")