store.scm 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  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-equal "map/accumulate-builds cutoff" ;https://issues.guix.gnu.org/50264
  415. (iota 20)
  416. ;; Make sure that, when the cutoff is reached, 'map/accumulate-builds' still
  417. ;; returns the right result and calls the build handler by batches.
  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 (map (lambda (i)
  423. (derivation %store (string-append "the-thing-"
  424. (number->string i))
  425. s `("-e" ,b)
  426. #:env-vars `(("foo" . ,(random-text)))
  427. #:sources (list b s)
  428. #:properties `((n . ,i))))
  429. (iota 20)))
  430. (calls '()))
  431. (define lst
  432. (with-build-handler (lambda (continue store things mode)
  433. (set! calls (cons things calls))
  434. (continue #f))
  435. (map/accumulate-builds %store
  436. (lambda (d)
  437. (build-derivations %store (list d))
  438. (assq-ref (derivation-properties d) 'n))
  439. d
  440. #:cutoff 7)))
  441. (match (reverse calls)
  442. (((batch1 ...) (batch2 ...) (batch3 ...))
  443. (and (equal? (map derivation-file-name (take d 8)) batch1)
  444. (equal? (map derivation-file-name (take (drop d 8) 8)) batch2)
  445. (equal? (map derivation-file-name (drop d 16)) batch3)
  446. lst)))))
  447. (test-assert "mapm/accumulate-builds"
  448. (let* ((d1 (run-with-store %store
  449. (gexp->derivation "foo" #~(mkdir #$output))))
  450. (d2 (run-with-store %store
  451. (gexp->derivation "bar" #~(mkdir #$output)))))
  452. (with-build-handler (lambda (continue store things mode)
  453. (equal? (map derivation-file-name (pk 'zz (list d1 d2)))
  454. (pk 'XX things)))
  455. (run-with-store %store
  456. (mapm/accumulate-builds built-derivations `((,d1) (,d2)))))))
  457. (test-equal "mapm/accumulate-builds, %current-target-system"
  458. (make-list 2 '("i586-pc-gnu" "i586-pc-gnu"))
  459. ;; Both the 'mapm' and 'mapm/accumulate-builds' procedures should see the
  460. ;; right #:target.
  461. (run-with-store %store
  462. (mlet %store-monad ((lst1 (mapm %store-monad
  463. (lambda _
  464. (current-target-system))
  465. '(a b)))
  466. (lst2 (mapm/accumulate-builds
  467. (lambda _
  468. (current-target-system))
  469. '(a b))))
  470. (return (list lst1 lst2)))
  471. #:system system
  472. #:target "i586-pc-gnu"))
  473. (test-assert "topologically-sorted, one item"
  474. (let* ((a (add-text-to-store %store "a" "a"))
  475. (b (add-text-to-store %store "b" "b" (list a)))
  476. (c (add-text-to-store %store "c" "c" (list b)))
  477. (d (add-text-to-store %store "d" "d" (list c)))
  478. (s (topologically-sorted %store (list d))))
  479. (equal? s (list a b c d))))
  480. (test-assert "topologically-sorted, several items"
  481. (let* ((a (add-text-to-store %store "a" "a"))
  482. (b (add-text-to-store %store "b" "b" (list a)))
  483. (c (add-text-to-store %store "c" "c" (list b)))
  484. (d (add-text-to-store %store "d" "d" (list c)))
  485. (s1 (topologically-sorted %store (list d a c b)))
  486. (s2 (topologically-sorted %store (list b d c a b d))))
  487. (equal? s1 s2 (list a b c d))))
  488. (test-assert "topologically-sorted, more difficult"
  489. (let* ((a (add-text-to-store %store "a" "a"))
  490. (b (add-text-to-store %store "b" "b" (list a)))
  491. (c (add-text-to-store %store "c" "c" (list b)))
  492. (d (add-text-to-store %store "d" "d" (list c)))
  493. (w (add-text-to-store %store "w" "w"))
  494. (x (add-text-to-store %store "x" "x" (list w)))
  495. (y (add-text-to-store %store "y" "y" (list x d)))
  496. (s1 (topologically-sorted %store (list y)))
  497. (s2 (topologically-sorted %store (list c y)))
  498. (s3 (topologically-sorted %store (cons y (references %store y)))))
  499. ;; The order in which 'references' returns the references of Y is
  500. ;; unspecified, so accommodate.
  501. (let* ((x-then-d? (equal? (references %store y) (list x d))))
  502. (and (equal? s1
  503. (if x-then-d?
  504. (list w x a b c d y)
  505. (list a b c d w x y)))
  506. (equal? s2
  507. (if x-then-d?
  508. (list a b c w x d y)
  509. (list a b c d w x y)))
  510. (lset= string=? s1 s3)))))
  511. (test-assert "current-build-output-port, UTF-8"
  512. ;; Are UTF-8 strings in the build log properly interpreted?
  513. (string-contains
  514. (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
  515. (call-with-output-string
  516. (lambda (port)
  517. (parameterize ((current-build-output-port port))
  518. (let* ((s "Here’s a Greek letter: λ.")
  519. (d (build-expression->derivation
  520. %store "foo" `(display ,s)
  521. #:guile-for-build
  522. (package-derivation s %bootstrap-guile (%current-system)))))
  523. (guard (c ((store-protocol-error? c) #t))
  524. (build-derivations %store (list d))))))))
  525. "Here’s a Greek letter: λ."))
  526. (test-assert "current-build-output-port, UTF-8 + garbage"
  527. ;; What about a mixture of UTF-8 + garbage?
  528. (string-contains
  529. (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
  530. (call-with-output-string
  531. (lambda (port)
  532. (parameterize ((current-build-output-port port))
  533. (let ((d (build-expression->derivation
  534. %store "foo"
  535. `(begin
  536. (use-modules (rnrs io ports))
  537. (display "garbage: ")
  538. (put-bytevector (current-output-port) #vu8(128))
  539. (display "lambda: λ\n"))
  540. #:guile-for-build
  541. (package-derivation %store %bootstrap-guile))))
  542. (guard (c ((store-protocol-error? c) #t))
  543. (build-derivations %store (list d))))))))
  544. "garbage: �lambda: λ"))
  545. (test-assert "log-file, derivation"
  546. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  547. (s (add-to-store %store "bash" #t "sha256"
  548. (search-bootstrap-binary "bash"
  549. (%current-system))))
  550. (d (derivation %store "the-thing"
  551. s `("-e" ,b)
  552. #:env-vars `(("foo" . ,(random-text)))
  553. #:inputs `((,b) (,s)))))
  554. (and (build-derivations %store (list d))
  555. (file-exists? (pk (log-file %store (derivation-file-name d)))))))
  556. (test-assert "log-file, output file name"
  557. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  558. (s (add-to-store %store "bash" #t "sha256"
  559. (search-bootstrap-binary "bash"
  560. (%current-system))))
  561. (d (derivation %store "the-thing"
  562. s `("-e" ,b)
  563. #:env-vars `(("foo" . ,(random-text)))
  564. #:inputs `((,b) (,s))))
  565. (o (derivation->output-path d)))
  566. (and (build-derivations %store (list d))
  567. (file-exists? (pk (log-file %store o)))
  568. (string=? (log-file %store (derivation-file-name d))
  569. (log-file %store o)))))
  570. (test-assert "no substitutes"
  571. (with-store s
  572. (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
  573. (d2 (package-derivation s %bootstrap-glibc (%current-system)))
  574. (o (map derivation->output-path (list d1 d2))))
  575. (set-build-options s #:use-substitutes? #f)
  576. (and (not (has-substitutes? s (derivation-file-name d1)))
  577. (not (has-substitutes? s (derivation-file-name d2)))
  578. (null? (substitutable-paths s o))
  579. (null? (substitutable-path-info s o))))))
  580. (test-assert "build-things with output path"
  581. (with-store s
  582. (let* ((c (random-text)) ;contents of the output
  583. (d (build-expression->derivation
  584. s "substitute-me"
  585. `(call-with-output-file %output
  586. (lambda (p)
  587. (display ,c p)))
  588. #:guile-for-build
  589. (package-derivation s %bootstrap-guile (%current-system))))
  590. (o (derivation->output-path d)))
  591. (set-build-options s #:use-substitutes? #f)
  592. ;; Pass 'build-things' the output file name, O. However, since there
  593. ;; are no substitutes for O, it will just do nothing.
  594. (build-things s (list o))
  595. (not (valid-path? s o)))))
  596. (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
  597. (test-assert "substitute query"
  598. (with-store s
  599. (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
  600. (o (derivation->output-path d)))
  601. ;; Create fake substituter data, to be read by 'guix substitute'.
  602. (with-derivation-narinfo d
  603. ;; Remove entry from the local cache.
  604. (false-if-exception
  605. (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
  606. "/guix/substitute")))
  607. ;; Make sure 'guix substitute' correctly communicates the above
  608. ;; data.
  609. (set-build-options s #:use-substitutes? #t
  610. #:substitute-urls (%test-substitute-urls))
  611. (and (has-substitutes? s o)
  612. (equal? (list o) (substitutable-paths s (list o)))
  613. (match (pk 'spi (substitutable-path-info s (list o)))
  614. (((? substitutable? s))
  615. (and (string=? (substitutable-deriver s)
  616. (derivation-file-name d))
  617. (null? (substitutable-references s))
  618. (equal? (substitutable-nar-size s) 1234)))))))))
  619. (test-assert "substitute query, alternating URLs"
  620. (let* ((d (with-store s
  621. (package-derivation s %bootstrap-guile (%current-system))))
  622. (o (derivation->output-path d)))
  623. (with-derivation-narinfo d
  624. ;; Remove entry from the local cache.
  625. (false-if-exception
  626. (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
  627. "/guix/substitute")))
  628. ;; Note: We reconnect to the daemon to force a new instance of 'guix
  629. ;; substitute' to be used; otherwise the #:substitute-urls of
  630. ;; 'set-build-options' would have no effect.
  631. (and (with-store s ;the right substitute URL
  632. (set-build-options s #:use-substitutes? #t
  633. #:substitute-urls (%test-substitute-urls))
  634. (has-substitutes? s o))
  635. (with-store s ;the wrong one
  636. (set-build-options s #:use-substitutes? #t
  637. #:substitute-urls (list
  638. "http://does-not-exist"))
  639. (not (has-substitutes? s o)))
  640. (with-store s ;the right one again
  641. (set-build-options s #:use-substitutes? #t
  642. #:substitute-urls (%test-substitute-urls))
  643. (has-substitutes? s o))
  644. (with-store s ;empty list of URLs
  645. (set-build-options s #:use-substitutes? #t
  646. #:substitute-urls '())
  647. (not (has-substitutes? s o)))))))
  648. (test-assert "substitute"
  649. (with-store s
  650. (let* ((c (random-text)) ; contents of the output
  651. (d (build-expression->derivation
  652. s "substitute-me"
  653. `(call-with-output-file %output
  654. (lambda (p)
  655. (exit 1) ; would actually fail
  656. (display ,c p)))
  657. #:guile-for-build
  658. (package-derivation s %bootstrap-guile (%current-system))))
  659. (o (derivation->output-path d)))
  660. (with-derivation-substitute d c
  661. (set-build-options s #:use-substitutes? #t
  662. #:substitute-urls (%test-substitute-urls))
  663. (and (has-substitutes? s o)
  664. (build-derivations s (list d))
  665. (canonical-file? o)
  666. (equal? c (call-with-input-file o get-string-all)))))))
  667. (test-assert "substitute, deduplication"
  668. (with-store s
  669. (let* ((c (random-text)) ; contents of the output
  670. (g (package-derivation s %bootstrap-guile))
  671. (d1 (build-expression->derivation s "substitute-me"
  672. `(begin ,c (exit 1))
  673. #:guile-for-build g))
  674. (d2 (build-expression->derivation s "build-me"
  675. `(call-with-output-file %output
  676. (lambda (p)
  677. (display ,c p)))
  678. #:guile-for-build g))
  679. (o1 (derivation->output-path d1))
  680. (o2 (derivation->output-path d2)))
  681. (with-derivation-substitute d1 c
  682. (set-build-options s #:use-substitutes? #t
  683. #:substitute-urls (%test-substitute-urls))
  684. (and (has-substitutes? s o1)
  685. (build-derivations s (list d2)) ;build
  686. (build-derivations s (list d1)) ;substitute
  687. (canonical-file? o1)
  688. (equal? c (call-with-input-file o1 get-string-all))
  689. (= (stat:ino (stat o1)) (stat:ino (stat o2))))))))
  690. (test-assert "substitute + build-things with output path"
  691. (with-store s
  692. (let* ((c (random-text)) ;contents of the output
  693. (d (build-expression->derivation
  694. s "substitute-me"
  695. `(call-with-output-file %output
  696. (lambda (p)
  697. (exit 1) ;would actually fail
  698. (display ,c p)))
  699. #:guile-for-build
  700. (package-derivation s %bootstrap-guile (%current-system))))
  701. (o (derivation->output-path d)))
  702. (with-derivation-substitute d c
  703. (set-build-options s #:use-substitutes? #t
  704. #:substitute-urls (%test-substitute-urls))
  705. (and (has-substitutes? s o)
  706. (build-things s (list o)) ;give the output path
  707. (valid-path? s o)
  708. (canonical-file? o)
  709. (equal? c (call-with-input-file o get-string-all)))))))
  710. (test-assert "substitute + build-things with specific output"
  711. (with-store s
  712. (let* ((c (random-text)) ;contents of the output
  713. (d (build-expression->derivation
  714. s "substitute-me" `(begin ,c (exit 1)) ;would fail
  715. #:outputs '("out" "one" "two")
  716. #:guile-for-build
  717. (package-derivation s %bootstrap-guile (%current-system))))
  718. (o (derivation->output-path d)))
  719. (with-derivation-substitute d c
  720. (set-build-options s #:use-substitutes? #t
  721. #:substitute-urls (%test-substitute-urls))
  722. (and (has-substitutes? s o)
  723. ;; Ask for nothing but the "out" output of D.
  724. (build-things s `((,(derivation-file-name d) . "out")))
  725. (valid-path? s o)
  726. (canonical-file? o)
  727. (equal? c (call-with-input-file o get-string-all)))))))
  728. (test-assert "substitute, corrupt output hash"
  729. ;; Tweak the substituter into installing a substitute whose hash doesn't
  730. ;; match the one announced in the narinfo. The daemon must notice this and
  731. ;; raise an error.
  732. (with-store s
  733. (let* ((c "hello, world") ; contents of the output
  734. (d (build-expression->derivation
  735. s "corrupt-substitute"
  736. `(mkdir %output)
  737. #:guile-for-build
  738. (package-derivation s %bootstrap-guile (%current-system))))
  739. (o (derivation->output-path d)))
  740. (with-derivation-substitute d c
  741. (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
  742. ;; Make sure we use 'guix substitute'.
  743. (set-build-options s
  744. #:use-substitutes? #t
  745. #:fallback? #f
  746. #:substitute-urls (%test-substitute-urls))
  747. (and (has-substitutes? s o)
  748. (guard (c ((store-protocol-error? c)
  749. ;; XXX: the daemon writes "hash mismatch in downloaded
  750. ;; path", but the actual error returned to the client
  751. ;; doesn't mention that.
  752. (pk 'corrupt c)
  753. (not (zero? (store-protocol-error-status c)))))
  754. (build-derivations s (list d))
  755. #f))))))
  756. (test-assert "substitute, corrupt output hash, build trace"
  757. ;; Likewise, and check the build trace.
  758. (with-store s
  759. (let* ((c "hello, world") ; contents of the output
  760. (d (build-expression->derivation
  761. s "corrupt-substitute"
  762. `(mkdir %output)
  763. #:guile-for-build
  764. (package-derivation s %bootstrap-guile (%current-system))))
  765. (o (derivation->output-path d)))
  766. ;; Make sure we use 'guix substitute'.
  767. (set-build-options s
  768. #:print-build-trace #t
  769. #:use-substitutes? #t
  770. #:fallback? #f
  771. #:substitute-urls (%test-substitute-urls))
  772. (with-derivation-substitute d c
  773. (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
  774. (define output
  775. (call-with-output-string
  776. (lambda (port)
  777. (parameterize ((current-build-output-port port))
  778. (guard (c ((store-protocol-error? c) #t))
  779. (build-derivations s (list d))
  780. #f)))))
  781. (define actual-hash
  782. (let-values (((port get-hash)
  783. (gcrypt:open-hash-port
  784. (gcrypt:hash-algorithm gcrypt:sha256))))
  785. (write-file-tree "foo" port
  786. #:file-type+size
  787. (lambda _
  788. (values 'regular (string-length c)))
  789. #:file-port
  790. (lambda _
  791. (open-input-string c)))
  792. (close-port port)
  793. (bytevector->nix-base32-string (get-hash))))
  794. (define expected-hash
  795. (bytevector->nix-base32-string (make-bytevector 32 0)))
  796. (define mismatch
  797. (string-append "@ hash-mismatch " o " sha256 "
  798. expected-hash " " actual-hash "\n"))
  799. (define failure
  800. (string-append "@ substituter-failed " o))
  801. (and (string-contains output mismatch)
  802. (string-contains output failure))))))
  803. (test-assert "substitute --fallback"
  804. (with-store s
  805. (let* ((t (random-text)) ; contents of the output
  806. (d (build-expression->derivation
  807. s "substitute-me-not"
  808. `(call-with-output-file %output
  809. (lambda (p)
  810. (display ,t p)))
  811. #:guile-for-build
  812. (package-derivation s %bootstrap-guile (%current-system))))
  813. (o (derivation->output-path d)))
  814. ;; Create fake substituter data, to be read by 'guix substitute'.
  815. (with-derivation-narinfo d
  816. ;; Make sure we use 'guix substitute'.
  817. (set-build-options s #:use-substitutes? #t
  818. #:substitute-urls (%test-substitute-urls))
  819. (and (has-substitutes? s o)
  820. (guard (c ((store-protocol-error? c)
  821. ;; The substituter failed as expected. Now make
  822. ;; sure that #:fallback? #t works correctly.
  823. (set-build-options s
  824. #:use-substitutes? #t
  825. #:substitute-urls
  826. (%test-substitute-urls)
  827. #:fallback? #t)
  828. (and (build-derivations s (list d))
  829. (equal? t (call-with-input-file o
  830. get-string-all)))))
  831. ;; Should fail.
  832. (build-derivations s (list d))
  833. #f))))))
  834. (test-assert "export/import several paths"
  835. (let* ((texts (unfold (cut >= <> 10)
  836. (lambda _ (random-text))
  837. 1+
  838. 0))
  839. (files (map (cut add-text-to-store %store "text" <>) texts))
  840. (dump (call-with-bytevector-output-port
  841. (cut export-paths %store files <>))))
  842. (delete-paths %store files)
  843. (and (every (negate file-exists?) files)
  844. (let* ((source (open-bytevector-input-port dump))
  845. (imported (import-paths %store source)))
  846. (and (equal? imported files)
  847. (every file-exists? files)
  848. (equal? texts
  849. (map (lambda (file)
  850. (call-with-input-file file
  851. get-string-all))
  852. files)))))))
  853. (test-assert "export/import paths, ensure topological order"
  854. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  855. (file1 (add-text-to-store %store "foo" (random-text)
  856. (list file0)))
  857. (file2 (add-text-to-store %store "bar" (random-text)
  858. (list file1)))
  859. (files (list file1 file2))
  860. (dump1 (call-with-bytevector-output-port
  861. (cute export-paths %store (list file1 file2) <>)))
  862. (dump2 (call-with-bytevector-output-port
  863. (cute export-paths %store (list file2 file1) <>))))
  864. (delete-paths %store files)
  865. (and (every (negate file-exists?) files)
  866. (bytevector=? dump1 dump2)
  867. (let* ((source (open-bytevector-input-port dump1))
  868. (imported (import-paths %store source)))
  869. ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
  870. (and (equal? imported (list file1 file2))
  871. (every file-exists? files)
  872. (equal? (list file0) (references %store file1))
  873. (equal? (list file1) (references %store file2)))))))
  874. (test-assert "export/import incomplete"
  875. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  876. (file1 (add-text-to-store %store "foo" (random-text)
  877. (list file0)))
  878. (file2 (add-text-to-store %store "bar" (random-text)
  879. (list file1)))
  880. (dump (call-with-bytevector-output-port
  881. (cute export-paths %store (list file2) <>))))
  882. (delete-paths %store (list file0 file1 file2))
  883. (guard (c ((store-protocol-error? c)
  884. (and (not (zero? (store-protocol-error-status c)))
  885. (string-contains (store-protocol-error-message c)
  886. "not valid"))))
  887. ;; Here we get an exception because DUMP does not include FILE0 and
  888. ;; FILE1, which are dependencies of FILE2.
  889. (import-paths %store (open-bytevector-input-port dump)))))
  890. (test-assert "export/import recursive"
  891. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  892. (file1 (add-text-to-store %store "foo" (random-text)
  893. (list file0)))
  894. (file2 (add-text-to-store %store "bar" (random-text)
  895. (list file1)))
  896. (dump (call-with-bytevector-output-port
  897. (cute export-paths %store (list file2) <>
  898. #:recursive? #t))))
  899. (delete-paths %store (list file0 file1 file2))
  900. (let ((imported (import-paths %store (open-bytevector-input-port dump))))
  901. (and (equal? imported (list file0 file1 file2))
  902. (every file-exists? (list file0 file1 file2))
  903. (equal? (list file0) (references %store file1))
  904. (equal? (list file1) (references %store file2))))))
  905. (test-assert "write-file & export-path yield the same result"
  906. ;; Here we compare 'write-file' and the daemon's own implementation.
  907. ;; 'write-file' is the reference because we know it sorts file
  908. ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
  909. ;; currently happen to be sorted as a side-effect of some unrelated
  910. ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
  911. ;; changes there.
  912. (run-with-store %store
  913. (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
  914. (out1 -> (derivation->output-path drv1))
  915. (data -> (unfold (cut >= <> 26)
  916. (lambda (i)
  917. (random-bytevector 128))
  918. 1+ 0))
  919. (build
  920. -> #~(begin
  921. (use-modules (rnrs io ports) (srfi srfi-1))
  922. (let ()
  923. (define letters
  924. (map (lambda (i)
  925. (string
  926. (integer->char
  927. (+ i (char->integer #\a)))))
  928. (iota 26)))
  929. (define (touch file data)
  930. (call-with-output-file file
  931. (lambda (port)
  932. (put-bytevector port data))))
  933. (mkdir #$output)
  934. (chdir #$output)
  935. ;; The files must be different so they have
  936. ;; different inode numbers, and the inode
  937. ;; order must differ from the lexicographic
  938. ;; order.
  939. (for-each touch
  940. (append (drop letters 10)
  941. (take letters 10))
  942. (list #$@data))
  943. #t)))
  944. (drv2 (gexp->derivation "bunch" build))
  945. (out2 -> (derivation->output-path drv2))
  946. (item-info -> (store-lift query-path-info)))
  947. (mbegin %store-monad
  948. (built-derivations (list drv1 drv2))
  949. (foldm %store-monad
  950. (lambda (item result)
  951. (define ref-hash
  952. (let-values (((port get) (gcrypt:open-sha256-port)))
  953. (write-file item port)
  954. (close-port port)
  955. (get)))
  956. ;; 'query-path-info' returns a hash produced by using the
  957. ;; daemon's C++ 'dump' function, which is the implementation
  958. ;; under test.
  959. (>>= (item-info item)
  960. (lambda (info)
  961. (return
  962. (and result
  963. (bytevector=? (path-info-hash info) ref-hash))))))
  964. #t
  965. (list out1 out2))))
  966. #:guile-for-build (%guile-for-build)))
  967. (test-assert "import not signed"
  968. (let* ((text (random-text))
  969. (file (add-file-tree-to-store %store
  970. `("tree" directory
  971. ("text" regular (data ,text))
  972. ("link" symlink "text"))))
  973. (dump (call-with-bytevector-output-port
  974. (lambda (port)
  975. (write-int 1 port) ;start
  976. (write-file file port) ;contents
  977. (write-int #x4558494e port) ;%export-magic
  978. (write-string file port) ;store item
  979. (write-string-list '() port) ;references
  980. (write-string "" port) ;deriver
  981. (write-int 0 port) ;not signed
  982. (write-int 0 port))))) ;done
  983. ;; Ensure 'import-paths' raises an exception.
  984. (guard (c ((store-protocol-error? c)
  985. (and (not (zero? (store-protocol-error-status c)))
  986. (string-contains (store-protocol-error-message c)
  987. "lacks a signature"))))
  988. (let* ((source (open-bytevector-input-port dump))
  989. (imported (import-paths %store source)))
  990. (pk 'unsigned-imported imported)
  991. #f))))
  992. (test-assert "import signed by unauthorized key"
  993. (let* ((text (random-text))
  994. (file (add-file-tree-to-store %store
  995. `("tree" directory
  996. ("text" regular (data ,text))
  997. ("link" symlink "text"))))
  998. (key (gcrypt:generate-key
  999. (gcrypt:string->canonical-sexp
  1000. "(genkey (ecdsa (curve Ed25519) (flags rfc6979)))")))
  1001. (dump (call-with-bytevector-output-port
  1002. (lambda (port)
  1003. (write-int 1 port) ;start
  1004. (write-file file port) ;contents
  1005. (write-int #x4558494e port) ;%export-magic
  1006. (write-string file port) ;store item
  1007. (write-string-list '() port) ;references
  1008. (write-string "" port) ;deriver
  1009. (write-int 1 port) ;signed
  1010. (write-string (gcrypt:canonical-sexp->string
  1011. (signature-sexp
  1012. (gcrypt:bytevector->hash-data
  1013. (gcrypt:sha256 #vu8(0 1 2))
  1014. #:key-type 'ecc)
  1015. (gcrypt:find-sexp-token key 'private-key)
  1016. (gcrypt:find-sexp-token key 'public-key)))
  1017. port)
  1018. (write-int 0 port))))) ;done
  1019. ;; Ensure 'import-paths' raises an exception.
  1020. (guard (c ((store-protocol-error? c)
  1021. (and (not (zero? (store-protocol-error-status c)))
  1022. (string-contains (store-protocol-error-message c)
  1023. "unauthorized public key"))))
  1024. (let* ((source (open-bytevector-input-port dump))
  1025. (imported (import-paths %store source)))
  1026. (pk 'unauthorized-imported imported)
  1027. #f))))
  1028. (test-assert "import corrupt path"
  1029. (let* ((text (random-text))
  1030. (file (add-text-to-store %store "text" text))
  1031. (dump (call-with-bytevector-output-port
  1032. (cut export-paths %store (list file) <>))))
  1033. (delete-paths %store (list file))
  1034. ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
  1035. ;; the file contents in DUMP, regardless of the store prefix.
  1036. (let* ((index #x70)
  1037. (byte (bytevector-u8-ref dump index)))
  1038. (bytevector-u8-set! dump index (logxor #xff byte)))
  1039. (and (not (file-exists? file))
  1040. (guard (c ((store-protocol-error? c)
  1041. (pk 'c c)
  1042. (and (not (zero? (store-protocol-error-status c)))
  1043. (string-contains (store-protocol-error-message c)
  1044. "corrupt"))))
  1045. (let* ((source (open-bytevector-input-port dump))
  1046. (imported (import-paths %store source)))
  1047. (pk 'corrupt-imported imported)
  1048. #f)))))
  1049. (test-assert "verify-store"
  1050. (let* ((text (random-text))
  1051. (file1 (add-text-to-store %store "foo" text))
  1052. (file2 (add-text-to-store %store "bar" (random-text)
  1053. (list file1))))
  1054. (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
  1055. (begin
  1056. (delete-file file1)
  1057. (not (pk 'verify2 (verify-store %store)))) ;bad! ;
  1058. (begin
  1059. ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
  1060. ;; without actually creating the file. ;
  1061. (call-with-output-file file1
  1062. (lambda (port)
  1063. (display text port)))
  1064. (pk 'verify3 (verify-store %store)))))) ;OK again
  1065. (test-assert "verify-store + check-contents"
  1066. ;; XXX: This test is I/O intensive.
  1067. (with-store s
  1068. (let* ((text (random-text))
  1069. (drv (build-expression->derivation
  1070. s "corrupt"
  1071. `(let ((out (assoc-ref %outputs "out")))
  1072. (call-with-output-file out
  1073. (lambda (port)
  1074. (display ,text port)))
  1075. #t)
  1076. #:guile-for-build
  1077. (package-derivation s %bootstrap-guile (%current-system))))
  1078. (file (derivation->output-path drv)))
  1079. (with-derivation-substitute drv text
  1080. (and (build-derivations s (list drv))
  1081. (verify-store s #:check-contents? #t) ;should be OK
  1082. (begin
  1083. (chmod file #o644)
  1084. (call-with-output-file file
  1085. (lambda (port)
  1086. (display "corrupt!" port)))
  1087. #t)
  1088. ;; Make sure the corruption is detected. We don't test repairing
  1089. ;; because only "trusted" users are allowed to do it, but we
  1090. ;; don't expose that notion of trusted users that nix-daemon
  1091. ;; supports because it seems dubious and redundant with what the
  1092. ;; OS provides (in Nix "trusted" users have additional
  1093. ;; privileges, such as overriding the set of substitute URLs, but
  1094. ;; we instead want to allow anyone to modify them, provided
  1095. ;; substitutes are signed by a root-approved key.)
  1096. (not (verify-store s #:check-contents? #t))
  1097. ;; Delete the corrupt item to leave the store in a clean state.
  1098. (delete-paths s (list file)))))))
  1099. (test-assert "build-things, check mode"
  1100. (with-store store
  1101. (call-with-temporary-output-file
  1102. (lambda (entropy entropy-port)
  1103. (write (random-text) entropy-port)
  1104. (force-output entropy-port)
  1105. (let* ((drv (build-expression->derivation
  1106. store "non-deterministic"
  1107. `(begin
  1108. (use-modules (rnrs io ports))
  1109. (let ((out (assoc-ref %outputs "out")))
  1110. (call-with-output-file out
  1111. (lambda (port)
  1112. ;; Rely on the fact that tests do not use the
  1113. ;; chroot, and thus ENTROPY is readable.
  1114. (display (call-with-input-file ,entropy
  1115. get-string-all)
  1116. port)))
  1117. #t))
  1118. #:guile-for-build
  1119. (package-derivation store %bootstrap-guile (%current-system))))
  1120. (file (derivation->output-path drv)))
  1121. (and (build-things store (list (derivation-file-name drv)))
  1122. (begin
  1123. (write (random-text) entropy-port)
  1124. (force-output entropy-port)
  1125. (guard (c ((store-protocol-error? c)
  1126. (pk 'determinism-exception c)
  1127. (and (not (zero? (store-protocol-error-status c)))
  1128. (string-contains (store-protocol-error-message c)
  1129. "deterministic"))))
  1130. ;; This one will produce a different result. Since we're in
  1131. ;; 'check' mode, this must fail.
  1132. (build-things store (list (derivation-file-name drv))
  1133. (build-mode check))
  1134. #f))))))))
  1135. (test-assert "build-succeeded trace in check mode"
  1136. (string-contains
  1137. (call-with-output-string
  1138. (lambda (port)
  1139. (let ((d (build-expression->derivation
  1140. %store "foo" '(mkdir (assoc-ref %outputs "out"))
  1141. #:guile-for-build
  1142. (package-derivation %store %bootstrap-guile))))
  1143. (build-derivations %store (list d))
  1144. (parameterize ((current-build-output-port port))
  1145. (build-derivations %store (list d) (build-mode check))))))
  1146. "@ build-succeeded"))
  1147. (test-assert "build multiple times"
  1148. (with-store store
  1149. ;; Ask to build twice.
  1150. (set-build-options store #:rounds 2 #:use-substitutes? #f)
  1151. (call-with-temporary-output-file
  1152. (lambda (entropy entropy-port)
  1153. (write (random-text) entropy-port)
  1154. (force-output entropy-port)
  1155. (let* ((drv (build-expression->derivation
  1156. store "non-deterministic"
  1157. `(begin
  1158. (use-modules (rnrs io ports))
  1159. (let ((out (assoc-ref %outputs "out")))
  1160. (call-with-output-file out
  1161. (lambda (port)
  1162. ;; Rely on the fact that tests do not use the
  1163. ;; chroot, and thus ENTROPY is accessible.
  1164. (display (call-with-input-file ,entropy
  1165. get-string-all)
  1166. port)
  1167. (call-with-output-file ,entropy
  1168. (lambda (port)
  1169. (write 'foobar port)))))
  1170. #t))
  1171. #:guile-for-build
  1172. (package-derivation store %bootstrap-guile (%current-system))))
  1173. (file (derivation->output-path drv)))
  1174. (guard (c ((store-protocol-error? c)
  1175. (pk 'multiple-build c)
  1176. (and (not (zero? (store-protocol-error-status c)))
  1177. (string-contains (store-protocol-error-message c)
  1178. "deterministic"))))
  1179. ;; This one will produce a different result on the second run.
  1180. (current-build-output-port (current-error-port))
  1181. (build-things store (list (derivation-file-name drv)))
  1182. #f))))))
  1183. (test-equal "store-lower"
  1184. "Lowered."
  1185. (let* ((add (store-lower text-file))
  1186. (file (add %store "foo" "Lowered.")))
  1187. (call-with-input-file file get-string-all)))
  1188. (test-equal "current-system"
  1189. "bar"
  1190. (parameterize ((%current-system "frob"))
  1191. (run-with-store %store
  1192. (mbegin %store-monad
  1193. (set-current-system "bar")
  1194. (current-system))
  1195. #:system "foo")))
  1196. (test-assert "query-path-info"
  1197. (let* ((ref (add-text-to-store %store "ref" "foo"))
  1198. (item (add-text-to-store %store "item" "bar" (list ref)))
  1199. (info (query-path-info %store item)))
  1200. (and (equal? (path-info-references info) (list ref))
  1201. (equal? (path-info-hash info)
  1202. (gcrypt:sha256
  1203. (string->utf8
  1204. (call-with-output-string (cut write-file item <>))))))))
  1205. (test-assert "path-info-deriver"
  1206. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  1207. (s (add-to-store %store "bash" #t "sha256"
  1208. (search-bootstrap-binary "bash"
  1209. (%current-system))))
  1210. (d (derivation %store "the-thing"
  1211. s `("-e" ,b)
  1212. #:env-vars `(("foo" . ,(random-text)))
  1213. #:inputs `((,b) (,s))))
  1214. (o (derivation->output-path d)))
  1215. (and (build-derivations %store (list d))
  1216. (not (path-info-deriver (query-path-info %store b)))
  1217. (string=? (derivation-file-name d)
  1218. (path-info-deriver (query-path-info %store o))))))
  1219. (test-equal "build-cores"
  1220. (list 0 42)
  1221. (with-store store
  1222. (let* ((build (add-text-to-store store "build.sh"
  1223. "echo $NIX_BUILD_CORES > $out"))
  1224. (bash (add-to-store store "bash" #t "sha256"
  1225. (search-bootstrap-binary "bash"
  1226. (%current-system))))
  1227. (drv1 (derivation store "the-thing" bash
  1228. `("-e" ,build)
  1229. #:inputs `((,bash) (,build))
  1230. #:env-vars `(("x" . ,(random-text)))))
  1231. (drv2 (derivation store "the-thing" bash
  1232. `("-e" ,build)
  1233. #:inputs `((,bash) (,build))
  1234. #:env-vars `(("x" . ,(random-text))))))
  1235. (and (build-derivations store (list drv1))
  1236. (begin
  1237. (set-build-options store #:build-cores 42)
  1238. (build-derivations store (list drv2)))
  1239. (list (call-with-input-file (derivation->output-path drv1)
  1240. read)
  1241. (call-with-input-file (derivation->output-path drv2)
  1242. read))))))
  1243. (test-equal "multiplexed-build-output"
  1244. '("Hello from first." "Hello from second.")
  1245. (with-store store
  1246. (let* ((build (add-text-to-store store "build.sh"
  1247. "echo Hello from $NAME.; echo > $out"))
  1248. (bash (add-to-store store "bash" #t "sha256"
  1249. (search-bootstrap-binary "bash"
  1250. (%current-system))))
  1251. (drv1 (derivation store "one" bash
  1252. `("-e" ,build)
  1253. #:inputs `((,bash) (,build))
  1254. #:env-vars `(("NAME" . "first")
  1255. ("x" . ,(random-text)))))
  1256. (drv2 (derivation store "two" bash
  1257. `("-e" ,build)
  1258. #:inputs `((,bash) (,build))
  1259. #:env-vars `(("NAME" . "second")
  1260. ("x" . ,(random-text))))))
  1261. (set-build-options store
  1262. #:print-build-trace #t
  1263. #:multiplexed-build-output? #t
  1264. #:max-build-jobs 10)
  1265. (let ((port (open-output-string)))
  1266. ;; Send the build log to PORT.
  1267. (parameterize ((current-build-output-port port))
  1268. (build-derivations store (list drv1 drv2)))
  1269. ;; Retrieve the build log; make sure it contains valid "@ build-log"
  1270. ;; traces that allow us to retrieve each builder's output (we assume
  1271. ;; there's exactly one "build-output" trace for each builder, which is
  1272. ;; reasonable.)
  1273. (let* ((log (get-output-string port))
  1274. (started (fold-matches
  1275. (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
  1276. log '() cons))
  1277. (done (fold-matches
  1278. (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
  1279. log '() cons))
  1280. (output (fold-matches
  1281. (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
  1282. log '() cons))
  1283. (drv-pid (lambda (name)
  1284. (lambda (m)
  1285. (let ((drv (match:substring m 1))
  1286. (pid (string->number
  1287. (match:substring m 4))))
  1288. (and (string-suffix? name drv) pid)))))
  1289. (pid-log (lambda (pid)
  1290. (lambda (m)
  1291. (let ((n (string->number
  1292. (match:substring m 1)))
  1293. (len (string->number
  1294. (match:substring m 2)))
  1295. (str (match:substring m 3)))
  1296. (and (= pid n)
  1297. (= (string-length str) (- len 1))
  1298. str)))))
  1299. (pid1 (any (drv-pid "one.drv") started))
  1300. (pid2 (any (drv-pid "two.drv") started)))
  1301. (list (any (pid-log pid1) output)
  1302. (any (pid-log pid2) output)))))))
  1303. (test-end "store")