store.scm 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 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-store)
  19. #:use-module (guix tests)
  20. #:use-module (guix config)
  21. #:use-module (guix store)
  22. #:use-module (guix utils)
  23. #:use-module (guix monads)
  24. #:use-module ((gcrypt hash) #:prefix gcrypt:)
  25. #:use-module ((gcrypt pk-crypto) #:prefix gcrypt:)
  26. #:use-module (guix pki)
  27. #:use-module (guix base32)
  28. #:use-module (guix packages)
  29. #:use-module (guix derivations)
  30. #:use-module (guix serialization)
  31. #:use-module (guix build utils)
  32. #:use-module (guix gexp)
  33. #:use-module (gnu packages)
  34. #:use-module (gnu packages bootstrap)
  35. #:use-module (ice-9 match)
  36. #:use-module (ice-9 regex)
  37. #:use-module (rnrs bytevectors)
  38. #:use-module (rnrs io ports)
  39. #:use-module (web uri)
  40. #:use-module (srfi srfi-1)
  41. #:use-module (srfi srfi-11)
  42. #:use-module (srfi srfi-26)
  43. #:use-module (srfi srfi-34)
  44. #:use-module (srfi srfi-64))
  45. ;; Test the (guix store) module.
  46. (define %store
  47. (open-connection-for-tests))
  48. (define %shell
  49. (or (getenv "SHELL") (getenv "CONFIG_SHELL") "/bin/sh"))
  50. (test-begin "store")
  51. (test-assert "open-connection with file:// URI"
  52. (let ((store (open-connection (string-append "file://"
  53. (%daemon-socket-uri)))))
  54. (and (add-text-to-store store "foo" "bar")
  55. (begin
  56. (close-connection store)
  57. #t))))
  58. (test-equal "connection handshake error"
  59. EPROTO
  60. (let ((port (%make-void-port "rw")))
  61. (guard (c ((store-connection-error? c)
  62. (and (eq? port (store-connection-error-file c))
  63. (store-connection-error-code c))))
  64. (open-connection #f #:port port)
  65. 'broken)))
  66. (test-equal "store-path-hash-part"
  67. "283gqy39v3g9dxjy26rynl0zls82fmcg"
  68. (store-path-hash-part
  69. (string-append (%store-prefix)
  70. "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
  71. (test-equal "store-path-hash-part #f"
  72. #f
  73. (store-path-hash-part
  74. (string-append (%store-prefix)
  75. "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
  76. (test-equal "store-path-package-name"
  77. "guile-2.0.7"
  78. (store-path-package-name
  79. (string-append (%store-prefix)
  80. "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
  81. (test-equal "store-path-package-name #f"
  82. #f
  83. (store-path-package-name
  84. "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
  85. (test-assert "direct-store-path?"
  86. (and (direct-store-path?
  87. (string-append (%store-prefix)
  88. "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
  89. (not (direct-store-path?
  90. (string-append
  91. (%store-prefix)
  92. "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))
  93. (not (direct-store-path? (%store-prefix)))))
  94. (test-skip (if %store 0 15))
  95. (test-equal "profiles/per-user exists and is not writable"
  96. #o755
  97. (stat:perms (stat (string-append %state-directory "/profiles/per-user"))))
  98. (test-equal "profiles/per-user/$USER exists"
  99. (list (getuid) #o755)
  100. (let ((s (stat (string-append %state-directory "/profiles/per-user/"
  101. (passwd:name (getpwuid (getuid)))))))
  102. (list (stat:uid s) (stat:perms s))))
  103. (test-equal "add-to-store"
  104. '("sha1" "sha256" "sha512" "sha3-256" "sha3-512" "blake2s-256")
  105. (let* ((file (search-path %load-path "guix.scm"))
  106. (content (call-with-input-file file get-bytevector-all)))
  107. (map (lambda (hash-algo)
  108. (let ((file (add-to-store %store "guix.scm" #f hash-algo file)))
  109. (and (direct-store-path? file)
  110. (bytevector=? (call-with-input-file file get-bytevector-all)
  111. content)
  112. hash-algo)))
  113. '("sha1" "sha256" "sha512" "sha3-256" "sha3-512" "blake2s-256"))))
  114. (test-equal "add-data-to-store"
  115. #vu8(1 2 3 4 5)
  116. (call-with-input-file (add-data-to-store %store "data" #vu8(1 2 3 4 5))
  117. get-bytevector-all))
  118. (test-assert "valid-path? live"
  119. (let ((p (add-text-to-store %store "hello" "hello, world")))
  120. (valid-path? %store p)))
  121. (test-assert "valid-path? false"
  122. (not (valid-path? %store
  123. (string-append (%store-prefix) "/"
  124. (make-string 32 #\e) "-foobar"))))
  125. (test-equal "with-store, multiple values" ;<https://bugs.gnu.org/42912>
  126. '(1 2 3)
  127. (call-with-values
  128. (lambda ()
  129. (with-store s
  130. (add-text-to-store s "foo" "bar")
  131. (values 1 2 3)))
  132. list))
  133. (test-assert "valid-path? error"
  134. (with-store s
  135. (guard (c ((store-protocol-error? c) #t))
  136. (valid-path? s "foo")
  137. #f)))
  138. (test-assert "valid-path? recovery"
  139. ;; Prior to Nix commit 51800e0 (18 Mar. 2014), the daemon would immediately
  140. ;; close the connection after receiving a 'valid-path?' RPC with a non-store
  141. ;; file name. See
  142. ;; <http://article.gmane.org/gmane.linux.distributions.nixos/12411> for
  143. ;; details.
  144. (with-store s
  145. (let-syntax ((true-if-error (syntax-rules ()
  146. ((_ exp)
  147. (guard (c ((store-protocol-error? c) #t))
  148. exp #f)))))
  149. (and (true-if-error (valid-path? s "foo"))
  150. (true-if-error (valid-path? s "bar"))
  151. (true-if-error (valid-path? s "baz"))
  152. (true-if-error (valid-path? s "chbouib"))
  153. (valid-path? s (add-text-to-store s "valid" "yeah"))))))
  154. (test-assert "hash-part->path"
  155. (let ((p (add-text-to-store %store "hello" "hello, world")))
  156. (equal? (hash-part->path %store (store-path-hash-part p))
  157. p)))
  158. (test-assert "dead-paths"
  159. (let ((p (add-text-to-store %store "random-text" (random-text))))
  160. (->bool (member p (dead-paths %store)))))
  161. ;; FIXME: Find a test for `live-paths'.
  162. ;;
  163. ;; (test-assert "temporary root is in live-paths"
  164. ;; (let* ((p1 (add-text-to-store %store "random-text"
  165. ;; (random-text) '()))
  166. ;; (b (add-text-to-store %store "link-builder"
  167. ;; (format #f "echo ~a > $out" p1)
  168. ;; '()))
  169. ;; (d1 (derivation %store "link"
  170. ;; "/bin/sh" `("-e" ,b)
  171. ;; #:inputs `((,b) (,p1))))
  172. ;; (p2 (derivation->output-path d1)))
  173. ;; (and (add-temp-root %store p2)
  174. ;; (build-derivations %store (list d1))
  175. ;; (valid-path? %store p1)
  176. ;; (member (pk p2) (live-paths %store)))))
  177. (test-assert "add-indirect-root and find-roots"
  178. (call-with-temporary-directory
  179. (lambda (directory)
  180. (let* ((item (add-text-to-store %store "something" (random-text)))
  181. (root (string-append directory "/gc-root")))
  182. (symlink item root)
  183. (add-indirect-root %store root)
  184. (let ((result (member (cons root item) (find-roots %store))))
  185. (delete-file root)
  186. result)))))
  187. (test-assert "permanent root"
  188. (let* ((p (with-store store
  189. (let ((p (add-text-to-store store "random-text"
  190. (random-text))))
  191. (add-permanent-root p)
  192. (add-permanent-root p) ; should not throw
  193. p))))
  194. (and (member p (live-paths %store))
  195. (begin
  196. (remove-permanent-root p)
  197. (->bool (member p (dead-paths %store)))))))
  198. (test-assert "dead path can be explicitly collected"
  199. (let ((p (add-text-to-store %store "random-text"
  200. (random-text) '())))
  201. (let-values (((paths freed) (delete-paths %store (list p))))
  202. (and (equal? paths (list p))
  203. ;; XXX: On some file systems (notably Btrfs), freed
  204. ;; may return 0. See <https://bugs.gnu.org/29363>.
  205. ;;(> freed 0)
  206. (not (file-exists? p))))))
  207. (test-assert "add-text-to-store/add-to-store vs. delete-paths"
  208. ;; Before, 'add-text-to-store' and 'add-to-store' would return the same
  209. ;; store item without noticing that it is no longer valid.
  210. (with-store store
  211. (let* ((text (random-text))
  212. (file (search-path %load-path "guix.scm"))
  213. (path1 (add-text-to-store store "delete-me" text))
  214. (path2 (add-to-store store "delete-me" #t "sha256" file))
  215. (deleted (delete-paths store (list path1 path2))))
  216. (and (string=? path1 (add-text-to-store store "delete-me" text))
  217. (string=? path2 (add-to-store store "delete-me" #t "sha256" file))
  218. (lset= string=? deleted (list path1 path2))
  219. (valid-path? store path1)
  220. (valid-path? store path2)
  221. (file-exists? path1)
  222. (file-exists? path2)))))
  223. (test-equal "add-file-tree-to-store"
  224. `(42
  225. ("." directory #t)
  226. ("./bar" directory #t)
  227. ("./foo" directory #t)
  228. ("./foo/a" regular "file a")
  229. ("./foo/b" symlink "a")
  230. ("./foo/c" directory #t)
  231. ("./foo/c/p" regular "file p")
  232. ("./foo/c/q" directory #t)
  233. ("./foo/c/q/x" regular
  234. ,(string-append "#!" %shell "\nexit 42"))
  235. ("./foo/c/q/y" symlink "..")
  236. ("./foo/c/q/z" directory #t))
  237. (let* ((tree `("file-tree" directory
  238. ("foo" directory
  239. ("a" regular (data "file a"))
  240. ("b" symlink "a")
  241. ("c" directory
  242. ("p" regular (data ,(string->utf8 "file p")))
  243. ("q" directory
  244. ("x" executable
  245. (data ,(string-append "#!" %shell "\nexit 42")))
  246. ("y" symlink "..")
  247. ("z" directory))))
  248. ("bar" directory)))
  249. (result (add-file-tree-to-store %store tree)))
  250. (cons (status:exit-val (system* (string-append result "/foo/c/q/x")))
  251. (with-directory-excursion result
  252. (map (lambda (file)
  253. (let ((type (stat:type (lstat file))))
  254. `(,file ,type
  255. ,(match type
  256. ((or 'regular 'executable)
  257. (call-with-input-file file
  258. get-string-all))
  259. ('symlink (readlink file))
  260. ('directory #t)))))
  261. (find-files "." #:directories? #t))))))
  262. (test-equal "add-file-tree-to-store, flat"
  263. "Hello, world!"
  264. (let* ((tree `("flat-file" regular (data "Hello, world!")))
  265. (result (add-file-tree-to-store %store tree)))
  266. (and (file-exists? result)
  267. (call-with-input-file result get-string-all))))
  268. (test-assert "references"
  269. (let* ((t1 (add-text-to-store %store "random1"
  270. (random-text)))
  271. (t2 (add-text-to-store %store "random2"
  272. (random-text) (list t1))))
  273. (and (equal? (list t1) (references %store t2))
  274. (equal? (list t2) (referrers %store t1))
  275. (null? (references %store t1))
  276. (null? (referrers %store t2)))))
  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-equal "with-build-handler"
  335. 'success
  336. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  337. (s (add-to-store %store "bash" #t "sha256"
  338. (search-bootstrap-binary "bash"
  339. (%current-system))))
  340. (d1 (derivation %store "the-thing"
  341. s `("-e" ,b)
  342. #:env-vars `(("foo" . ,(random-text)))
  343. #:sources (list b s)))
  344. (d2 (derivation %store "the-thing"
  345. s `("-e" ,b)
  346. #:env-vars `(("foo" . ,(random-text))
  347. ("bar" . "baz"))
  348. #:sources (list b s)))
  349. (o1 (derivation->output-path d1))
  350. (o2 (derivation->output-path d2)))
  351. (with-build-handler
  352. (let ((counter 0))
  353. (lambda (continue store things mode)
  354. (match things
  355. ((drv)
  356. (set! counter (+ 1 counter))
  357. (if (string=? drv (derivation-file-name d1))
  358. (continue #t)
  359. (and (string=? drv (derivation-file-name d2))
  360. (= counter 2)
  361. 'success))))))
  362. (build-derivations %store (list d1))
  363. (build-derivations %store (list d2))
  364. 'fail)))
  365. (test-equal "with-build-handler + with-store"
  366. 'success
  367. ;; Check that STORE remains valid when the build handler invokes CONTINUE,
  368. ;; even though 'with-build-handler' is outside the dynamic extent of
  369. ;; 'with-store'.
  370. (with-build-handler (lambda (continue store things mode)
  371. (match things
  372. ((drv)
  373. (and (string-suffix? "thingie.drv" drv)
  374. (not (port-closed?
  375. (store-connection-socket store)))
  376. (continue #t)))))
  377. (with-store store
  378. (let* ((b (add-text-to-store store "build" "echo $foo > $out" '()))
  379. (s (add-to-store store "bash" #t "sha256"
  380. (search-bootstrap-binary "bash"
  381. (%current-system))))
  382. (d (derivation store "thingie"
  383. s `("-e" ,b)
  384. #:env-vars `(("foo" . ,(random-text)))
  385. #:sources (list b s))))
  386. (build-derivations store (list d))
  387. ;; Here STORE's socket should still be open.
  388. (and (valid-path? store (derivation->output-path d))
  389. 'success)))))
  390. (test-assert "map/accumulate-builds"
  391. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  392. (s (add-to-store %store "bash" #t "sha256"
  393. (search-bootstrap-binary "bash"
  394. (%current-system))))
  395. (d1 (derivation %store "the-thing"
  396. s `("-e" ,b)
  397. #:env-vars `(("foo" . ,(random-text)))
  398. #:sources (list b s)))
  399. (d2 (derivation %store "the-thing"
  400. s `("-e" ,b)
  401. #:env-vars `(("foo" . ,(random-text))
  402. ("bar" . "baz"))
  403. #:sources (list b s))))
  404. (with-build-handler (lambda (continue store things mode)
  405. (equal? (map derivation-file-name (list d1 d2))
  406. things))
  407. (map/accumulate-builds %store
  408. (lambda (drv)
  409. (build-derivations %store (list drv))
  410. (add-to-store %store "content-addressed"
  411. #t "sha256"
  412. (derivation->output-path drv)))
  413. (list d1 d2)))))
  414. (test-assert "mapm/accumulate-builds"
  415. (let* ((d1 (run-with-store %store
  416. (gexp->derivation "foo" #~(mkdir #$output))))
  417. (d2 (run-with-store %store
  418. (gexp->derivation "bar" #~(mkdir #$output)))))
  419. (with-build-handler (lambda (continue store things mode)
  420. (equal? (map derivation-file-name (pk 'zz (list d1 d2)))
  421. (pk 'XX things)))
  422. (run-with-store %store
  423. (mapm/accumulate-builds built-derivations `((,d1) (,d2)))))))
  424. (test-equal "mapm/accumulate-builds, %current-target-system"
  425. (make-list 2 '("i586-pc-gnu" "i586-pc-gnu"))
  426. ;; Both the 'mapm' and 'mapm/accumulate-builds' procedures should see the
  427. ;; right #:target.
  428. (run-with-store %store
  429. (mlet %store-monad ((lst1 (mapm %store-monad
  430. (lambda _
  431. (current-target-system))
  432. '(a b)))
  433. (lst2 (mapm/accumulate-builds
  434. (lambda _
  435. (current-target-system))
  436. '(a b))))
  437. (return (list lst1 lst2)))
  438. #:system system
  439. #:target "i586-pc-gnu"))
  440. (test-assert "topologically-sorted, one item"
  441. (let* ((a (add-text-to-store %store "a" "a"))
  442. (b (add-text-to-store %store "b" "b" (list a)))
  443. (c (add-text-to-store %store "c" "c" (list b)))
  444. (d (add-text-to-store %store "d" "d" (list c)))
  445. (s (topologically-sorted %store (list d))))
  446. (equal? s (list a b c d))))
  447. (test-assert "topologically-sorted, several items"
  448. (let* ((a (add-text-to-store %store "a" "a"))
  449. (b (add-text-to-store %store "b" "b" (list a)))
  450. (c (add-text-to-store %store "c" "c" (list b)))
  451. (d (add-text-to-store %store "d" "d" (list c)))
  452. (s1 (topologically-sorted %store (list d a c b)))
  453. (s2 (topologically-sorted %store (list b d c a b d))))
  454. (equal? s1 s2 (list a b c d))))
  455. (test-assert "topologically-sorted, more difficult"
  456. (let* ((a (add-text-to-store %store "a" "a"))
  457. (b (add-text-to-store %store "b" "b" (list a)))
  458. (c (add-text-to-store %store "c" "c" (list b)))
  459. (d (add-text-to-store %store "d" "d" (list c)))
  460. (w (add-text-to-store %store "w" "w"))
  461. (x (add-text-to-store %store "x" "x" (list w)))
  462. (y (add-text-to-store %store "y" "y" (list x d)))
  463. (s1 (topologically-sorted %store (list y)))
  464. (s2 (topologically-sorted %store (list c y)))
  465. (s3 (topologically-sorted %store (cons y (references %store y)))))
  466. ;; The order in which 'references' returns the references of Y is
  467. ;; unspecified, so accommodate.
  468. (let* ((x-then-d? (equal? (references %store y) (list x d))))
  469. (and (equal? s1
  470. (if x-then-d?
  471. (list w x a b c d y)
  472. (list a b c d w x y)))
  473. (equal? s2
  474. (if x-then-d?
  475. (list a b c w x d y)
  476. (list a b c d w x y)))
  477. (lset= string=? s1 s3)))))
  478. (test-assert "current-build-output-port, UTF-8"
  479. ;; Are UTF-8 strings in the build log properly interpreted?
  480. (string-contains
  481. (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
  482. (call-with-output-string
  483. (lambda (port)
  484. (parameterize ((current-build-output-port port))
  485. (let* ((s "Here’s a Greek letter: λ.")
  486. (d (build-expression->derivation
  487. %store "foo" `(display ,s)
  488. #:guile-for-build
  489. (package-derivation s %bootstrap-guile (%current-system)))))
  490. (guard (c ((store-protocol-error? c) #t))
  491. (build-derivations %store (list d))))))))
  492. "Here’s a Greek letter: λ."))
  493. (test-assert "current-build-output-port, UTF-8 + garbage"
  494. ;; What about a mixture of UTF-8 + garbage?
  495. (string-contains
  496. (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
  497. (call-with-output-string
  498. (lambda (port)
  499. (parameterize ((current-build-output-port port))
  500. (let ((d (build-expression->derivation
  501. %store "foo"
  502. `(begin
  503. (use-modules (rnrs io ports))
  504. (display "garbage: ")
  505. (put-bytevector (current-output-port) #vu8(128))
  506. (display "lambda: λ\n"))
  507. #:guile-for-build
  508. (package-derivation %store %bootstrap-guile))))
  509. (guard (c ((store-protocol-error? c) #t))
  510. (build-derivations %store (list d))))))))
  511. "garbage: �lambda: λ"))
  512. (test-assert "log-file, derivation"
  513. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  514. (s (add-to-store %store "bash" #t "sha256"
  515. (search-bootstrap-binary "bash"
  516. (%current-system))))
  517. (d (derivation %store "the-thing"
  518. s `("-e" ,b)
  519. #:env-vars `(("foo" . ,(random-text)))
  520. #:inputs `((,b) (,s)))))
  521. (and (build-derivations %store (list d))
  522. (file-exists? (pk (log-file %store (derivation-file-name d)))))))
  523. (test-assert "log-file, output file name"
  524. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  525. (s (add-to-store %store "bash" #t "sha256"
  526. (search-bootstrap-binary "bash"
  527. (%current-system))))
  528. (d (derivation %store "the-thing"
  529. s `("-e" ,b)
  530. #:env-vars `(("foo" . ,(random-text)))
  531. #:inputs `((,b) (,s))))
  532. (o (derivation->output-path d)))
  533. (and (build-derivations %store (list d))
  534. (file-exists? (pk (log-file %store o)))
  535. (string=? (log-file %store (derivation-file-name d))
  536. (log-file %store o)))))
  537. (test-assert "no substitutes"
  538. (with-store s
  539. (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
  540. (d2 (package-derivation s %bootstrap-glibc (%current-system)))
  541. (o (map derivation->output-path (list d1 d2))))
  542. (set-build-options s #:use-substitutes? #f)
  543. (and (not (has-substitutes? s (derivation-file-name d1)))
  544. (not (has-substitutes? s (derivation-file-name d2)))
  545. (null? (substitutable-paths s o))
  546. (null? (substitutable-path-info s o))))))
  547. (test-assert "build-things with output path"
  548. (with-store s
  549. (let* ((c (random-text)) ;contents of the output
  550. (d (build-expression->derivation
  551. s "substitute-me"
  552. `(call-with-output-file %output
  553. (lambda (p)
  554. (display ,c p)))
  555. #:guile-for-build
  556. (package-derivation s %bootstrap-guile (%current-system))))
  557. (o (derivation->output-path d)))
  558. (set-build-options s #:use-substitutes? #f)
  559. ;; Pass 'build-things' the output file name, O. However, since there
  560. ;; are no substitutes for O, it will just do nothing.
  561. (build-things s (list o))
  562. (not (valid-path? s o)))))
  563. (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
  564. (test-assert "substitute query"
  565. (with-store s
  566. (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
  567. (o (derivation->output-path d)))
  568. ;; Create fake substituter data, to be read by 'guix substitute'.
  569. (with-derivation-narinfo d
  570. ;; Remove entry from the local cache.
  571. (false-if-exception
  572. (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
  573. "/guix/substitute")))
  574. ;; Make sure 'guix substitute' correctly communicates the above
  575. ;; data.
  576. (set-build-options s #:use-substitutes? #t
  577. #:substitute-urls (%test-substitute-urls))
  578. (and (has-substitutes? s o)
  579. (equal? (list o) (substitutable-paths s (list o)))
  580. (match (pk 'spi (substitutable-path-info s (list o)))
  581. (((? substitutable? s))
  582. (and (string=? (substitutable-deriver s)
  583. (derivation-file-name d))
  584. (null? (substitutable-references s))
  585. (equal? (substitutable-nar-size s) 1234)))))))))
  586. (test-assert "substitute query, alternating URLs"
  587. (let* ((d (with-store s
  588. (package-derivation s %bootstrap-guile (%current-system))))
  589. (o (derivation->output-path d)))
  590. (with-derivation-narinfo d
  591. ;; Remove entry from the local cache.
  592. (false-if-exception
  593. (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
  594. "/guix/substitute")))
  595. ;; Note: We reconnect to the daemon to force a new instance of 'guix
  596. ;; substitute' to be used; otherwise the #:substitute-urls of
  597. ;; 'set-build-options' would have no effect.
  598. (and (with-store s ;the right substitute URL
  599. (set-build-options s #:use-substitutes? #t
  600. #:substitute-urls (%test-substitute-urls))
  601. (has-substitutes? s o))
  602. (with-store s ;the wrong one
  603. (set-build-options s #:use-substitutes? #t
  604. #:substitute-urls (list
  605. "http://does-not-exist"))
  606. (not (has-substitutes? s o)))
  607. (with-store s ;the right one again
  608. (set-build-options s #:use-substitutes? #t
  609. #:substitute-urls (%test-substitute-urls))
  610. (has-substitutes? s o))
  611. (with-store s ;empty list of URLs
  612. (set-build-options s #:use-substitutes? #t
  613. #:substitute-urls '())
  614. (not (has-substitutes? s o)))))))
  615. (test-assert "substitute"
  616. (with-store s
  617. (let* ((c (random-text)) ; contents of the output
  618. (d (build-expression->derivation
  619. s "substitute-me"
  620. `(call-with-output-file %output
  621. (lambda (p)
  622. (exit 1) ; would actually fail
  623. (display ,c p)))
  624. #:guile-for-build
  625. (package-derivation s %bootstrap-guile (%current-system))))
  626. (o (derivation->output-path d)))
  627. (with-derivation-substitute d c
  628. (set-build-options s #:use-substitutes? #t
  629. #:substitute-urls (%test-substitute-urls))
  630. (and (has-substitutes? s o)
  631. (build-derivations s (list d))
  632. (canonical-file? o)
  633. (equal? c (call-with-input-file o get-string-all)))))))
  634. (test-assert "substitute, deduplication"
  635. (with-store s
  636. (let* ((c (random-text)) ; contents of the output
  637. (g (package-derivation s %bootstrap-guile))
  638. (d1 (build-expression->derivation s "substitute-me"
  639. `(begin ,c (exit 1))
  640. #:guile-for-build g))
  641. (d2 (build-expression->derivation s "build-me"
  642. `(call-with-output-file %output
  643. (lambda (p)
  644. (display ,c p)))
  645. #:guile-for-build g))
  646. (o1 (derivation->output-path d1))
  647. (o2 (derivation->output-path d2)))
  648. (with-derivation-substitute d1 c
  649. (set-build-options s #:use-substitutes? #t
  650. #:substitute-urls (%test-substitute-urls))
  651. (and (has-substitutes? s o1)
  652. (build-derivations s (list d2)) ;build
  653. (build-derivations s (list d1)) ;substitute
  654. (canonical-file? o1)
  655. (equal? c (call-with-input-file o1 get-string-all))
  656. (= (stat:ino (stat o1)) (stat:ino (stat o2))))))))
  657. (test-assert "substitute + build-things with output path"
  658. (with-store s
  659. (let* ((c (random-text)) ;contents of the output
  660. (d (build-expression->derivation
  661. s "substitute-me"
  662. `(call-with-output-file %output
  663. (lambda (p)
  664. (exit 1) ;would actually fail
  665. (display ,c p)))
  666. #:guile-for-build
  667. (package-derivation s %bootstrap-guile (%current-system))))
  668. (o (derivation->output-path d)))
  669. (with-derivation-substitute d c
  670. (set-build-options s #:use-substitutes? #t
  671. #:substitute-urls (%test-substitute-urls))
  672. (and (has-substitutes? s o)
  673. (build-things s (list o)) ;give the output path
  674. (valid-path? s o)
  675. (canonical-file? o)
  676. (equal? c (call-with-input-file o get-string-all)))))))
  677. (test-assert "substitute + build-things with specific output"
  678. (with-store s
  679. (let* ((c (random-text)) ;contents of the output
  680. (d (build-expression->derivation
  681. s "substitute-me" `(begin ,c (exit 1)) ;would fail
  682. #:outputs '("out" "one" "two")
  683. #:guile-for-build
  684. (package-derivation s %bootstrap-guile (%current-system))))
  685. (o (derivation->output-path d)))
  686. (with-derivation-substitute d c
  687. (set-build-options s #:use-substitutes? #t
  688. #:substitute-urls (%test-substitute-urls))
  689. (and (has-substitutes? s o)
  690. ;; Ask for nothing but the "out" output of D.
  691. (build-things s `((,(derivation-file-name d) . "out")))
  692. (valid-path? s o)
  693. (canonical-file? o)
  694. (equal? c (call-with-input-file o get-string-all)))))))
  695. (test-assert "substitute, corrupt output hash"
  696. ;; Tweak the substituter into installing a substitute whose hash doesn't
  697. ;; match the one announced in the narinfo. The daemon must notice this and
  698. ;; raise an error.
  699. (with-store s
  700. (let* ((c "hello, world") ; contents of the output
  701. (d (build-expression->derivation
  702. s "corrupt-substitute"
  703. `(mkdir %output)
  704. #:guile-for-build
  705. (package-derivation s %bootstrap-guile (%current-system))))
  706. (o (derivation->output-path d)))
  707. (with-derivation-substitute d c
  708. (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
  709. ;; Make sure we use 'guix substitute'.
  710. (set-build-options s
  711. #:use-substitutes? #t
  712. #:fallback? #f
  713. #:substitute-urls (%test-substitute-urls))
  714. (and (has-substitutes? s o)
  715. (guard (c ((store-protocol-error? c)
  716. ;; XXX: the daemon writes "hash mismatch in downloaded
  717. ;; path", but the actual error returned to the client
  718. ;; doesn't mention that.
  719. (pk 'corrupt c)
  720. (not (zero? (store-protocol-error-status c)))))
  721. (build-derivations s (list d))
  722. #f))))))
  723. (test-assert "substitute, corrupt output hash, build trace"
  724. ;; Likewise, and check the build trace.
  725. (with-store s
  726. (let* ((c "hello, world") ; contents of the output
  727. (d (build-expression->derivation
  728. s "corrupt-substitute"
  729. `(mkdir %output)
  730. #:guile-for-build
  731. (package-derivation s %bootstrap-guile (%current-system))))
  732. (o (derivation->output-path d)))
  733. ;; Make sure we use 'guix substitute'.
  734. (set-build-options s
  735. #:print-build-trace #t
  736. #:use-substitutes? #t
  737. #:fallback? #f
  738. #:substitute-urls (%test-substitute-urls))
  739. (with-derivation-substitute d c
  740. (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
  741. (define output
  742. (call-with-output-string
  743. (lambda (port)
  744. (parameterize ((current-build-output-port port))
  745. (guard (c ((store-protocol-error? c) #t))
  746. (build-derivations s (list d))
  747. #f)))))
  748. (define actual-hash
  749. (let-values (((port get-hash)
  750. (gcrypt:open-hash-port
  751. (gcrypt:hash-algorithm gcrypt:sha256))))
  752. (write-file-tree "foo" port
  753. #:file-type+size
  754. (lambda _
  755. (values 'regular (string-length c)))
  756. #:file-port
  757. (lambda _
  758. (open-input-string c)))
  759. (close-port port)
  760. (bytevector->nix-base32-string (get-hash))))
  761. (define expected-hash
  762. (bytevector->nix-base32-string (make-bytevector 32 0)))
  763. (define mismatch
  764. (string-append "@ hash-mismatch " o " sha256 "
  765. expected-hash " " actual-hash "\n"))
  766. (define failure
  767. (string-append "@ substituter-failed " o))
  768. (and (string-contains output mismatch)
  769. (string-contains output failure))))))
  770. (test-assert "substitute --fallback"
  771. (with-store s
  772. (let* ((t (random-text)) ; contents of the output
  773. (d (build-expression->derivation
  774. s "substitute-me-not"
  775. `(call-with-output-file %output
  776. (lambda (p)
  777. (display ,t p)))
  778. #:guile-for-build
  779. (package-derivation s %bootstrap-guile (%current-system))))
  780. (o (derivation->output-path d)))
  781. ;; Create fake substituter data, to be read by 'guix substitute'.
  782. (with-derivation-narinfo d
  783. ;; Make sure we use 'guix substitute'.
  784. (set-build-options s #:use-substitutes? #t
  785. #:substitute-urls (%test-substitute-urls))
  786. (and (has-substitutes? s o)
  787. (guard (c ((store-protocol-error? c)
  788. ;; The substituter failed as expected. Now make
  789. ;; sure that #:fallback? #t works correctly.
  790. (set-build-options s
  791. #:use-substitutes? #t
  792. #:substitute-urls
  793. (%test-substitute-urls)
  794. #:fallback? #t)
  795. (and (build-derivations s (list d))
  796. (equal? t (call-with-input-file o
  797. get-string-all)))))
  798. ;; Should fail.
  799. (build-derivations s (list d))
  800. #f))))))
  801. (test-assert "export/import several paths"
  802. (let* ((texts (unfold (cut >= <> 10)
  803. (lambda _ (random-text))
  804. 1+
  805. 0))
  806. (files (map (cut add-text-to-store %store "text" <>) texts))
  807. (dump (call-with-bytevector-output-port
  808. (cut export-paths %store files <>))))
  809. (delete-paths %store files)
  810. (and (every (negate file-exists?) files)
  811. (let* ((source (open-bytevector-input-port dump))
  812. (imported (import-paths %store source)))
  813. (and (equal? imported files)
  814. (every file-exists? files)
  815. (equal? texts
  816. (map (lambda (file)
  817. (call-with-input-file file
  818. get-string-all))
  819. files)))))))
  820. (test-assert "export/import paths, ensure topological order"
  821. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  822. (file1 (add-text-to-store %store "foo" (random-text)
  823. (list file0)))
  824. (file2 (add-text-to-store %store "bar" (random-text)
  825. (list file1)))
  826. (files (list file1 file2))
  827. (dump1 (call-with-bytevector-output-port
  828. (cute export-paths %store (list file1 file2) <>)))
  829. (dump2 (call-with-bytevector-output-port
  830. (cute export-paths %store (list file2 file1) <>))))
  831. (delete-paths %store files)
  832. (and (every (negate file-exists?) files)
  833. (bytevector=? dump1 dump2)
  834. (let* ((source (open-bytevector-input-port dump1))
  835. (imported (import-paths %store source)))
  836. ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
  837. (and (equal? imported (list file1 file2))
  838. (every file-exists? files)
  839. (equal? (list file0) (references %store file1))
  840. (equal? (list file1) (references %store file2)))))))
  841. (test-assert "export/import incomplete"
  842. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  843. (file1 (add-text-to-store %store "foo" (random-text)
  844. (list file0)))
  845. (file2 (add-text-to-store %store "bar" (random-text)
  846. (list file1)))
  847. (dump (call-with-bytevector-output-port
  848. (cute export-paths %store (list file2) <>))))
  849. (delete-paths %store (list file0 file1 file2))
  850. (guard (c ((store-protocol-error? c)
  851. (and (not (zero? (store-protocol-error-status c)))
  852. (string-contains (store-protocol-error-message c)
  853. "not valid"))))
  854. ;; Here we get an exception because DUMP does not include FILE0 and
  855. ;; FILE1, which are dependencies of FILE2.
  856. (import-paths %store (open-bytevector-input-port dump)))))
  857. (test-assert "export/import recursive"
  858. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  859. (file1 (add-text-to-store %store "foo" (random-text)
  860. (list file0)))
  861. (file2 (add-text-to-store %store "bar" (random-text)
  862. (list file1)))
  863. (dump (call-with-bytevector-output-port
  864. (cute export-paths %store (list file2) <>
  865. #:recursive? #t))))
  866. (delete-paths %store (list file0 file1 file2))
  867. (let ((imported (import-paths %store (open-bytevector-input-port dump))))
  868. (and (equal? imported (list file0 file1 file2))
  869. (every file-exists? (list file0 file1 file2))
  870. (equal? (list file0) (references %store file1))
  871. (equal? (list file1) (references %store file2))))))
  872. (test-assert "write-file & export-path yield the same result"
  873. ;; Here we compare 'write-file' and the daemon's own implementation.
  874. ;; 'write-file' is the reference because we know it sorts file
  875. ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
  876. ;; currently happen to be sorted as a side-effect of some unrelated
  877. ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
  878. ;; changes there.
  879. (run-with-store %store
  880. (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
  881. (out1 -> (derivation->output-path drv1))
  882. (data -> (unfold (cut >= <> 26)
  883. (lambda (i)
  884. (random-bytevector 128))
  885. 1+ 0))
  886. (build
  887. -> #~(begin
  888. (use-modules (rnrs io ports) (srfi srfi-1))
  889. (let ()
  890. (define letters
  891. (map (lambda (i)
  892. (string
  893. (integer->char
  894. (+ i (char->integer #\a)))))
  895. (iota 26)))
  896. (define (touch file data)
  897. (call-with-output-file file
  898. (lambda (port)
  899. (put-bytevector port data))))
  900. (mkdir #$output)
  901. (chdir #$output)
  902. ;; The files must be different so they have
  903. ;; different inode numbers, and the inode
  904. ;; order must differ from the lexicographic
  905. ;; order.
  906. (for-each touch
  907. (append (drop letters 10)
  908. (take letters 10))
  909. (list #$@data))
  910. #t)))
  911. (drv2 (gexp->derivation "bunch" build))
  912. (out2 -> (derivation->output-path drv2))
  913. (item-info -> (store-lift query-path-info)))
  914. (mbegin %store-monad
  915. (built-derivations (list drv1 drv2))
  916. (foldm %store-monad
  917. (lambda (item result)
  918. (define ref-hash
  919. (let-values (((port get) (gcrypt:open-sha256-port)))
  920. (write-file item port)
  921. (close-port port)
  922. (get)))
  923. ;; 'query-path-info' returns a hash produced by using the
  924. ;; daemon's C++ 'dump' function, which is the implementation
  925. ;; under test.
  926. (>>= (item-info item)
  927. (lambda (info)
  928. (return
  929. (and result
  930. (bytevector=? (path-info-hash info) ref-hash))))))
  931. #t
  932. (list out1 out2))))
  933. #:guile-for-build (%guile-for-build)))
  934. (test-assert "import not signed"
  935. (let* ((text (random-text))
  936. (file (add-file-tree-to-store %store
  937. `("tree" directory
  938. ("text" regular (data ,text))
  939. ("link" symlink "text"))))
  940. (dump (call-with-bytevector-output-port
  941. (lambda (port)
  942. (write-int 1 port) ;start
  943. (write-file file port) ;contents
  944. (write-int #x4558494e port) ;%export-magic
  945. (write-string file port) ;store item
  946. (write-string-list '() port) ;references
  947. (write-string "" port) ;deriver
  948. (write-int 0 port) ;not signed
  949. (write-int 0 port))))) ;done
  950. ;; Ensure 'import-paths' raises an exception.
  951. (guard (c ((store-protocol-error? c)
  952. (and (not (zero? (store-protocol-error-status c)))
  953. (string-contains (store-protocol-error-message c)
  954. "lacks a signature"))))
  955. (let* ((source (open-bytevector-input-port dump))
  956. (imported (import-paths %store source)))
  957. (pk 'unsigned-imported imported)
  958. #f))))
  959. (test-assert "import signed by unauthorized key"
  960. (let* ((text (random-text))
  961. (file (add-file-tree-to-store %store
  962. `("tree" directory
  963. ("text" regular (data ,text))
  964. ("link" symlink "text"))))
  965. (key (gcrypt:generate-key
  966. (gcrypt:string->canonical-sexp
  967. "(genkey (ecdsa (curve Ed25519) (flags rfc6979)))")))
  968. (dump (call-with-bytevector-output-port
  969. (lambda (port)
  970. (write-int 1 port) ;start
  971. (write-file file port) ;contents
  972. (write-int #x4558494e port) ;%export-magic
  973. (write-string file port) ;store item
  974. (write-string-list '() port) ;references
  975. (write-string "" port) ;deriver
  976. (write-int 1 port) ;signed
  977. (write-string (gcrypt:canonical-sexp->string
  978. (signature-sexp
  979. (gcrypt:bytevector->hash-data
  980. (gcrypt:sha256 #vu8(0 1 2))
  981. #:key-type 'ecc)
  982. (gcrypt:find-sexp-token key 'private-key)
  983. (gcrypt:find-sexp-token key 'public-key)))
  984. port)
  985. (write-int 0 port))))) ;done
  986. ;; Ensure 'import-paths' raises an exception.
  987. (guard (c ((store-protocol-error? c)
  988. (and (not (zero? (store-protocol-error-status c)))
  989. (string-contains (store-protocol-error-message c)
  990. "unauthorized public key"))))
  991. (let* ((source (open-bytevector-input-port dump))
  992. (imported (import-paths %store source)))
  993. (pk 'unauthorized-imported imported)
  994. #f))))
  995. (test-assert "import corrupt path"
  996. (let* ((text (random-text))
  997. (file (add-text-to-store %store "text" text))
  998. (dump (call-with-bytevector-output-port
  999. (cut export-paths %store (list file) <>))))
  1000. (delete-paths %store (list file))
  1001. ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
  1002. ;; the file contents in DUMP, regardless of the store prefix.
  1003. (let* ((index #x70)
  1004. (byte (bytevector-u8-ref dump index)))
  1005. (bytevector-u8-set! dump index (logxor #xff byte)))
  1006. (and (not (file-exists? file))
  1007. (guard (c ((store-protocol-error? c)
  1008. (pk 'c c)
  1009. (and (not (zero? (store-protocol-error-status c)))
  1010. (string-contains (store-protocol-error-message c)
  1011. "corrupt"))))
  1012. (let* ((source (open-bytevector-input-port dump))
  1013. (imported (import-paths %store source)))
  1014. (pk 'corrupt-imported imported)
  1015. #f)))))
  1016. (test-assert "verify-store"
  1017. (let* ((text (random-text))
  1018. (file1 (add-text-to-store %store "foo" text))
  1019. (file2 (add-text-to-store %store "bar" (random-text)
  1020. (list file1))))
  1021. (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
  1022. (begin
  1023. (delete-file file1)
  1024. (not (pk 'verify2 (verify-store %store)))) ;bad! ;
  1025. (begin
  1026. ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
  1027. ;; without actually creating the file. ;
  1028. (call-with-output-file file1
  1029. (lambda (port)
  1030. (display text port)))
  1031. (pk 'verify3 (verify-store %store)))))) ;OK again
  1032. (test-assert "verify-store + check-contents"
  1033. ;; XXX: This test is I/O intensive.
  1034. (with-store s
  1035. (let* ((text (random-text))
  1036. (drv (build-expression->derivation
  1037. s "corrupt"
  1038. `(let ((out (assoc-ref %outputs "out")))
  1039. (call-with-output-file out
  1040. (lambda (port)
  1041. (display ,text port)))
  1042. #t)
  1043. #:guile-for-build
  1044. (package-derivation s %bootstrap-guile (%current-system))))
  1045. (file (derivation->output-path drv)))
  1046. (with-derivation-substitute drv text
  1047. (and (build-derivations s (list drv))
  1048. (verify-store s #:check-contents? #t) ;should be OK
  1049. (begin
  1050. (chmod file #o644)
  1051. (call-with-output-file file
  1052. (lambda (port)
  1053. (display "corrupt!" port)))
  1054. #t)
  1055. ;; Make sure the corruption is detected. We don't test repairing
  1056. ;; because only "trusted" users are allowed to do it, but we
  1057. ;; don't expose that notion of trusted users that nix-daemon
  1058. ;; supports because it seems dubious and redundant with what the
  1059. ;; OS provides (in Nix "trusted" users have additional
  1060. ;; privileges, such as overriding the set of substitute URLs, but
  1061. ;; we instead want to allow anyone to modify them, provided
  1062. ;; substitutes are signed by a root-approved key.)
  1063. (not (verify-store s #:check-contents? #t))
  1064. ;; Delete the corrupt item to leave the store in a clean state.
  1065. (delete-paths s (list file)))))))
  1066. (test-assert "build-things, check mode"
  1067. (with-store store
  1068. (call-with-temporary-output-file
  1069. (lambda (entropy entropy-port)
  1070. (write (random-text) entropy-port)
  1071. (force-output entropy-port)
  1072. (let* ((drv (build-expression->derivation
  1073. store "non-deterministic"
  1074. `(begin
  1075. (use-modules (rnrs io ports))
  1076. (let ((out (assoc-ref %outputs "out")))
  1077. (call-with-output-file out
  1078. (lambda (port)
  1079. ;; Rely on the fact that tests do not use the
  1080. ;; chroot, and thus ENTROPY is readable.
  1081. (display (call-with-input-file ,entropy
  1082. get-string-all)
  1083. port)))
  1084. #t))
  1085. #:guile-for-build
  1086. (package-derivation store %bootstrap-guile (%current-system))))
  1087. (file (derivation->output-path drv)))
  1088. (and (build-things store (list (derivation-file-name drv)))
  1089. (begin
  1090. (write (random-text) entropy-port)
  1091. (force-output entropy-port)
  1092. (guard (c ((store-protocol-error? c)
  1093. (pk 'determinism-exception c)
  1094. (and (not (zero? (store-protocol-error-status c)))
  1095. (string-contains (store-protocol-error-message c)
  1096. "deterministic"))))
  1097. ;; This one will produce a different result. Since we're in
  1098. ;; 'check' mode, this must fail.
  1099. (build-things store (list (derivation-file-name drv))
  1100. (build-mode check))
  1101. #f))))))))
  1102. (test-assert "build-succeeded trace in check mode"
  1103. (string-contains
  1104. (call-with-output-string
  1105. (lambda (port)
  1106. (let ((d (build-expression->derivation
  1107. %store "foo" '(mkdir (assoc-ref %outputs "out"))
  1108. #:guile-for-build
  1109. (package-derivation %store %bootstrap-guile))))
  1110. (build-derivations %store (list d))
  1111. (parameterize ((current-build-output-port port))
  1112. (build-derivations %store (list d) (build-mode check))))))
  1113. "@ build-succeeded"))
  1114. (test-assert "build multiple times"
  1115. (with-store store
  1116. ;; Ask to build twice.
  1117. (set-build-options store #:rounds 2 #:use-substitutes? #f)
  1118. (call-with-temporary-output-file
  1119. (lambda (entropy entropy-port)
  1120. (write (random-text) entropy-port)
  1121. (force-output entropy-port)
  1122. (let* ((drv (build-expression->derivation
  1123. store "non-deterministic"
  1124. `(begin
  1125. (use-modules (rnrs io ports))
  1126. (let ((out (assoc-ref %outputs "out")))
  1127. (call-with-output-file out
  1128. (lambda (port)
  1129. ;; Rely on the fact that tests do not use the
  1130. ;; chroot, and thus ENTROPY is accessible.
  1131. (display (call-with-input-file ,entropy
  1132. get-string-all)
  1133. port)
  1134. (call-with-output-file ,entropy
  1135. (lambda (port)
  1136. (write 'foobar port)))))
  1137. #t))
  1138. #:guile-for-build
  1139. (package-derivation store %bootstrap-guile (%current-system))))
  1140. (file (derivation->output-path drv)))
  1141. (guard (c ((store-protocol-error? c)
  1142. (pk 'multiple-build c)
  1143. (and (not (zero? (store-protocol-error-status c)))
  1144. (string-contains (store-protocol-error-message c)
  1145. "deterministic"))))
  1146. ;; This one will produce a different result on the second run.
  1147. (current-build-output-port (current-error-port))
  1148. (build-things store (list (derivation-file-name drv)))
  1149. #f))))))
  1150. (test-equal "store-lower"
  1151. "Lowered."
  1152. (let* ((add (store-lower text-file))
  1153. (file (add %store "foo" "Lowered.")))
  1154. (call-with-input-file file get-string-all)))
  1155. (test-equal "current-system"
  1156. "bar"
  1157. (parameterize ((%current-system "frob"))
  1158. (run-with-store %store
  1159. (mbegin %store-monad
  1160. (set-current-system "bar")
  1161. (current-system))
  1162. #:system "foo")))
  1163. (test-assert "query-path-info"
  1164. (let* ((ref (add-text-to-store %store "ref" "foo"))
  1165. (item (add-text-to-store %store "item" "bar" (list ref)))
  1166. (info (query-path-info %store item)))
  1167. (and (equal? (path-info-references info) (list ref))
  1168. (equal? (path-info-hash info)
  1169. (gcrypt:sha256
  1170. (string->utf8
  1171. (call-with-output-string (cut write-file item <>))))))))
  1172. (test-assert "path-info-deriver"
  1173. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  1174. (s (add-to-store %store "bash" #t "sha256"
  1175. (search-bootstrap-binary "bash"
  1176. (%current-system))))
  1177. (d (derivation %store "the-thing"
  1178. s `("-e" ,b)
  1179. #:env-vars `(("foo" . ,(random-text)))
  1180. #:inputs `((,b) (,s))))
  1181. (o (derivation->output-path d)))
  1182. (and (build-derivations %store (list d))
  1183. (not (path-info-deriver (query-path-info %store b)))
  1184. (string=? (derivation-file-name d)
  1185. (path-info-deriver (query-path-info %store o))))))
  1186. (test-equal "build-cores"
  1187. (list 0 42)
  1188. (with-store store
  1189. (let* ((build (add-text-to-store store "build.sh"
  1190. "echo $NIX_BUILD_CORES > $out"))
  1191. (bash (add-to-store store "bash" #t "sha256"
  1192. (search-bootstrap-binary "bash"
  1193. (%current-system))))
  1194. (drv1 (derivation store "the-thing" bash
  1195. `("-e" ,build)
  1196. #:inputs `((,bash) (,build))
  1197. #:env-vars `(("x" . ,(random-text)))))
  1198. (drv2 (derivation store "the-thing" bash
  1199. `("-e" ,build)
  1200. #:inputs `((,bash) (,build))
  1201. #:env-vars `(("x" . ,(random-text))))))
  1202. (and (build-derivations store (list drv1))
  1203. (begin
  1204. (set-build-options store #:build-cores 42)
  1205. (build-derivations store (list drv2)))
  1206. (list (call-with-input-file (derivation->output-path drv1)
  1207. read)
  1208. (call-with-input-file (derivation->output-path drv2)
  1209. read))))))
  1210. (test-equal "multiplexed-build-output"
  1211. '("Hello from first." "Hello from second.")
  1212. (with-store store
  1213. (let* ((build (add-text-to-store store "build.sh"
  1214. "echo Hello from $NAME.; echo > $out"))
  1215. (bash (add-to-store store "bash" #t "sha256"
  1216. (search-bootstrap-binary "bash"
  1217. (%current-system))))
  1218. (drv1 (derivation store "one" bash
  1219. `("-e" ,build)
  1220. #:inputs `((,bash) (,build))
  1221. #:env-vars `(("NAME" . "first")
  1222. ("x" . ,(random-text)))))
  1223. (drv2 (derivation store "two" bash
  1224. `("-e" ,build)
  1225. #:inputs `((,bash) (,build))
  1226. #:env-vars `(("NAME" . "second")
  1227. ("x" . ,(random-text))))))
  1228. (set-build-options store
  1229. #:print-build-trace #t
  1230. #:multiplexed-build-output? #t
  1231. #:max-build-jobs 10)
  1232. (let ((port (open-output-string)))
  1233. ;; Send the build log to PORT.
  1234. (parameterize ((current-build-output-port port))
  1235. (build-derivations store (list drv1 drv2)))
  1236. ;; Retrieve the build log; make sure it contains valid "@ build-log"
  1237. ;; traces that allow us to retrieve each builder's output (we assume
  1238. ;; there's exactly one "build-output" trace for each builder, which is
  1239. ;; reasonable.)
  1240. (let* ((log (get-output-string port))
  1241. (started (fold-matches
  1242. (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
  1243. log '() cons))
  1244. (done (fold-matches
  1245. (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
  1246. log '() cons))
  1247. (output (fold-matches
  1248. (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
  1249. log '() cons))
  1250. (drv-pid (lambda (name)
  1251. (lambda (m)
  1252. (let ((drv (match:substring m 1))
  1253. (pid (string->number
  1254. (match:substring m 4))))
  1255. (and (string-suffix? name drv) pid)))))
  1256. (pid-log (lambda (pid)
  1257. (lambda (m)
  1258. (let ((n (string->number
  1259. (match:substring m 1)))
  1260. (len (string->number
  1261. (match:substring m 2)))
  1262. (str (match:substring m 3)))
  1263. (and (= pid n)
  1264. (= (string-length str) (- len 1))
  1265. str)))))
  1266. (pid1 (any (drv-pid "one.drv") started))
  1267. (pid2 (any (drv-pid "two.drv") started)))
  1268. (list (any (pid-log pid1) output)
  1269. (any (pid-log pid2) output)))))))
  1270. (test-end "store")